background-task-list.e2e.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Web e2e scenario: the session-header background-task list over the real
  2. // host. No model call is involved — a genuine `run_in_background` bash call
  3. // registers with `ctx.tasks`, and the assertion chain is the whole delivery
  4. // path: registry change feed → api-proxy `session/tasks` frame → the client's
  5. // `tasksBySession` mirror → the header action.
  6. import { readFile } from 'node:fs/promises'
  7. import { fileURLToPath } from 'node:url'
  8. import { join } from 'node:path'
  9. import type { Browser, Page } from 'playwright'
  10. import { chromium } from 'playwright'
  11. import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest'
  12. import type { Agent } from '@deepseek-ai/dsh-agent'
  13. import { CallId } from '@deepseek-ai/dsh-llm'
  14. import { SessionId } from '@deepseek-ai/dsh-session'
  15. import { TaskId } from '@deepseek-ai/dsh-tasks'
  16. import {
  17. assertFixtureInventory, captureStableAria, compareOrRefreshGolden,
  18. launchWebScaffold, seedSession, watchConsole, webSnapshotMode, type WebScaffold,
  19. } from './scaffold.ts'
  20. import { newEnglishPage, saveFailureShot } from './support.ts'
  21. const FIXTURE = fileURLToPath(new URL('./snapshots/fresh-round-trip/session.jsonl', import.meta.url))
  22. const SNAPSHOT_DIR = fileURLToPath(new URL('./snapshots/background-task-list', import.meta.url))
  23. const RUNNING_EXPECTED = join(SNAPSHOT_DIR, 'running.expected.md')
  24. const SETTLED_EXPECTED = join(SNAPSHOT_DIR, 'settled.expected.md')
  25. const MODE = webSnapshotMode()
  26. const SEED_ID = 'background-task-list-web-e2e'
  27. // Long enough that the running assertions never race the process exiting on
  28. // their own; the test kills it explicitly to reach the settled state.
  29. const COMMAND = 'sleep 45'
  30. /**
  31. * Wait for the Host to publish the live Agent that opening a session resumes.
  32. * @param scaffold - the booted web scaffold.
  33. * @param sessionId - the opened session's identity.
  34. * @returns the registered Agent instance.
  35. */
  36. async function liveAgent(scaffold: WebScaffold, sessionId: SessionId): Promise<Agent> {
  37. const deadline = Date.now() + 30_000
  38. for (;;) {
  39. const found = scaffold.ctx.agents.get(sessionId)
  40. if (found !== undefined) return found
  41. if (Date.now() > deadline) throw new Error(`opening session "${sessionId}" published no live Agent`)
  42. await new Promise(resolve => setTimeout(resolve, 100))
  43. }
  44. }
  45. describe.skipIf(MODE === 'record')('web e2e: background task list', () => {
  46. let scaffold: WebScaffold
  47. let browser: Browser
  48. let page: Page
  49. let tripwire: ReturnType<typeof watchConsole>
  50. let agent: Agent
  51. let taskId: TaskId
  52. beforeAll(async () => {
  53. scaffold = await launchWebScaffold({})
  54. await seedSession(scaffold, await readFile(FIXTURE, 'utf8'), SEED_ID)
  55. browser = await chromium.launch()
  56. page = await newEnglishPage(browser)
  57. tripwire = watchConsole(page)
  58. await page.goto(scaffold.baseUrl, { waitUntil: 'load' })
  59. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  60. const groupRow = page.locator('[role="treeitem"]').first()
  61. await groupRow.waitFor({ timeout: 15_000 })
  62. await groupRow.click()
  63. const sessionRow = page.locator('[role="treeitem"]').nth(1)
  64. await sessionRow.waitFor({ timeout: 10_000 })
  65. await sessionRow.click()
  66. // Opening the session drives the Host's ordinary Agent resolution; the
  67. // task owner must be that exact live instance, never a second one.
  68. // `expect.poll` is test-scoped, so this hook polls by hand.
  69. agent = await liveAgent(scaffold, SessionId(SEED_ID))
  70. }, 120_000)
  71. afterAll(async () => {
  72. await browser?.close()
  73. await scaffold?.close()
  74. })
  75. it('shows a running background task in the session header without a refresh', async () => {
  76. onTestFailed(() => saveFailureShot(page, 'web-e2e-background-task-running'))
  77. // Point assertion, not a poll: `expect.poll` retries until a predicate
  78. // holds, so polling for zero passes at t=0 and proves nothing. The
  79. // "renders nothing without a task" branch is owned by the component suite.
  80. const trigger = page.getByRole('button', { name: '1 background task running' })
  81. expect(await trigger.count()).toBe(0)
  82. const started = await scaffold.ctx.tools.execute({
  83. signal: new AbortController().signal,
  84. callId: CallId('background-task-list-e2e'),
  85. name: 'bash',
  86. arguments: { command: COMMAND, description: 'Hold a background slot open', run_in_background: true },
  87. agent,
  88. })
  89. const reported = started.content.map(block => block.type === 'text' ? block.text : '').join('')
  90. const matched = /\bbash-\d+\b/.exec(reported)
  91. if (matched === null) throw new Error(`background bash reported no task id: ${reported}`)
  92. taskId = TaskId(matched[0])
  93. await trigger.waitFor({ timeout: 15_000 })
  94. await trigger.click()
  95. const row = page.getByRole('list', { name: 'Background tasks' }).getByRole('listitem').first()
  96. await row.waitFor({ timeout: 10_000 })
  97. await expect.poll(() => row.textContent()).toContain(COMMAND)
  98. const snapshot = await captureStableAria(page, '[class*="menu"]', scaffold.workspaceCwd)
  99. await compareOrRefreshGolden(RUNNING_EXPECTED, snapshot, MODE)
  100. expect(tripwire.pageErrors).toEqual([])
  101. expect(tripwire.warnings).toEqual([])
  102. }, 60_000)
  103. it('flips the open list to the cancelled outcome when the registry settles it', async () => {
  104. onTestFailed(() => saveFailureShot(page, 'web-e2e-background-task-settled'))
  105. expect(scaffold.ctx.tasks.kill(taskId, agent, 'web e2e cancellation')).toBe('requested')
  106. // The trigger drops its live count once the task leaves running/stopping,
  107. // which is also the proof that settlement reached the browser unprompted.
  108. const idle = page.getByRole('button', { name: '1 background task' })
  109. await idle.waitFor({ timeout: 20_000 })
  110. const snapshot = await captureStableAria(page, '[class*="menu"]', scaffold.workspaceCwd)
  111. await compareOrRefreshGolden(SETTLED_EXPECTED, snapshot, MODE)
  112. expect(tripwire.pageErrors).toEqual([])
  113. expect(tripwire.warnings).toEqual([])
  114. }, 60_000)
  115. it('keeps its snapshot inventory closed', async () => {
  116. await assertFixtureInventory(SNAPSHOT_DIR, ['running.expected.md', 'settled.expected.md'])
  117. })
  118. })