ptc-round.e2e.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // PTC mode browser round trip with nested sub-calls and details selection.
  2. // Record: DSH_SNAPSHOT=record writes session.v2.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.v2.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 dispatches = sessionEvents.filter(event => (event.type as string) === 'tool/code-dispatch')
  65. expect(dispatches.length).toBeGreaterThanOrEqual(2)
  66. for (const dispatch of dispatches) {
  67. const data = dispatch.data as unknown as {
  68. parentCallId: string
  69. subCallId: string
  70. name: string
  71. isError: boolean
  72. content: { type: string }[]
  73. }
  74. expect(data.subCallId.startsWith(`${data.parentCallId}:code:`)).toBe(true)
  75. expect(Array.isArray(data.content)).toBe(true)
  76. expect(typeof data.isError).toBe('boolean')
  77. }
  78. const bash = dispatches.find(dispatch => (dispatch.data as { name: string }).name === 'bash')
  79. expect(bash).toBeDefined()
  80. const bashContent = (bash!.data as { content: { type: string; text?: string }[] }).content
  81. expect(bashContent.filter(block => block.type === 'text').map(block => block.text).join('')).toContain('CODE_ROUND_OK')
  82. })
  83. it.skipIf(MODE === 'record')('renders the code parent row with always-visible nested sub-rows', async () => {
  84. onTestFailed(() => saveFailureShot(page, 'web-e2e-ptc-rows'))
  85. await expect.poll(() => page.getByText('DONE', { exact: true }).count(), { timeout: 15_000 }).toBeGreaterThanOrEqual(1)
  86. // The parent run_code row wears the code variant with the model-authored
  87. // description as its summary (the presentCall contract).
  88. const codeRow = page.locator('[data-variant="code"]').first()
  89. await expandOwningTurnProcess(page, codeRow)
  90. await codeRow.waitFor({ timeout: 10_000 })
  91. const nest = page.locator('[data-subcalls]').first()
  92. await nest.waitFor({ timeout: 10_000 })
  93. expect(await nest.locator('[data-sample="bash"]').count()).toBeGreaterThanOrEqual(1)
  94. expect(await nest.locator('[data-state="error"]').count()).toBeGreaterThanOrEqual(1)
  95. }, 60_000)
  96. it.skipIf(MODE === 'record')('expands the nested bash terminal inline before and after reload', async () => {
  97. onTestFailed(() => saveFailureShot(page, 'web-e2e-ptc-details'))
  98. let liveTerminalAria: string | undefined
  99. for (const reloaded of [false, true]) {
  100. if (reloaded) {
  101. const warningStart = tripwire.warnings.length
  102. await page.reload({ waitUntil: 'load' })
  103. acknowledgeReloadConnectionLoss(tripwire, warningStart)
  104. await page.getByText('DONE', { exact: true }).waitFor({ timeout: 15_000 })
  105. }
  106. const nest = page.locator('[data-subcalls]').first()
  107. const frame = page.locator('[style*="grid-template-columns"]').first()
  108. expect(await frame.getAttribute('data-details-collapsed')).toBe('true')
  109. await expandOwningTurnProcess(page, nest)
  110. const row = nest.locator('[data-sample="bash"]').first()
  111. await expect.poll(() => row.getAttribute('data-state')).toBe('ok')
  112. await expect.poll(() => row.getAttribute('aria-expanded')).toBe('false')
  113. await row.click()
  114. await expect.poll(() => row.getAttribute('aria-expanded')).toBe('true')
  115. const terminal = row.locator('xpath=..').locator('[data-terminal]')
  116. await terminal.waitFor()
  117. await terminal.getByText('echo CODE_ROUND_OK', { exact: true }).waitFor()
  118. await terminal.getByText('CODE_ROUND_OK', { exact: true }).waitFor()
  119. await expect.poll(() => terminal.locator('[data-state]').getAttribute('data-state')).toBe('done')
  120. await expect.poll(() => frame.getAttribute('data-details-collapsed'), { timeout: 5_000 }).toBe('true')
  121. const aria = await terminal.ariaSnapshot()
  122. if (reloaded) expect(aria).toBe(liveTerminalAria)
  123. else liveTerminalAria = aria
  124. }
  125. })
  126. it.skipIf(MODE === 'record')('matches the expanded conversation aria golden with stable anchors', async () => {
  127. onTestFailed(() => saveFailureShot(page, 'web-e2e-ptc-aria'))
  128. const row = page.locator('[data-subcalls] [data-sample="bash"]').first()
  129. await expandOwningTurnProcess(page, row)
  130. if (await row.getAttribute('aria-expanded') !== 'true') await row.click()
  131. const snapshot = await captureExpandedTurnProcessAria(
  132. page,
  133. '[class*="centerCol"]',
  134. scaffold.workspaceCwd,
  135. )
  136. await compareOrRefreshGolden(UI_EXPECTED, snapshot, MODE)
  137. })
  138. it.skipIf(MODE === 'record')('stayed clean: no page errors, no reconnect churn', () => {
  139. expect(tripwire.pageErrors).toEqual([])
  140. expect(tripwire.warnings).toEqual([])
  141. })
  142. })