background-job-list.e2e.ts 5.5 KB

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