queue-actions.e2e.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // Keyless browser coverage for pending queue actions through the shipped Web
  2. // composition and real HTTP/SSE wire. Replay overrides park consecutive turns
  3. // so the page can edit and remove exact occurrences, then stop the active turn
  4. // while proving the preserved Queue advances in FIFO order.
  5. import { existsSync } from 'node:fs'
  6. import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
  7. import { tmpdir } from 'node:os'
  8. import { fileURLToPath } from 'node:url'
  9. import { join } from 'node:path'
  10. import type { Browser, Page } from 'playwright'
  11. import { chromium } from 'playwright'
  12. import { afterEach, describe, expect, it, onTestFailed } from 'vitest'
  13. import { deriveReplayScript, parseSessionLog, type ReplayEntry } from '@deepseek-ai/dsh-llm-replay'
  14. import type { SessionEvent } from '@deepseek-ai/dsh-session'
  15. import {
  16. assertFixtureInventory, captureExpandedTurnProcessAria, captureStableAria, compareOrRefreshGolden,
  17. launchWebScaffold, watchConsole, webSnapshotMode, type WebScaffold,
  18. } from './scaffold.ts'
  19. import { connectFreshWorkspace, newEnglishPage, saveFailureShot } from './support.ts'
  20. const SNAPSHOT_DIR = fileURLToPath(new URL('../../../snapshots/web/queue-actions', import.meta.url))
  21. const FIXTURE = fileURLToPath(new URL('../../../snapshots/web/live-interactions/session.jsonl', import.meta.url))
  22. const COLLAPSED_EXPECTED = join(SNAPSHOT_DIR, 'collapsed.expected.md')
  23. const EDITING_EXPECTED = join(SNAPSHOT_DIR, 'editing.expected.md')
  24. const LAYOUT_EXPECTED = join(SNAPSHOT_DIR, 'layout.expected.md')
  25. const PRESERVED_EXPECTED = join(SNAPSHOT_DIR, 'preserved.expected.md')
  26. const PRESERVED_EXPANDED_EXPECTED = join(SNAPSHOT_DIR, 'preserved-expanded.expected.md')
  27. const UI_EXPECTED = join(SNAPSHOT_DIR, 'ui.expected.md')
  28. const MODE = webSnapshotMode()
  29. const ACTIVE_PROMPT = 'Reply with a one-sentence description of event sourcing, then stop.'
  30. const REMOVE = 'Queue item to remove'
  31. const EDIT = 'Queue item to edit'
  32. const EDITED = 'Edited queue item'
  33. const TAIL = 'Queue item preserved after stop'
  34. const WAKE = 'Wake the preserved queue'
  35. /** Durable turn-end classifications observed by the scenario. */
  36. function turnEndReasons(events: readonly SessionEvent[]): string[] {
  37. return events.flatMap(event => event.type === 'turn/end' ? [event.data.reason.kind] : [])
  38. }
  39. describe('web e2e: queue row actions', () => {
  40. let scaffold: WebScaffold | undefined
  41. let browser: Browser | undefined
  42. let page: Page
  43. let overrideDir: string | undefined
  44. afterEach(async () => {
  45. const failures: unknown[] = []
  46. await browser?.close().catch((error: unknown) => failures.push(error))
  47. browser = undefined
  48. const closing = scaffold
  49. scaffold = undefined
  50. await closing?.close().catch((error: unknown) => failures.push(error))
  51. if (overrideDir !== undefined) {
  52. await rm(overrideDir, { recursive: true, force: true })
  53. .catch((error: unknown) => failures.push(error))
  54. }
  55. overrideDir = undefined
  56. if (failures.length === 1) throw failures[0]
  57. if (failures.length > 1) throw new AggregateError(failures, 'queue-actions teardown failed')
  58. })
  59. it.skipIf(MODE === 'record')('edits and removes exact occurrences and preserves Queue across stop', async () => {
  60. overrideDir = await mkdtemp(join(tmpdir(), 'dsh-web-queue-actions-'))
  61. const readyFile = join(overrideDir, '.hang-ready')
  62. const overridePath = join(overrideDir, 'replay.override.json')
  63. const recorded = deriveReplayScript(parseSessionLog(await readFile(FIXTURE, 'utf8')))
  64. expect(recorded).toHaveLength(1)
  65. const replay: ReplayEntry[] = [
  66. { kind: 'hang', readyFile },
  67. recorded[0]!,
  68. recorded[0]!,
  69. recorded[0]!,
  70. ]
  71. await writeFile(overridePath, JSON.stringify(replay))
  72. const sessionEvents: SessionEvent[] = []
  73. scaffold = await launchWebScaffold({ replayFixture: FIXTURE, replayOverride: overridePath, compareReplaySession: false })
  74. scaffold.ctx.on('session/event', (_session, event: SessionEvent) => { sessionEvents.push(event) })
  75. browser = await chromium.launch()
  76. page = await newEnglishPage(browser)
  77. const tripwire = watchConsole(page)
  78. await page.goto(scaffold.authenticatedUrl, { waitUntil: 'load' })
  79. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  80. await connectFreshWorkspace(page, scaffold.workspaceCwd)
  81. onTestFailed(() => saveFailureShot(page, 'web-e2e-queue-actions'))
  82. const input = page.locator('[data-composer-input]').first()
  83. const firstSettled = scaffold.whenTurnSettled()
  84. await input.fill(ACTIVE_PROMPT)
  85. await input.press('Enter')
  86. await expect.poll(() => existsSync(readyFile), { timeout: 15_000 }).toBe(true)
  87. for (const text of [REMOVE, EDIT]) {
  88. // A just-submitted composer is read-only for the prompt round-trip.
  89. await page.locator('[data-composer-input][contenteditable="true"]').first().waitFor({ timeout: 10_000 })
  90. await input.fill(text)
  91. await input.press('Enter')
  92. }
  93. const queueHeader = page.getByRole('button', { name: '2 queued messages' })
  94. await expect.poll(() => queueHeader.getAttribute('aria-expanded'), { timeout: 10_000 })
  95. .toBe('false')
  96. const collapsedSnapshot = await captureStableAria(
  97. page,
  98. '[class*="centerCol"]',
  99. scaffold.workspaceCwd,
  100. )
  101. await compareOrRefreshGolden(COLLAPSED_EXPECTED, collapsedSnapshot, MODE)
  102. await queueHeader.click()
  103. await expect.poll(
  104. () => page.getByRole('button', { name: 'Remove queued message' }).count(),
  105. { timeout: 10_000 },
  106. ).toBe(2)
  107. await page.setViewportSize({ width: 640, height: 1000 })
  108. const queueBox = await page.locator('[data-queue-dock]').boundingBox()
  109. const composerBox = await page.locator('[data-composer-card]').boundingBox()
  110. expect(queueBox).not.toBeNull()
  111. expect(composerBox).not.toBeNull()
  112. expect(queueBox!.x).toBeGreaterThanOrEqual(composerBox!.x)
  113. expect(queueBox!.x + queueBox!.width)
  114. .toBeLessThanOrEqual(composerBox!.x + composerBox!.width)
  115. const queueLeftInset = queueBox!.x - composerBox!.x
  116. const queueRightInset = composerBox!.x + composerBox!.width - queueBox!.x - queueBox!.width
  117. const composerMetrics = await page.locator('[data-composer-card]').evaluate((element) => {
  118. const style = getComputedStyle(element)
  119. return {
  120. dockInset: Number.parseFloat(style.getPropertyValue('--dsh-composer-dock-inset')),
  121. }
  122. })
  123. expect(queueLeftInset).toBeCloseTo(composerMetrics.dockInset, 1)
  124. expect(queueRightInset).toBeCloseTo(composerMetrics.dockInset, 1)
  125. await page.setViewportSize({ width: 1680, height: 1000 })
  126. const editRow = page.locator('[data-queue-dock] li', { hasText: EDIT })
  127. await editRow.getByRole('button', { name: 'Edit queued message' }).click()
  128. const editor = page.getByRole('textbox', { name: 'Edit queued message' })
  129. await editor.fill(EDITED)
  130. const editingSnapshot = await captureStableAria(page, '[class*="centerCol"]', scaffold.workspaceCwd)
  131. await compareOrRefreshGolden(EDITING_EXPECTED, editingSnapshot, MODE)
  132. await page.getByRole('button', { name: 'Save queued message' }).click()
  133. await page.getByText(EDITED, { exact: true }).waitFor()
  134. const removeRow = page.locator('[data-queue-dock] li', { hasText: REMOVE })
  135. await removeRow.getByRole('button', { name: 'Remove queued message' }).click()
  136. await expect.poll(() => page.getByText(REMOVE, { exact: true }).count()).toBe(0)
  137. const snapshot = await captureStableAria(page, '[class*="centerCol"]', scaffold.workspaceCwd)
  138. await compareOrRefreshGolden(UI_EXPECTED, snapshot, MODE)
  139. expect(sessionEvents.filter(event => event.type === 'user/message' && event.data.source.kind === 'user')).toHaveLength(1)
  140. expect(tripwire.pageErrors).toEqual([])
  141. expect(tripwire.warnings).toEqual([])
  142. await input.fill(TAIL)
  143. await input.press('Enter')
  144. await expect.poll(
  145. () => page.getByRole('button', { name: 'Remove queued message' }).count(),
  146. { timeout: 10_000 },
  147. ).toBe(2)
  148. await page.getByRole('button', { name: 'Stop generating' }).click()
  149. await firstSettled
  150. await expect.poll(() => page.getByRole('button', { name: 'Stop generating' }).count())
  151. .toBe(0)
  152. await expect.poll(() => page.getByRole('button', { name: 'Remove queued message' }).count())
  153. .toBe(2)
  154. const preservedSnapshot = await captureStableAria(page, '[class*="centerCol"]', scaffold.workspaceCwd)
  155. await compareOrRefreshGolden(PRESERVED_EXPECTED, preservedSnapshot, MODE)
  156. const expanded = await captureExpandedTurnProcessAria(
  157. page,
  158. '[class*="centerCol"]',
  159. scaffold.workspaceCwd,
  160. )
  161. await compareOrRefreshGolden(PRESERVED_EXPANDED_EXPECTED, expanded, MODE)
  162. const settled = scaffold.whenTurnSettled()
  163. await input.fill(WAKE)
  164. await input.press('Enter')
  165. await settled
  166. await expect.poll(() => turnEndReasons(sessionEvents), { timeout: 15_000 })
  167. .toEqual(['aborted', 'completed', 'completed', 'completed'])
  168. expect(sessionEvents.flatMap(event => event.type === 'user/message' && event.data.source.kind === 'user'
  169. ? event.data.content.flatMap(block => block.type === 'text' ? [block.text] : [])
  170. : [])).toEqual([ACTIVE_PROMPT, EDITED, TAIL, WAKE])
  171. await expect.poll(() => page.locator('[data-queue-dock]').count()).toBe(0)
  172. }, 120_000)
  173. it.skipIf(MODE === 'record')('orders Todo before Goal and Queue on one responsive card column', async () => {
  174. overrideDir = await mkdtemp(join(tmpdir(), 'dsh-web-context-layout-'))
  175. const readyFile = join(overrideDir, '.hang-ready')
  176. const overridePath = join(overrideDir, 'replay.override.json')
  177. await writeFile(overridePath, JSON.stringify([{ kind: 'hang', readyFile } satisfies ReplayEntry]))
  178. const sessionEvents: SessionEvent[] = []
  179. scaffold = await launchWebScaffold({ replayFixture: FIXTURE, replayOverride: overridePath, compareReplaySession: false })
  180. scaffold.ctx.on('session/event', (_session, event: SessionEvent) => { sessionEvents.push(event) })
  181. browser = await chromium.launch()
  182. page = await newEnglishPage(browser)
  183. const tripwire = watchConsole(page)
  184. await page.goto(scaffold.authenticatedUrl, { waitUntil: 'load' })
  185. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  186. await connectFreshWorkspace(page, scaffold.workspaceCwd)
  187. onTestFailed(() => saveFailureShot(page, 'web-e2e-context-layout'))
  188. const input = page.locator('[data-composer-input]').first()
  189. const settled = scaffold.whenTurnSettled()
  190. await page.locator('[data-composer-input][contenteditable="true"]').first().waitFor({ timeout: 10_000 })
  191. await input.fill('/goal Keep the composer context panels aligned')
  192. await input.press('Enter')
  193. await expect.poll(() => existsSync(readyFile), { timeout: 15_000 }).toBe(true)
  194. await page.locator('[data-goal-bar]').waitFor({ timeout: 10_000 })
  195. const sessions = scaffold.ctx.sessions.list()
  196. expect(sessions).toHaveLength(1)
  197. sessions[0]!.append('todo/write', {
  198. todos: [
  199. { content: 'Confirm the panel order', status: 'completed' },
  200. { content: 'Align the panel widths', status: 'in_progress' },
  201. ],
  202. })
  203. await page.locator('[data-testid="todo-panel"]').waitFor({ timeout: 10_000 })
  204. for (const text of ['Layout queue first', 'Layout queue second']) {
  205. // A just-submitted composer is read-only for the prompt round-trip.
  206. await page.locator('[data-composer-input][contenteditable="true"]').first().waitFor({ timeout: 10_000 })
  207. await input.fill(text)
  208. await input.press('Enter')
  209. }
  210. const queueHeader = page.getByRole('button', { name: '2 queued messages' })
  211. await expect.poll(() => queueHeader.getAttribute('aria-expanded'), { timeout: 10_000 })
  212. .toBe('false')
  213. const layoutSnapshot = await captureStableAria(
  214. page,
  215. '[class*="centerCol"]',
  216. scaffold.workspaceCwd,
  217. )
  218. await compareOrRefreshGolden(LAYOUT_EXPECTED, layoutSnapshot, MODE)
  219. const expectAlignedContextPanels = async () => {
  220. const queuePanelBox = await page.locator('[data-queue-dock] > div').boundingBox()
  221. const todoBox = await page.locator('[data-testid="todo-panel"]').boundingBox()
  222. const goalBox = await page.locator('[data-goal-bar] > div').boundingBox()
  223. expect(queuePanelBox).not.toBeNull()
  224. expect(todoBox).not.toBeNull()
  225. expect(goalBox).not.toBeNull()
  226. expect(todoBox!.y).toBeLessThan(goalBox!.y)
  227. expect(goalBox!.y).toBeLessThan(queuePanelBox!.y)
  228. expect(todoBox!.x).toBeCloseTo(goalBox!.x, 1)
  229. expect(todoBox!.x).toBeCloseTo(queuePanelBox!.x, 1)
  230. expect(todoBox!.width).toBeCloseTo(goalBox!.width, 1)
  231. expect(todoBox!.width).toBeCloseTo(queuePanelBox!.width, 1)
  232. }
  233. await expectAlignedContextPanels()
  234. await page.setViewportSize({ width: 640, height: 1000 })
  235. await expectAlignedContextPanels()
  236. await page.setViewportSize({ width: 1680, height: 1000 })
  237. await queueHeader.click()
  238. const removeButtons = page.getByRole('button', { name: 'Remove queued message' })
  239. await expect.poll(() => removeButtons.count(), { timeout: 10_000 }).toBe(2)
  240. await removeButtons.first().click()
  241. await expect.poll(() => removeButtons.count(), { timeout: 10_000 }).toBe(1)
  242. await removeButtons.first().click()
  243. await expect.poll(() => page.locator('[data-queue-dock]').count(), { timeout: 10_000 }).toBe(0)
  244. await page.getByRole('button', { name: 'Clear goal' }).click()
  245. await expect.poll(() => page.locator('[data-goal-bar]').count(), { timeout: 10_000 }).toBe(0)
  246. await page.getByRole('button', { name: 'Stop generating' }).click()
  247. await settled
  248. expect(turnEndReasons(sessionEvents)).toEqual(['aborted'])
  249. expect(tripwire.pageErrors).toEqual([])
  250. expect(tripwire.warnings).toEqual([])
  251. }, 120_000)
  252. it.skipIf(MODE === 'record')('keeps its snapshot inventory closed', async () => {
  253. await assertFixtureInventory(
  254. SNAPSHOT_DIR,
  255. [
  256. 'collapsed.expected.md', 'editing.expected.md', 'layout.expected.md',
  257. 'preserved.expected.md', 'preserved-expanded.expected.md', 'ui.expected.md',
  258. ],
  259. )
  260. })
  261. })