1
0

question-composer.e2e.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Web e2e scenario: the resident question composer. The shipped composition
  2. // already exposes ask_user_question (the ui-question row's node half mounts
  3. // the tool), so a recorded turn where the model asks blocks mid-turn on the
  4. // real userInteraction seam: the composer renders in the browser, the test
  5. // answers through it, and the turn completes with the answer in the log.
  6. // Replay is fully deterministic — the question content arrives from replayed
  7. // chunks, the composer wait is real, and the answer click is the test's own
  8. // gesture (the ONE place a drive step legitimately reacts to model content:
  9. // the 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 { saveFailureShot } from './support.ts'
  22. const SNAPSHOT_DIR = fileURLToPath(new URL('./snapshots/question-composer', import.meta.url))
  23. const FIXTURE = join(SNAPSHOT_DIR, 'session.jsonl')
  24. const UI_EXPECTED = join(SNAPSHOT_DIR, 'ui.expected.md')
  25. // Second golden: the answered transcript — the question resolved into its
  26. // tool round trip and the final reply, the state the waiting golden cannot see.
  27. const ANSWERED_EXPECTED = join(SNAPSHOT_DIR, 'answered.expected.md')
  28. const MODE = webSnapshotMode()
  29. const PROMPT = 'Use the ask_user_question tool to ask me exactly one question with id "color", question "Which color do you prefer?", header "Pick one", and options labeled "Blue" and "Green". After I answer, reply with the single word DONE and stop.'
  30. describe('web e2e: resident question composer round trip', () => {
  31. let scaffold: WebScaffold
  32. let browser: Browser
  33. let page: Page
  34. let tripwire: ReturnType<typeof watchConsole>
  35. const sessionEvents: SessionEvent[] = []
  36. beforeAll(async () => {
  37. scaffold = await launchWebScaffold(MODE === 'record' ? {} : { replayFixture: FIXTURE, paceMs: 15 })
  38. scaffold.ctx.on('session/event', (_session, event: SessionEvent) => { sessionEvents.push(event) })
  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('asks through the composer, answers, and completes with the answer logged', async () => {
  50. onTestFailed(() => saveFailureShot(page, 'web-e2e-question'))
  51. if (MODE !== 'record') {
  52. expect(fixtureUserPrompts(await readFile(FIXTURE, 'utf8'))).toEqual([PROMPT])
  53. }
  54. const input = page.locator('textarea').first()
  55. await input.waitFor({ timeout: 10_000 })
  56. const settled = scaffold.whenTurnSettled(MODE === 'record' ? 180_000 : 30_000)
  57. await input.fill(PROMPT)
  58. await input.press('Enter')
  59. // The composer takes over the input area while the tool blocks. Its
  60. // presence is a STABLE waiting state (not a transient): it stays until
  61. // answered, so a plain waitFor is race-free.
  62. const composer = page.locator('[data-question-key]')
  63. await composer.waitFor({ timeout: MODE === 'record' ? 120_000 : 30_000 })
  64. await expect.poll(() => composer.getByText('Which color do you prefer?').count(), { timeout: 10_000 }).toBeGreaterThan(0)
  65. if (MODE !== 'record') {
  66. // This golden owns the stable question surface; the answered-state
  67. // golden below owns the resulting transcript.
  68. const snapshot = await captureStableAria(page, '[data-question-key]', scaffold.workspaceCwd)
  69. await compareOrRefreshGolden(UI_EXPECTED, snapshot, MODE)
  70. }
  71. await composer.getByRole('radio', { name: 'Blue' }).click()
  72. // Submit: Enter on the focused option (the composer's documented submit).
  73. await composer.getByRole('radio', { name: 'Blue' }).press('Enter')
  74. const sessionId = await settled
  75. if (MODE === 'record') {
  76. await recordFixture(scaffold, sessionId, FIXTURE)
  77. return
  78. }
  79. // World state: the tool result carries the chosen answer, and DONE lands.
  80. const results = sessionEvents.filter(e => e.type === 'tool/result')
  81. expect(JSON.stringify(results.at(-1))).toContain('Blue')
  82. await expect.poll(() => page.getByText('DONE', { exact: true }).count(), { timeout: 15_000 }).toBeGreaterThanOrEqual(1)
  83. // Composer gone; regular input restored.
  84. expect(await page.locator('[data-question-key]').count()).toBe(0)
  85. await expect.poll(() => page.locator('textarea').first().isEnabled(), { timeout: 10_000 }).toBe(true)
  86. // Golden of the answered transcript: the ask_user_question round trip
  87. // rendered as history (question tool row + DONE), composer takeover gone.
  88. const snapshot = await captureStableAria(page, '[class*="centerCol"]', scaffold.workspaceCwd)
  89. await compareOrRefreshGolden(ANSWERED_EXPECTED, snapshot, MODE)
  90. expect(tripwire.pageErrors).toEqual([])
  91. expect(tripwire.warnings).toEqual([])
  92. }, 200_000)
  93. it.skipIf(MODE === 'record')('keeps the fixture inventory closed', async () => {
  94. await assertFixtureInventory(SNAPSHOT_DIR, ['session.jsonl', 'ui.expected.md', 'answered.expected.md'])
  95. })
  96. })