workspace-management.e2e.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. // Web e2e scenarios: workspace management — the create-by-name dialog, the
  2. // rename round trip over the real wire (workspace.rename RPC + durable
  3. // registry), duplicate-name pre-check, the flat "In one list" view with its
  4. // persisted group-by preference, and the session hover card. Zero model
  5. // calls: workspace.create/rename are host RPCs with no model involvement,
  6. // and the one session row the flat/hover scenarios need comes from a seeded
  7. // fixture (the seeded-history seed reused verbatim — no new recording).
  8. import { mkdir, readFile, stat, writeFile } from 'node:fs/promises'
  9. import { fileURLToPath } from 'node:url'
  10. import { join } from 'node:path'
  11. import type { Browser, Page } from 'playwright'
  12. import { chromium } from 'playwright'
  13. import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest'
  14. import { SessionId } from '@deepseek-ai/dsh-session'
  15. import {
  16. acknowledgeReloadConnectionLoss, assertFixtureInventory, launchWebScaffold, seedSession, watchConsole,
  17. webSnapshotMode, type WebScaffold,
  18. } from './scaffold.ts'
  19. import { saveFailureShot } from './support.ts'
  20. const SNAPSHOT_DIR = fileURLToPath(new URL('./snapshots/workspace-management', import.meta.url))
  21. // The seed is another scenario's committed fixture, reused read-only: this
  22. // spec needs any one cold session row, not new recorded content.
  23. const SEED = fileURLToPath(new URL('./snapshots/seeded-history/seed.jsonl', import.meta.url))
  24. const MODE = webSnapshotMode()
  25. const SEED_ID = 'workspace-management-web-e2e'
  26. describe('web e2e: workspace management (create / rename / flat view / hover card)', () => {
  27. let scaffold: WebScaffold
  28. let browser: Browser
  29. let page: Page
  30. let tripwire: ReturnType<typeof watchConsole>
  31. beforeAll(async () => {
  32. scaffold = await launchWebScaffold({})
  33. // Seed one cold session (Ungrouped bucket) for the flat view + hover card.
  34. const sessionCwd = join(scaffold.workspaceCwd, 'workspace')
  35. await mkdir(sessionCwd, { recursive: true })
  36. await writeFile(join(sessionCwd, 'a.txt'), 'alpha\n')
  37. await writeFile(join(sessionCwd, 'b.txt'), 'beta\n')
  38. await seedSession(scaffold, await readFile(SEED, 'utf8'), SEED_ID)
  39. browser = await chromium.launch()
  40. page = await browser.newPage({ viewport: { width: 1680, height: 1000 } })
  41. tripwire = watchConsole(page)
  42. await page.goto(scaffold.baseUrl, { waitUntil: 'load' })
  43. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  44. }, 120_000)
  45. afterAll(async () => {
  46. await browser?.close()
  47. await scaffold?.close()
  48. })
  49. it('creates two workspaces by name through the region-header dialog', async () => {
  50. onTestFailed(() => saveFailureShot(page, 'web-e2e-ws-create'))
  51. const createByName = async (name: string): Promise<void> => {
  52. await page.getByRole('button', { name: 'Create workspace' }).click()
  53. // The pick menu's Create workspace submenu opens on hover/focus.
  54. await page.getByRole('menuitem', { name: 'Create workspace' }).hover()
  55. await page.getByRole('menuitem', { name: 'Create a new workspace' }).click()
  56. const dialog = page.getByRole('dialog', { name: 'Create a new workspace' })
  57. await dialog.waitFor({ timeout: 10_000 })
  58. await dialog.getByLabel('New workspace name').fill(name)
  59. await dialog.getByRole('button', { name: 'Create workspace' }).click()
  60. await expect.poll(() => page.getByRole('dialog', { name: 'Create a new workspace' }).count(), { timeout: 10_000 }).toBe(0)
  61. // The real workspace materializes in the tree as a group row.
  62. await expect.poll(() => page.getByText(name, { exact: true }).count(), { timeout: 10_000 }).toBeGreaterThanOrEqual(1)
  63. }
  64. await createByName('alpha-ws')
  65. await createByName('beta-ws')
  66. // Durable on the host: both registered, newest first (create prepends).
  67. const titles = scaffold.ctx.workspace.list().map(workspace => workspace.title)
  68. expect(titles.slice(0, 2)).toEqual(['beta-ws', 'alpha-ws'])
  69. expect(tripwire.pageErrors).toEqual([])
  70. }, 90_000)
  71. it('renames a workspace over the wire with a duplicate-name pre-check', async () => {
  72. onTestFailed(() => saveFailureShot(page, 'web-e2e-ws-rename'))
  73. // The actions button is display:none until its row hovers — hover the
  74. // group row first, then the revealed button becomes actionable.
  75. await page.locator('[role="treeitem"]').filter({ hasText: 'alpha-ws' }).first().hover()
  76. await page.getByRole('button', { name: 'Workspace actions for alpha-ws' }).click()
  77. await page.getByRole('menuitem', { name: 'Rename' }).click()
  78. const dialog = page.getByRole('dialog', { name: 'Rename workspace' })
  79. await dialog.waitFor({ timeout: 10_000 })
  80. const input = dialog.getByLabel('Workspace name')
  81. // Client pre-check: a name colliding with another live workspace raises
  82. // the inline alert and blocks the primary button before any wire call.
  83. await input.fill('beta-ws')
  84. await expect.poll(() => dialog.getByRole('alert').count(), { timeout: 5_000 }).toBe(1)
  85. expect(await dialog.getByRole('button', { name: 'Rename' }).isDisabled()).toBe(true)
  86. // A fresh name goes through workspace.rename to the durable registry.
  87. await input.fill('gamma-ws')
  88. await expect.poll(() => dialog.getByRole('alert').count(), { timeout: 5_000 }).toBe(0)
  89. await dialog.getByRole('button', { name: 'Rename' }).click()
  90. await expect.poll(() => page.getByRole('dialog', { name: 'Rename workspace' }).count(), { timeout: 10_000 }).toBe(0)
  91. await expect.poll(() => page.getByText('gamma-ws', { exact: true }).count(), { timeout: 10_000 }).toBeGreaterThanOrEqual(1)
  92. expect(await page.getByText('alpha-ws', { exact: true }).count()).toBe(0)
  93. // Host durability, then reload: the projection is rebuilt from the wire.
  94. expect(scaffold.ctx.workspace.list().map(workspace => workspace.title)).toContain('gamma-ws')
  95. const warningStart = tripwire.warnings.length
  96. await page.reload({ waitUntil: 'load' })
  97. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  98. acknowledgeReloadConnectionLoss(tripwire, warningStart)
  99. await expect.poll(() => page.getByText('gamma-ws', { exact: true }).count(), { timeout: 15_000 }).toBeGreaterThanOrEqual(1)
  100. expect(tripwire.pageErrors).toEqual([])
  101. }, 90_000)
  102. it('deletes only the Workspace registration and keeps its current Session, folder, and log', async () => {
  103. onTestFailed(() => saveFailureShot(page, 'web-e2e-ws-delete'))
  104. const slotConsoleErrors: string[] = []
  105. const transientSlotErrors: string[] = []
  106. page.on('console', (message) => {
  107. if (message.type() === 'error' && /slot entry crashed/i.test(message.text())) {
  108. slotConsoleErrors.push(message.text())
  109. }
  110. })
  111. await page.exposeFunction('recordDshSlotError', (key: string) => {
  112. if (!transientSlotErrors.includes(key)) transientSlotErrors.push(key)
  113. })
  114. await page.evaluate(() => {
  115. const target = window as unknown as { recordDshSlotError(key: string): Promise<void> }
  116. const seen = new Set<string>()
  117. const collect = (): void => {
  118. for (const node of document.querySelectorAll<HTMLElement>('[data-slot-error]')) {
  119. const key = node.dataset.slotError ?? ''
  120. if (!seen.has(key)) {
  121. seen.add(key)
  122. void target.recordDshSlotError(key)
  123. }
  124. }
  125. }
  126. new MutationObserver(collect).observe(document.documentElement, { childList: true, subtree: true })
  127. collect()
  128. })
  129. // Register the scaffold's existing project directory through the real UI.
  130. await page.getByRole('button', { name: 'Create workspace' }).click()
  131. await page.getByRole('menuitem', { name: 'Create workspace' }).hover()
  132. await page.getByRole('menuitem', { name: 'Use an existing folder' }).click()
  133. const useFolder = page.getByRole('dialog', { name: 'Use an existing folder' })
  134. await useFolder.getByLabel('Existing folder path').fill(scaffold.workspaceCwd)
  135. await useFolder.getByRole('button', { name: 'Use folder' }).click()
  136. await expect.poll(() => useFolder.count(), { timeout: 10_000 }).toBe(0)
  137. const workspace = await scaffold.ctx.workspace.resolveByPath(scaffold.workspaceCwd)
  138. if (workspace === undefined) throw new Error('GUI did not register the existing project directory')
  139. await workspace.attachSession(SessionId(SEED_ID))
  140. const header = (await scaffold.ctx.sessionPersistence.list())
  141. .find(candidate => candidate.id === SEED_ID)
  142. if (header === undefined) throw new Error('seeded Session log disappeared before deletion')
  143. const logLocation = scaffold.ctx.sessionPersistence.locate(header)
  144. if (logLocation === undefined) throw new Error('JSONL persistence did not expose the seeded log path')
  145. expect(await readFile(join(scaffold.workspaceCwd, 'workspace', 'a.txt'), 'utf8')).toBe('alpha\n')
  146. await stat(logLocation.path)
  147. // Open the seeded (first/accounted) Session so deletion must preserve the
  148. // current selection while it moves into Ungrouped.
  149. const groupRow = page.locator('[role="treeitem"]').filter({ hasText: workspace.title }).first()
  150. await groupRow.waitFor({ timeout: 10_000 })
  151. const groupSection = groupRow.locator('..')
  152. if (await groupSection.locator('[role="treeitem"]').count() < 2) await groupRow.click()
  153. await expect.poll(
  154. () => groupSection.locator('[role="treeitem"]').count(),
  155. { timeout: 10_000 },
  156. ).toBeGreaterThanOrEqual(2)
  157. const seededRow = groupSection.locator('[role="treeitem"]').nth(1)
  158. await seededRow.click()
  159. await expect.poll(() => seededRow.getAttribute('aria-selected'), { timeout: 10_000 }).toBe('true')
  160. await groupRow.hover()
  161. await page.getByRole('button', { name: `Workspace actions for ${workspace.title}` }).click()
  162. await page.getByRole('menuitem', { name: 'Delete workspace' }).click()
  163. const dialog = page.getByRole('dialog', { name: 'Delete workspace' })
  164. await dialog.waitFor({ timeout: 10_000 })
  165. const copy = await dialog.textContent()
  166. expect(copy).toContain('workspace list')
  167. expect(copy).toContain('folder and session logs will be kept')
  168. expect(copy).toContain('sessions will appear under Ungrouped')
  169. await dialog.getByRole('button', { name: 'Delete workspace' }).click()
  170. await expect.poll(() => dialog.count(), { timeout: 10_000 }).toBe(0)
  171. expect(scaffold.ctx.workspace.get(workspace.id)).toBeUndefined()
  172. await expect.poll(
  173. () => page.getByRole('button', { name: `Workspace actions for ${workspace.title}` }).count(),
  174. { timeout: 10_000 },
  175. ).toBe(0)
  176. await expect.poll(() => page.getByText('Ungrouped', { exact: true }).count(), { timeout: 10_000 })
  177. .toBeGreaterThanOrEqual(1)
  178. await expect.poll(
  179. () => page.locator('[role="treeitem"][aria-selected="true"]').count(),
  180. { timeout: 10_000 },
  181. ).toBe(1)
  182. expect(await readFile(join(scaffold.workspaceCwd, 'workspace', 'a.txt'), 'utf8')).toBe('alpha\n')
  183. await stat(logLocation.path)
  184. expect((await scaffold.ctx.sessionPersistence.inspect(SessionId(SEED_ID))).events.length).toBeGreaterThan(0)
  185. // Re-registering the exact deleted path immediately, without a reload, is
  186. // a supported reversible flow. It creates a fresh Workspace id without
  187. // re-adopting the retained Session.
  188. await page.getByRole('button', { name: 'Create workspace' }).click()
  189. await page.getByRole('menuitem', { name: 'Create workspace' }).hover()
  190. await page.getByRole('menuitem', { name: 'Use an existing folder' }).click()
  191. const reuseFolder = page.getByRole('dialog', { name: 'Use an existing folder' })
  192. await reuseFolder.getByLabel('Existing folder path').fill(scaffold.workspaceCwd)
  193. await reuseFolder.getByRole('button', { name: 'Use folder' }).click()
  194. await expect.poll(() => reuseFolder.count(), { timeout: 10_000 }).toBe(0)
  195. const reregistered = await scaffold.ctx.workspace.resolveByPath(scaffold.workspaceCwd)
  196. expect(reregistered?.id).toBeDefined()
  197. expect(reregistered?.id).not.toBe(workspace.id)
  198. expect(reregistered?.sessionIds).toEqual([])
  199. await expect.poll(() => page.getByText('Ungrouped', { exact: true }).count(), { timeout: 10_000 })
  200. .toBeGreaterThanOrEqual(1)
  201. expect(await readFile(join(scaffold.workspaceCwd, 'workspace', 'a.txt'), 'utf8')).toBe('alpha\n')
  202. await stat(logLocation.path)
  203. // Restore the deleted-registry state so reload still verifies deletion
  204. // persistence independently of the successful re-registration above.
  205. if (reregistered === undefined) throw new Error('same-path re-registration did not materialize')
  206. await scaffold.ctx.workspace.delete(reregistered.id)
  207. await expect.poll(
  208. () => page.getByRole('button', { name: `Workspace actions for ${reregistered.title}` }).count(),
  209. { timeout: 10_000 },
  210. ).toBe(0)
  211. const warningStart = tripwire.warnings.length
  212. await page.reload({ waitUntil: 'load' })
  213. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  214. acknowledgeReloadConnectionLoss(tripwire, warningStart)
  215. await expect.poll(() => page.getByText('Ungrouped', { exact: true }).count(), { timeout: 15_000 })
  216. .toBeGreaterThanOrEqual(1)
  217. await expect.poll(
  218. () => page.locator('[role="treeitem"][aria-selected="true"]').count(),
  219. { timeout: 15_000 },
  220. ).toBe(1)
  221. expect(scaffold.ctx.workspace.get(workspace.id)).toBeUndefined()
  222. expect(await readFile(join(scaffold.workspaceCwd, 'workspace', 'a.txt'), 'utf8')).toBe('alpha\n')
  223. await stat(logLocation.path)
  224. expect((await scaffold.ctx.sessionPersistence.inspect(SessionId(SEED_ID))).events.length).toBeGreaterThan(0)
  225. expect(transientSlotErrors).toEqual([])
  226. expect(slotConsoleErrors).toEqual([])
  227. expect(tripwire.pageErrors).toEqual([])
  228. }, 90_000)
  229. it('reuses a deleted title for a different new directory without any transient error surface', async () => {
  230. onTestFailed(() => saveFailureShot(page, 'web-e2e-ws-reuse-title'))
  231. const title = 'same-name'
  232. const oldPath = join(scaffold.workspaceCwd, 'adopted', title)
  233. await mkdir(oldPath, { recursive: true })
  234. const transientErrors: string[] = []
  235. const consoleErrors: string[] = []
  236. page.on('console', (message) => {
  237. if (message.type() === 'error') consoleErrors.push(message.text())
  238. })
  239. await page.exposeFunction('recordDshTransientWorkspaceError', (message: string) => {
  240. if (!transientErrors.includes(message)) transientErrors.push(message)
  241. })
  242. await page.evaluate(() => {
  243. const target = window as unknown as {
  244. recordDshTransientWorkspaceError(message: string): Promise<void>
  245. }
  246. const collect = (): void => {
  247. for (const node of document.querySelectorAll<HTMLElement>('[data-slot-error], [role="alert"]')) {
  248. const message = node.dataset.slotError ?? node.textContent?.trim() ?? ''
  249. if (message !== '') void target.recordDshTransientWorkspaceError(message)
  250. }
  251. }
  252. new MutationObserver(collect).observe(document.documentElement, { childList: true, subtree: true })
  253. collect()
  254. })
  255. await page.getByRole('button', { name: 'Create workspace' }).click()
  256. await page.getByRole('menuitem', { name: 'Create workspace' }).hover()
  257. await page.getByRole('menuitem', { name: 'Use an existing folder' }).click()
  258. const adopt = page.getByRole('dialog', { name: 'Use an existing folder' })
  259. await adopt.getByLabel('Existing folder path').fill(oldPath)
  260. await adopt.getByRole('button', { name: 'Use folder' }).click()
  261. await expect.poll(() => adopt.count(), { timeout: 10_000 }).toBe(0)
  262. const oldWorkspace = await scaffold.ctx.workspace.resolveByPath(oldPath)
  263. if (oldWorkspace === undefined) throw new Error('old same-name Workspace was not registered')
  264. const oldRow = page.locator('[role="treeitem"]').filter({ hasText: title }).first()
  265. await oldRow.hover()
  266. await page.getByRole('button', { name: `Workspace actions for ${title}` }).click()
  267. await page.getByRole('menuitem', { name: 'Delete workspace' }).click()
  268. await page.getByRole('dialog', { name: 'Delete workspace' })
  269. .getByRole('button', { name: 'Delete workspace' }).click()
  270. await expect.poll(() => scaffold.ctx.workspace.get(oldWorkspace.id), { timeout: 10_000 }).toBeUndefined()
  271. await page.getByRole('button', { name: 'Create workspace' }).click()
  272. await page.getByRole('menuitem', { name: 'Create workspace' }).hover()
  273. await page.getByRole('menuitem', { name: 'Create a new workspace' }).click()
  274. const create = page.getByRole('dialog', { name: 'Create a new workspace' })
  275. await create.getByLabel('New workspace name').fill(title)
  276. await create.getByRole('button', { name: 'Create workspace' }).click()
  277. await expect.poll(() => create.count(), { timeout: 10_000 }).toBe(0)
  278. const fresh = scaffold.ctx.workspace.list().find(workspace => workspace.title === title)
  279. expect(fresh?.id).toBeDefined()
  280. expect(fresh?.id).not.toBe(oldWorkspace.id)
  281. expect(fresh?.path).toBe(join(scaffold.workspaceCwd, title))
  282. expect(transientErrors).toEqual([])
  283. expect(consoleErrors).toEqual([])
  284. expect(tripwire.pageErrors).toEqual([])
  285. }, 90_000)
  286. it('switches to the flat "In one list" view and persists the preference', async () => {
  287. onTestFailed(() => saveFailureShot(page, 'web-e2e-ws-flat'))
  288. // Grouped default: workspace group rows render (the seeded session sits
  289. // under Ungrouped; the created workspaces are empty groups).
  290. await expect.poll(() => page.getByText('Workspaces', { exact: true }).count(), { timeout: 10_000 }).toBe(1)
  291. await page.getByRole('button', { name: 'Group by' }).click()
  292. await page.getByRole('menuitem', { name: 'In one list' }).click()
  293. // Flat mode: the section label flips and the seeded session is a
  294. // top-level row with no group headers above it.
  295. await expect.poll(() => page.getByText('Sessions', { exact: true }).count(), { timeout: 10_000 }).toBeGreaterThanOrEqual(1)
  296. await expect.poll(() => page.getByText('Ungrouped', { exact: true }).count(), { timeout: 5_000 }).toBe(0)
  297. await expect.poll(() => page.locator('[role="treeitem"]').count(), { timeout: 10_000 }).toBeGreaterThanOrEqual(1)
  298. expect(await page.evaluate(() => localStorage.getItem('dsh.workspace.view'))).toContain('flat')
  299. // Persisted across reload; then restore grouped for inter-spec hygiene.
  300. const warningStart = tripwire.warnings.length
  301. await page.reload({ waitUntil: 'load' })
  302. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  303. acknowledgeReloadConnectionLoss(tripwire, warningStart)
  304. await expect.poll(() => page.getByText('Ungrouped', { exact: true }).count(), { timeout: 15_000 }).toBe(0)
  305. await page.getByRole('button', { name: 'Group by' }).click()
  306. await page.getByRole('menuitem', { name: 'WorkSpace' }).click()
  307. await expect.poll(() => page.getByText('Ungrouped', { exact: true }).count(), { timeout: 10_000 }).toBeGreaterThanOrEqual(1)
  308. expect(tripwire.pageErrors).toEqual([])
  309. }, 90_000)
  310. it('shows the session hover card after a dwell on the row', async () => {
  311. onTestFailed(() => saveFailureShot(page, 'web-e2e-ws-hover'))
  312. // Expand Ungrouped to reveal the seeded session row, then dwell on it
  313. // (the card opens after a 500ms hover delay, portaled to body).
  314. const ungroupedRow = page.getByText('Ungrouped', { exact: true }).locator('..').locator('..')
  315. const ungroupedSection = ungroupedRow.locator('..')
  316. // Initial-current auto-expansion can race this following test's gesture;
  317. // converge on expanded rather than assuming which update wins first.
  318. await expect.poll(async () => {
  319. if (await ungroupedRow.getAttribute('aria-expanded') !== 'true') {
  320. await page.getByText('Ungrouped', { exact: true }).click()
  321. await page.waitForTimeout(50)
  322. }
  323. return await ungroupedRow.getAttribute('aria-expanded')
  324. }, { timeout: 5_000 }).toBe('true')
  325. // The only visible child is the non-blank persisted Session; the blank
  326. // Session created while adopting the Workspace remains hidden.
  327. const sessionRow = ungroupedSection.locator('[role="treeitem"]').nth(1)
  328. await sessionRow.waitFor({ timeout: 10_000 })
  329. await sessionRow.hover()
  330. // Card content: the full title plus the Idle status line (display-only
  331. // card; no aria role — text anchors are the stable selector).
  332. await expect.poll(() => page.getByText('Idle', { exact: true }).count(), { timeout: 5_000 }).toBeGreaterThanOrEqual(1)
  333. // Leaving the anchor closes it with no delay.
  334. await page.getByRole('button', { name: '设置' }).hover()
  335. await expect.poll(() => page.getByText('Idle', { exact: true }).count(), { timeout: 5_000 }).toBe(0)
  336. expect(tripwire.pageErrors).toEqual([])
  337. }, 60_000)
  338. it.skipIf(MODE === 'record')('issued zero model calls and stayed clean', async () => {
  339. expect(tripwire.warnings).toEqual([])
  340. // This spec mints no fixture directory contents of its own; the seed it
  341. // reuses is owned (and inventory-guarded) by seeded-history.
  342. await assertFixtureInventory(SNAPSHOT_DIR, ['.gitkeep'])
  343. })
  344. })