cordis-tool-round.e2e.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Web e2e scenario for the opt-in Cordis tools. Record mode drives a real
  2. // model through inspect, mount, and unmount; replay pins the same shipped Web
  3. // composition, durable calls, generic rows, highlighted Plugin source, and
  4. // conversation accessibility tree.
  5. import { readFile } from 'node:fs/promises'
  6. import { fileURLToPath } from 'node:url'
  7. import type { Browser, Page } from 'playwright'
  8. import { chromium } from 'playwright'
  9. import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest'
  10. import type { SessionEvent } from '@deepseek-ai/dsh-session'
  11. import {
  12. captureStableAria, compareOrRefreshGolden, fixtureUserPrompts,
  13. launchWebScaffold, recordFixture, watchConsole, webSnapshotMode, type WebScaffold,
  14. } from './scaffold.ts'
  15. import { connectFreshWorkspace, newEnglishPage, saveFailureShot } from './support.ts'
  16. const FIXTURE = fileURLToPath(new URL('./snapshots/cordis-tool-round/session.jsonl', import.meta.url))
  17. const UI_EXPECTED = fileURLToPath(new URL('./snapshots/cordis-tool-round/ui.expected.md', import.meta.url))
  18. const MODE = webSnapshotMode()
  19. const CORDIS_TOOLS = ['cordis_inspect', 'cordis_mount', 'cordis_unmount'] as const
  20. const MOUNT_CODE = 'return { name: "snapshot-noop", apply(ctx) {} }'
  21. const PROMPT = 'Use only Cordis tools. First call cordis_inspect with what "temporary". '
  22. + `Then call cordis_mount with this exact code: ${JSON.stringify(MOUNT_CODE)}. `
  23. + 'Read its returned id and call cordis_unmount with that exact id. '
  24. + 'After all three calls succeed, reply exactly CORDIS_UI_DONE and stop.'
  25. function assertCompleteCordisLifecycle(events: readonly SessionEvent[]): void {
  26. const turnEnd = events.findLast(
  27. (event): event is Extract<SessionEvent, { type: 'turn/end' }> => event.type === 'turn/end',
  28. )
  29. const reason = turnEnd?.data.reason
  30. const reasonSummary = { kind: reason?.kind }
  31. expect(reasonSummary).toEqual({ kind: 'completed' })
  32. const calls = events.filter(
  33. (event): event is Extract<SessionEvent, { type: 'tool/call' }> => event.type === 'tool/call',
  34. )
  35. expect(calls.map(event => event.data.name)).toEqual(CORDIS_TOOLS)
  36. const callIds = new Set(calls.map(event => String(event.data.callId)))
  37. const results = events.filter(
  38. (event): event is Extract<SessionEvent, { type: 'tool/result' }> =>
  39. event.type === 'tool/result' && callIds.has(String(event.data.message.source.callId)),
  40. )
  41. expect(results).toHaveLength(CORDIS_TOOLS.length)
  42. expect(results.every(event => !event.data.message.content[0].isError)).toBe(true)
  43. }
  44. describe('web e2e: Cordis tools use the generic row variants', () => {
  45. let scaffold: WebScaffold
  46. let browser: Browser
  47. let page: Page
  48. let tripwire: ReturnType<typeof watchConsole>
  49. const sessionEvents: SessionEvent[] = []
  50. beforeAll(async () => {
  51. scaffold = await launchWebScaffold({
  52. cordisTools: true,
  53. ...(MODE === 'record' ? {} : { replayFixture: FIXTURE, paceMs: 15 }),
  54. })
  55. scaffold.ctx.on('session/event', (_session, event: SessionEvent) => { sessionEvents.push(event) })
  56. browser = await chromium.launch()
  57. page = await newEnglishPage(browser)
  58. tripwire = watchConsole(page)
  59. await page.goto(scaffold.baseUrl, { waitUntil: 'load' })
  60. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  61. await connectFreshWorkspace(page, scaffold.workspaceCwd)
  62. }, 120_000)
  63. afterAll(async () => {
  64. await browser?.close()
  65. await scaffold?.close()
  66. })
  67. it('drives the recorded Cordis lifecycle to a settled turn (all modes)', async () => {
  68. onTestFailed(() => saveFailureShot(page, 'web-e2e-cordis-drive'))
  69. if (MODE !== 'record') {
  70. expect(fixtureUserPrompts(await readFile(FIXTURE, 'utf8'))).toEqual([PROMPT])
  71. }
  72. const input = page.locator('textarea').first()
  73. await input.waitFor({ timeout: 10_000 })
  74. const settled = scaffold.whenTurnSettled()
  75. await input.fill(PROMPT)
  76. await input.press('Enter')
  77. const sessionId = await settled
  78. if (MODE === 'record') {
  79. assertCompleteCordisLifecycle(sessionEvents)
  80. await expect.poll(() => page.getByText('CORDIS_UI_DONE', { exact: true }).count(), { timeout: 15_000 })
  81. .toBeGreaterThanOrEqual(1)
  82. await recordFixture(scaffold, sessionId, FIXTURE)
  83. }
  84. }, 200_000)
  85. it.skipIf(MODE === 'record')('the durable log carries one complete Cordis lifecycle', () => {
  86. assertCompleteCordisLifecycle(sessionEvents)
  87. })
  88. it.skipIf(MODE === 'record')('renders Cordis lifecycle titles over the generic row mechanics', async () => {
  89. onTestFailed(() => saveFailureShot(page, 'web-e2e-cordis-rows'))
  90. await expect.poll(() => page.getByText('CORDIS_UI_DONE', { exact: true }).count(), { timeout: 15_000 })
  91. .toBeGreaterThanOrEqual(1)
  92. const inspectRow = page.locator('[data-tool="cordis_inspect"]').filter({ hasText: 'Inspect' }).first()
  93. await inspectRow.waitFor({ timeout: 10_000 })
  94. const mountRow = page.locator('[data-tool="cordis_mount"]').filter({ hasText: 'Mount temporary Plugin' }).first()
  95. await mountRow.waitFor({ timeout: 10_000 })
  96. // The whole summary row is the expand toggle (unified tool-row interaction).
  97. await mountRow.locator('[aria-expanded]').first().click()
  98. await expect.poll(() => mountRow.locator('pre.shiki').textContent(), { timeout: 10_000 })
  99. .toContain(MOUNT_CODE)
  100. const unmountRow = page.locator('[data-tool="cordis_unmount"]').filter({ hasText: 'Unmount temporary Plugin' }).first()
  101. await unmountRow.waitFor({ timeout: 10_000 })
  102. await expect.poll(() => unmountRow.textContent()).toContain('dyn-')
  103. await expect(unmountRow.getAttribute('data-state')).resolves.toBe('ok')
  104. })
  105. it.skipIf(MODE === 'record')('matches the conversation aria golden', async () => {
  106. onTestFailed(() => saveFailureShot(page, 'web-e2e-cordis-aria'))
  107. const snapshot = await captureStableAria(page, '[class*="centerCol"]', scaffold.workspaceCwd)
  108. await compareOrRefreshGolden(UI_EXPECTED, snapshot, MODE)
  109. })
  110. it.skipIf(MODE === 'record')('stayed clean: no page errors or reconnect churn', () => {
  111. expect(tripwire.pageErrors).toEqual([])
  112. expect(tripwire.warnings).toEqual([])
  113. })
  114. })