file-upload-round.e2e.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // Web e2e scenario: generic file upload round trip. A real chromium picks a
  2. // file through the composer paperclip input; the upload RPC stores the exact
  3. // bytes below the scaffold's isolated DSH_HOME, the prompt cites the staged
  4. // reference, request assembly projects the file block to handle text, and the
  5. // model (replayed or live) reads the saved copy with the REAL read tool. The
  6. // content-addressed store makes the saved path identical across record and
  7. // replay once the workspace cwd is tokenized, so the recorded read arguments
  8. // replay verbatim against a freshly re-uploaded object.
  9. // Record: DSH_SNAPSHOT=record rewrites session.v3.jsonl, then a keyless
  10. // DSH_SNAPSHOT=refresh regenerates ui.expected.md.
  11. import { readFile } from 'node:fs/promises'
  12. import { fileURLToPath } from 'node:url'
  13. import type { Browser, Page } from 'playwright'
  14. import { chromium } from 'playwright'
  15. import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest'
  16. import type { SessionEvent } from '@deepseek-ai/dsh-session'
  17. import {
  18. assertFixtureInventory, captureStableAria, compareOrRefreshGolden, fixtureUserPrompts,
  19. launchWebScaffold, recordFixture, watchConsole, webSnapshotMode, type WebScaffold,
  20. } from './scaffold.ts'
  21. import { connectFreshWorkspace, newEnglishPage, saveFailureShot } from './support.ts'
  22. const SNAPSHOT_DIR = fileURLToPath(new URL('../../../snapshots/web/file-upload-round', import.meta.url))
  23. const FIXTURE = fileURLToPath(new URL('../../../snapshots/web/file-upload-round/session.v3.jsonl', import.meta.url))
  24. const UI_EXPECTED = fileURLToPath(new URL('../../../snapshots/web/file-upload-round/ui.expected.md', import.meta.url))
  25. const TRAJECTORY_EXPECTED = fileURLToPath(new URL('../../../snapshots/web/file-upload-round/trajectory.expected.md', import.meta.url))
  26. const OVERRIDE = fileURLToPath(new URL('../../../snapshots/web/file-upload-round/replay.override.json', import.meta.url))
  27. const DRAFT_EXPECTED = fileURLToPath(new URL('./expected/file-upload-round/draft.expected.md', import.meta.url))
  28. const HISTORY_EXPECTED = fileURLToPath(new URL('./expected/file-upload-round/history.expected.md', import.meta.url))
  29. const IMAGE_FIXTURE = fileURLToPath(new URL('../../../snapshots/session/read-image/workspace/red.png', import.meta.url))
  30. const MODE = webSnapshotMode()
  31. /** The uploaded fixture file: constant bytes so record and replay share one content digest. */
  32. const FILE_NAME = 'poem.txt'
  33. const FILE_TEXT = 'UPLOAD_ROUND_OK\n'
  34. const PROMPT = 'Read the attached file with the read tool, reply with exactly the single word it contains, and stop.'
  35. const IMAGE_NAMES = Array.from({ length: 10 }, (_unused, index) => `reference-${String(index + 1)}.png`)
  36. /** Browser-measured relations for the mixed composer attachment rail. */
  37. interface DraftRailGeometry {
  38. readonly order: readonly string[]
  39. readonly oneGroup: boolean
  40. readonly oneRow: boolean
  41. readonly equalHeight: boolean
  42. readonly fileWider: boolean
  43. readonly horizontalOverflow: boolean
  44. readonly noWrap: boolean
  45. }
  46. /** Render stable relations instead of platform-dependent absolute coordinates. */
  47. function renderDraftRailGeometry(geometry: DraftRailGeometry): string {
  48. return [
  49. '# Mixed composer attachment rail',
  50. '',
  51. `- selection order: ${geometry.order.join(' > ')}`,
  52. `- one attachment group: ${String(geometry.oneGroup)}`,
  53. `- all cards share one row: ${String(geometry.oneRow)}`,
  54. `- every card is 64px high: ${String(geometry.equalHeight)}`,
  55. `- the file card is wider than an image: ${String(geometry.fileWider)}`,
  56. `- overflowing cards scroll horizontally: ${String(geometry.horizontalOverflow)}`,
  57. `- the rail does not wrap: ${String(geometry.noWrap)}`,
  58. ].join('\n')
  59. }
  60. /** Browser-measured relations for one durable mixed-attachment message. */
  61. interface HistoryAttachmentGeometry {
  62. readonly order: readonly string[]
  63. readonly oneGroup: boolean
  64. readonly oneRow: boolean
  65. readonly equalHeight: boolean
  66. readonly imageIsTile: boolean
  67. readonly fileWider: boolean
  68. readonly wrapsWhenNeeded: boolean
  69. readonly rightAligned: boolean
  70. }
  71. /** Render stable history-layout relations instead of absolute coordinates. */
  72. function renderHistoryAttachmentGeometry(geometry: HistoryAttachmentGeometry): string {
  73. return [
  74. '# Mixed history attachment flow',
  75. '',
  76. `- source order: ${geometry.order.join(' > ')}`,
  77. `- one attachment group: ${String(geometry.oneGroup)}`,
  78. `- file and image share one row: ${String(geometry.oneRow)}`,
  79. `- both cards are 64px high: ${String(geometry.equalHeight)}`,
  80. `- the image is a 64px tile: ${String(geometry.imageIsTile)}`,
  81. `- the file card is wider than the image: ${String(geometry.fileWider)}`,
  82. `- the group wraps when needed: ${String(geometry.wrapsWhenNeeded)}`,
  83. `- the group is right-aligned: ${String(geometry.rightAligned)}`,
  84. ].join('\n')
  85. }
  86. describe('web e2e: generic file upload through the real assembly', () => {
  87. let scaffold: WebScaffold
  88. let browser: Browser
  89. let page: Page
  90. let tripwire: ReturnType<typeof watchConsole>
  91. const sessionEvents: SessionEvent[] = []
  92. beforeAll(async () => {
  93. scaffold = await launchWebScaffold({
  94. compareReplaySession: true,
  95. // The override rescripts the recorded read arguments with a
  96. // `{{fromRequest:…}}` placeholder: the saved-copy path differs per run,
  97. // and the live handle line in the request carries the current one.
  98. ...(MODE === 'record' ? {} : { replayFixture: FIXTURE, replayOverride: OVERRIDE, paceMs: 15 }),
  99. })
  100. scaffold.ctx.on('session/event', (_session, event: SessionEvent) => { sessionEvents.push(event) })
  101. browser = await chromium.launch()
  102. page = await newEnglishPage(browser)
  103. await page.setViewportSize({ width: 900, height: 900 })
  104. tripwire = watchConsole(page)
  105. await page.goto(scaffold.authenticatedUrl, { waitUntil: 'load' })
  106. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  107. await connectFreshWorkspace(page, scaffold.workspaceCwd)
  108. }, 120_000)
  109. afterAll(async () => {
  110. await browser?.close()
  111. await scaffold?.close()
  112. })
  113. it('uploads on pick, gates send on the staged receipt, and settles the turn (all modes)', async () => {
  114. onTestFailed(() => saveFailureShot(page, 'web-e2e-file-upload-drive'))
  115. if (MODE !== 'record') {
  116. // Drift guard: the committed fixture must carry exactly the drive prompt.
  117. expect(fixtureUserPrompts(await readFile(FIXTURE, 'utf8'))).toEqual([PROMPT])
  118. }
  119. const input = page.locator('[data-composer-input]').first()
  120. await input.waitFor({ timeout: 10_000 })
  121. const modelTrigger = page.getByRole('button', { name: /^Select model, current/ })
  122. await modelTrigger.click()
  123. await page.getByRole('menuitem', { name: /^Model\b/ }).click()
  124. await page.getByRole('menuitemradio', { name: 'DeepSeek-V4-Flash-Vision-Exp' }).click()
  125. await expect.poll(() => modelTrigger.getAttribute('aria-label'), { timeout: 10_000 })
  126. .toContain('DeepSeek-V4-Flash-Vision-Exp')
  127. const imageBytes = await readFile(IMAGE_FIXTURE)
  128. // Pick through the composer's hidden file input: the upload RPC runs
  129. // immediately and the pending card appears before any prompt is typed.
  130. await page.locator('input[type="file"]').setInputFiles([
  131. { name: FILE_NAME, mimeType: 'text/plain', buffer: Buffer.from(FILE_TEXT) },
  132. ...IMAGE_NAMES.map(name => ({ name, mimeType: 'image/png', buffer: imageBytes })),
  133. ])
  134. await page.getByTitle(FILE_NAME).waitFor({ timeout: 10_000 })
  135. const rail = page.getByRole('group', { name: 'Pending attachments' })
  136. await expect.poll(() => rail.locator(':scope > *').count(), { timeout: 10_000 })
  137. .toBe(IMAGE_NAMES.length + 1)
  138. const geometry = await rail.evaluate((element): DraftRailGeometry => {
  139. const cards = [...element.children] as HTMLElement[]
  140. const boxes = cards.map(card => card.getBoundingClientRect())
  141. const imageWidth = boxes[cards.findIndex(card => card.querySelector('img') !== null)]?.width ?? 0
  142. const fileWidth = boxes[cards.findIndex(card => card.querySelector('[title="poem.txt"]') !== null)]?.width ?? 0
  143. return {
  144. order: cards.map(card => card.querySelector('img')?.getAttribute('alt')
  145. ?? card.querySelector<HTMLElement>('[title]')?.title ?? ''),
  146. oneGroup: document.querySelectorAll('[role="group"][aria-label="Pending attachments"]').length === 1,
  147. oneRow: boxes.every(box => Math.abs(box.top - (boxes[0]?.top ?? box.top)) < 0.5),
  148. equalHeight: boxes.every(box => Math.abs(box.height - 64) < 0.5),
  149. fileWider: fileWidth > imageWidth,
  150. horizontalOverflow: element.scrollWidth > element.clientWidth,
  151. noWrap: getComputedStyle(element).flexWrap === 'nowrap',
  152. }
  153. })
  154. await compareOrRefreshGolden(DRAFT_EXPECTED, renderDraftRailGeometry(geometry), MODE)
  155. for (const name of IMAGE_NAMES.slice(1)) {
  156. await page.getByRole('button', { name: `Remove image ${name}` }).click({ force: true })
  157. }
  158. await expect.poll(() => rail.locator(':scope > *').count(), { timeout: 10_000 }).toBe(2)
  159. await input.fill(PROMPT)
  160. // Send unlocks only after the upload receipt lands (the staged file gate).
  161. const send = page.getByRole('button', { name: 'Send message' })
  162. await send.waitFor({ state: 'visible', timeout: 15_000 })
  163. await expect.poll(() => send.isEnabled(), { timeout: 15_000 }).toBe(true)
  164. const settled = scaffold.whenTurnSettled()
  165. await send.click()
  166. const restoreDeadline = Date.now() + 10_000
  167. let retainedCards = 0
  168. while (retainedCards === 0
  169. && !sessionEvents.some(event => event.type === 'turn/start')
  170. && Date.now() < restoreDeadline) {
  171. await page.waitForTimeout(100)
  172. retainedCards = await rail.locator(':scope > *').count()
  173. }
  174. if (retainedCards !== 0) {
  175. const feedback = await page.locator('[role="alert"], [role="status"]').allTextContents()
  176. throw new Error(`submission retained ${String(retainedCards)} draft cards; feedback=${JSON.stringify(feedback)}; events=${sessionEvents.map(event => event.type).join(',')}`)
  177. }
  178. const sessionId = await settled
  179. if (MODE === 'record') await recordFixture(scaffold, sessionId, FIXTURE)
  180. }, 200_000)
  181. it.skipIf(MODE === 'record')('persists the file block and reads the stored copy with the real read tool', () => {
  182. const userMessage = sessionEvents.find(
  183. (event): event is Extract<SessionEvent, { type: 'user/message' }> =>
  184. event.type === 'user/message' && event.data.source.kind === 'user',
  185. )
  186. if (userMessage === undefined) throw new Error('the replayed turn recorded no user message')
  187. const fileBlock = userMessage.data.content.find(block => block.type === 'file')
  188. if (fileBlock?.type !== 'file') throw new Error('the user message carries no file block')
  189. expect(fileBlock.attachment.name).toBe(FILE_NAME)
  190. expect(fileBlock.attachment.bytes).toBe(Buffer.byteLength(FILE_TEXT))
  191. expect(String(fileBlock.attachment.attachmentId)).toMatch(/^sha256:[0-9a-f]{64}$/)
  192. const readCall = sessionEvents.find(
  193. (event): event is Extract<SessionEvent, { type: 'tool/call' }> =>
  194. event.type === 'tool/call' && event.data.name === 'read',
  195. )
  196. if (readCall === undefined) throw new Error('the replayed turn did not call the read tool')
  197. expect(readCall.data.arguments).toContain(FILE_NAME)
  198. const readResult = sessionEvents.find(
  199. (event): event is Extract<SessionEvent, { type: 'tool/result' }> =>
  200. event.type === 'tool/result' && event.data.message.source.callId === readCall.data.callId,
  201. )
  202. if (readResult === undefined) throw new Error('the read call produced no durable result')
  203. const content = readResult.data.message.content[0]
  204. expect(content.isError).toBe(false)
  205. expect(content.content.filter(block => block.type === 'text').map(block => block.text).join(''))
  206. .toContain('UPLOAD_ROUND_OK')
  207. const turnEnds = sessionEvents.filter(event => event.type === 'turn/end')
  208. expect(turnEnds.length).toBe(1)
  209. expect((turnEnds[0] as SessionEvent & { data: { reason: { kind: string } } }).data.reason.kind).toBe('completed')
  210. })
  211. it.skipIf(MODE === 'record')('renders the durable file card beside the settled answer', async () => {
  212. onTestFailed(() => saveFailureShot(page, 'web-e2e-file-upload-aria'))
  213. await expect.poll(() => page.getByText('UPLOAD_ROUND_OK', { exact: false }).count(), { timeout: 15_000 })
  214. .toBeGreaterThanOrEqual(1)
  215. await page.getByTitle(FILE_NAME).first().waitFor({ timeout: 10_000 })
  216. const snapshot = await captureStableAria(page, '[class*="centerCol"]', scaffold.workspaceCwd)
  217. await compareOrRefreshGolden(UI_EXPECTED, snapshot, MODE)
  218. })
  219. it.skipIf(MODE === 'record')('keeps a mixed durable message in one ordered wrapping attachment flow', async () => {
  220. const groups = page.locator('[data-message-attachments]')
  221. await expect.poll(() => groups.count(), { timeout: 10_000 }).toBe(1)
  222. const geometry = await groups.first().evaluate((element): HistoryAttachmentGeometry => {
  223. const cards = [...element.children] as HTMLElement[]
  224. const renderedCards = cards.map((card) => {
  225. const box = card.getBoundingClientRect()
  226. return box.width === 0 && box.height === 0 && card.firstElementChild instanceof HTMLElement
  227. ? card.firstElementChild
  228. : card
  229. })
  230. const boxes = renderedCards.map(card => card.getBoundingClientRect())
  231. const imageIndex = cards.findIndex(card => card.querySelector('img') !== null)
  232. const fileIndex = cards.findIndex(card => card.getAttribute('title') === 'poem.txt')
  233. const imageBox = boxes[imageIndex]
  234. const fileBox = boxes[fileIndex]
  235. return {
  236. order: cards.map(card => card.getAttribute('title') ?? card.querySelector('img')?.getAttribute('alt') ?? ''),
  237. oneGroup: document.querySelectorAll('[data-message-attachments]').length === 1,
  238. oneRow: boxes.every(box => Math.abs(box.top - (boxes[0]?.top ?? box.top)) < 0.5),
  239. equalHeight: boxes.every(box => Math.abs(box.height - 64) < 0.5),
  240. imageIsTile: imageBox !== undefined && Math.abs(imageBox.width - 64) < 0.5,
  241. fileWider: fileBox !== undefined && imageBox !== undefined && fileBox.width > imageBox.width,
  242. wrapsWhenNeeded: getComputedStyle(element).flexWrap === 'wrap',
  243. rightAligned: getComputedStyle(element).justifyContent === 'flex-end',
  244. }
  245. })
  246. await compareOrRefreshGolden(HISTORY_EXPECTED, renderHistoryAttachmentGeometry(geometry), MODE)
  247. })
  248. it.skipIf(MODE === 'record')('marks the durable file in Trajectory without copying the Chat card', async () => {
  249. await page.getByRole('tab', { name: 'Trajectory', exact: true }).click()
  250. await page.getByLabel('Trajectory timeline').waitFor({ timeout: 30_000 })
  251. await page.getByRole('row', { name: /Files ×1/ }).waitFor({ timeout: 10_000 })
  252. const snapshot = await captureStableAria(
  253. page,
  254. '[data-trajectory-row-key][aria-label*="Files ×1"]',
  255. scaffold.workspaceCwd,
  256. )
  257. await compareOrRefreshGolden(TRAJECTORY_EXPECTED, snapshot, MODE)
  258. })
  259. it.skipIf(MODE === 'record')('stayed clean and kept the exact fixture inventory', async () => {
  260. expect(tripwire.pageErrors).toEqual([])
  261. expect(tripwire.warnings).toEqual([])
  262. await assertFixtureInventory(SNAPSHOT_DIR, [
  263. 'session.v3.jsonl', 'replay.override.json', 'ui.expected.md', 'trajectory.expected.md',
  264. ])
  265. })
  266. })