workflow-run.e2e.ts 11 KB

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