workflow-run.e2e.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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, expandTurnProcesses, newEnglishPage, REPO_ROOT, saveFailureShot,
  19. } from './support.ts'
  20. const MODE = webSnapshotMode()
  21. const SNAPSHOT_DIR = fileURLToPath(new URL('../../../snapshots/web/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, 'snapshots/session/workflow-run/session.v2.jsonl')
  25. const CHILD_FIXTURE = join(REPO_ROOT, 'snapshots/session/workflow-run/session.1.v2.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. compareReplaySession: false,
  54. })
  55. browser = await chromium.launch()
  56. page = await newEnglishPage(browser)
  57. tripwire = watchConsole(page)
  58. await page.goto(scaffold.authenticatedUrl, { waitUntil: 'load' })
  59. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  60. await connectFreshWorkspace(page, scaffold.workspaceCwd)
  61. }, 120_000)
  62. afterAll(async () => {
  63. await browser?.close()
  64. await scaffold?.close()
  65. })
  66. it('shows the live member, opens its local child, then retains the settled record beside the tool row', async () => {
  67. onTestFailed(() => saveFailureShot(page, 'web-e2e-workflow-run-live'))
  68. const settled = waitForParentSettlement()
  69. const input = page.locator('[data-composer-input]').first()
  70. await input.fill(prompt)
  71. await input.press('Enter')
  72. const workflow = page.locator('[data-workflow-run][data-run-status="running"]')
  73. await workflow.waitFor({ timeout: 30_000 })
  74. const disclosures = workflow.locator('[data-disclosure-row]')
  75. await disclosures.nth(1).waitFor({ timeout: 15_000 })
  76. const runDisclosure = disclosures.nth(0)
  77. const phaseDisclosure = disclosures.nth(1)
  78. expect(await runDisclosure.getAttribute('role')).toBe('button')
  79. expect(await runDisclosure.getAttribute('aria-expanded')).toBe('true')
  80. expect(await phaseDisclosure.getAttribute('role')).toBe('button')
  81. expect(await phaseDisclosure.getAttribute('aria-expanded')).toBe('true')
  82. expect(await runDisclosure.evaluate(element => getComputedStyle(element).cursor)).toBe('pointer')
  83. expect(await phaseDisclosure.evaluate(element => getComputedStyle(element).cursor)).toBe('pointer')
  84. const member = page.getByRole('button', { name: /^Open Reply with exactly the word/ })
  85. await member.waitFor({ timeout: 15_000 })
  86. await phaseDisclosure.click()
  87. expect(await phaseDisclosure.getAttribute('aria-expanded')).toBe('false')
  88. expect(await member.count()).toBe(0)
  89. const liveSnapshot = await captureStableAria(page, '[data-workflow-run]', scaffold.workspaceCwd)
  90. await compareOrRefreshGolden(UI_LIVE_EXPECTED, liveSnapshot, MODE)
  91. await phaseDisclosure.press('Enter')
  92. await member.waitFor()
  93. await runDisclosure.click()
  94. expect(await runDisclosure.getAttribute('aria-expanded')).toBe('false')
  95. expect(await disclosures.count()).toBe(1)
  96. await runDisclosure.press('Space')
  97. expect(await disclosures.count()).toBe(2)
  98. expect(await phaseDisclosure.getAttribute('aria-expanded')).toBe('true')
  99. await member.focus()
  100. const lightColor = await member.locator('[data-member-label]').evaluate(element => getComputedStyle(element).color)
  101. await page.setViewportSize({ width: 560, height: 800 })
  102. await page.evaluate(() => { document.body.setAttribute('data-ds-dark-theme', '') })
  103. const darkNarrow = await page.locator('[data-workflow-run]').evaluate((element) => {
  104. const panel = element as HTMLElement
  105. panel.style.width = '356px'
  106. const label = element.querySelector('[data-member-label]')
  107. const labelWrap = element.querySelector('[data-member-label-wrap]')
  108. const status = element.querySelector('[data-member-status-text]')
  109. const disclosures = element.querySelectorAll('[data-disclosure-row]')
  110. const runHeader = disclosures[0]
  111. const phaseHeader = disclosures[1]
  112. const phaseTitle = phaseHeader?.children.item(1) as HTMLElement | null
  113. const phaseStatus = element.querySelector('[data-phase-status-text]')
  114. const originalPhaseTitle = phaseTitle?.textContent ?? ''
  115. if (phaseTitle !== null) phaseTitle.textContent = 'A phase name long enough to require ellipsis in the narrow layout'
  116. const phaseTitleRight = phaseTitle?.getBoundingClientRect().right ?? 0
  117. const phaseStatusLeft = phaseStatus?.getBoundingClientRect().left ?? 0
  118. if (phaseTitle !== null) phaseTitle.textContent = originalPhaseTitle
  119. return {
  120. clientWidth: element.clientWidth,
  121. scrollWidth: element.scrollWidth,
  122. color: label === null ? '' : getComputedStyle(label).color,
  123. decoration: label === null ? '' : getComputedStyle(label).textDecorationLine,
  124. focusWidth: labelWrap === null ? '' : getComputedStyle(labelWrap).outlineWidth,
  125. statusWidth: status?.getBoundingClientRect().width ?? 0,
  126. statusFontSize: status === null ? '' : getComputedStyle(status).fontSize,
  127. runHeight: runHeader?.getBoundingClientRect().height ?? 0,
  128. phaseHeight: phaseHeader?.getBoundingClientRect().height ?? 0,
  129. phaseTitleRight,
  130. phaseStatusLeft,
  131. }
  132. })
  133. expect(darkNarrow.clientWidth).toBe(356)
  134. expect(darkNarrow.scrollWidth).toBeLessThanOrEqual(darkNarrow.clientWidth)
  135. expect(darkNarrow.color).not.toBe(lightColor)
  136. expect(darkNarrow.decoration).toContain('underline')
  137. expect(Number.parseFloat(darkNarrow.focusWidth)).toBeGreaterThanOrEqual(2)
  138. expect(darkNarrow.statusWidth).toBe(64)
  139. expect(darkNarrow.statusFontSize).toBe('13px')
  140. expect(darkNarrow.runHeight).toBe(32)
  141. expect(darkNarrow.phaseHeight).toBe(32)
  142. expect(darkNarrow.phaseTitleRight).toBeLessThanOrEqual(darkNarrow.phaseStatusLeft)
  143. await page.locator('[data-workflow-run]').evaluate((element) => {
  144. (element as HTMLElement).style.removeProperty('width')
  145. document.body.removeAttribute('data-ds-dark-theme')
  146. })
  147. await page.setViewportSize({ width: 1280, height: 800 })
  148. await member.click()
  149. await page.getByText(CHILD_PROMPT, { exact: true }).waitFor({ timeout: 15_000 })
  150. const sessions = page.getByRole('tree', { name: 'Sessions' })
  151. await sessions.getByRole('treeitem', { name: /Use the workflow tool exactly/ }).click()
  152. await settled
  153. await expandTurnProcesses(page)
  154. await page.locator('[data-workflow-run][data-run-status="completed"]').waitFor()
  155. expect(await page.locator('[data-chat-flow-kind="tool-call"]').count()).toBeGreaterThanOrEqual(1)
  156. expect(await page.locator('[data-chat-flow-kind="workflow-run"]').count()).toBe(1)
  157. const terminalWorkflow = page.getByRole('button', { name: /^snapshot-flow/ })
  158. await terminalWorkflow.waitFor()
  159. expect(await terminalWorkflow.getAttribute('aria-expanded')).toBe('false')
  160. expect(await terminalWorkflow.evaluate(element => getComputedStyle(element).cursor)).toBe('pointer')
  161. await terminalWorkflow.click()
  162. const terminalPhase = page.getByRole('button', { name: /^Run/ })
  163. await terminalPhase.waitFor()
  164. expect(await terminalPhase.getAttribute('aria-expanded')).toBe('false')
  165. expect(await terminalPhase.evaluate(element => getComputedStyle(element).cursor)).toBe('pointer')
  166. await terminalPhase.click()
  167. await page.getByText(CHILD_PROMPT, { exact: false }).waitFor()
  168. await expect.poll(
  169. () => page.getByRole('button', { name: /^Open Reply with exactly the word/ }).count(),
  170. { timeout: 10_000 },
  171. ).toBe(0)
  172. }, 90_000)
  173. it('rebuilds the terminal record from history after reload', async () => {
  174. onTestFailed(() => saveFailureShot(page, 'web-e2e-workflow-run-history'))
  175. await page.reload({ waitUntil: 'load' })
  176. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  177. await expandTurnProcesses(page)
  178. const workflow = page.getByRole('button', { name: /^snapshot-flow/ })
  179. await workflow.waitFor({ timeout: 15_000 })
  180. expect(await workflow.getAttribute('aria-expanded')).toBe('false')
  181. const snapshot = await captureStableAria(page, '[data-chat-flow]', scaffold.workspaceCwd)
  182. await compareOrRefreshGolden(UI_EXPECTED, snapshot, MODE)
  183. await workflow.click()
  184. const phase = page.getByRole('button', { name: /^Run/ })
  185. await phase.waitFor()
  186. expect(await phase.getAttribute('aria-expanded')).toBe('false')
  187. await phase.click()
  188. await page.getByText(CHILD_PROMPT, { exact: false }).waitFor()
  189. expect(await page.getByRole('button', { name: /^Open Reply with exactly the word/ }).count()).toBe(0)
  190. }, 60_000)
  191. it('stays clean and owns only its one golden', async () => {
  192. expect(tripwire.pageErrors).toEqual([])
  193. expect(tripwire.warnings).toEqual([])
  194. await assertFixtureInventory(SNAPSHOT_DIR, ['ui-live.expected.md', 'ui.expected.md'])
  195. })
  196. })