seeded-history.e2e.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 cold session also carries the one
  7. // keyless command-row surface: an Access-chip pick runs `/permission` on the
  8. // host, so the settled row's copy has a golden here. The seed is a recorded fixture under the
  9. // same record discipline as every other: DSH_SNAPSHOT=record drives the turn
  10. // live through the composer (real read tool against seeded workspace files)
  11. // and harvests seed.jsonl; replay/refresh seed it cold and only render.
  12. import { readFile, writeFile, mkdir } from 'node:fs/promises'
  13. import { fileURLToPath } from 'node:url'
  14. import type { Browser, Page } from 'playwright'
  15. import { chromium } from 'playwright'
  16. import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest'
  17. import { createUserMessage } from '@deepseek-ai/dsh-llm'
  18. import { SessionId } from '@deepseek-ai/dsh-session'
  19. import { join } from 'node:path'
  20. import {
  21. assertFixtureInventory, captureStableAria, compareOrRefreshGolden, fixtureUserPrompts,
  22. launchWebScaffold, recordFixture, seedSession, watchConsole, webSnapshotMode, type WebScaffold,
  23. } from './scaffold.ts'
  24. import { newEnglishPage, saveFailureShot } from './support.ts'
  25. const SNAPSHOT_DIR = fileURLToPath(new URL('./snapshots/seeded-history', import.meta.url))
  26. const SEED = fileURLToPath(new URL('./snapshots/seeded-history/seed.jsonl', import.meta.url))
  27. const UI_EXPECTED = fileURLToPath(new URL('./snapshots/seeded-history/ui.expected.md', import.meta.url))
  28. // The command-row golden: the same conversation after one /permission switch,
  29. // which is the only surface that shows a settled command row's copy.
  30. const COMMAND_ROW_EXPECTED = fileURLToPath(new URL('./snapshots/seeded-history/command-row.expected.md', import.meta.url))
  31. const MODE = webSnapshotMode()
  32. const SEED_ID = 'seeded-history-web-e2e'
  33. 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.'
  34. describe('web e2e: seeded history renders through cold resume', () => {
  35. let scaffold: WebScaffold
  36. let browser: Browser
  37. let page: Page
  38. let tripwire: ReturnType<typeof watchConsole>
  39. beforeAll(async () => {
  40. scaffold = await launchWebScaffold({})
  41. // The workspace-aware flow runs sessions in <workspaceRoot>/workspace
  42. // (the composer's default draft name); the read-tool targets must live in
  43. // that session cwd. Pre-creating the directory is safe: create-by-name
  44. // adopts an existing directory.
  45. const sessionCwd = join(scaffold.workspaceCwd, 'workspace')
  46. await mkdir(sessionCwd, { recursive: true })
  47. await writeFile(join(sessionCwd, 'a.txt'), 'alpha\n')
  48. await writeFile(join(sessionCwd, 'b.txt'), 'beta\n')
  49. if (MODE !== 'record') {
  50. const raw = await readFile(SEED, 'utf8')
  51. expect(fixtureUserPrompts(raw), 'seed fixture must carry exactly the drive prompt').toEqual([PROMPT])
  52. await seedSession(scaffold, raw, SEED_ID)
  53. }
  54. browser = await chromium.launch()
  55. page = await newEnglishPage(browser)
  56. tripwire = watchConsole(page)
  57. await page.goto(scaffold.baseUrl, { waitUntil: 'load' })
  58. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  59. }, 120_000)
  60. afterAll(async () => {
  61. await browser?.close()
  62. await scaffold?.close()
  63. })
  64. it.skipIf(MODE !== 'record')('records the seed turn live through the composer', async () => {
  65. onTestFailed(() => saveFailureShot(page, 'web-e2e-seeded-record'))
  66. const input = page.locator('textarea').first()
  67. await input.waitFor({ timeout: 10_000 })
  68. const settled = scaffold.whenTurnSettled()
  69. await input.fill(PROMPT)
  70. await input.press('Enter')
  71. const sessionId = await settled
  72. await recordFixture(scaffold, sessionId, SEED)
  73. }, 200_000)
  74. it.skipIf(MODE === 'record')('serves the projections baseline on the real composition tail page', async () => {
  75. // Composition regression tripwire: the projection registry must be a row
  76. // in the SHIPPED cordis.yml — with it absent every domain unit's optional
  77. // injection stays silent and this block disappears (no titles/todos on
  78. // the web), while fixture-level suites stay green. Assert through the
  79. // real HTTP wire against the booted real host.
  80. const response = await fetch(`${scaffold.baseUrl}/api/session.history`, {
  81. method: 'POST',
  82. headers: { 'content-type': 'application/json' },
  83. body: JSON.stringify({
  84. type: 'client-request', rpcId: 'seeded-projections', method: 'session.history',
  85. payload: { sessionId: SEED_ID },
  86. }),
  87. })
  88. expect(response.ok).toBe(true)
  89. const body = await response.json() as {
  90. result: { ok: boolean; value?: { projections?: { asOfSeq: number; values: Record<string, unknown> } } }
  91. }
  92. expect(body.result.ok).toBe(true)
  93. const projections = body.result.value?.projections
  94. expect(projections).toBeDefined()
  95. expect(projections?.asOfSeq).toBeGreaterThanOrEqual(0)
  96. // The seed carries a session/title event: the title unit must serve it.
  97. expect(typeof projections?.values.title).toBe('string')
  98. // tool-todo is composed but the seed has no todo/write: whole-value null,
  99. // key PRESENT (absence would mean the unit never registered).
  100. expect(projections?.values).toHaveProperty('todos', null)
  101. })
  102. it.skipIf(MODE === 'record')('lists the seeded session cold and renders its history from the log', async () => {
  103. onTestFailed(() => saveFailureShot(page, 'web-e2e-seeded-history'))
  104. // The sidebar tree collapses workspace groups by default: click the group
  105. // row (treeitem 0) to expand, then the revealed session row.
  106. const groupRow = page.locator('[role="treeitem"]').first()
  107. await groupRow.waitFor({ timeout: 15_000 })
  108. await groupRow.click()
  109. const sessionRow = page.locator('[role="treeitem"]').nth(1)
  110. await sessionRow.waitFor({ timeout: 10_000 })
  111. await sessionRow.click()
  112. // Settled barrier for history: the recorded final assistant text renders.
  113. await expect.poll(() => page.getByText('DONE', { exact: true }).count(), { timeout: 15_000 }).toBe(1)
  114. // Tool cards render from logged tool/call + tool/result alone (views are
  115. // host-recomputed per page; the generic card is the documented default).
  116. const toolRows = page.locator('[data-variant], [data-sample]')
  117. await expect.poll(() => toolRows.count(), { timeout: 10_000 }).toBeGreaterThanOrEqual(2)
  118. expect(await page.getByText('a.txt', { exact: false }).count()).toBeGreaterThan(0)
  119. const agent = scaffold.ctx.agents.get(SessionId(SEED_ID))
  120. if (agent === undefined) throw new Error('seeded session did not attach an agent')
  121. agent.inject(createUserMessage({
  122. content: [{
  123. type: 'text',
  124. text: '<system-reminder>\n'
  125. + 'The following workspace instructions may be relevant to your work. '
  126. + 'Use them as guidance when applicable.\n\n'
  127. + Array.from({ length: 24 }, (_, index) => `Instruction ${index + 1}: preserve the logged context contract.`).join('\n')
  128. + '\n</system-reminder>',
  129. }],
  130. source: {
  131. kind: 'workspace-instructions',
  132. baseline: true,
  133. changes: [{
  134. action: 'set',
  135. scope: '.\u0000AGENTS.md',
  136. path: 'AGENTS.md',
  137. digest: 'context-injection-browser-snapshot',
  138. }],
  139. },
  140. }))
  141. await page.getByRole('button', { name: 'Context injection' }).waitFor({ timeout: 10_000 })
  142. }, 60_000)
  143. it.skipIf(MODE === 'record')('matches the historical conversation aria golden', async () => {
  144. onTestFailed(() => saveFailureShot(page, 'web-e2e-seeded-aria'))
  145. // This scenario deliberately leaves the LLM seam open to prove zero
  146. // model calls. History still restores the routed id, but without an
  147. // advertised catalog row the selector prompts for a listed replacement.
  148. await page.getByRole('button', { name: 'Select model', exact: true })
  149. .waitFor({ timeout: 10_000 })
  150. const snapshot = (await captureStableAria(page, '[class*="centerCol"]', scaffold.workspaceCwd))
  151. .split(SEED_ID).join('{{seededId}}')
  152. await compareOrRefreshGolden(UI_EXPECTED, snapshot, MODE)
  153. })
  154. it.skipIf(MODE === 'record')('matches the Figma context disclosure geometry', async () => {
  155. onTestFailed(() => saveFailureShot(page, 'web-e2e-context-injection'))
  156. const disclosure = page.getByRole('button', { name: 'Context injection' })
  157. expect(await disclosure.getAttribute('aria-expanded')).toBe('false')
  158. const collapsedIcon = disclosure.locator('svg').first()
  159. const collapsedIconBox = await collapsedIcon.boundingBox()
  160. expect(collapsedIconBox?.width).toBe(14)
  161. expect(collapsedIconBox?.height).toBe(14)
  162. await disclosure.click()
  163. await expect.poll(() => disclosure.getAttribute('aria-expanded')).toBe('true')
  164. const body = page.locator('[data-context-injection-body]')
  165. await body.waitFor({ timeout: 5_000 })
  166. const headerBox = await disclosure.boundingBox()
  167. const bodyBox = await body.boundingBox()
  168. if (headerBox === null || bodyBox === null) throw new Error('context disclosure geometry is not measurable')
  169. expect(headerBox.height).toBe(24)
  170. expect(bodyBox.x - headerBox.x).toBe(22)
  171. expect(bodyBox.y - headerBox.y - headerBox.height).toBe(4)
  172. expect(bodyBox.height).toBe(141)
  173. const style = await body.evaluate((element) => {
  174. const computed = getComputedStyle(element)
  175. return {
  176. backgroundColor: computed.backgroundColor,
  177. borderRadius: computed.borderRadius,
  178. color: computed.color,
  179. fontSize: computed.fontSize,
  180. lineHeight: computed.lineHeight,
  181. padding: [
  182. computed.paddingTop,
  183. computed.paddingRight,
  184. computed.paddingBottom,
  185. computed.paddingLeft,
  186. ],
  187. scrolls: element.scrollHeight > element.clientHeight,
  188. }
  189. })
  190. expect(style).toEqual({
  191. backgroundColor: 'rgb(249, 250, 251)',
  192. borderRadius: '8px',
  193. color: 'rgb(129, 133, 140)',
  194. fontSize: '11px',
  195. lineHeight: '16px',
  196. padding: ['10px', '16px', '12px', '12px'],
  197. scrolls: true,
  198. })
  199. await disclosure.click()
  200. await expect.poll(() => disclosure.getAttribute('aria-expanded')).toBe('false')
  201. })
  202. it.skipIf(MODE === 'record')('file-path tool rows rebuilt from the cold log stay details-inert', async () => {
  203. onTestFailed(() => saveFailureShot(page, 'web-e2e-seeded-toolrow'))
  204. // Interaction over cold-resumed history: read summaries are host-open
  205. // file links (not expand-in-place / not details). Runs after the golden
  206. // capture; still zero model calls.
  207. const fileLink = page.locator('[data-variant="read"] button').first()
  208. await fileLink.waitFor({ timeout: 10_000 })
  209. const frame = page.locator('[style*="grid-template-columns"]').first()
  210. expect(await frame.getAttribute('data-details-collapsed')).toBe('true')
  211. await fileLink.click()
  212. await expect.poll(() => frame.getAttribute('data-details-collapsed'), { timeout: 5_000 }).toBe('true')
  213. // Path label survives from the recorded args (a.txt).
  214. await expect.poll(() => page.getByText('a.txt', { exact: false }).count(), { timeout: 5_000 }).toBeGreaterThan(0)
  215. })
  216. it.skipIf(MODE === 'record')('an Access-chip switch lands one command row: bare name, non-repeating settlement text', async () => {
  217. onTestFailed(() => saveFailureShot(page, 'web-e2e-seeded-command-row'))
  218. // The Access chip submits `/permission <preset>` — a host command with no
  219. // model call, so the settled row renders keylessly over this cold history.
  220. // The row copy is the assertion: `permission · preset workspace-write`,
  221. // where neither half repeats the other (the dispatched `/` and its
  222. // argument stay out of the title, and the settlement text never restates
  223. // the command's own name).
  224. await page.getByRole('button', { name: 'Access mode, current: Full access' }).click()
  225. await page.getByRole('menuitem', { name: 'Workspace Write' }).click()
  226. await page.getByRole('button', { name: 'Access mode, current: Workspace Write' }).waitFor({ timeout: 10_000 })
  227. // Scoped to the row itself, so unrelated page text that happens to read
  228. // `permission` (a future resident slash menu) cannot satisfy or break it.
  229. const row = page.locator('[data-variant="others"]').filter({ hasText: 'preset workspace-write' })
  230. await expect.poll(() => row.count(), { timeout: 10_000 }).toBe(1)
  231. expect(await row.getByText('permission', { exact: true }).count()).toBe(1)
  232. expect(await row.getByText('/permission workspace-write', { exact: true }).count()).toBe(0)
  233. const snapshot = (await captureStableAria(page, '[class*="centerCol"]', scaffold.workspaceCwd))
  234. .split(SEED_ID).join('{{seededId}}')
  235. await compareOrRefreshGolden(COMMAND_ROW_EXPECTED, snapshot, MODE)
  236. }, 60_000)
  237. it.skipIf(MODE === 'record')('issued zero model calls and stayed clean', async () => {
  238. // No replay fixture was installed and the llm seam is open — any stray
  239. // stream would have failed the turn loudly. Cleanliness pins the wire.
  240. expect(tripwire.pageErrors).toEqual([])
  241. expect(tripwire.warnings).toEqual([])
  242. await assertFixtureInventory(SNAPSHOT_DIR, ['command-row.expected.md', 'seed.jsonl', 'ui.expected.md'])
  243. })
  244. })