1
0

code-mode-round.e2e.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 { 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. }, 120_000)
  48. afterAll(async () => {
  49. await browser?.close()
  50. await scaffold?.close()
  51. })
  52. it('drives the recorded prompt to a settled turn (all modes)', async () => {
  53. onTestFailed(() => saveFailureShot(page, 'web-e2e-code-mode-drive'))
  54. if (MODE !== 'record') {
  55. // Drift guard: the committed fixture must carry exactly the drive prompt.
  56. expect(fixtureUserPrompts(await readFile(FIXTURE, 'utf8'))).toEqual([PROMPT])
  57. }
  58. const input = page.locator('textarea').first()
  59. await input.waitFor({ timeout: 10_000 })
  60. const settled = scaffold.whenTurnSettled()
  61. await input.fill(PROMPT)
  62. await input.press('Enter')
  63. const sessionId = await settled
  64. if (MODE === 'record') {
  65. await recordFixture(scaffold, sessionId, FIXTURE)
  66. }
  67. }, 200_000)
  68. it.skipIf(MODE === 'record')('the durable log carries run_code with full-content sub-dispatches', () => {
  69. // Wire discipline: code mode collapsed the call surface to run_code.
  70. const calls = sessionEvents.filter(event => event.type === 'tool/call')
  71. expect(calls.length).toBeGreaterThanOrEqual(1)
  72. expect(new Set(calls.map(call => (call.data as { name: string }).name))).toEqual(new Set(['run_code']))
  73. // Sub-dispatches logged with the complete tool/result vocabulary.
  74. const dispatches = sessionEvents.filter(event => (event.type as string) === 'tool/code-dispatch')
  75. expect(dispatches.length).toBeGreaterThanOrEqual(2)
  76. for (const dispatch of dispatches) {
  77. const data = dispatch.data as unknown as {
  78. parentCallId: string
  79. subCallId: string
  80. name: string
  81. isError: boolean
  82. content: { type: string }[]
  83. }
  84. expect(data.subCallId.startsWith(`${data.parentCallId}:code:`)).toBe(true)
  85. expect(Array.isArray(data.content)).toBe(true)
  86. expect(typeof data.isError).toBe('boolean')
  87. }
  88. const bash = dispatches.find(dispatch => (dispatch.data as { name: string }).name === 'bash')
  89. expect(bash).toBeDefined()
  90. const bashContent = (bash!.data as { content: { type: string; text?: string }[] }).content
  91. expect(bashContent.filter(block => block.type === 'text').map(block => block.text).join('')).toContain('CODE_ROUND_OK')
  92. })
  93. it.skipIf(MODE === 'record')('renders the code parent row with always-visible nested sub-rows', async () => {
  94. onTestFailed(() => saveFailureShot(page, 'web-e2e-code-mode-rows'))
  95. await expect.poll(() => page.getByText('DONE', { exact: true }).count(), { timeout: 15_000 }).toBeGreaterThanOrEqual(1)
  96. // The parent run_code row wears the code variant with the model-authored
  97. // description as its summary (the PR1 presentCall contract).
  98. const codeRow = page.locator('[data-variant="code"]').first()
  99. await codeRow.waitFor({ timeout: 10_000 })
  100. // Nested rows are visible WITHOUT any expand interaction, inside the
  101. // sub-call nest, each rendered by the same components as native rows:
  102. // the bash sub-call landed in the bash sample registration.
  103. const nest = page.locator('[data-subcalls]').first()
  104. await nest.waitFor({ timeout: 10_000 })
  105. expect(await nest.locator('[data-sample="bash-global"]').count()).toBeGreaterThanOrEqual(1)
  106. // The failing read sub-call wears the same error state a native failed
  107. // row wears (the recorded program tolerates a read of missing.txt).
  108. expect(await nest.locator('[data-state="error"]').count()).toBeGreaterThanOrEqual(1)
  109. }, 60_000)
  110. it.skipIf(MODE === 'record')('a sub-row click opens the details panel on the sub-call material', async () => {
  111. onTestFailed(() => saveFailureShot(page, 'web-e2e-code-mode-details'))
  112. const nest = page.locator('[data-subcalls]').first()
  113. await nest.locator('[data-sample="bash-global"]').first().click()
  114. // The details column opens (width > 0) and shows the sub-call's complete
  115. // output — the full-content log contract, no truncation marker anywhere.
  116. await page.waitForFunction(() => {
  117. const frame = document.querySelector('[class*="frame"]')
  118. if (frame === null) return false
  119. return Number(getComputedStyle(frame).gridTemplateColumns.split(' ').pop()!.replace('px', '')) > 0
  120. }, undefined, { timeout: 10_000 })
  121. await expect.poll(() => page.getByText('CODE_ROUND_OK', { exact: false }).count(), { timeout: 5_000 })
  122. .toBeGreaterThanOrEqual(1)
  123. })
  124. it.skipIf(MODE === 'record')('matches the conversation aria golden with stable anchors', async () => {
  125. onTestFailed(() => saveFailureShot(page, 'web-e2e-code-mode-aria'))
  126. const snapshot = await captureStableAria(page, '[class*="centerCol"]', scaffold.workspaceCwd)
  127. await compareOrRefreshGolden(UI_EXPECTED, snapshot, MODE)
  128. })
  129. it.skipIf(MODE === 'record')('stayed clean: no page errors, no reconnect churn', () => {
  130. expect(tripwire.pageErrors).toEqual([])
  131. expect(tripwire.warnings).toEqual([])
  132. })
  133. })