workspace-management.e2e.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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, 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 {
  15. acknowledgeReloadConnectionLoss, assertFixtureInventory, launchWebScaffold, seedSession, watchConsole,
  16. webSnapshotMode, type WebScaffold,
  17. } from './scaffold.ts'
  18. import { saveFailureShot } from './support.ts'
  19. const SNAPSHOT_DIR = fileURLToPath(new URL('./snapshots/workspace-management', import.meta.url))
  20. // The seed is another scenario's committed fixture, reused read-only: this
  21. // spec needs any one cold session row, not new recorded content.
  22. const SEED = fileURLToPath(new URL('./snapshots/seeded-history/seed.jsonl', import.meta.url))
  23. const MODE = webSnapshotMode()
  24. const SEED_ID = 'workspace-management-web-e2e'
  25. describe('web e2e: workspace management (create / rename / flat view / hover card)', () => {
  26. let scaffold: WebScaffold
  27. let browser: Browser
  28. let page: Page
  29. let tripwire: ReturnType<typeof watchConsole>
  30. beforeAll(async () => {
  31. scaffold = await launchWebScaffold({})
  32. // Seed one cold session (Ungrouped bucket) for the flat view + hover card.
  33. const sessionCwd = join(scaffold.workspaceCwd, 'workspace')
  34. await mkdir(sessionCwd, { recursive: true })
  35. await writeFile(join(sessionCwd, 'a.txt'), 'alpha\n')
  36. await writeFile(join(sessionCwd, 'b.txt'), 'beta\n')
  37. await seedSession(scaffold, await readFile(SEED, 'utf8'), SEED_ID)
  38. browser = await chromium.launch()
  39. page = await browser.newPage({ viewport: { width: 1680, height: 1000 } })
  40. tripwire = watchConsole(page)
  41. await page.goto(scaffold.baseUrl, { waitUntil: 'load' })
  42. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  43. }, 120_000)
  44. afterAll(async () => {
  45. await browser?.close()
  46. await scaffold?.close()
  47. })
  48. it('creates two workspaces by name through the region-header dialog', async () => {
  49. onTestFailed(() => saveFailureShot(page, 'web-e2e-ws-create'))
  50. const createByName = async (name: string): Promise<void> => {
  51. await page.getByRole('button', { name: 'Create workspace' }).click()
  52. // The pick menu's Create workspace submenu opens on hover/focus.
  53. await page.getByRole('menuitem', { name: 'Create workspace' }).hover()
  54. await page.getByRole('menuitem', { name: 'Create a new workspace' }).click()
  55. const dialog = page.getByRole('dialog', { name: 'Create a new workspace' })
  56. await dialog.waitFor({ timeout: 10_000 })
  57. await dialog.getByLabel('New workspace name').fill(name)
  58. await dialog.getByRole('button', { name: 'Create workspace' }).click()
  59. await expect.poll(() => page.getByRole('dialog', { name: 'Create a new workspace' }).count(), { timeout: 10_000 }).toBe(0)
  60. // The real workspace materializes in the tree as a group row.
  61. await expect.poll(() => page.getByText(name, { exact: true }).count(), { timeout: 10_000 }).toBeGreaterThanOrEqual(1)
  62. }
  63. await createByName('alpha-ws')
  64. await createByName('beta-ws')
  65. // Durable on the host: both registered, newest first (create prepends).
  66. const titles = scaffold.ctx.workspace.list().map(workspace => workspace.title)
  67. expect(titles.slice(0, 2)).toEqual(['beta-ws', 'alpha-ws'])
  68. expect(tripwire.pageErrors).toEqual([])
  69. }, 90_000)
  70. it('renames a workspace over the wire with a duplicate-name pre-check', async () => {
  71. onTestFailed(() => saveFailureShot(page, 'web-e2e-ws-rename'))
  72. // The actions button is display:none until its row hovers — hover the
  73. // group row first, then the revealed button becomes actionable.
  74. await page.locator('[role="treeitem"]').filter({ hasText: 'alpha-ws' }).first().hover()
  75. await page.getByRole('button', { name: 'Workspace actions for alpha-ws' }).click()
  76. await page.getByRole('menuitem', { name: 'Rename' }).click()
  77. const dialog = page.getByRole('dialog', { name: 'Rename workspace' })
  78. await dialog.waitFor({ timeout: 10_000 })
  79. const input = dialog.getByLabel('Workspace name')
  80. // Client pre-check: a name colliding with another live workspace raises
  81. // the inline alert and blocks the primary button before any wire call.
  82. await input.fill('beta-ws')
  83. await expect.poll(() => dialog.getByRole('alert').count(), { timeout: 5_000 }).toBe(1)
  84. expect(await dialog.getByRole('button', { name: 'Rename' }).isDisabled()).toBe(true)
  85. // A fresh name goes through workspace.rename to the durable registry.
  86. await input.fill('gamma-ws')
  87. await expect.poll(() => dialog.getByRole('alert').count(), { timeout: 5_000 }).toBe(0)
  88. await dialog.getByRole('button', { name: 'Rename' }).click()
  89. await expect.poll(() => page.getByRole('dialog', { name: 'Rename workspace' }).count(), { timeout: 10_000 }).toBe(0)
  90. await expect.poll(() => page.getByText('gamma-ws', { exact: true }).count(), { timeout: 10_000 }).toBeGreaterThanOrEqual(1)
  91. expect(await page.getByText('alpha-ws', { exact: true }).count()).toBe(0)
  92. // Host durability, then reload: the projection is rebuilt from the wire.
  93. expect(scaffold.ctx.workspace.list().map(workspace => workspace.title)).toContain('gamma-ws')
  94. const warningStart = tripwire.warnings.length
  95. await page.reload({ waitUntil: 'load' })
  96. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  97. acknowledgeReloadConnectionLoss(tripwire, warningStart)
  98. await expect.poll(() => page.getByText('gamma-ws', { exact: true }).count(), { timeout: 15_000 }).toBeGreaterThanOrEqual(1)
  99. expect(tripwire.pageErrors).toEqual([])
  100. }, 90_000)
  101. it('switches to the flat "In one list" view and persists the preference', async () => {
  102. onTestFailed(() => saveFailureShot(page, 'web-e2e-ws-flat'))
  103. // Grouped default: workspace group rows render (the seeded session sits
  104. // under Ungrouped; the created workspaces are empty groups).
  105. await expect.poll(() => page.getByText('Workspaces', { exact: true }).count(), { timeout: 10_000 }).toBe(1)
  106. await page.getByRole('button', { name: 'Group by' }).click()
  107. await page.getByRole('menuitem', { name: 'In one list' }).click()
  108. // Flat mode: the section label flips and the seeded session is a
  109. // top-level row with no group headers above it.
  110. await expect.poll(() => page.getByText('Sessions', { exact: true }).count(), { timeout: 10_000 }).toBeGreaterThanOrEqual(1)
  111. await expect.poll(() => page.getByText('Ungrouped', { exact: true }).count(), { timeout: 5_000 }).toBe(0)
  112. await expect.poll(() => page.locator('[role="treeitem"]').count(), { timeout: 10_000 }).toBeGreaterThanOrEqual(1)
  113. expect(await page.evaluate(() => localStorage.getItem('dsh.workspace.view'))).toContain('flat')
  114. // Persisted across reload; then restore grouped for inter-spec hygiene.
  115. const warningStart = tripwire.warnings.length
  116. await page.reload({ waitUntil: 'load' })
  117. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  118. acknowledgeReloadConnectionLoss(tripwire, warningStart)
  119. await expect.poll(() => page.getByText('Ungrouped', { exact: true }).count(), { timeout: 15_000 }).toBe(0)
  120. await page.getByRole('button', { name: 'Group by' }).click()
  121. await page.getByRole('menuitem', { name: 'WorkSpace' }).click()
  122. await expect.poll(() => page.getByText('Ungrouped', { exact: true }).count(), { timeout: 10_000 }).toBeGreaterThanOrEqual(1)
  123. expect(tripwire.pageErrors).toEqual([])
  124. }, 90_000)
  125. it('shows the session hover card after a dwell on the row', async () => {
  126. onTestFailed(() => saveFailureShot(page, 'web-e2e-ws-hover'))
  127. // Expand Ungrouped to reveal the seeded session row, then dwell on it
  128. // (the card opens after a 500ms hover delay, portaled to body).
  129. await page.getByText('Ungrouped', { exact: true }).click()
  130. // A cold summary carries no durable title, so the row falls back to a
  131. // cwd-derived display title — anchored on the run-local workspace-root
  132. // basename rather than a literal.
  133. const wsBase = scaffold.workspaceCwd.split('/').pop()!
  134. const sessionRow = page.locator('[role="treeitem"]').filter({ hasText: wsBase }).first()
  135. await sessionRow.waitFor({ timeout: 10_000 })
  136. await sessionRow.hover()
  137. // Card content: the full title plus the Idle status line (display-only
  138. // card; no aria role — text anchors are the stable selector).
  139. await expect.poll(() => page.getByText('Idle', { exact: true }).count(), { timeout: 5_000 }).toBeGreaterThanOrEqual(1)
  140. // Leaving the anchor closes it with no delay.
  141. await page.getByRole('button', { name: '设置' }).hover()
  142. await expect.poll(() => page.getByText('Idle', { exact: true }).count(), { timeout: 5_000 }).toBe(0)
  143. expect(tripwire.pageErrors).toEqual([])
  144. }, 60_000)
  145. it.skipIf(MODE === 'record')('issued zero model calls and stayed clean', async () => {
  146. expect(tripwire.warnings).toEqual([])
  147. // This spec mints no fixture directory contents of its own; the seed it
  148. // reuses is owned (and inventory-guarded) by seeded-history.
  149. await assertFixtureInventory(SNAPSHOT_DIR, ['.gitkeep'])
  150. })
  151. })