present.e2e.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /** Recorded source-file delivery, edits, reload, deletion, and Session ZIP behavior. */
  2. import { readFile, unlink, mkdir, mkdtemp, writeFile, rm, realpath } from 'node:fs/promises'
  3. import { join, delimiter } from 'node:path'
  4. import { fileURLToPath } from 'node:url'
  5. import { chromium, type Browser, type Page } from 'playwright'
  6. import { unzipSync, strFromU8 } from 'fflate'
  7. import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'
  8. import { tmpdir, release } from 'node:os'
  9. import type { SessionEvent, SessionId } from '@deepseek-ai/dsh-session'
  10. import type {} from '@deepseek-ai/dsh-tool-present/types'
  11. import {
  12. acknowledgeReloadConnectionLoss, assertFinalWorkspaceSnapshot, captureExpandedTurnProcessAria,
  13. compareOrRefreshGolden, fixtureUserPrompts, launchWebScaffold, recordFixture,
  14. watchConsole, webSnapshotMode, type WebScaffold,
  15. } from './scaffold.ts'
  16. import { connectFreshWorkspace, newEnglishPage } from './support.ts'
  17. const DIR = fileURLToPath(new URL('../../../snapshots/web/present', import.meta.url))
  18. const FIXTURE = join(DIR, 'session.v3.jsonl')
  19. const MODE = webSnapshotMode()
  20. const PROMPT = 'Use one run_code program to do the following in order. Call present for missing.txt and catch its error without creating that file. '
  21. + 'Use bash to run exactly `printf "DELIVERED_REPORT\\n" > report.txt; printf "DELIVERED_NOTE\\n" > 说明.txt`. '
  22. + 'Call present for report.txt and 说明.txt. After present succeeds, deliberately throw the string "AFTER_PRESENT" (not an Error object) from that same run_code program. '
  23. + 'Do not retry the program or create any other files. Finish by mentioning `report.txt` and `说明.txt` in inline code, and put PRESENT_DONE in a separate paragraph.'
  24. // The recorded Bash scenario and executable opener fixture require a POSIX host outside WSL.
  25. describe.skipIf(process.platform === 'win32' || release().toLowerCase().includes('microsoft'))('web e2e: explicit file delivery', () => {
  26. let scaffold: WebScaffold
  27. let browser: Browser
  28. let page: Page
  29. let tripwire: ReturnType<typeof watchConsole>
  30. let sessionId: SessionId
  31. let cwd: string
  32. let disposeApproval: (() => void) | undefined
  33. const events: SessionEvent[] = []
  34. let nativeRoot: string | undefined
  35. let openLog: string
  36. const opened = async (): Promise<Array<{ path: string; content: string }>> => (await readFile(openLog, 'utf8')).split('\n').filter(Boolean).map(line => JSON.parse(line) as { path: string; content: string })
  37. const downloads: string[] = []
  38. beforeAll(async () => {
  39. nativeRoot = await mkdtemp(join(tmpdir(), 'dsh-present-native-'))
  40. openLog = join(nativeRoot, 'opened.jsonl')
  41. await writeFile(openLog, '')
  42. // Exercise the built Host through its actual OS command, replacing only the desktop application.
  43. const command = process.platform === 'darwin' ? 'open' : 'xdg-open'
  44. await writeFile(join(nativeRoot, command), `#!${process.execPath}
  45. const fs = require('node:fs');
  46. fs.appendFileSync(${JSON.stringify(openLog)}, JSON.stringify({ path: process.argv[2], content: fs.readFileSync(process.argv[2], 'utf8') }) + '\\n');
  47. `, { mode: 0o700 })
  48. vi.stubEnv('PATH', `${nativeRoot}${delimiter}${process.env.PATH ?? ''}`)
  49. await mkdir(DIR, { recursive: true })
  50. scaffold = await launchWebScaffold({
  51. agentPresets: { roots: [], default: 'ptc' }, compareReplaySession: true,
  52. ...(MODE === 'record' ? {} : { replayFixture: FIXTURE }),
  53. })
  54. disposeApproval = scaffold.ctx.on('approval/request', () => Promise.resolve('allowed-once'), { prepend: true })
  55. scaffold.ctx.on('session/event', (_session, event) => { events.push(event) })
  56. browser = await chromium.launch()
  57. page = await newEnglishPage(browser)
  58. tripwire = watchConsole(page)
  59. page.on('download', (download) => { downloads.push(download.suggestedFilename()) })
  60. await page.goto(scaffold.authenticatedUrl, { waitUntil: 'load' })
  61. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  62. await connectFreshWorkspace(page, scaffold.workspaceCwd)
  63. }, 120_000)
  64. afterAll(async () => {
  65. try {
  66. await browser?.close()
  67. } finally {
  68. disposeApproval?.()
  69. try {
  70. await scaffold?.close()
  71. } finally {
  72. vi.unstubAllEnvs()
  73. if (nativeRoot !== undefined) await rm(nativeRoot, { recursive: true, force: true })
  74. }
  75. }
  76. })
  77. it('declares nested deliveries even when the enclosing program subsequently fails', async () => {
  78. if (MODE !== 'record') expect(fixtureUserPrompts(await readFile(FIXTURE, 'utf8'))).toEqual([PROMPT])
  79. const settled = scaffold.whenTurnSettled()
  80. const input = page.locator('[data-composer-input]').first()
  81. await input.fill(PROMPT)
  82. await input.press('Enter')
  83. sessionId = await settled
  84. const workspace = scaffold.ctx.agents.get(sessionId)?.session.header.cwd
  85. if (workspace === undefined) throw new Error('present Session has no workspace')
  86. cwd = workspace
  87. if (MODE === 'record') await recordFixture(scaffold, sessionId, FIXTURE)
  88. await page.getByText(/^PRESENT_DONE\.?$/).waitFor({ timeout: 30_000 })
  89. await assertFinalWorkspaceSnapshot(DIR, cwd)
  90. expect(events.filter(event => event.type === 'deliverables/presented').flatMap(event => event.data.files.map(file => file.path)))
  91. .toEqual(['report.txt', '说明.txt'])
  92. for (const event of events) {
  93. if (event.type === 'deliverables/presented') {
  94. expect(event.data.files).toEqual([
  95. { path: 'report.txt', description: 'delivered report' },
  96. { path: '说明.txt', description: 'delivered note' },
  97. ])
  98. }
  99. }
  100. expect(events.some(event => event.type === 'tool/ptc-dispatch' && event.data.name === 'present' && event.data.isError)).toBe(true)
  101. expect(events.some(event => event.type === 'tool/result' && event.data.message.content[0].isError)).toBe(true)
  102. }, 200_000)
  103. it('opens current source files after edits and reload, and reports deletion without downloading', async () => {
  104. await writeFile(join(cwd, 'report.txt'), 'EDITED_REPORT\n')
  105. await writeFile(join(cwd, '说明.txt'), 'EDITED_NOTE\n')
  106. for (const reload of [false, true]) {
  107. if (reload) {
  108. const warningStart = tripwire.warnings.length
  109. await page.reload({ waitUntil: 'load' })
  110. acknowledgeReloadConnectionLoss(tripwire, warningStart)
  111. await page.getByText(/^PRESENT_DONE\.?$/).waitFor({ timeout: 30_000 })
  112. }
  113. const row = page.locator('[data-presented-files-row]')
  114. await row.waitFor()
  115. expect(await row.getByRole('button').count()).toBe(2)
  116. for (const [name, bytes] of [['report.txt', 'EDITED_REPORT\n'], ['说明.txt', 'EDITED_NOTE\n']] as const) {
  117. const count = (await opened()).length
  118. const response = page.waitForResponse(response => response.url().includes('/api/present.open?') && response.request().method() === 'POST')
  119. await row.getByRole('button', { name: `Open ${name} in default app`, exact: true }).click()
  120. expect((await response).status()).toBe(204)
  121. await page.waitForFunction(() => document.querySelector('[data-presented-files-row] button:disabled') === null)
  122. expect(await opened()).toHaveLength(count + 1)
  123. expect((await opened()).at(-1)).toEqual({ path: await realpath(join(cwd, name)), content: bytes })
  124. }
  125. }
  126. const count = (await opened()).length
  127. const openedResponse = page.waitForResponse(response => response.url().includes('/api/present.open?') && response.request().method() === 'POST')
  128. await page.locator('code').getByRole('button', { name: 'Open report.txt in default app', exact: true }).click()
  129. await page.waitForFunction(() => document.querySelector('[data-presented-files-row] button:disabled') === null)
  130. expect((await openedResponse).status()).toBe(204)
  131. expect(await opened()).toHaveLength(count + 1)
  132. expect((await opened()).at(-1)).toEqual({ path: await realpath(join(cwd, 'report.txt')), content: 'EDITED_REPORT\n' })
  133. expect(downloads).toEqual([])
  134. const response = await page.request.get(new URL(`/api/session.export?sessionId=${sessionId}`, scaffold.authenticatedUrl).href)
  135. expect(response.status()).toBe(200)
  136. const entries = unzipSync(await response.body())
  137. expect(Object.keys(entries)).toHaveLength(1)
  138. const exported = strFromU8(Object.values(entries)[0]!)
  139. expect(exported).toContain('deliverables/presented')
  140. const declarations = exported.trim().split('\n').map(line => JSON.parse(line) as SessionEvent)
  141. .filter(event => event.type === 'deliverables/presented')
  142. expect(declarations).toHaveLength(1)
  143. expect(declarations[0]!.data.files).toEqual([
  144. { path: 'report.txt', description: 'delivered report' },
  145. { path: '说明.txt', description: 'delivered note' },
  146. ])
  147. expect(exported).not.toContain('EDITED_REPORT')
  148. if (MODE !== 'record') {
  149. const aria = await captureExpandedTurnProcessAria(page, '[class*="centerCol"]', scaffold.workspaceCwd)
  150. await compareOrRefreshGolden(join(DIR, 'ui.expected.md'), aria, MODE)
  151. await page.locator('[data-turn-process]').click()
  152. const failed = page.locator('[data-tool="present"][data-state="error"]')
  153. const delivered = page.locator('[data-tool="present"][data-state="ok"]')
  154. expect(await failed.count()).toBe(1)
  155. expect(await delivered.count()).toBe(1)
  156. expect(await failed.innerText()).toContain('Delivery failed')
  157. expect(await delivered.innerText()).toContain('Delivered')
  158. await page.locator('[data-turn-process]').click()
  159. await page.setViewportSize({ width: 480, height: 900 })
  160. const row = page.locator('[data-presented-files-row]')
  161. await row.scrollIntoViewIfNeeded()
  162. for (const card of await row.getByRole('button').all()) {
  163. const bounds = await card.boundingBox()
  164. expect(bounds).not.toBeNull()
  165. expect(bounds!.x).toBeGreaterThanOrEqual(0)
  166. expect(bounds!.x + bounds!.width).toBeLessThanOrEqual(480)
  167. }
  168. }
  169. const beforeDelete = (await opened()).length
  170. await unlink(join(cwd, 'report.txt'))
  171. const missing = page.waitForResponse(response => response.url().includes('/api/present.open?'))
  172. await page.locator('[data-presented-files-row]').getByRole('button', { name: 'Open report.txt in default app', exact: true }).click()
  173. expect((await missing).status()).toBe(404)
  174. await page.getByText('Could not open. Click to retry.', { exact: true }).waitFor()
  175. expect(await opened()).toHaveLength(beforeDelete)
  176. expect(downloads).toEqual([])
  177. expect(tripwire.pageErrors).toEqual([])
  178. expect(tripwire.warnings).toEqual([])
  179. })
  180. })