produced-files.e2e.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Web e2e scenario: the single-line produced-files summary a finished turn
  2. // ends with. Cold-seeds ten writes (zero model calls), then verifies the real
  3. // assembled lane keeps a precise +N and a capability-gated folder handoff.
  4. // The folder request is intercepted so one real browser click can exercise
  5. // the full client carrier without launching a native application in CI.
  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, vi } from 'vitest'
  10. import { CallId, createAssistantMessage, createToolResultMessage, createUserMessage } from '@deepseek-ai/dsh-llm'
  11. import { SESSION_FORMAT_VERSION, Session, SessionId } from '@deepseek-ai/dsh-session'
  12. import type {} from '@deepseek-ai/dsh-session-title'
  13. import {
  14. launchWebScaffold, seedSession, watchConsole, webSnapshotMode, type WebScaffold,
  15. } from './scaffold.ts'
  16. import { newEnglishPage, saveFailureShot } from './support.ts'
  17. const MODE = webSnapshotMode()
  18. const OVERLAY = fileURLToPath(new URL('./produced-files.overlay.yml', import.meta.url))
  19. const SEED_ID = 'produced-files-web-e2e'
  20. const DONE = 'PRODUCED_FILES_DONE'
  21. /** Short leading names plus a long third name make the narrow lane deterministically show two. */
  22. const PRODUCED = [
  23. '关于我.md',
  24. 'index.html',
  25. 'long-generated-experience-specification-for-produced-files-overflow.md',
  26. 'styles.css',
  27. 'app.ts',
  28. 'schema.json',
  29. 'README.md',
  30. 'preview.svg',
  31. 'notes.txt',
  32. 'manifest.yaml',
  33. ] as const
  34. /** Build one settled turn whose successful write calls carry ten locations. */
  35. function producedFixture(): string {
  36. const session = Session.create(SessionId('produced-files-source'))
  37. const eventTimeOrigin = new Date().setHours(12, 0, 0, 0)
  38. session.append('turn/start', { turn: 1 })
  39. const user = session.append('user/message', createUserMessage({
  40. content: [{ type: 'text', text: 'Create the site files.' }],
  41. source: { kind: 'user' },
  42. }), { surfaceOp: 'append' })
  43. session.append('session/title', {
  44. title: 'Produced files overflow', messageSeqs: [user.seq], source: { kind: 'fallback' },
  45. })
  46. session.append('step/start', { turn: 1, step: 1 })
  47. const calls = PRODUCED.map((path, index) => ({
  48. path,
  49. callId: CallId(`produced-files-${String(index)}`),
  50. args: JSON.stringify({ file_path: path, content: `content of ${path}\n` }),
  51. }))
  52. session.append('assistant/message', {
  53. turn: 1,
  54. step: 1,
  55. message: createAssistantMessage({
  56. content: calls.map(call => ({
  57. type: 'tool-call' as const,
  58. id: call.callId,
  59. name: 'write',
  60. arguments: call.args,
  61. })),
  62. source: { provider: 'deepseek-official', model: 'deepseek-v4-flash' },
  63. }),
  64. }, { surfaceOp: 'append' })
  65. for (const call of calls) {
  66. const source = session.append('tool/call', {
  67. turn: 1, step: 1, callId: call.callId, name: 'write', arguments: call.args,
  68. })
  69. session.append('tool/result', {
  70. turn: 1,
  71. step: 1,
  72. message: createToolResultMessage({
  73. callId: call.callId,
  74. content: [{ type: 'text', text: `Created ${call.path}` }],
  75. isError: false,
  76. }),
  77. }, { surfaceOp: 'append', sourceEventSeqs: [source.seq] })
  78. }
  79. session.append('step/start', { turn: 1, step: 2 })
  80. session.append('assistant/message', {
  81. turn: 1,
  82. step: 2,
  83. message: createAssistantMessage({
  84. content: [{ type: 'text', text: `Created the site.\n\n${DONE}` }],
  85. source: { provider: 'deepseek-official', model: 'deepseek-v4-flash' },
  86. }),
  87. }, { surfaceOp: 'append' })
  88. session.append('step/end', { turn: 1, step: 2 })
  89. session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  90. return [
  91. JSON.stringify({
  92. type: 'session', version: SESSION_FORMAT_VERSION, id: '{{sessionId}}',
  93. createdAt: 0, cwd: '{{cwd}}',
  94. }),
  95. ...session.events.map(event => JSON.stringify({
  96. ...event, time: eventTimeOrigin + event.seq * 1_000,
  97. })),
  98. '',
  99. ].join('\n')
  100. }
  101. describe('web e2e: a finished turn ends with the files it produced', () => {
  102. let scaffold: WebScaffold
  103. let browser: Browser
  104. let page: Page
  105. let tripwire: ReturnType<typeof watchConsole>
  106. beforeAll(async () => {
  107. scaffold = await launchWebScaffold({ extraOverlayPath: OVERLAY })
  108. await seedSession(scaffold, producedFixture(), SEED_ID)
  109. browser = await chromium.launch()
  110. page = await newEnglishPage(browser)
  111. // Keep the responsive sidebar available while selecting the cold seed;
  112. // the assertion itself narrows the conversation after navigation.
  113. await page.setViewportSize({ width: 1280, height: 900 })
  114. tripwire = watchConsole(page)
  115. await page.goto(scaffold.baseUrl, { waitUntil: 'load' })
  116. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  117. }, 120_000)
  118. afterAll(async () => {
  119. await browser?.close()
  120. await scaffold?.close()
  121. })
  122. it.skipIf(MODE === 'record')('keeps a narrow ten-file summary on one line with +8 and a folder action', async () => {
  123. onTestFailed(() => saveFailureShot(page, 'web-e2e-produced-files'))
  124. const groupRow = page.locator('[role="treeitem"]').first()
  125. await groupRow.waitFor({ timeout: 15_000 })
  126. if (await groupRow.getAttribute('aria-expanded') !== 'true') await groupRow.click()
  127. const sessionRow = page.locator('[role="treeitem"]').nth(1)
  128. await sessionRow.waitFor({ timeout: 10_000 })
  129. await sessionRow.click()
  130. await expect.poll(() => page.getByText(DONE, { exact: true }).count(), { timeout: 15_000 }).toBe(1)
  131. await page.setViewportSize({ width: 780, height: 900 })
  132. const row = page.locator('[data-produced-files-row]')
  133. await row.waitFor({ timeout: 15_000 })
  134. const chips = row.getByRole('button')
  135. await expect.poll(() => chips.count()).toBe(2)
  136. expect(await chips.nth(0).innerText()).toBe('关于我.md')
  137. expect(await chips.nth(1).innerText()).toBe('index.html')
  138. expect(await row.getByText('+ 8 files', { exact: true }).count()).toBe(1)
  139. const showFolder = page.getByRole('button', { name: 'Show in folder', exact: true })
  140. expect(await showFolder.count()).toBe(1)
  141. expect(await page.getByText('Produced', { exact: true }).count()).toBe(1)
  142. const openPath = vi.spyOn(scaffold.ctx.apiProxy.host, 'openPath')
  143. .mockImplementation(async (request, _signal) => ({
  144. rpcId: request.rpcId,
  145. result: { ok: true, value: { opened: true as const } },
  146. }))
  147. try {
  148. const [response] = await Promise.all([
  149. page.waitForResponse(response => new URL(response.url()).pathname === '/api/host.openPath'),
  150. showFolder.click({ clickCount: 1 }),
  151. ])
  152. expect(response.status()).toBe(200)
  153. expect(openPath).toHaveBeenCalledTimes(1)
  154. expect(openPath.mock.calls[0]![0].payload).toEqual({ path: `${scaffold.workspaceCwd}/.` })
  155. } finally {
  156. openPath.mockRestore()
  157. }
  158. const tops = await row.locator(':scope > *').evaluateAll(elements =>
  159. elements.map(element => element.getBoundingClientRect().top))
  160. expect(new Set(tops.map(top => Math.round(top))).size).toBe(1)
  161. const geometry = await row.evaluate(element => ({
  162. clientWidth: element.clientWidth, scrollWidth: element.scrollWidth,
  163. }))
  164. expect(geometry.scrollWidth).toBeLessThanOrEqual(geometry.clientWidth)
  165. expect(tripwire.pageErrors).toEqual([])
  166. expect(tripwire.warnings).toEqual([])
  167. }, 90_000)
  168. })