message-actions.e2e.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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')('keeps an action tooltip above the sticky composer', async () => {
  242. onTestFailed(() => saveFailureShot(page, 'web-e2e-message-action-tooltip-layer'))
  243. await page.evaluate(() => { (document.activeElement as HTMLElement | null)?.blur() })
  244. await page.mouse.move(0, 0)
  245. const copy = page.getByRole('button', { name: 'Copy', exact: true }).last()
  246. const composer = page.locator('[data-composer-seat]')
  247. const tooltip = page.getByRole('tooltip', { name: 'Copy', exact: true })
  248. try {
  249. // Grow the sticky seat upward so the bottom tooltip overlaps it without
  250. // depending on the fixture's resting composer height.
  251. await composer.evaluate((element) => { element.style.paddingTop = '48px' })
  252. await copy.evaluate((button) => {
  253. const scrollport = button.closest<HTMLElement>('[data-conversation-scroll]')
  254. const composer = scrollport?.querySelector<HTMLElement>('[data-composer-seat]') ?? null
  255. if (scrollport === null || composer === null) throw new Error('conversation geometry is unavailable')
  256. const buttonRect = button.getBoundingClientRect()
  257. const composerTop = composer.getBoundingClientRect().top
  258. scrollport.scrollTop += buttonRect.bottom - (composerTop - 8)
  259. })
  260. await copy.hover()
  261. await tooltip.waitFor({ state: 'visible', timeout: 5_000 })
  262. // Tooltip is intentionally pointer-transparent. Enable hit testing only
  263. // for this stacking probe; paint order is unchanged.
  264. await tooltip.evaluate((element) => { element.style.pointerEvents = 'auto' })
  265. const probe = await tooltip.evaluate((element) => {
  266. const rect = element.getBoundingClientRect()
  267. const x = rect.left + rect.width / 2
  268. const y = rect.top + rect.height / 2
  269. const composer = document.querySelector<HTMLElement>('[data-composer-seat]')
  270. if (composer === null) throw new Error('composer geometry is unavailable')
  271. const composerRect = composer.getBoundingClientRect()
  272. const hit = document.elementFromPoint(x, y)
  273. return {
  274. insideComposer: composerRect.top <= y && y <= composerRect.bottom,
  275. hitsTooltip: hit !== null && element.contains(hit),
  276. }
  277. })
  278. expect(probe.insideComposer).toBe(true)
  279. expect(probe.hitsTooltip).toBe(true)
  280. } finally {
  281. if (await tooltip.count() > 0) {
  282. await tooltip.evaluate((element) => { element.style.removeProperty('pointer-events') })
  283. }
  284. if (await composer.count() > 0) {
  285. await composer.evaluate((element) => { element.style.removeProperty('padding-top') })
  286. }
  287. await page.mouse.move(0, 0)
  288. if (await copy.count() > 0) await copy.evaluate((element) => { element.blur() })
  289. if (await tooltip.count() > 0) await tooltip.waitFor({ state: 'hidden', timeout: 5_000 })
  290. }
  291. expect(tripwire.pageErrors).toEqual([])
  292. }, 60_000)
  293. it.skipIf(MODE === 'record')('matches the conversation aria golden with IconActions and clocks', async () => {
  294. onTestFailed(() => saveFailureShot(page, 'web-e2e-message-actions-aria'))
  295. await page.getByRole('button', { name: /^Select model, current/ })
  296. .waitFor({ timeout: 10_000 })
  297. await page.getByText(/Cache hit \d+%/u).first().waitFor({ timeout: 10_000 })
  298. // Keep a footer focused so opacity-hidden actions stay in the a11y tree
  299. // as an active/focused control during the capture.
  300. await page.getByRole('button', { name: 'Copy' }).first().focus()
  301. const snapshot = (await captureStableAria(page, '[class*="centerCol"]', scaffold.workspaceCwd))
  302. .split(SEED_ID).join('{{seededId}}')
  303. await compareOrRefreshGolden(UI_EXPECTED, snapshot, MODE)
  304. })
  305. it.skipIf(MODE === 'record')('forks through the settled-message and session-row actions', async () => {
  306. onTestFailed(() => saveFailureShot(page, 'web-e2e-message-fork'))
  307. // The second answer is followed by another completed user turn in the source.
  308. await page.getByRole('button', { name: 'Branch into a new conversation' }).nth(1).click()
  309. await expect.poll(
  310. () => scaffold.ctx.agents.list().find(agent => agent.session.header.parentSession === SessionId(SEED_ID)),
  311. { timeout: 15_000 },
  312. ).toBeDefined()
  313. const child = scaffold.ctx.agents.list().find(agent =>
  314. agent.session.header.parentSession === SessionId(SEED_ID))!
  315. expect(child.inbox.nextTurn).toEqual([])
  316. expect(child.session.snapshotEvents().some(event => event.type === 'agent/inbox/spliced'
  317. && event.data.inserted.some(message => message.content.some(part =>
  318. part.type === 'text' && part.text === NEXT_PROMPT)))).toBe(false)
  319. await expect.poll(
  320. () => page.locator('[role="treeitem"]').count(),
  321. { timeout: 10_000 },
  322. ).toBe(3)
  323. await expect.poll(
  324. () => page.locator('[role="treeitem"][aria-selected="true"]').count(),
  325. { timeout: 10_000 },
  326. ).toBe(1)
  327. await expect.poll(
  328. () => page.locator('[role="treeitem"][aria-selected="true"]').textContent(),
  329. { timeout: 10_000 },
  330. ).toContain('Use the read tool twice (1)')
  331. // The row action owns a distinct ui-workspace injection from the message
  332. // action above, so exercise both through the loaded app before capture.
  333. const sourceRow = page.locator('[role="treeitem"][aria-selected="true"]')
  334. const rowBox = await sourceRow.boundingBox()
  335. if (rowBox === null) throw new Error('fork source row has no layout box')
  336. const actionButton = sourceRow.locator('button[aria-label^="Session actions for "]')
  337. await sourceRow.hover({ position: { x: rowBox.width - 16, y: rowBox.height / 2 } })
  338. await expect.poll(() => actionButton.isVisible(), { timeout: 2_000 }).toBe(true)
  339. const buttonBox = await actionButton.boundingBox()
  340. if (buttonBox === null) throw new Error('fork source row action has no layout box')
  341. await page.mouse.click(buttonBox.x + buttonBox.width / 2, buttonBox.y + buttonBox.height / 2)
  342. await page.getByRole('menuitem', { name: 'Fork session' }).click()
  343. await expect.poll(
  344. () => scaffold.ctx.agents.list().filter(agent => agent.session.header.parentSession !== undefined).length,
  345. { timeout: 15_000 },
  346. ).toBe(2)
  347. await expect.poll(
  348. () => page.locator('[role="treeitem"]').count(),
  349. { timeout: 10_000 },
  350. ).toBe(4)
  351. await expect.poll(
  352. () => page.locator('[role="treeitem"][aria-selected="true"]').count(),
  353. { timeout: 10_000 },
  354. ).toBe(1)
  355. // The child row is published before its inherited title rename settles;
  356. // wait for that second RPC projection before freezing the ARIA tree.
  357. await expect.poll(
  358. () => page.locator('[role="treeitem"][aria-selected="true"]').textContent(),
  359. { timeout: 10_000 },
  360. ).toContain('Use the read tool twice (2)')
  361. const tree = await captureStableAria(
  362. page,
  363. '[role="tree"][aria-label="Sessions"]',
  364. scaffold.workspaceCwd,
  365. )
  366. await compareOrRefreshGolden(FORK_EXPECTED, tree, MODE)
  367. })
  368. it.skipIf(MODE === 'record')('issued zero model calls and kept a closed inventory', async () => {
  369. expect(tripwire.pageErrors).toEqual([])
  370. expect(tripwire.warnings).toEqual([])
  371. await assertFixtureInventory(SNAPSHOT_DIR, ['fork.expected.md', 'ui.expected.md'])
  372. })
  373. })