seeded-history.e2e.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Web e2e scenario: seeded history. A recorded session seeded cold through
  2. // the REAL persistence API renders purely from the log — the surface nothing
  3. // else covers: sidebar cold listing, the implicit resume/attach inside the
  4. // history RPC, history-page tool views, and the client fold of historical
  5. // events — with ZERO model calls in replay (no replay fixture; a stray stream
  6. // fails loud on the open llm seam). The seed is a recorded fixture under the
  7. // same record discipline as every other: DSH_SNAPSHOT=record drives the turn
  8. // live through the composer (real read tool against seeded workspace files)
  9. // and harvests seed.jsonl; replay/refresh seed it cold and only render.
  10. import { readFile, writeFile, mkdir } from 'node:fs/promises'
  11. import { fileURLToPath } from 'node:url'
  12. import type { Browser, Page } from 'playwright'
  13. import { chromium } from 'playwright'
  14. import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest'
  15. import { join } from 'node:path'
  16. import {
  17. assertFixtureInventory, captureStableAria, compareOrRefreshGolden, fixtureUserPrompts,
  18. launchWebScaffold, recordFixture, seedSession, watchConsole, webSnapshotMode, type WebScaffold,
  19. } from './scaffold.ts'
  20. import { saveFailureShot } from './support.ts'
  21. const SNAPSHOT_DIR = fileURLToPath(new URL('./snapshots/seeded-history', import.meta.url))
  22. const SEED = fileURLToPath(new URL('./snapshots/seeded-history/seed.jsonl', import.meta.url))
  23. const UI_EXPECTED = fileURLToPath(new URL('./snapshots/seeded-history/ui.expected.md', import.meta.url))
  24. const MODE = webSnapshotMode()
  25. const SEED_ID = 'seeded-history-web-e2e'
  26. const PROMPT = 'Use the read tool twice in one assistant message: read a.txt and b.txt. Then reply with the single word DONE and stop.'
  27. describe('web e2e: seeded history renders through cold resume', () => {
  28. let scaffold: WebScaffold
  29. let browser: Browser
  30. let page: Page
  31. let tripwire: ReturnType<typeof watchConsole>
  32. beforeAll(async () => {
  33. scaffold = await launchWebScaffold({})
  34. // The workspace-aware flow runs sessions in <workspaceRoot>/workspace
  35. // (the composer's default draft name); the read-tool targets must live in
  36. // that session cwd. Pre-creating the directory is safe: create-by-name
  37. // adopts an existing directory.
  38. const sessionCwd = join(scaffold.workspaceCwd, 'workspace')
  39. await mkdir(sessionCwd, { recursive: true })
  40. await writeFile(join(sessionCwd, 'a.txt'), 'alpha\n')
  41. await writeFile(join(sessionCwd, 'b.txt'), 'beta\n')
  42. if (MODE !== 'record') {
  43. const raw = await readFile(SEED, 'utf8')
  44. expect(fixtureUserPrompts(raw), 'seed fixture must carry exactly the drive prompt').toEqual([PROMPT])
  45. await seedSession(scaffold, raw, SEED_ID)
  46. }
  47. browser = await chromium.launch()
  48. page = await browser.newPage({ viewport: { width: 1680, height: 1000 } })
  49. tripwire = watchConsole(page)
  50. await page.goto(scaffold.baseUrl, { waitUntil: 'load' })
  51. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  52. }, 120_000)
  53. afterAll(async () => {
  54. await browser?.close()
  55. await scaffold?.close()
  56. })
  57. it.skipIf(MODE !== 'record')('records the seed turn live through the composer', async () => {
  58. onTestFailed(() => saveFailureShot(page, 'web-e2e-seeded-record'))
  59. const input = page.locator('textarea').first()
  60. await input.waitFor({ timeout: 10_000 })
  61. const settled = scaffold.whenTurnSettled()
  62. await input.fill(PROMPT)
  63. await input.press('Enter')
  64. const sessionId = await settled
  65. await recordFixture(scaffold, sessionId, SEED)
  66. }, 200_000)
  67. it.skipIf(MODE === 'record')('lists the seeded session cold and renders its history from the log', async () => {
  68. onTestFailed(() => saveFailureShot(page, 'web-e2e-seeded-history'))
  69. // The sidebar tree collapses workspace groups by default: click the group
  70. // row (treeitem 0) to expand, then the revealed session row.
  71. const groupRow = page.locator('[role="treeitem"]').first()
  72. await groupRow.waitFor({ timeout: 15_000 })
  73. await groupRow.click()
  74. const sessionRow = page.locator('[role="treeitem"]').nth(1)
  75. await sessionRow.waitFor({ timeout: 10_000 })
  76. await sessionRow.click()
  77. // Settled barrier for history: the recorded final assistant text renders.
  78. await expect.poll(() => page.getByText('DONE', { exact: true }).count(), { timeout: 15_000 }).toBe(1)
  79. // Tool cards render from logged tool/call + tool/result alone (views are
  80. // host-recomputed per page; the generic card is the documented default).
  81. const toolRows = page.locator('[data-variant], [data-sample]')
  82. await expect.poll(() => toolRows.count(), { timeout: 10_000 }).toBeGreaterThanOrEqual(2)
  83. expect(await page.getByText('a.txt', { exact: false }).count()).toBeGreaterThan(0)
  84. }, 60_000)
  85. it.skipIf(MODE === 'record')('matches the historical conversation aria golden', async () => {
  86. onTestFailed(() => saveFailureShot(page, 'web-e2e-seeded-aria'))
  87. await page.getByRole('button', {
  88. // This scenario deliberately leaves the LLM seam open to prove zero
  89. // model calls. History still restores the selected id, but no catalog
  90. // adapter exists to provide its presentation name.
  91. name: '选择模型,当前 deepseek-v4-flash',
  92. }).waitFor({ timeout: 10_000 })
  93. const snapshot = (await captureStableAria(page, '[class*="centerCol"]', scaffold.workspaceCwd))
  94. .split(SEED_ID).join('{{seededId}}')
  95. await compareOrRefreshGolden(UI_EXPECTED, snapshot, MODE)
  96. })
  97. it.skipIf(MODE === 'record')('expands and collapses a tool row rebuilt from the cold log', async () => {
  98. onTestFailed(() => saveFailureShot(page, 'web-e2e-seeded-toolrow'))
  99. // Interaction over cold-resumed history: read rows are expand-in-place
  100. // rows (rowExpands routes the click to toggleExpand, not openDetails), so
  101. // the gesture under test is the inline fold over log-rebuilt content.
  102. // Runs after the golden capture; still zero model calls.
  103. const row = page.locator('[data-variant] [data-clickable][role="button"]').first()
  104. await row.waitFor({ timeout: 10_000 })
  105. expect(await row.getAttribute('aria-expanded')).toBe('false')
  106. await row.click()
  107. await expect.poll(() => row.getAttribute('aria-expanded'), { timeout: 5_000 }).toBe('true')
  108. // The expanded body renders the recorded tool result (a.txt's contents).
  109. await expect.poll(() => page.getByText('alpha', { exact: false }).count(), { timeout: 5_000 }).toBeGreaterThan(0)
  110. await row.click()
  111. await expect.poll(() => row.getAttribute('aria-expanded'), { timeout: 5_000 }).toBe('false')
  112. })
  113. it.skipIf(MODE === 'record')('issued zero model calls and stayed clean', async () => {
  114. // No replay fixture was installed and the llm seam is open — any stray
  115. // stream would have failed the turn loudly. Cleanliness pins the wire.
  116. expect(tripwire.pageErrors).toEqual([])
  117. expect(tripwire.warnings).toEqual([])
  118. await assertFixtureInventory(SNAPSHOT_DIR, ['seed.jsonl', 'ui.expected.md'])
  119. })
  120. })