| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- // Web e2e scenario: message IconActions + clocks. Cold-seeds a deterministic
- // completed-turn-tail fork case (zero model calls) and pins the settled
- // conversation aria after the footers are focus-revealed — the surface package
- // jsdom tests cannot substitute for (docs/testing.md snapshot rule).
- import { mkdir, readFile, writeFile } from 'node:fs/promises'
- import { join } from 'node:path'
- import { fileURLToPath } from 'node:url'
- import type { Browser, Page } from 'playwright'
- import { chromium } from 'playwright'
- import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest'
- import { SessionId } from '@deepseek-ai/dsh-session'
- import {
- assertFixtureInventory, captureStableAria, compareOrRefreshGolden, fixtureUserPrompts,
- launchWebScaffold, parseSeedFixture, renderSeedFixture, seedSession, watchConsole, webSnapshotMode, type WebScaffold,
- } from './scaffold.ts'
- import { newEnglishPage, saveFailureShot } from './support.ts'
- const SNAPSHOT_DIR = fileURLToPath(new URL('../../../snapshots/web/message-actions', import.meta.url))
- // Borrowed read-only: this scenario needs any settled user+assistant pair, not
- // a new recording (workspace-management / sidebar-scrollbar pattern).
- const SEED = fileURLToPath(new URL('../../../snapshots/web/seeded-history/session.jsonl', import.meta.url))
- const UI_EXPECTED = join(SNAPSHOT_DIR, 'ui.expected.md')
- const FORK_EXPECTED = join(SNAPSHOT_DIR, 'fork.expected.md')
- const MODE = webSnapshotMode()
- const SEED_ID = 'message-actions-web-e2e'
- 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.'
- const MID_TURN_TEXT = 'I will read both files before answering.'
- const SECOND_PROMPT = 'Now give the final answer.'
- /**
- * Adapt the borrowed recording into response -> tools -> interrupted Think,
- * followed by one ordinary completed response. The first response keeps
- * copy/clock but is not a legal branch point; the second is the real turn tail.
- * @param raw - Recorded seeded-history JSONL.
- * @returns A contiguous, closed two-turn fixture.
- */
- function completedTailFixture(raw: string): string {
- const decoded = parseSeedFixture(raw)
- const kept = decoded.events.filter(event => event.seq < 101).map((event) => {
- if (event.type === 'assistant/message' && event.seq === 64) {
- const data = event.data as unknown as { content?: unknown[] }
- const content = data.content
- if (!Array.isArray(content)) throw new Error('borrowed step-one assistant message has no content')
- return {
- ...event,
- data: { ...data, content: [...content.slice(0, 1), { type: 'text', text: MID_TURN_TEXT }, ...content.slice(1)] },
- }
- }
- return event
- })
- let seq = kept.length
- let time = (kept.at(-1)?.time ?? -1) + 1
- const at = (event: Record<string, unknown>): { seq: number; time: number } & Record<string, unknown> => ({
- ...event,
- seq: seq++,
- time: time++,
- })
- const tail = [
- at({ type: 'step/end', data: { turn: 1, step: 2 } }),
- at({ type: 'turn/end', data: { turn: 1, reason: { kind: 'aborted' } } }),
- at({ type: 'turn/start', data: { turn: 2, trigger: { kind: 'message', source: { kind: 'user', rpcId: '{{rpcId}}' } } } }),
- at({ type: 'user/message', data: { content: [{ type: 'text', text: SECOND_PROMPT }], source: { kind: 'user', rpcId: '{{rpcId}}' } }, surfaceOp: 'append' }),
- at({ type: 'step/start', data: { turn: 2, step: 1 } }),
- at({ type: 'assistant/message', data: { turn: 2, step: 1, content: [{ type: 'text', text: 'DONE' }], provenance: { provider: 'deepseek-official', model: 'deepseek-v4-flash' } }, sourceEventSeqs: [], surfaceOp: 'append' }),
- at({ type: 'step/end', data: { turn: 2, step: 1 } }),
- at({ type: 'turn/end', data: { turn: 2, reason: { kind: 'completed' } } }),
- ]
- return renderSeedFixture(decoded.headerLine, [...kept, ...tail])
- }
- describe('web e2e: message IconActions and clocks on settled history', () => {
- let scaffold: WebScaffold
- let browser: Browser
- let page: Page
- let tripwire: ReturnType<typeof watchConsole>
- beforeAll(async () => {
- scaffold = await launchWebScaffold({})
- const sessionCwd = join(scaffold.workspaceCwd, 'workspace')
- await mkdir(sessionCwd, { recursive: true })
- await writeFile(join(sessionCwd, 'a.txt'), 'alpha\n')
- await writeFile(join(sessionCwd, 'b.txt'), 'beta\n')
- const raw = completedTailFixture(await readFile(SEED, 'utf8'))
- expect(fixtureUserPrompts(raw), 'adapted seed must carry both prompts').toEqual([PROMPT, SECOND_PROMPT])
- await seedSession(scaffold, raw, SEED_ID)
- browser = await chromium.launch()
- page = await newEnglishPage(browser)
- tripwire = watchConsole(page)
- await page.goto(scaffold.baseUrl, { waitUntil: 'load' })
- await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
- }, 120_000)
- afterAll(async () => {
- await browser?.close()
- await scaffold?.close()
- })
- it.skipIf(MODE === 'record')('enables branch only on the completed transcript tail', async () => {
- onTestFailed(() => saveFailureShot(page, 'web-e2e-message-actions'))
- const groupRow = page.locator('[role="treeitem"]').first()
- await groupRow.waitFor({ timeout: 15_000 })
- await groupRow.click()
- const sessionRow = page.locator('[role="treeitem"]').nth(1)
- await sessionRow.waitFor({ timeout: 10_000 })
- await sessionRow.click()
- await expect.poll(() => page.getByText(MID_TURN_TEXT, { exact: true }).count(), { timeout: 15_000 }).toBe(1)
- await expect.poll(() => page.getByText('DONE', { exact: true }).count(), { timeout: 15_000 }).toBe(1)
- // Focus-reveal the footers (hover:hover keeps them opacity-hidden until
- // hover/focus-within). Branch renders only under assistant answers — user
- // bubbles carry none — and only a completed transcript tail enables it.
- const copyButtons = page.getByRole('button', { name: 'Copy' })
- await expect.poll(() => copyButtons.count(), { timeout: 10_000 }).toBeGreaterThanOrEqual(4)
- await copyButtons.first().focus()
- const branchButtons = page.getByRole('button', { name: 'Branch into a new conversation' })
- await expect.poll(() => branchButtons.count(), { timeout: 5_000 }).toBe(2)
- await expect.poll(
- () => branchButtons.evaluateAll(buttons => buttons.map(button => button.getAttribute('aria-disabled'))),
- { timeout: 5_000 },
- ).toEqual(['true', null])
- await branchButtons.first().focus()
- await expect.poll(() => page.getByRole('tooltip').textContent(), { timeout: 5_000 })
- .toBe('Available only on the last message of a completed turn')
- await expect.poll(() => page.getByRole('button', { name: 'Edit' }).count(), { timeout: 5_000 }).toBe(0)
- }, 60_000)
- it.skipIf(MODE === 'record')('matches the conversation aria golden with IconActions and clocks', async () => {
- onTestFailed(() => saveFailureShot(page, 'web-e2e-message-actions-aria'))
- await page.getByRole('button', { name: /^Select model, current/ })
- .waitFor({ timeout: 10_000 })
- await page.getByText(/Cache hit \d+%/u).first().waitFor({ timeout: 10_000 })
- // Keep a footer focused so opacity-hidden actions stay in the a11y tree
- // as an active/focused control during the capture.
- await page.getByRole('button', { name: 'Copy' }).first().focus()
- const snapshot = (await captureStableAria(page, '[class*="centerCol"]', scaffold.workspaceCwd))
- .split(SEED_ID).join('{{seededId}}')
- await compareOrRefreshGolden(UI_EXPECTED, snapshot, MODE)
- })
- it.skipIf(MODE === 'record')('forks through the settled-message and session-row actions', async () => {
- onTestFailed(() => saveFailureShot(page, 'web-e2e-message-fork'))
- // The last message action belongs to the completed second-turn assistant.
- await page.getByRole('button', { name: 'Branch into a new conversation' }).last().click()
- await expect.poll(
- () => scaffold.ctx.agents.list().find(agent => agent.session.header.parentSession === SessionId(SEED_ID)),
- { timeout: 15_000 },
- ).toBeDefined()
- await expect.poll(
- () => page.locator('[role="treeitem"]').count(),
- { timeout: 10_000 },
- ).toBe(3)
- await expect.poll(
- () => page.locator('[role="treeitem"][aria-selected="true"]').count(),
- { timeout: 10_000 },
- ).toBe(1)
- // The row action owns a distinct ui-workspace injection from the message
- // action above, so exercise both through the loaded app before capture.
- const sourceRow = page.locator('[role="treeitem"][aria-selected="true"]')
- const rowBox = await sourceRow.boundingBox()
- if (rowBox === null) throw new Error('fork source row has no layout box')
- const actionButton = sourceRow.locator('button[aria-label^="Session actions for "]')
- await sourceRow.hover({ position: { x: rowBox.width - 16, y: rowBox.height / 2 } })
- await expect.poll(() => actionButton.isVisible(), { timeout: 2_000 }).toBe(true)
- const buttonBox = await actionButton.boundingBox()
- if (buttonBox === null) throw new Error('fork source row action has no layout box')
- await page.mouse.click(buttonBox.x + buttonBox.width / 2, buttonBox.y + buttonBox.height / 2)
- await page.getByRole('menuitem', { name: 'Fork session' }).click()
- await expect.poll(
- () => scaffold.ctx.agents.list().filter(agent => agent.session.header.parentSession !== undefined).length,
- { timeout: 15_000 },
- ).toBe(2)
- await expect.poll(
- () => page.locator('[role="treeitem"]').count(),
- { timeout: 10_000 },
- ).toBe(4)
- await expect.poll(
- () => page.locator('[role="treeitem"][aria-selected="true"]').count(),
- { timeout: 10_000 },
- ).toBe(1)
- // The child row is published before its inherited title rename settles;
- // wait for that second RPC projection before freezing the ARIA tree.
- await expect.poll(
- () => page.locator('[role="treeitem"][aria-selected="true"]').textContent(),
- { timeout: 10_000 },
- ).toContain('Use the read tool twice (2)')
- const tree = await captureStableAria(
- page,
- '[role="tree"][aria-label="Sessions"]',
- scaffold.workspaceCwd,
- )
- await compareOrRefreshGolden(FORK_EXPECTED, tree, MODE)
- })
- it.skipIf(MODE === 'record')('issued zero model calls and kept a closed inventory', async () => {
- expect(tripwire.pageErrors).toEqual([])
- expect(tripwire.warnings).toEqual([])
- await assertFixtureInventory(SNAPSHOT_DIR, ['fork.expected.md', 'ui.expected.md'])
- })
- })
|