workflow-run.e2e.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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, onTestFinished } 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.v3.jsonl')
  25. const CHILD_FIXTURE = join(REPO_ROOT, 'snapshots/session/workflow-run/session.1.v3.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 releaseChild = Promise.withResolvers<undefined>()
  34. const waitForParentSettlement = (): Promise<SessionId> => new Promise((resolve, reject) => {
  35. let dispose = (): void => {}
  36. dispose = scaffold.ctx.on('session/event', (session: Session, event: SessionEvent) => {
  37. if (event.type !== 'turn/end' || session.header.origin === 'subagent') return
  38. dispose()
  39. void (async () => {
  40. await scaffold.ctx.agents.get(session.id)?.whenIdle()
  41. await scaffold.ctx.sessions.flush(session)
  42. resolve(session.id)
  43. })().catch(reject)
  44. })
  45. })
  46. beforeAll(async () => {
  47. const prompts = fixtureUserPrompts(await readFile(PARENT_FIXTURE, 'utf8'))
  48. expect(prompts).toHaveLength(1)
  49. prompt = prompts[0]!
  50. scaffold = await launchWebScaffold({
  51. replayFixture: PARENT_FIXTURE,
  52. replayChildFixtures: [CHILD_FIXTURE],
  53. compareReplaySession: false,
  54. })
  55. // Keep the live child available throughout disclosure, layout, and navigation checks.
  56. scaffold.ctx.on('llm/stream', async function* (options, next) {
  57. const session = options.sessionId === undefined ? undefined : scaffold.ctx.sessions.get(options.sessionId)
  58. if (session?.header.origin === 'subagent') await releaseChild.promise
  59. yield* next()
  60. }, { prepend: true })
  61. browser = await chromium.launch()
  62. page = await newEnglishPage(browser)
  63. tripwire = watchConsole(page)
  64. await page.goto(scaffold.authenticatedUrl, { waitUntil: 'load' })
  65. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  66. await connectFreshWorkspace(page, scaffold.workspaceCwd)
  67. }, 120_000)
  68. afterAll(async () => {
  69. releaseChild.resolve(undefined)
  70. await browser?.close()
  71. await scaffold?.close()
  72. })
  73. it('shows the live member, opens its local child, then retains the settled record beside the tool row', async () => {
  74. onTestFailed(() => saveFailureShot(page, 'web-e2e-workflow-run-live'))
  75. const settled = waitForParentSettlement()
  76. onTestFinished(() => {
  77. releaseChild.resolve(undefined)
  78. })
  79. const input = page.locator('[data-composer-input]').first()
  80. await input.fill(prompt)
  81. await input.press('Enter')
  82. const workflow = page.locator('[data-workflow-run][data-run-status="running"]')
  83. await workflow.waitFor({ timeout: 30_000 })
  84. const disclosures = workflow.locator('[data-disclosure-row]')
  85. await disclosures.nth(1).waitFor({ timeout: 15_000 })
  86. const runDisclosure = disclosures.nth(0)
  87. const phaseDisclosure = disclosures.nth(1)
  88. expect(await runDisclosure.getAttribute('role')).toBe('button')
  89. expect(await runDisclosure.getAttribute('aria-expanded')).toBe('true')
  90. expect(await phaseDisclosure.getAttribute('role')).toBe('button')
  91. expect(await phaseDisclosure.getAttribute('aria-expanded')).toBe('true')
  92. expect(await runDisclosure.evaluate(element => getComputedStyle(element).cursor)).toBe('pointer')
  93. expect(await phaseDisclosure.evaluate(element => getComputedStyle(element).cursor)).toBe('pointer')
  94. const member = page.getByRole('button', { name: /^Open Reply with exactly the word/ })
  95. await member.waitFor({ timeout: 15_000 })
  96. await phaseDisclosure.click()
  97. expect(await phaseDisclosure.getAttribute('aria-expanded')).toBe('false')
  98. expect(await member.count()).toBe(0)
  99. const liveSnapshot = await captureStableAria(page, '[data-workflow-run]', scaffold.workspaceCwd)
  100. await compareOrRefreshGolden(UI_LIVE_EXPECTED, liveSnapshot, MODE)
  101. await phaseDisclosure.press('Enter')
  102. await member.waitFor()
  103. await runDisclosure.click()
  104. expect(await runDisclosure.getAttribute('aria-expanded')).toBe('false')
  105. expect(await disclosures.count()).toBe(1)
  106. await runDisclosure.press('Space')
  107. expect(await disclosures.count()).toBe(2)
  108. expect(await phaseDisclosure.getAttribute('aria-expanded')).toBe('true')
  109. const lightColor = await member.locator('[data-member-label]').evaluate(element => getComputedStyle(element).color)
  110. await page.setViewportSize({ width: 560, height: 800 })
  111. await page.evaluate(() => { document.body.setAttribute('data-ds-dark-theme', '') })
  112. // Exercise keyboard focus after the responsive layout has changed.
  113. await phaseDisclosure.focus()
  114. await phaseDisclosure.press('Tab')
  115. await expect.poll(() => member.evaluate(element => element.matches(':focus-visible'))).toBe(true)
  116. const darkNarrow = await page.locator('[data-workflow-run]').evaluate((element) => {
  117. const panel = element as HTMLElement
  118. panel.style.width = '356px'
  119. const label = element.querySelector('[data-member-label]')
  120. const labelWrap = element.querySelector('[data-member-label-wrap]')
  121. const status = element.querySelector('[data-member-status-text]')
  122. const disclosures = element.querySelectorAll('[data-disclosure-row]')
  123. const runHeader = disclosures[0]
  124. const phaseHeader = disclosures[1]
  125. const phaseTitle = phaseHeader?.children.item(1) as HTMLElement | null
  126. const phaseStatus = element.querySelector('[data-phase-status-text]')
  127. const originalPhaseTitle = phaseTitle?.textContent ?? ''
  128. if (phaseTitle !== null) phaseTitle.textContent = 'A phase name long enough to require ellipsis in the narrow layout'
  129. const phaseTitleRight = phaseTitle?.getBoundingClientRect().right ?? 0
  130. const phaseStatusLeft = phaseStatus?.getBoundingClientRect().left ?? 0
  131. if (phaseTitle !== null) phaseTitle.textContent = originalPhaseTitle
  132. return {
  133. clientWidth: element.clientWidth,
  134. scrollWidth: element.scrollWidth,
  135. color: label === null ? '' : getComputedStyle(label).color,
  136. decoration: label === null ? '' : getComputedStyle(label).textDecorationLine,
  137. focusWidth: labelWrap === null ? '' : getComputedStyle(labelWrap).outlineWidth,
  138. statusWidth: status?.getBoundingClientRect().width ?? 0,
  139. statusFontSize: status === null ? '' : getComputedStyle(status).fontSize,
  140. runHeight: runHeader?.getBoundingClientRect().height ?? 0,
  141. phaseHeight: phaseHeader?.getBoundingClientRect().height ?? 0,
  142. phaseTitleRight,
  143. phaseStatusLeft,
  144. }
  145. })
  146. expect(darkNarrow.clientWidth).toBe(356)
  147. expect(darkNarrow.scrollWidth).toBeLessThanOrEqual(darkNarrow.clientWidth)
  148. expect(darkNarrow.color).not.toBe(lightColor)
  149. expect(darkNarrow.decoration).toContain('underline')
  150. expect(Number.parseFloat(darkNarrow.focusWidth)).toBeGreaterThanOrEqual(2)
  151. expect(darkNarrow.statusWidth).toBe(64)
  152. expect(darkNarrow.statusFontSize).toBe('13px')
  153. expect(darkNarrow.runHeight).toBe(32)
  154. expect(darkNarrow.phaseHeight).toBe(32)
  155. expect(darkNarrow.phaseTitleRight).toBeLessThanOrEqual(darkNarrow.phaseStatusLeft)
  156. await page.locator('[data-workflow-run]').evaluate((element) => {
  157. (element as HTMLElement).style.removeProperty('width')
  158. document.body.removeAttribute('data-ds-dark-theme')
  159. })
  160. await page.setViewportSize({ width: 1280, height: 800 })
  161. await member.click()
  162. await page.getByText(CHILD_PROMPT, { exact: true }).waitFor({ timeout: 15_000 })
  163. const sessions = page.getByRole('tree', { name: 'Sessions' })
  164. await sessions.getByRole('treeitem', { name: /Use the workflow tool exactly/ }).click()
  165. releaseChild.resolve(undefined)
  166. await settled
  167. await expandTurnProcesses(page)
  168. await page.locator('[data-workflow-run][data-run-status="completed"]').waitFor()
  169. expect(await page.locator('[data-chat-flow-kind="tool-call"]').count()).toBeGreaterThanOrEqual(1)
  170. expect(await page.locator('[data-chat-flow-kind="workflow-run"]').count()).toBe(1)
  171. const terminalWorkflow = page.getByRole('button', { name: /^snapshot-flow/ })
  172. await terminalWorkflow.waitFor()
  173. expect(await terminalWorkflow.getAttribute('aria-expanded')).toBe('false')
  174. expect(await terminalWorkflow.evaluate(element => getComputedStyle(element).cursor)).toBe('pointer')
  175. await terminalWorkflow.click()
  176. const terminalPhase = page.getByRole('button', { name: /^Run/ })
  177. await terminalPhase.waitFor()
  178. expect(await terminalPhase.getAttribute('aria-expanded')).toBe('false')
  179. expect(await terminalPhase.evaluate(element => getComputedStyle(element).cursor)).toBe('pointer')
  180. await terminalPhase.click()
  181. await page.getByText(CHILD_PROMPT, { exact: false }).waitFor()
  182. await expect.poll(
  183. () => page.getByRole('button', { name: /^Open Reply with exactly the word/ }).count(),
  184. { timeout: 10_000 },
  185. ).toBe(0)
  186. }, 90_000)
  187. it('rebuilds the terminal record from history after reload', async () => {
  188. onTestFailed(() => saveFailureShot(page, 'web-e2e-workflow-run-history'))
  189. await page.reload({ waitUntil: 'load' })
  190. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  191. await expandTurnProcesses(page)
  192. const workflow = page.getByRole('button', { name: /^snapshot-flow/ })
  193. await workflow.waitFor({ timeout: 15_000 })
  194. expect(await workflow.getAttribute('aria-expanded')).toBe('false')
  195. const snapshot = await captureStableAria(page, '[data-chat-flow]', scaffold.workspaceCwd)
  196. await compareOrRefreshGolden(UI_EXPECTED, snapshot, MODE)
  197. await workflow.click()
  198. const phase = page.getByRole('button', { name: /^Run/ })
  199. await phase.waitFor()
  200. expect(await phase.getAttribute('aria-expanded')).toBe('false')
  201. await phase.click()
  202. await page.getByText(CHILD_PROMPT, { exact: false }).waitFor()
  203. expect(await page.getByRole('button', { name: /^Open Reply with exactly the word/ }).count()).toBe(0)
  204. }, 60_000)
  205. it('stays clean and owns only its one golden', async () => {
  206. expect(tripwire.pageErrors).toEqual([])
  207. expect(tripwire.warnings).toEqual([])
  208. await assertFixtureInventory(SNAPSHOT_DIR, ['ui-live.expected.md', 'ui.expected.md'])
  209. })
  210. })