cordis-tool-round.e2e.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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, 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 = reason?.kind === 'error'
  31. ? { kind: reason.kind, code: reason.failure?.code, status: reason.failure?.status }
  32. : { kind: reason?.kind }
  33. expect(reasonSummary).toEqual({ kind: 'completed' })
  34. const calls = events.filter(
  35. (event): event is Extract<SessionEvent, { type: 'tool/call' }> => event.type === 'tool/call',
  36. )
  37. expect(calls.map(event => event.data.name)).toEqual(CORDIS_TOOLS)
  38. const callIds = new Set(calls.map(event => String(event.data.callId)))
  39. const results = events.filter(
  40. (event): event is Extract<SessionEvent, { type: 'tool/result' }> =>
  41. event.type === 'tool/result' && callIds.has(String(event.data.callId)),
  42. )
  43. expect(results).toHaveLength(CORDIS_TOOLS.length)
  44. expect(results.every(event => !event.data.isError)).toBe(true)
  45. }
  46. describe('web e2e: Cordis tools use the generic row variants', () => {
  47. let scaffold: WebScaffold
  48. let browser: Browser
  49. let page: Page
  50. let tripwire: ReturnType<typeof watchConsole>
  51. const sessionEvents: SessionEvent[] = []
  52. beforeAll(async () => {
  53. scaffold = await launchWebScaffold({
  54. cordisTools: true,
  55. ...(MODE === 'record' ? {} : { replayFixture: FIXTURE, paceMs: 15 }),
  56. })
  57. scaffold.ctx.on('session/event', (_session, event: SessionEvent) => { sessionEvents.push(event) })
  58. browser = await chromium.launch()
  59. page = await browser.newPage({ viewport: { width: 1680, height: 1000 } })
  60. tripwire = watchConsole(page)
  61. await page.goto(scaffold.baseUrl, { waitUntil: 'load' })
  62. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  63. await connectFreshWorkspace(page)
  64. }, 120_000)
  65. afterAll(async () => {
  66. await browser?.close()
  67. await scaffold?.close()
  68. })
  69. it('drives the recorded Cordis lifecycle to a settled turn (all modes)', async () => {
  70. onTestFailed(() => saveFailureShot(page, 'web-e2e-cordis-drive'))
  71. if (MODE !== 'record') {
  72. expect(fixtureUserPrompts(await readFile(FIXTURE, 'utf8'))).toEqual([PROMPT])
  73. }
  74. const input = page.locator('textarea').first()
  75. await input.waitFor({ timeout: 10_000 })
  76. const settled = scaffold.whenTurnSettled()
  77. await input.fill(PROMPT)
  78. await input.press('Enter')
  79. const sessionId = await settled
  80. if (MODE === 'record') {
  81. assertCompleteCordisLifecycle(sessionEvents)
  82. await expect.poll(() => page.getByText('CORDIS_UI_DONE', { exact: true }).count(), { timeout: 15_000 })
  83. .toBeGreaterThanOrEqual(1)
  84. await recordFixture(scaffold, sessionId, FIXTURE)
  85. }
  86. }, 200_000)
  87. it.skipIf(MODE === 'record')('the durable log carries one complete Cordis lifecycle', () => {
  88. assertCompleteCordisLifecycle(sessionEvents)
  89. })
  90. it.skipIf(MODE === 'record')('renders Cordis lifecycle titles over the generic row mechanics', async () => {
  91. onTestFailed(() => saveFailureShot(page, 'web-e2e-cordis-rows'))
  92. await expect.poll(() => page.getByText('CORDIS_UI_DONE', { exact: true }).count(), { timeout: 15_000 })
  93. .toBeGreaterThanOrEqual(1)
  94. const inspectRow = page.locator('[data-tool="cordis_inspect"]').filter({ hasText: 'Inspect' }).first()
  95. await inspectRow.waitFor({ timeout: 10_000 })
  96. const mountRow = page.locator('[data-tool="cordis_mount"]').filter({ hasText: 'Mount temporary Plugin' }).first()
  97. await mountRow.waitFor({ timeout: 10_000 })
  98. await mountRow.locator('button[aria-expanded]').click()
  99. await expect.poll(() => mountRow.locator('pre.shiki').textContent(), { timeout: 10_000 })
  100. .toContain(MOUNT_CODE)
  101. const unmountRow = page.locator('[data-tool="cordis_unmount"]').filter({ hasText: 'Unmount temporary Plugin' }).first()
  102. await unmountRow.waitFor({ timeout: 10_000 })
  103. await expect.poll(() => unmountRow.textContent()).toContain('dyn-')
  104. await expect(unmountRow.getAttribute('data-state')).resolves.toBe('ok')
  105. })
  106. it.skipIf(MODE === 'record')('matches the conversation aria golden', async () => {
  107. onTestFailed(() => saveFailureShot(page, 'web-e2e-cordis-aria'))
  108. const snapshot = await captureStableAria(page, '[class*="centerCol"]', scaffold.workspaceCwd)
  109. await compareOrRefreshGolden(UI_EXPECTED, snapshot, MODE)
  110. })
  111. it.skipIf(MODE === 'record')('stayed clean: no page errors or reconnect churn', () => {
  112. expect(tripwire.pageErrors).toEqual([])
  113. expect(tripwire.warnings).toEqual([])
  114. })
  115. })