message-actions.e2e.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // Web e2e scenario: message IconActions + clocks. Cold-seeds a deterministic
  2. // completed-turn-tail fork case with an unchanged resume header (zero model
  3. // calls) and pins the settled conversation aria after the footers are
  4. // focus-revealed — the surface package jsdom tests cannot substitute for
  5. // (docs/testing.md snapshot rule).
  6. import { mkdir, readFile, writeFile } from 'node:fs/promises'
  7. import { join } from 'node:path'
  8. import { fileURLToPath } from 'node:url'
  9. import type { Browser, Page } from 'playwright'
  10. import { chromium } from 'playwright'
  11. import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest'
  12. import { SessionId } from '@deepseek-ai/dsh-session'
  13. import {
  14. assertFixtureInventory, captureStableAria, compareOrRefreshGolden, fixtureUserPrompts,
  15. launchWebScaffold, parseSeedFixture, renderSeedFixture, seedSession, watchConsole, webSnapshotMode, type WebScaffold,
  16. } from './scaffold.ts'
  17. import { newEnglishPage, saveFailureShot } from './support.ts'
  18. const SNAPSHOT_DIR = fileURLToPath(new URL('../../../snapshots/web/message-actions', import.meta.url))
  19. // Borrowed read-only: this scenario needs any settled user+assistant pair, not
  20. // a new recording (workspace-management / sidebar-scrollbar pattern).
  21. const SEED = fileURLToPath(new URL('../../../snapshots/web/seeded-history/session.v3.jsonl', import.meta.url))
  22. const UI_EXPECTED = join(SNAPSHOT_DIR, 'ui.expected.md')
  23. const FORK_EXPECTED = join(SNAPSHOT_DIR, 'fork.expected.md')
  24. const MODE = webSnapshotMode()
  25. const SEED_ID = 'message-actions-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. const MID_TURN_TEXT = 'I will read both files before answering.'
  28. const SECOND_PROMPT = 'Now give the final answer.'
  29. const NEXT_PROMPT = 'Keep this later input in the original conversation.'
  30. /**
  31. * Adapt the borrowed recording into response -> tools -> interrupted Think,
  32. * followed by two ordinary completed responses. The first response keeps
  33. * copy/clock but is not a legal branch point; the second is the real turn tail.
  34. * @param raw - Recorded seeded-history JSONL.
  35. * @returns A contiguous, closed three-turn fixture.
  36. */
  37. function completedTailFixture(raw: string): string {
  38. const decoded = parseSeedFixture(raw)
  39. const stepTwoStart = decoded.events.findIndex(event =>
  40. event.type === 'step/start' && event.data.turn === 1 && event.data.step === 2)
  41. if (stepTwoStart < 0) throw new Error('borrowed fixture has no step-two start')
  42. const kept = decoded.events.slice(0, stepTwoStart + 1).map((event) => {
  43. if (event.type !== 'assistant/message' || event.data.turn !== 1 || event.data.step !== 1) return event
  44. const finish = event.data.stream.findIndex(record =>
  45. record.type === 'chunk' && record.chunk.type === 'finish')
  46. if (finish < 0) throw new Error('borrowed step-one Assistant message has no finish record')
  47. const finishRecord = event.data.stream[finish]!
  48. if (finishRecord.type !== 'chunk') throw new Error('borrowed step-one finish is not a chunk record')
  49. const streamTime = finishRecord.time
  50. return {
  51. ...event,
  52. data: {
  53. ...event.data,
  54. message: {
  55. ...event.data.message,
  56. content: [...event.data.message.content, { type: 'text' as const, text: MID_TURN_TEXT }],
  57. },
  58. stream: [
  59. ...event.data.stream.slice(0, finish),
  60. { type: 'chunk' as const, time: streamTime, chunk: { type: 'block-start' as const, index: 3, blockType: 'text' as const } },
  61. { type: 'text-chunks' as const, time0: streamTime, index: 3, dt: [], texts: [MID_TURN_TEXT] },
  62. {
  63. type: 'chunk' as const,
  64. time: streamTime,
  65. chunk: { type: 'block-end' as const, index: 3, block: { type: 'text' as const, text: MID_TURN_TEXT } },
  66. },
  67. ...event.data.stream.slice(finish),
  68. ],
  69. },
  70. }
  71. })
  72. const inheritedHeader = kept.findLast(event => event.type === 'request/header')
  73. if (inheritedHeader?.type !== 'request/header') {
  74. throw new Error('borrowed recording has no request header')
  75. }
  76. let seq = (kept.at(-1)?.seq ?? -1) + 1
  77. let time = (kept.at(-1)?.time ?? -1) + 1
  78. const at = (event: Record<string, unknown>): { seq: number; time: number } & Record<string, unknown> => ({
  79. ...event,
  80. seq: seq++,
  81. time: time++,
  82. })
  83. const tail = [
  84. at({
  85. type: 'assistant/attempt',
  86. data: {
  87. turn: 1,
  88. step: 2,
  89. stream: [
  90. { type: 'chunk', time, chunk: { type: 'block-start', index: 0, blockType: 'reasoning' } },
  91. { type: 'reasoning-chunks', time0: time, index: 0, dt: [], texts: ['This path was interrupted.'] },
  92. {
  93. type: 'chunk',
  94. time,
  95. chunk: { type: 'block-end', index: 0, block: { type: 'reasoning', text: 'This path was interrupted.' } },
  96. },
  97. { type: 'chunk', time, chunk: { type: 'finish', reason: { kind: 'stop' } } },
  98. ],
  99. },
  100. }),
  101. at({ type: 'step/end', data: { turn: 1, step: 2 } }),
  102. at({ type: 'turn/end', data: { turn: 1, reason: { kind: 'aborted', reason: { kind: 'user' } } } }),
  103. at({ type: 'turn/start', data: { turn: 2 } }),
  104. at({
  105. type: 'user/message',
  106. data: {
  107. content: [{ type: 'text', text: SECOND_PROMPT }],
  108. source: { kind: 'user' },
  109. role: 'user',
  110. id: '{{message:98}}',
  111. },
  112. surfaceOp: 'append',
  113. }),
  114. at({ type: 'step/start', data: { turn: 2, step: 1 } }),
  115. at({ type: 'request/header', data: { header: inheritedHeader.data.header, reason: 'resume' } }),
  116. at({
  117. type: 'assistant/message',
  118. data: {
  119. turn: 2,
  120. step: 1,
  121. message: {
  122. role: 'assistant',
  123. content: [{ type: 'text', text: 'DONE' }],
  124. source: { kind: 'model', provider: 'deepseek-official', model: 'deepseek-v4-flash' },
  125. id: '{{message:99}}',
  126. },
  127. stream: [
  128. { type: 'chunk', time: 0, chunk: { type: 'block-start', index: 0, blockType: 'text' } },
  129. { type: 'text-chunks', time0: 0, index: 0, dt: [], texts: ['DONE'] },
  130. { type: 'chunk', time: 0, chunk: { type: 'block-end', index: 0, block: { type: 'text', text: 'DONE' } } },
  131. { type: 'chunk', time: 0, chunk: { type: 'finish', reason: { kind: 'stop' } } },
  132. ],
  133. },
  134. surfaceOp: 'append',
  135. }),
  136. at({ type: 'step/end', data: { turn: 2, step: 1 } }),
  137. at({ type: 'turn/end', data: { turn: 2, reason: { kind: 'completed' } } }),
  138. at({
  139. type: 'agent/inbox/spliced',
  140. data: {
  141. target: 'next-turn', start: 0,
  142. inserted: [{
  143. role: 'user', id: '{{message:100}}', source: { kind: 'user' },
  144. content: [{ type: 'text', text: NEXT_PROMPT }],
  145. }],
  146. },
  147. }),
  148. at({ type: 'turn/start', data: { turn: 3 } }),
  149. at({
  150. type: 'agent/inbox/spliced',
  151. data: { target: 'next-turn', start: 0, removedCount: 1, inserted: [] },
  152. }),
  153. at({ type: 'step/start', data: { turn: 3, step: 1 } }),
  154. at({
  155. type: 'user/message', surfaceOp: 'append',
  156. data: {
  157. role: 'user', id: '{{message:100}}', source: { kind: 'user' },
  158. content: [{ type: 'text', text: NEXT_PROMPT }],
  159. },
  160. }),
  161. at({
  162. type: 'assistant/message', surfaceOp: 'append',
  163. data: {
  164. turn: 3, step: 1,
  165. message: {
  166. role: 'assistant', id: '{{message:101}}',
  167. source: { kind: 'model', provider: 'deepseek-official', model: 'deepseek-v4-flash' },
  168. content: [{ type: 'text', text: 'ORIGINAL ONLY' }],
  169. },
  170. stream: [
  171. { type: 'chunk', time: 0, chunk: { type: 'block-start', index: 0, blockType: 'text' } },
  172. { type: 'text-chunks', time0: 0, index: 0, dt: [], texts: ['ORIGINAL ONLY'] },
  173. { type: 'chunk', time: 0, chunk: { type: 'block-end', index: 0, block: { type: 'text', text: 'ORIGINAL ONLY' } } },
  174. { type: 'chunk', time: 0, chunk: { type: 'finish', reason: { kind: 'stop' } } },
  175. ],
  176. },
  177. }),
  178. at({ type: 'step/end', data: { turn: 3, step: 1 } }),
  179. at({ type: 'turn/end', data: { turn: 3, reason: { kind: 'completed' } } }),
  180. ]
  181. return renderSeedFixture(decoded.headerLine, [...kept, ...tail])
  182. }
  183. describe('web e2e: message IconActions and clocks on settled history', () => {
  184. let scaffold: WebScaffold
  185. let browser: Browser
  186. let page: Page
  187. let tripwire: ReturnType<typeof watchConsole>
  188. beforeAll(async () => {
  189. scaffold = await launchWebScaffold({})
  190. const sessionCwd = join(scaffold.workspaceCwd, 'workspace')
  191. await mkdir(sessionCwd, { recursive: true })
  192. await writeFile(join(sessionCwd, 'a.txt'), 'alpha\n')
  193. await writeFile(join(sessionCwd, 'b.txt'), 'beta\n')
  194. const raw = completedTailFixture(await readFile(SEED, 'utf8'))
  195. expect(fixtureUserPrompts(raw), 'adapted seed must carry all prompts').toEqual([PROMPT, SECOND_PROMPT, NEXT_PROMPT])
  196. expect(parseSeedFixture(raw).events.flatMap(event => event.type === 'request/header'
  197. ? [event.data.reason]
  198. : []), 'adapted seed must carry an unchanged resume header').toEqual(['initial', 'resume'])
  199. await seedSession(scaffold, raw, SEED_ID)
  200. browser = await chromium.launch()
  201. page = await newEnglishPage(browser)
  202. tripwire = watchConsole(page)
  203. await page.goto(scaffold.authenticatedUrl, { waitUntil: 'load' })
  204. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  205. }, 120_000)
  206. afterAll(async () => {
  207. await browser?.close()
  208. await scaffold?.close()
  209. })
  210. it.skipIf(MODE === 'record')('enables branch only on the completed transcript tail', async () => {
  211. onTestFailed(() => saveFailureShot(page, 'web-e2e-message-actions'))
  212. const groupRow = page.locator('[role="treeitem"]').first()
  213. await groupRow.waitFor({ timeout: 15_000 })
  214. await groupRow.click()
  215. const sessionRow = page.locator('[role="treeitem"]').nth(1)
  216. await sessionRow.waitFor({ timeout: 10_000 })
  217. await sessionRow.click()
  218. await expect.poll(() => page.getByText(MID_TURN_TEXT, { exact: true }).count(), { timeout: 15_000 }).toBe(1)
  219. await expect.poll(() => page.getByText('DONE', { exact: true }).count(), { timeout: 15_000 }).toBe(1)
  220. await expect.poll(
  221. () => page.getByRole('button', { name: 'System prompt', exact: true }).count(),
  222. { timeout: 10_000 },
  223. ).toBe(2)
  224. // Focus-reveal the footers (hover:hover keeps them opacity-hidden until
  225. // hover/focus-within). Branch renders only under assistant answers — user
  226. // bubbles carry none — and only a completed transcript tail enables it.
  227. const copyButtons = page.getByRole('button', { name: 'Copy' })
  228. await expect.poll(() => copyButtons.count(), { timeout: 10_000 }).toBeGreaterThanOrEqual(4)
  229. await copyButtons.first().focus()
  230. const branchButtons = page.getByRole('button', { name: 'Branch into a new conversation' })
  231. await expect.poll(() => branchButtons.count(), { timeout: 5_000 }).toBe(3)
  232. await expect.poll(
  233. () => branchButtons.evaluateAll(buttons => buttons.map(button => button.getAttribute('aria-disabled'))),
  234. { timeout: 5_000 },
  235. ).toEqual(['true', null, null])
  236. await branchButtons.first().focus()
  237. await expect.poll(() => page.getByRole('tooltip').textContent(), { timeout: 5_000 })
  238. .toBe('Available only on the last message of a completed turn')
  239. await expect.poll(() => page.getByRole('button', { name: 'Edit' }).count(), { timeout: 5_000 }).toBe(0)
  240. }, 60_000)
  241. it.skipIf(MODE === 'record')('matches the conversation aria golden with IconActions and clocks', async () => {
  242. onTestFailed(() => saveFailureShot(page, 'web-e2e-message-actions-aria'))
  243. await page.getByRole('button', { name: /^Select model, current/ })
  244. .waitFor({ timeout: 10_000 })
  245. await page.getByText(/Cache hit \d+%/u).first().waitFor({ timeout: 10_000 })
  246. // Keep a footer focused so opacity-hidden actions stay in the a11y tree
  247. // as an active/focused control during the capture.
  248. await page.getByRole('button', { name: 'Copy' }).first().focus()
  249. const snapshot = (await captureStableAria(page, '[class*="centerCol"]', scaffold.workspaceCwd))
  250. .split(SEED_ID).join('{{seededId}}')
  251. await compareOrRefreshGolden(UI_EXPECTED, snapshot, MODE)
  252. })
  253. it.skipIf(MODE === 'record')('forks through the settled-message and session-row actions', async () => {
  254. onTestFailed(() => saveFailureShot(page, 'web-e2e-message-fork'))
  255. // The second answer is followed by another completed user turn in the source.
  256. await page.getByRole('button', { name: 'Branch into a new conversation' }).nth(1).click()
  257. await expect.poll(
  258. () => scaffold.ctx.agents.list().find(agent => agent.session.header.parentSession === SessionId(SEED_ID)),
  259. { timeout: 15_000 },
  260. ).toBeDefined()
  261. const child = scaffold.ctx.agents.list().find(agent =>
  262. agent.session.header.parentSession === SessionId(SEED_ID))!
  263. expect(child.inbox.nextTurn).toEqual([])
  264. expect(child.session.snapshotEvents().some(event => event.type === 'agent/inbox/spliced'
  265. && event.data.inserted.some(message => message.content.some(part =>
  266. part.type === 'text' && part.text === NEXT_PROMPT)))).toBe(false)
  267. await expect.poll(
  268. () => page.locator('[role="treeitem"]').count(),
  269. { timeout: 10_000 },
  270. ).toBe(3)
  271. await expect.poll(
  272. () => page.locator('[role="treeitem"][aria-selected="true"]').count(),
  273. { timeout: 10_000 },
  274. ).toBe(1)
  275. await expect.poll(
  276. () => page.locator('[role="treeitem"][aria-selected="true"]').textContent(),
  277. { timeout: 10_000 },
  278. ).toContain('Use the read tool twice (1)')
  279. // The row action owns a distinct ui-workspace injection from the message
  280. // action above, so exercise both through the loaded app before capture.
  281. const sourceRow = page.locator('[role="treeitem"][aria-selected="true"]')
  282. const rowBox = await sourceRow.boundingBox()
  283. if (rowBox === null) throw new Error('fork source row has no layout box')
  284. const actionButton = sourceRow.locator('button[aria-label^="Session actions for "]')
  285. await sourceRow.hover({ position: { x: rowBox.width - 16, y: rowBox.height / 2 } })
  286. await expect.poll(() => actionButton.isVisible(), { timeout: 2_000 }).toBe(true)
  287. const buttonBox = await actionButton.boundingBox()
  288. if (buttonBox === null) throw new Error('fork source row action has no layout box')
  289. await page.mouse.click(buttonBox.x + buttonBox.width / 2, buttonBox.y + buttonBox.height / 2)
  290. await page.getByRole('menuitem', { name: 'Fork session' }).click()
  291. await expect.poll(
  292. () => scaffold.ctx.agents.list().filter(agent => agent.session.header.parentSession !== undefined).length,
  293. { timeout: 15_000 },
  294. ).toBe(2)
  295. await expect.poll(
  296. () => page.locator('[role="treeitem"]').count(),
  297. { timeout: 10_000 },
  298. ).toBe(4)
  299. await expect.poll(
  300. () => page.locator('[role="treeitem"][aria-selected="true"]').count(),
  301. { timeout: 10_000 },
  302. ).toBe(1)
  303. // The child row is published before its inherited title rename settles;
  304. // wait for that second RPC projection before freezing the ARIA tree.
  305. await expect.poll(
  306. () => page.locator('[role="treeitem"][aria-selected="true"]').textContent(),
  307. { timeout: 10_000 },
  308. ).toContain('Use the read tool twice (2)')
  309. const tree = await captureStableAria(
  310. page,
  311. '[role="tree"][aria-label="Sessions"]',
  312. scaffold.workspaceCwd,
  313. )
  314. await compareOrRefreshGolden(FORK_EXPECTED, tree, MODE)
  315. })
  316. it.skipIf(MODE === 'record')('issued zero model calls and kept a closed inventory', async () => {
  317. expect(tripwire.pageErrors).toEqual([])
  318. expect(tripwire.warnings).toEqual([])
  319. await assertFixtureInventory(SNAPSHOT_DIR, ['fork.expected.md', 'ui.expected.md'])
  320. })
  321. })