plan-review.e2e.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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/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 APPROVED_EXPECTED = join(SNAPSHOT_DIR, 'approved.expected.md')
  28. const MODE = webSnapshotMode()
  29. // One command line: /plan enters plan mode and submits the rest as the turn's
  30. // message. The task is deliberately self-contained (nothing to explore in a
  31. // fresh workspace) so the recorded turn is a plan and its review, and the
  32. // approved continuation is one word.
  33. const TASK = 'Plan a small change: add a --greeting flag to a CLI. Do not read or write any files. '
  34. + 'Call exit_plan_mode with a short plan of at most five bullet points. '
  35. + 'Once the plan is approved, reply with the single word DONE and stop.'
  36. const LINE = `/plan ${TASK}`
  37. describe('web e2e: plan review takeover round trip', () => {
  38. let scaffold: WebScaffold
  39. let browser: Browser
  40. let page: Page
  41. let tripwire: ReturnType<typeof watchConsole>
  42. const sessionEvents: SessionEvent[] = []
  43. beforeAll(async () => {
  44. scaffold = await launchWebScaffold(MODE === 'record' ? {} : { replayFixture: FIXTURE, paceMs: 15 })
  45. scaffold.ctx.on('session/event', (_session, event: SessionEvent) => { sessionEvents.push(event) })
  46. browser = await chromium.launch()
  47. // English page: the decision copy is the surface under test, and the
  48. // golden pins one language.
  49. page = await newEnglishPage(browser)
  50. tripwire = watchConsole(page)
  51. await page.goto(scaffold.baseUrl, { waitUntil: 'load' })
  52. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  53. await connectFreshWorkspace(page)
  54. }, 120_000)
  55. afterAll(async () => {
  56. await browser?.close()
  57. await scaffold?.close()
  58. })
  59. it('reviews the plan on a decision card and approves through it', async () => {
  60. onTestFailed(() => saveFailureShot(page, 'web-e2e-plan-review'))
  61. if (MODE !== 'record') {
  62. expect(fixtureUserPrompts(await readFile(FIXTURE, 'utf8'))).toEqual([TASK])
  63. }
  64. const input = page.locator('textarea').first()
  65. await input.waitFor({ timeout: 10_000 })
  66. const settled = scaffold.whenTurnSettled(MODE === 'record' ? 180_000 : 30_000)
  67. await input.fill(LINE)
  68. await input.press('Enter')
  69. // The card takes over the input area while exit_plan_mode blocks. Its
  70. // presence is a STABLE waiting state (it stays until answered), so a plain
  71. // waitFor is race-free.
  72. const card = page.locator('[data-plan-review-key]')
  73. await card.waitFor({ timeout: MODE === 'record' ? 120_000 : 30_000 })
  74. // The plan-review request must NOT land on the generic question flow.
  75. expect(await page.locator('[data-question-key]').count()).toBe(0)
  76. await expect.poll(() => card.getByText('Plan review').count(), { timeout: 10_000 }).toBeGreaterThan(0)
  77. if (MODE !== 'record') {
  78. const snapshot = await captureStableAria(page, '[data-plan-review-key]', scaffold.workspaceCwd)
  79. await compareOrRefreshGolden(REVIEW_EXPECTED, snapshot, MODE)
  80. }
  81. await card.getByRole('button', { name: 'Approve' }).click()
  82. const sessionId = await settled
  83. if (MODE === 'record') {
  84. await recordFixture(scaffold, sessionId, FIXTURE)
  85. return
  86. }
  87. // World state: the approval reached the tool, and plan mode is left behind.
  88. const results = sessionEvents.filter(e => e.type === 'tool/result')
  89. expect(JSON.stringify(results.at(-1))).toContain('Plan approved')
  90. await expect.poll(() => page.getByText('DONE', { exact: true }).count(), { timeout: 15_000 }).toBeGreaterThanOrEqual(1)
  91. // Card gone; regular input restored.
  92. expect(await page.locator('[data-plan-review-key]').count()).toBe(0)
  93. await expect.poll(() => page.locator('textarea').first().isEnabled(), { timeout: 10_000 }).toBe(true)
  94. const snapshot = await captureStableAria(page, '[class*="centerCol"]', scaffold.workspaceCwd)
  95. await compareOrRefreshGolden(APPROVED_EXPECTED, snapshot, MODE)
  96. expect(tripwire.pageErrors).toEqual([])
  97. expect(tripwire.warnings).toEqual([])
  98. }, 200_000)
  99. it.skipIf(MODE === 'record')('keeps the fixture inventory closed', async () => {
  100. await assertFixtureInventory(SNAPSHOT_DIR, ['session.jsonl', 'review.expected.md', 'approved.expected.md'])
  101. })
  102. })