plan-review.e2e.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Web e2e scenario: the plan-review takeover. The shipped composition mounts
  2. // plan mode and its client seat, so `/plan <task>` enters plan mode for real
  3. // and the recorded turn ends on exit_plan_mode blocking against the live
  4. // userInteraction seam. The composer is then occupied by the plan decision
  5. // card — not the generic question flow — and approving it through the card
  6. // completes the turn with the approval in the log.
  7. // Replay is deterministic: the plan content arrives from replayed chunks, the
  8. // review wait is real, and the approve click is the test's own gesture (the
  9. // turn cannot complete without it, in record and replay alike).
  10. import { readFile } from 'node:fs/promises'
  11. import { fileURLToPath } from 'node:url'
  12. import { join } from 'node:path'
  13. import type { Browser, Page } from 'playwright'
  14. import { chromium } from 'playwright'
  15. import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest'
  16. import type { SessionEvent } from '@deepseek-ai/dsh-session'
  17. import {
  18. assertFixtureInventory, captureStableAria, compareOrRefreshGolden, fixtureUserPrompts,
  19. launchWebScaffold, recordFixture, watchConsole, webSnapshotMode, type WebScaffold,
  20. } from './scaffold.ts'
  21. import { connectFreshWorkspace, newEnglishPage, saveFailureShot } from './support.ts'
  22. const SNAPSHOT_DIR = fileURLToPath(new URL('../../../snapshots/web/plan-review', import.meta.url))
  23. const FIXTURE = join(SNAPSHOT_DIR, 'session.jsonl')
  24. // The waiting golden owns the decision card; the approved golden owns the
  25. // transcript the approval leaves behind — the state the card cannot see.
  26. const REVIEW_EXPECTED = join(SNAPSHOT_DIR, 'review.expected.md')
  27. const SIDEBAR_EXPECTED = join(SNAPSHOT_DIR, 'sidebar.expected.md')
  28. const APPROVED_EXPECTED = join(SNAPSHOT_DIR, 'approved.expected.md')
  29. const MODE = webSnapshotMode()
  30. // One command line: /plan enters plan mode and submits the rest as the turn's
  31. // message. The task is deliberately self-contained (nothing to explore in a
  32. // fresh workspace) so the recorded turn is a plan and its review, and the
  33. // approved continuation is one word.
  34. const TASK = 'Plan a small change: add a --greeting flag to a CLI. Do not read or write any files. '
  35. + 'Call exit_plan_mode with a short plan of at most five bullet points. '
  36. + 'Once the plan is approved, reply with the single word DONE and stop.'
  37. const LINE = `/plan ${TASK}`
  38. describe('web e2e: plan review takeover round trip', () => {
  39. let scaffold: WebScaffold
  40. let browser: Browser
  41. let page: Page
  42. let tripwire: ReturnType<typeof watchConsole>
  43. const sessionEvents: SessionEvent[] = []
  44. beforeAll(async () => {
  45. scaffold = await launchWebScaffold(MODE === 'record' ? {} : { replayFixture: FIXTURE, paceMs: 15, compareReplaySession: true })
  46. scaffold.ctx.on('session/event', (_session, event: SessionEvent) => { sessionEvents.push(event) })
  47. browser = await chromium.launch()
  48. // English page: the decision copy is the surface under test, and the
  49. // golden pins one language.
  50. page = await newEnglishPage(browser)
  51. tripwire = watchConsole(page)
  52. await page.goto(scaffold.baseUrl, { waitUntil: 'load' })
  53. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  54. await connectFreshWorkspace(page, scaffold.workspaceCwd)
  55. }, 120_000)
  56. afterAll(async () => {
  57. await browser?.close()
  58. await scaffold?.close()
  59. })
  60. it('reviews the plan on a decision card and approves through it', async () => {
  61. onTestFailed(() => saveFailureShot(page, 'web-e2e-plan-review'))
  62. if (MODE !== 'record') {
  63. expect(fixtureUserPrompts(await readFile(FIXTURE, 'utf8'))).toEqual([TASK])
  64. }
  65. const input = page.locator('textarea').first()
  66. await input.waitFor({ timeout: 10_000 })
  67. const settled = scaffold.whenTurnSettled(MODE === 'record' ? 180_000 : 30_000)
  68. await input.fill(LINE)
  69. await input.press('Enter')
  70. // The card takes over the input area while exit_plan_mode blocks. Its
  71. // presence is a STABLE waiting state (it stays until answered), so a plain
  72. // waitFor is race-free.
  73. const card = page.locator('[data-plan-review-key]')
  74. await card.waitFor({ timeout: MODE === 'record' ? 120_000 : 30_000 })
  75. // The plan-review request must NOT land on the generic question flow.
  76. expect(await page.locator('[data-question-key]').count()).toBe(0)
  77. await expect.poll(() => card.getByText('Plan review').count(), { timeout: 10_000 }).toBeGreaterThan(0)
  78. const selectedRow = page.locator('[role="treeitem"][aria-selected="true"]')
  79. await expect.poll(() => selectedRow.locator('[data-state="warning"]').count(), { timeout: 10_000 }).toBe(1)
  80. await expect.poll(() => selectedRow.getByText('Plan awaiting review', { exact: true }).count(), { timeout: 10_000 }).toBe(1)
  81. if (MODE !== 'record') {
  82. const snapshot = await captureStableAria(page, '[data-plan-review-key]', scaffold.workspaceCwd)
  83. await compareOrRefreshGolden(REVIEW_EXPECTED, snapshot, MODE)
  84. const sidebar = await captureStableAria(page, '[role="treeitem"][aria-selected="true"]', scaffold.workspaceCwd)
  85. await compareOrRefreshGolden(SIDEBAR_EXPECTED, sidebar, MODE)
  86. }
  87. await card.getByRole('button', { name: 'Approve' }).click()
  88. const sessionId = await settled
  89. if (MODE === 'record') {
  90. await recordFixture(scaffold, sessionId, FIXTURE)
  91. return
  92. }
  93. // World state: the approval reached the tool, and plan mode is left behind.
  94. const results = sessionEvents.filter(e => e.type === 'tool/result')
  95. expect(JSON.stringify(results.at(-1))).toContain('Plan approved')
  96. await expect.poll(() => page.getByText('DONE', { exact: true }).count(), { timeout: 15_000 }).toBeGreaterThanOrEqual(1)
  97. // Card gone; regular input restored.
  98. expect(await page.locator('[data-plan-review-key]').count()).toBe(0)
  99. expect(await selectedRow.locator('[data-state="warning"]').count()).toBe(0)
  100. await expect.poll(() => page.locator('textarea').first().isEnabled(), { timeout: 10_000 }).toBe(true)
  101. const snapshot = await captureStableAria(page, '[class*="centerCol"]', scaffold.workspaceCwd)
  102. await compareOrRefreshGolden(APPROVED_EXPECTED, snapshot, MODE)
  103. expect(tripwire.pageErrors).toEqual([])
  104. expect(tripwire.warnings).toEqual([])
  105. }, 200_000)
  106. it.skipIf(MODE === 'record')('keeps the fixture inventory closed', async () => {
  107. await assertFixtureInventory(SNAPSHOT_DIR, [
  108. 'session.jsonl', 'review.expected.md', 'sidebar.expected.md', 'approved.expected.md',
  109. ])
  110. })
  111. })