ptc-round.e2e.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // PTC mode browser round trip with nested sub-calls and details selection.
  2. // Record: DSH_SNAPSHOT=record writes session.v3.jsonl, then a keyless
  3. // DSH_SNAPSHOT=refresh regenerates ui.expected.md.
  4. import { readFile } from 'node:fs/promises'
  5. import { fileURLToPath } from 'node:url'
  6. import type { Browser, Page } from 'playwright'
  7. import { chromium } from 'playwright'
  8. import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest'
  9. import type { SessionEvent } from '@deepseek-ai/dsh-session'
  10. import {
  11. acknowledgeReloadConnectionLoss, captureExpandedTurnProcessAria, captureStableAria, compareOrRefreshGolden, fixtureUserPrompts,
  12. launchWebScaffold, recordFixture, watchConsole, webSnapshotMode, type WebScaffold,
  13. } from './scaffold.ts'
  14. import { connectFreshWorkspace, expandOwningTurnProcess, newEnglishPage, saveFailureShot } from './support.ts'
  15. const FIXTURE = fileURLToPath(new URL('../../../snapshots/web/ptc-round/session.v3.jsonl', import.meta.url))
  16. const UI_EXPECTED = fileURLToPath(new URL('../../../snapshots/web/ptc-round/ui.expected.md', import.meta.url))
  17. const TRAJECTORY_EXPECTED = fileURLToPath(new URL('../../../snapshots/web/ptc-round/trajectory.expected.md', import.meta.url))
  18. const CODE_EXPECTED = fileURLToPath(new URL('../../../snapshots/web/ptc-round/code.expected.md', import.meta.url))
  19. const MODE = webSnapshotMode()
  20. // Elicits the successful and failed sub-rows this scenario asserts.
  21. const PROMPT = 'Using ONE run_code program: run bash `echo CODE_ROUND_OK`, then read the file missing.txt '
  22. + 'catching its error in the program. Return an object with both outcomes. Then reply DONE and stop.'
  23. describe('web e2e: PTC mode round renders nested sub-calls', () => {
  24. let scaffold: WebScaffold
  25. let browser: Browser
  26. let page: Page
  27. let tripwire: ReturnType<typeof watchConsole>
  28. const sessionEvents: SessionEvent[] = []
  29. beforeAll(async () => {
  30. scaffold = await launchWebScaffold({
  31. agentPresets: { roots: [], default: 'ptc' },
  32. compareReplaySession: true,
  33. ...(MODE === 'record' ? {} : { replayFixture: FIXTURE, paceMs: 15 }),
  34. })
  35. scaffold.ctx.on('session/event', (_session, event: SessionEvent) => { sessionEvents.push(event) })
  36. browser = await chromium.launch()
  37. page = await newEnglishPage(browser)
  38. tripwire = watchConsole(page)
  39. await page.goto(scaffold.authenticatedUrl, { waitUntil: 'load' })
  40. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  41. await connectFreshWorkspace(page, scaffold.workspaceCwd)
  42. }, 120_000)
  43. afterAll(async () => {
  44. await browser?.close()
  45. await scaffold?.close()
  46. })
  47. it('drives the recorded prompt to a settled turn (all modes)', async () => {
  48. onTestFailed(() => saveFailureShot(page, 'web-e2e-ptc-drive'))
  49. if (MODE !== 'record') {
  50. expect(fixtureUserPrompts(await readFile(FIXTURE, 'utf8'))).toEqual([PROMPT])
  51. }
  52. const input = page.locator('[data-composer-input]').first()
  53. await input.waitFor({ timeout: 10_000 })
  54. const settled = scaffold.whenTurnSettled()
  55. await input.fill(PROMPT)
  56. await input.press('Enter')
  57. const sessionId = await settled
  58. if (MODE === 'record') {
  59. await recordFixture(scaffold, sessionId, FIXTURE)
  60. }
  61. }, 200_000)
  62. it.skipIf(MODE === 'record')('the durable log carries run_code with full-content sub-dispatches', () => {
  63. const calls = sessionEvents.filter(event => event.type === 'tool/call')
  64. expect(calls.length).toBeGreaterThanOrEqual(1)
  65. expect(new Set(calls.map(call => (call.data as { name: string }).name))).toEqual(new Set(['run_code']))
  66. const starts = sessionEvents.filter(event => event.type === 'tool/ptc-dispatch-start')
  67. const dispatches = sessionEvents.filter(event => event.type === 'tool/ptc-dispatch')
  68. expect(dispatches.length).toBeGreaterThanOrEqual(2)
  69. for (const dispatch of dispatches) {
  70. const data = dispatch.data
  71. expect(calls.some(call => call.data.callId === data.rootCallId)).toBe(true)
  72. expect(data.parentCallId).toBe(data.rootCallId)
  73. expect(starts.filter(start => start.data.subCallId === data.subCallId)).toMatchObject([{
  74. data: {
  75. rootCallId: data.rootCallId,
  76. parentCallId: data.parentCallId,
  77. subCallId: data.subCallId,
  78. name: data.name,
  79. arguments: data.arguments,
  80. },
  81. }])
  82. expect(Array.isArray(data.content)).toBe(true)
  83. expect(typeof data.isError).toBe('boolean')
  84. }
  85. const bash = dispatches.find(dispatch => (dispatch.data as { name: string }).name === 'bash')
  86. expect(bash).toBeDefined()
  87. const bashContent = (bash!.data as { content: { type: string; text?: string }[] }).content
  88. expect(bashContent.filter(block => block.type === 'text').map(block => block.text).join('')).toContain('CODE_ROUND_OK')
  89. })
  90. it.skipIf(MODE === 'record')('renders the code parent row with always-visible nested sub-rows', async () => {
  91. onTestFailed(() => saveFailureShot(page, 'web-e2e-ptc-rows'))
  92. await expect.poll(() => page.getByText('DONE', { exact: true }).count(), { timeout: 15_000 }).toBeGreaterThanOrEqual(1)
  93. // The parent run_code row wears the code variant with the model-authored
  94. // description as its summary (the presentCall contract).
  95. const codeRow = page.locator('[data-variant="code"]').first()
  96. await expandOwningTurnProcess(page, codeRow)
  97. await codeRow.waitFor({ timeout: 10_000 })
  98. const nest = page.locator('[data-subcalls]').first()
  99. await nest.waitFor({ timeout: 10_000 })
  100. expect(await nest.locator('[data-sample="bash"]').count()).toBeGreaterThanOrEqual(1)
  101. expect(await nest.locator('[data-state="error"]').count()).toBeGreaterThanOrEqual(1)
  102. }, 60_000)
  103. it.skipIf(MODE === 'record')('expands the nested bash terminal inline before and after reload', async () => {
  104. onTestFailed(() => saveFailureShot(page, 'web-e2e-ptc-rightbar'))
  105. let liveTerminalAria: string | undefined
  106. for (const reloaded of [false, true]) {
  107. if (reloaded) {
  108. const warningStart = tripwire.warnings.length
  109. await page.reload({ waitUntil: 'load' })
  110. acknowledgeReloadConnectionLoss(tripwire, warningStart)
  111. await page.getByText('DONE', { exact: true }).waitFor({ timeout: 15_000 })
  112. }
  113. const nest = page.locator('[data-subcalls]').first()
  114. const frame = page.locator('[style*="grid-template-columns"]').first()
  115. expect(await frame.getAttribute('data-rightbar-collapsed')).toBe('true')
  116. await expandOwningTurnProcess(page, nest)
  117. const row = nest.locator('[data-sample="bash"]').first()
  118. await expect.poll(() => row.getAttribute('data-state')).toBe('ok')
  119. await expect.poll(() => row.getAttribute('aria-expanded')).toBe('false')
  120. await row.click()
  121. await expect.poll(() => row.getAttribute('aria-expanded')).toBe('true')
  122. const terminal = row.locator('xpath=..').locator('[data-terminal]')
  123. await terminal.waitFor()
  124. await terminal.getByText('echo CODE_ROUND_OK', { exact: true }).waitFor()
  125. await terminal.getByText('CODE_ROUND_OK', { exact: true }).waitFor()
  126. await expect.poll(() => terminal.locator('[data-state]').getAttribute('data-state')).toBe('done')
  127. await expect.poll(() => frame.getAttribute('data-rightbar-collapsed'), { timeout: 5_000 }).toBe('true')
  128. const aria = await terminal.ariaSnapshot()
  129. if (reloaded) expect(aria).toBe(liveTerminalAria)
  130. else liveTerminalAria = aria
  131. }
  132. })
  133. it.skipIf(MODE === 'record')('matches the expanded conversation aria golden with stable anchors', async () => {
  134. onTestFailed(() => saveFailureShot(page, 'web-e2e-ptc-aria'))
  135. const row = page.locator('[data-subcalls] [data-sample="bash"]').first()
  136. await expandOwningTurnProcess(page, row)
  137. if (await row.getAttribute('aria-expanded') !== 'true') await row.click()
  138. const snapshot = await captureExpandedTurnProcessAria(
  139. page,
  140. '[class*="centerCol"]',
  141. scaffold.workspaceCwd,
  142. )
  143. await compareOrRefreshGolden(UI_EXPECTED, snapshot, MODE)
  144. })
  145. it.skipIf(MODE === 'record')('inspects recorded PTC source, wrapping, and original JSON in the trajectory', async () => {
  146. onTestFailed(() => saveFailureShot(page, 'web-e2e-ptc-inspector'))
  147. const call = sessionEvents.find(event => event.type === 'tool/call' && event.data.name === 'run_code')
  148. if (call?.type !== 'tool/call') throw new Error('recorded PTC call missing')
  149. const args = JSON.parse(call.data.arguments) as { code: string; description: string }
  150. await page.getByRole('tab', { name: 'Trajectory', exact: true }).click()
  151. const row = page.locator('tr[data-kind="tool"]').filter({ hasText: 'run_code' }).first()
  152. await row.click()
  153. await page.getByRole('tab', { name: 'Code', exact: true }).waitFor()
  154. expect(await page.getByRole('tabpanel').textContent()).toContain(args.description)
  155. expect(await page.getByRole('tabpanel').locator('dl').first().locator('dt').allTextContents())
  156. .toEqual(['Hierarchy', 'Status'])
  157. const overview = await Promise.all([1, 2].map(index => captureStableAria(
  158. page, `[role="tabpanel"] [class*="overviewSections"] > section:nth-child(${index})`, scaffold.workspaceCwd,
  159. )))
  160. await compareOrRefreshGolden(TRAJECTORY_EXPECTED, overview.join('\n'), MODE)
  161. await page.getByRole('button', { name: 'Code', exact: true }).click()
  162. const source = page.locator('[data-line-numbers] pre')
  163. await source.waitFor()
  164. expect(await source.textContent()).toBe(args.code.endsWith('\n') ? args.code.slice(0, -1) : args.code)
  165. const wrap = page.getByRole('button', { name: 'Wrap lines', exact: true })
  166. expect(await wrap.getAttribute('aria-pressed')).toBe('false')
  167. const content = page.locator('[data-wrap]').filter({ has: source })
  168. expect(await content.evaluate(element => element.scrollWidth > element.clientWidth)).toBe(true)
  169. await wrap.click()
  170. expect(await wrap.getAttribute('aria-pressed')).toBe('true')
  171. await expect.poll(() => content.evaluate(element => element.scrollWidth - element.clientWidth)).toBeLessThanOrEqual(1)
  172. const code = await captureStableAria(page, '[role="tabpanel"]', scaffold.workspaceCwd)
  173. await compareOrRefreshGolden(CODE_EXPECTED, code, MODE)
  174. await page.getByRole('button', { name: 'Original JSON' }).click()
  175. const json = page.getByRole('tree', { name: 'parameters JSON' })
  176. await json.waitFor()
  177. expect(await json.textContent()).toContain(args.description)
  178. await page.getByRole('button', { name: 'Original JSON' }).click()
  179. expect(await source.textContent()).toBe(args.code.endsWith('\n') ? args.code.slice(0, -1) : args.code)
  180. await page.getByRole('tab', { name: 'Summary', exact: true }).click()
  181. expect(await wrap.getAttribute('aria-pressed')).toBe('true')
  182. })
  183. it.skipIf(MODE === 'record')('stayed clean: no page errors, no reconnect churn', () => {
  184. expect(tripwire.pageErrors).toEqual([])
  185. expect(tripwire.warnings).toEqual([])
  186. })
  187. })