ptc-round.e2e.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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, 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 MODE = webSnapshotMode()
  18. // Elicits the successful and failed sub-rows this scenario asserts.
  19. const PROMPT = 'Using ONE run_code program: run bash `echo CODE_ROUND_OK`, then read the file missing.txt '
  20. + 'catching its error in the program. Return an object with both outcomes. Then reply DONE and stop.'
  21. describe('web e2e: PTC mode round renders nested sub-calls', () => {
  22. let scaffold: WebScaffold
  23. let browser: Browser
  24. let page: Page
  25. let tripwire: ReturnType<typeof watchConsole>
  26. const sessionEvents: SessionEvent[] = []
  27. beforeAll(async () => {
  28. scaffold = await launchWebScaffold({
  29. agentPresets: { roots: [], default: 'ptc' },
  30. compareReplaySession: true,
  31. ...(MODE === 'record' ? {} : { replayFixture: FIXTURE, paceMs: 15 }),
  32. })
  33. scaffold.ctx.on('session/event', (_session, event: SessionEvent) => { sessionEvents.push(event) })
  34. browser = await chromium.launch()
  35. page = await newEnglishPage(browser)
  36. tripwire = watchConsole(page)
  37. await page.goto(scaffold.authenticatedUrl, { waitUntil: 'load' })
  38. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  39. await connectFreshWorkspace(page, scaffold.workspaceCwd)
  40. }, 120_000)
  41. afterAll(async () => {
  42. await browser?.close()
  43. await scaffold?.close()
  44. })
  45. it('drives the recorded prompt to a settled turn (all modes)', async () => {
  46. onTestFailed(() => saveFailureShot(page, 'web-e2e-ptc-drive'))
  47. if (MODE !== 'record') {
  48. expect(fixtureUserPrompts(await readFile(FIXTURE, 'utf8'))).toEqual([PROMPT])
  49. }
  50. const input = page.locator('[data-composer-input]').first()
  51. await input.waitFor({ timeout: 10_000 })
  52. const settled = scaffold.whenTurnSettled()
  53. await input.fill(PROMPT)
  54. await input.press('Enter')
  55. const sessionId = await settled
  56. if (MODE === 'record') {
  57. await recordFixture(scaffold, sessionId, FIXTURE)
  58. }
  59. }, 200_000)
  60. it.skipIf(MODE === 'record')('the durable log carries run_code with full-content sub-dispatches', () => {
  61. const calls = sessionEvents.filter(event => event.type === 'tool/call')
  62. expect(calls.length).toBeGreaterThanOrEqual(1)
  63. expect(new Set(calls.map(call => (call.data as { name: string }).name))).toEqual(new Set(['run_code']))
  64. const starts = sessionEvents.filter(event => event.type === 'tool/ptc-dispatch-start')
  65. const dispatches = sessionEvents.filter(event => event.type === 'tool/ptc-dispatch')
  66. expect(dispatches.length).toBeGreaterThanOrEqual(2)
  67. for (const dispatch of dispatches) {
  68. const data = dispatch.data
  69. expect(calls.some(call => call.data.callId === data.rootCallId)).toBe(true)
  70. expect(data.parentCallId).toBe(data.rootCallId)
  71. expect(starts.filter(start => start.data.subCallId === data.subCallId)).toMatchObject([{
  72. data: {
  73. rootCallId: data.rootCallId,
  74. parentCallId: data.parentCallId,
  75. subCallId: data.subCallId,
  76. name: data.name,
  77. arguments: data.arguments,
  78. },
  79. }])
  80. expect(Array.isArray(data.content)).toBe(true)
  81. expect(typeof data.isError).toBe('boolean')
  82. }
  83. const bash = dispatches.find(dispatch => (dispatch.data as { name: string }).name === 'bash')
  84. expect(bash).toBeDefined()
  85. const bashContent = (bash!.data as { content: { type: string; text?: string }[] }).content
  86. expect(bashContent.filter(block => block.type === 'text').map(block => block.text).join('')).toContain('CODE_ROUND_OK')
  87. })
  88. it.skipIf(MODE === 'record')('renders the code parent row with always-visible nested sub-rows', async () => {
  89. onTestFailed(() => saveFailureShot(page, 'web-e2e-ptc-rows'))
  90. await expect.poll(() => page.getByText('DONE', { exact: true }).count(), { timeout: 15_000 }).toBeGreaterThanOrEqual(1)
  91. // The parent run_code row wears the code variant with the model-authored
  92. // description as its summary (the presentCall contract).
  93. const codeRow = page.locator('[data-variant="code"]').first()
  94. await expandOwningTurnProcess(page, codeRow)
  95. await codeRow.waitFor({ timeout: 10_000 })
  96. const nest = page.locator('[data-subcalls]').first()
  97. await nest.waitFor({ timeout: 10_000 })
  98. expect(await nest.locator('[data-sample="bash"]').count()).toBeGreaterThanOrEqual(1)
  99. expect(await nest.locator('[data-state="error"]').count()).toBeGreaterThanOrEqual(1)
  100. }, 60_000)
  101. it.skipIf(MODE === 'record')('expands the nested bash terminal inline before and after reload', async () => {
  102. onTestFailed(() => saveFailureShot(page, 'web-e2e-ptc-rightbar'))
  103. let liveTerminalAria: string | undefined
  104. for (const reloaded of [false, true]) {
  105. if (reloaded) {
  106. const warningStart = tripwire.warnings.length
  107. await page.reload({ waitUntil: 'load' })
  108. acknowledgeReloadConnectionLoss(tripwire, warningStart)
  109. await page.getByText('DONE', { exact: true }).waitFor({ timeout: 15_000 })
  110. }
  111. const nest = page.locator('[data-subcalls]').first()
  112. const frame = page.locator('[style*="grid-template-columns"]').first()
  113. expect(await frame.getAttribute('data-rightbar-collapsed')).toBe('true')
  114. await expandOwningTurnProcess(page, nest)
  115. const row = nest.locator('[data-sample="bash"]').first()
  116. await expect.poll(() => row.getAttribute('data-state')).toBe('ok')
  117. await expect.poll(() => row.getAttribute('aria-expanded')).toBe('false')
  118. await row.click()
  119. await expect.poll(() => row.getAttribute('aria-expanded')).toBe('true')
  120. const terminal = row.locator('xpath=..').locator('[data-terminal]')
  121. await terminal.waitFor()
  122. await terminal.getByText('echo CODE_ROUND_OK', { exact: true }).waitFor()
  123. await terminal.getByText('CODE_ROUND_OK', { exact: true }).waitFor()
  124. await expect.poll(() => terminal.locator('[data-state]').getAttribute('data-state')).toBe('done')
  125. await expect.poll(() => frame.getAttribute('data-rightbar-collapsed'), { timeout: 5_000 }).toBe('true')
  126. const aria = await terminal.ariaSnapshot()
  127. if (reloaded) expect(aria).toBe(liveTerminalAria)
  128. else liveTerminalAria = aria
  129. }
  130. })
  131. it.skipIf(MODE === 'record')('matches the expanded conversation aria golden with stable anchors', async () => {
  132. onTestFailed(() => saveFailureShot(page, 'web-e2e-ptc-aria'))
  133. const row = page.locator('[data-subcalls] [data-sample="bash"]').first()
  134. await expandOwningTurnProcess(page, row)
  135. if (await row.getAttribute('aria-expanded') !== 'true') await row.click()
  136. const snapshot = await captureExpandedTurnProcessAria(
  137. page,
  138. '[class*="centerCol"]',
  139. scaffold.workspaceCwd,
  140. )
  141. await compareOrRefreshGolden(UI_EXPECTED, snapshot, MODE)
  142. })
  143. it.skipIf(MODE === 'record')('stayed clean: no page errors, no reconnect churn', () => {
  144. expect(tripwire.pageErrors).toEqual([])
  145. expect(tripwire.warnings).toEqual([])
  146. })
  147. })