code-mode-round.e2e.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Web e2e scenario: a Code Mode round trip. The scaffold boots the SAME
  2. // shipped tree with the tools row patched to mode: code (the run_code-only
  3. // wire), a real chromium sends a prompt engineered to elicit one run_code
  4. // program with several sub-calls, and the UI must render the code-variant
  5. // parent row with its always-visible nested sub-rows — each sub-row the same
  6. // component a native call renders through — plus details-panel resolution for
  7. // a clicked sub-row. Drive steps wait only on generic completion
  8. // (whenTurnSettled); assertion steps run in replay/refresh only.
  9. // Record: DSH_SNAPSHOT=record rewrites session.jsonl, then a keyless
  10. // DSH_SNAPSHOT=refresh regenerates ui.expected.md.
  11. import { readFile } from 'node:fs/promises'
  12. import { fileURLToPath } from 'node:url'
  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. captureStableAria, compareOrRefreshGolden, fixtureUserPrompts,
  19. launchWebScaffold, recordFixture, watchConsole, webSnapshotMode, type WebScaffold,
  20. } from './scaffold.ts'
  21. import { connectFreshWorkspace, saveFailureShot } from './support.ts'
  22. const FIXTURE = fileURLToPath(new URL('./snapshots/code-mode-round/session.jsonl', import.meta.url))
  23. const UI_EXPECTED = fileURLToPath(new URL('./snapshots/code-mode-round/ui.expected.md', import.meta.url))
  24. const MODE = webSnapshotMode()
  25. // The scenario's one drive prompt: elicits one program with a bash sub-call
  26. // and a failing read the program tolerates — the sub-row set the assertions
  27. // (and the PR gif) need. Never asserted against model prose.
  28. const PROMPT = 'Using ONE run_code program: run bash `echo CODE_ROUND_OK`, then read the file missing.txt '
  29. + 'catching its error in the program. Return an object with both outcomes. Then reply DONE and stop.'
  30. describe('web e2e: Code Mode round renders nested sub-calls', () => {
  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({
  38. toolsMode: 'code',
  39. ...(MODE === 'record' ? {} : { replayFixture: FIXTURE, paceMs: 15 }),
  40. })
  41. scaffold.ctx.on('session/event', (_session, event: SessionEvent) => { sessionEvents.push(event) })
  42. browser = await chromium.launch()
  43. page = await browser.newPage({ viewport: { width: 1680, height: 1000 } })
  44. tripwire = watchConsole(page)
  45. await page.goto(scaffold.baseUrl, { waitUntil: 'load' })
  46. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  47. // Fresh world: connect a Workspace so the composer scenarios start live.
  48. await connectFreshWorkspace(page)
  49. }, 120_000)
  50. afterAll(async () => {
  51. await browser?.close()
  52. await scaffold?.close()
  53. })
  54. it('drives the recorded prompt to a settled turn (all modes)', async () => {
  55. onTestFailed(() => saveFailureShot(page, 'web-e2e-code-mode-drive'))
  56. if (MODE !== 'record') {
  57. // Drift guard: the committed fixture must carry exactly the drive prompt.
  58. expect(fixtureUserPrompts(await readFile(FIXTURE, 'utf8'))).toEqual([PROMPT])
  59. }
  60. const input = page.locator('textarea').first()
  61. await input.waitFor({ timeout: 10_000 })
  62. const settled = scaffold.whenTurnSettled()
  63. await input.fill(PROMPT)
  64. await input.press('Enter')
  65. const sessionId = await settled
  66. if (MODE === 'record') {
  67. await recordFixture(scaffold, sessionId, FIXTURE)
  68. }
  69. }, 200_000)
  70. it.skipIf(MODE === 'record')('the durable log carries run_code with full-content sub-dispatches', () => {
  71. // Wire discipline: code mode collapsed the call surface to run_code.
  72. const calls = sessionEvents.filter(event => event.type === 'tool/call')
  73. expect(calls.length).toBeGreaterThanOrEqual(1)
  74. expect(new Set(calls.map(call => (call.data as { name: string }).name))).toEqual(new Set(['run_code']))
  75. // Sub-dispatches logged with the complete tool/result vocabulary.
  76. const dispatches = sessionEvents.filter(event => (event.type as string) === 'tool/code-dispatch')
  77. expect(dispatches.length).toBeGreaterThanOrEqual(2)
  78. for (const dispatch of dispatches) {
  79. const data = dispatch.data as unknown as {
  80. parentCallId: string
  81. subCallId: string
  82. name: string
  83. isError: boolean
  84. content: { type: string }[]
  85. }
  86. expect(data.subCallId.startsWith(`${data.parentCallId}:code:`)).toBe(true)
  87. expect(Array.isArray(data.content)).toBe(true)
  88. expect(typeof data.isError).toBe('boolean')
  89. }
  90. const bash = dispatches.find(dispatch => (dispatch.data as { name: string }).name === 'bash')
  91. expect(bash).toBeDefined()
  92. const bashContent = (bash!.data as { content: { type: string; text?: string }[] }).content
  93. expect(bashContent.filter(block => block.type === 'text').map(block => block.text).join('')).toContain('CODE_ROUND_OK')
  94. })
  95. it.skipIf(MODE === 'record')('renders the code parent row with always-visible nested sub-rows', async () => {
  96. onTestFailed(() => saveFailureShot(page, 'web-e2e-code-mode-rows'))
  97. await expect.poll(() => page.getByText('DONE', { exact: true }).count(), { timeout: 15_000 }).toBeGreaterThanOrEqual(1)
  98. // The parent run_code row wears the code variant with the model-authored
  99. // description as its summary (the PR1 presentCall contract).
  100. const codeRow = page.locator('[data-variant="code"]').first()
  101. await codeRow.waitFor({ timeout: 10_000 })
  102. // Nested rows are visible WITHOUT any expand interaction, inside the
  103. // sub-call nest, each rendered by the same components as native rows:
  104. // the bash sub-call landed in the bash sample registration.
  105. const nest = page.locator('[data-subcalls]').first()
  106. await nest.waitFor({ timeout: 10_000 })
  107. expect(await nest.locator('[data-sample="bash-global"]').count()).toBeGreaterThanOrEqual(1)
  108. // The failing read sub-call wears the same error state a native failed
  109. // row wears (the recorded program tolerates a read of missing.txt).
  110. expect(await nest.locator('[data-state="error"]').count()).toBeGreaterThanOrEqual(1)
  111. }, 60_000)
  112. it.skipIf(MODE === 'record')('a sub-row click opens the details panel on the sub-call material', async () => {
  113. onTestFailed(() => saveFailureShot(page, 'web-e2e-code-mode-details'))
  114. const nest = page.locator('[data-subcalls]').first()
  115. await nest.locator('[data-sample="bash-global"]').first().click()
  116. // The details column opens (width > 0) and shows the sub-call's complete
  117. // output — the full-content log contract, no truncation marker anywhere.
  118. await page.waitForFunction(() => {
  119. const frame = document.querySelector('[class*="frame"]')
  120. if (frame === null) return false
  121. return Number(getComputedStyle(frame).gridTemplateColumns.split(' ').pop()!.replace('px', '')) > 0
  122. }, undefined, { timeout: 10_000 })
  123. await expect.poll(() => page.getByText('CODE_ROUND_OK', { exact: false }).count(), { timeout: 5_000 })
  124. .toBeGreaterThanOrEqual(1)
  125. })
  126. it.skipIf(MODE === 'record')('matches the conversation aria golden with stable anchors', async () => {
  127. onTestFailed(() => saveFailureShot(page, 'web-e2e-code-mode-aria'))
  128. const snapshot = await captureStableAria(page, '[class*="centerCol"]', scaffold.workspaceCwd)
  129. await compareOrRefreshGolden(UI_EXPECTED, snapshot, MODE)
  130. })
  131. it.skipIf(MODE === 'record')('stayed clean: no page errors, no reconnect churn', () => {
  132. expect(tripwire.pageErrors).toEqual([])
  133. expect(tripwire.warnings).toEqual([])
  134. })
  135. })