workflow-run.e2e.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Keyless shipped-Web acceptance for the durable workflow Conversation Node.
  2. // Reuses the existing recorded workflow parent/child model fixtures; the real
  3. // workflow tool, worker, subagent provider, Session log, browser plugin graph,
  4. // and navigation all execute during replay.
  5. import { readFile } from 'node:fs/promises'
  6. import { join } from 'node:path'
  7. import { fileURLToPath } from 'node:url'
  8. import type { Browser, Page } from 'playwright'
  9. import { chromium } from 'playwright'
  10. import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest'
  11. import type { Session, SessionEvent, SessionId } from '@deepseek-ai/dsh-session'
  12. import {
  13. assertFixtureInventory, captureStableAria, compareOrRefreshGolden,
  14. fixtureUserPrompts, launchWebScaffold, watchConsole, webSnapshotMode,
  15. type WebScaffold,
  16. } from './scaffold.ts'
  17. import {
  18. connectFreshWorkspace, newEnglishPage, REPO_ROOT, saveFailureShot,
  19. } from './support.ts'
  20. const MODE = webSnapshotMode()
  21. const SNAPSHOT_DIR = fileURLToPath(new URL('./snapshots/workflow-run', import.meta.url))
  22. const UI_LIVE_EXPECTED = join(SNAPSHOT_DIR, 'ui-live.expected.md')
  23. const UI_EXPECTED = join(SNAPSHOT_DIR, 'ui.expected.md')
  24. const PARENT_FIXTURE = join(REPO_ROOT, 'examples/acp-agent/tests/snapshots/workflow-run/session.jsonl')
  25. const CHILD_FIXTURE = join(REPO_ROOT, 'examples/acp-agent/tests/snapshots/workflow-run/session.1.jsonl')
  26. const CHILD_PROMPT = 'Reply with exactly the word WF_CHILD_OK and nothing else.'
  27. describe.skipIf(MODE === 'record')('web e2e: durable workflow run in Chat', () => {
  28. let scaffold: WebScaffold
  29. let browser: Browser
  30. let page: Page
  31. let tripwire: ReturnType<typeof watchConsole>
  32. let prompt: string
  33. const waitForParentSettlement = (): Promise<SessionId> => new Promise((resolve, reject) => {
  34. let dispose = (): void => {}
  35. dispose = scaffold.ctx.on('session/event', (session: Session, event: SessionEvent) => {
  36. if (event.type !== 'turn/end' || session.header.origin === 'subagent') return
  37. dispose()
  38. void (async () => {
  39. await scaffold.ctx.agents.get(session.id)?.whenIdle()
  40. await scaffold.ctx.sessions.flush(session)
  41. resolve(session.id)
  42. })().catch(reject)
  43. })
  44. })
  45. beforeAll(async () => {
  46. const prompts = fixtureUserPrompts(await readFile(PARENT_FIXTURE, 'utf8'))
  47. expect(prompts).toHaveLength(1)
  48. prompt = prompts[0]!
  49. scaffold = await launchWebScaffold({
  50. replayFixture: PARENT_FIXTURE,
  51. replayChildFixtures: [CHILD_FIXTURE],
  52. paceMs: 50,
  53. })
  54. browser = await chromium.launch()
  55. page = await newEnglishPage(browser)
  56. tripwire = watchConsole(page)
  57. await page.goto(scaffold.baseUrl, { waitUntil: 'load' })
  58. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  59. await connectFreshWorkspace(page, scaffold.workspaceCwd)
  60. }, 120_000)
  61. afterAll(async () => {
  62. await browser?.close()
  63. await scaffold?.close()
  64. })
  65. it('shows the live member, opens its local child, then retains the settled record beside the tool row', async () => {
  66. onTestFailed(() => saveFailureShot(page, 'web-e2e-workflow-run-live'))
  67. const settled = waitForParentSettlement()
  68. const input = page.locator('textarea').first()
  69. await input.fill(prompt)
  70. await input.press('Enter')
  71. const workflow = page.locator('[data-workflow-run][data-run-status="running"]')
  72. await workflow.waitFor({ timeout: 30_000 })
  73. const disclosures = workflow.locator('[data-disclosure-row]')
  74. await disclosures.nth(1).waitFor({ timeout: 15_000 })
  75. const runDisclosure = disclosures.nth(0)
  76. const phaseDisclosure = disclosures.nth(1)
  77. expect(await runDisclosure.getAttribute('role')).toBe('button')
  78. expect(await runDisclosure.getAttribute('aria-expanded')).toBe('true')
  79. expect(await phaseDisclosure.getAttribute('role')).toBe('button')
  80. expect(await phaseDisclosure.getAttribute('aria-expanded')).toBe('true')
  81. expect(await runDisclosure.evaluate(element => getComputedStyle(element).cursor)).toBe('pointer')
  82. expect(await phaseDisclosure.evaluate(element => getComputedStyle(element).cursor)).toBe('pointer')
  83. const member = page.getByRole('button', { name: /^Open Reply with exactly the word/ })
  84. await member.waitFor({ timeout: 15_000 })
  85. await phaseDisclosure.click()
  86. expect(await phaseDisclosure.getAttribute('aria-expanded')).toBe('false')
  87. expect(await member.count()).toBe(0)
  88. const liveSnapshot = await captureStableAria(page, '[data-workflow-run]', scaffold.workspaceCwd)
  89. await compareOrRefreshGolden(UI_LIVE_EXPECTED, liveSnapshot, MODE)
  90. await phaseDisclosure.press('Enter')
  91. await member.waitFor()
  92. await runDisclosure.click()
  93. expect(await runDisclosure.getAttribute('aria-expanded')).toBe('false')
  94. expect(await disclosures.count()).toBe(1)
  95. await runDisclosure.press('Space')
  96. expect(await disclosures.count()).toBe(2)
  97. expect(await phaseDisclosure.getAttribute('aria-expanded')).toBe('true')
  98. await member.focus()
  99. const lightColor = await member.locator('[data-member-label]').evaluate(element => getComputedStyle(element).color)
  100. await page.setViewportSize({ width: 560, height: 800 })
  101. await page.evaluate(() => { document.body.setAttribute('data-ds-dark-theme', '') })
  102. const darkNarrow = await page.locator('[data-workflow-run]').evaluate((element) => {
  103. const panel = element as HTMLElement
  104. panel.style.width = '356px'
  105. const label = element.querySelector('[data-member-label]')
  106. const labelWrap = element.querySelector('[data-member-label-wrap]')
  107. const status = element.querySelector('[data-member-status-text]')
  108. const disclosures = element.querySelectorAll('[data-disclosure-row]')
  109. const runHeader = disclosures[0]
  110. const phaseHeader = disclosures[1]
  111. const phaseTitle = phaseHeader?.children.item(1) as HTMLElement | null
  112. const phaseStatus = element.querySelector('[data-phase-status-text]')
  113. const originalPhaseTitle = phaseTitle?.textContent ?? ''
  114. if (phaseTitle !== null) phaseTitle.textContent = 'A phase name long enough to require ellipsis in the narrow layout'
  115. const phaseTitleRight = phaseTitle?.getBoundingClientRect().right ?? 0
  116. const phaseStatusLeft = phaseStatus?.getBoundingClientRect().left ?? 0
  117. if (phaseTitle !== null) phaseTitle.textContent = originalPhaseTitle
  118. return {
  119. clientWidth: element.clientWidth,
  120. scrollWidth: element.scrollWidth,
  121. color: label === null ? '' : getComputedStyle(label).color,
  122. decoration: label === null ? '' : getComputedStyle(label).textDecorationLine,
  123. focusWidth: labelWrap === null ? '' : getComputedStyle(labelWrap).outlineWidth,
  124. statusWidth: status?.getBoundingClientRect().width ?? 0,
  125. statusFontSize: status === null ? '' : getComputedStyle(status).fontSize,
  126. runHeight: runHeader?.getBoundingClientRect().height ?? 0,
  127. phaseHeight: phaseHeader?.getBoundingClientRect().height ?? 0,
  128. phaseTitleRight,
  129. phaseStatusLeft,
  130. }
  131. })
  132. expect(darkNarrow.clientWidth).toBe(356)
  133. expect(darkNarrow.scrollWidth).toBeLessThanOrEqual(darkNarrow.clientWidth)
  134. expect(darkNarrow.color).not.toBe(lightColor)
  135. expect(darkNarrow.decoration).toContain('underline')
  136. expect(Number.parseFloat(darkNarrow.focusWidth)).toBeGreaterThanOrEqual(2)
  137. expect(darkNarrow.statusWidth).toBe(64)
  138. expect(darkNarrow.statusFontSize).toBe('13px')
  139. expect(darkNarrow.runHeight).toBe(32)
  140. expect(darkNarrow.phaseHeight).toBe(32)
  141. expect(darkNarrow.phaseTitleRight).toBeLessThanOrEqual(darkNarrow.phaseStatusLeft)
  142. await page.locator('[data-workflow-run]').evaluate((element) => {
  143. (element as HTMLElement).style.removeProperty('width')
  144. document.body.removeAttribute('data-ds-dark-theme')
  145. })
  146. await page.setViewportSize({ width: 1280, height: 800 })
  147. await member.click()
  148. await page.getByText(CHILD_PROMPT, { exact: true }).waitFor({ timeout: 15_000 })
  149. const sessions = page.getByRole('tree', { name: 'Sessions' })
  150. await sessions.getByRole('treeitem', { name: /Use the workflow tool exactly/ }).click()
  151. await settled
  152. await page.locator('[data-workflow-run][data-run-status="completed"]').waitFor()
  153. expect(await page.locator('[data-chat-flow-kind="tool-call"]').count()).toBeGreaterThanOrEqual(1)
  154. expect(await page.locator('[data-chat-flow-kind="workflow-run"]').count()).toBe(1)
  155. const terminalWorkflow = page.getByRole('button', { name: /^snapshot-flow/ })
  156. await terminalWorkflow.waitFor()
  157. expect(await terminalWorkflow.getAttribute('aria-expanded')).toBe('false')
  158. expect(await terminalWorkflow.evaluate(element => getComputedStyle(element).cursor)).toBe('pointer')
  159. await terminalWorkflow.click()
  160. const terminalPhase = page.getByRole('button', { name: /^Run/ })
  161. await terminalPhase.waitFor()
  162. expect(await terminalPhase.getAttribute('aria-expanded')).toBe('false')
  163. expect(await terminalPhase.evaluate(element => getComputedStyle(element).cursor)).toBe('pointer')
  164. await terminalPhase.click()
  165. await page.getByText(CHILD_PROMPT, { exact: false }).waitFor()
  166. await expect.poll(
  167. () => page.getByRole('button', { name: /^Open Reply with exactly the word/ }).count(),
  168. { timeout: 10_000 },
  169. ).toBe(0)
  170. }, 90_000)
  171. it('rebuilds the terminal record from history after reload', async () => {
  172. onTestFailed(() => saveFailureShot(page, 'web-e2e-workflow-run-history'))
  173. await page.reload({ waitUntil: 'load' })
  174. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  175. const workflow = page.getByRole('button', { name: /^snapshot-flow/ })
  176. await workflow.waitFor({ timeout: 15_000 })
  177. expect(await workflow.getAttribute('aria-expanded')).toBe('false')
  178. const snapshot = await captureStableAria(page, '[data-chat-flow]', scaffold.workspaceCwd)
  179. await compareOrRefreshGolden(UI_EXPECTED, snapshot, MODE)
  180. await workflow.click()
  181. const phase = page.getByRole('button', { name: /^Run/ })
  182. await phase.waitFor()
  183. expect(await phase.getAttribute('aria-expanded')).toBe('false')
  184. await phase.click()
  185. await page.getByText(CHILD_PROMPT, { exact: false }).waitFor()
  186. expect(await page.getByRole('button', { name: /^Open Reply with exactly the word/ }).count()).toBe(0)
  187. }, 60_000)
  188. it('stays clean and owns only its one golden', async () => {
  189. expect(tripwire.pageErrors).toEqual([])
  190. expect(tripwire.warnings).toEqual([])
  191. await assertFixtureInventory(SNAPSHOT_DIR, ['ui-live.expected.md', 'ui.expected.md'])
  192. })
  193. })