document-preview.e2e.ts 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. /** Keyless document-preview smoke through a real Session, Files tab, and shipped renderers. */
  2. import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'
  3. import { tmpdir } from 'node:os'
  4. import { join, relative } from 'node:path'
  5. import { fileURLToPath } from 'node:url'
  6. import type { Browser, Locator, Page } from 'playwright'
  7. import { chromium } from 'playwright'
  8. import { afterAll, beforeAll, describe, expect, it, onTestFailed, vi } from 'vitest'
  9. import { realOfficeBytes } from './office-fixture.ts'
  10. import { pdfFixture, selectionPdfFixture } from '../../../packages/client/ui-sidebar-documentpreview/tests/pdf-fixture.ts'
  11. import { assertFixtureInventory, compareOrRefreshGolden, launchWebScaffold, watchConsole, webSnapshotMode, type WebScaffold } from './scaffold.ts'
  12. import { connectFreshWorkspace, newEnglishPage, saveFailureShot } from './support.ts'
  13. const FIXTURE = fileURLToPath(new URL('../../../snapshots/web/lifecycle-chrome/session.v3.jsonl', import.meta.url))
  14. const SNAPSHOT_DIR = fileURLToPath(new URL('../../../snapshots/web/document-preview', import.meta.url))
  15. const EXPECTED = join(SNAPSHOT_DIR, 'document.expected.md')
  16. const PAGING_PATCH = join(SNAPSHOT_DIR, 'paging.patch.yml')
  17. const PAGE_LINES = 64
  18. const SHOT_DIR = fileURLToPath(new URL('../../../.artifacts/screenshots/0908-document-preview', import.meta.url))
  19. const PROMPT = 'Reply with the single word LIGHTHOUSE and stop.'
  20. const MODE = webSnapshotMode()
  21. const TINY_PNG = Buffer.from(
  22. 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=',
  23. 'base64',
  24. )
  25. /** Successful render evidence stays outside the committed snapshot inventory. */
  26. async function successShot(page: Page, name: string): Promise<void> {
  27. await mkdir(SHOT_DIR, { recursive: true })
  28. await page.screenshot({ path: join(SHOT_DIR, `${name}-${MODE}-${process.pid}.png`), fullPage: true })
  29. }
  30. /** Exercise native browser selection and copy, including the text overlay's canvas alignment. */
  31. async function copyPdfText(page: Page, preview: Locator, expected: string): Promise<void> {
  32. const text = preview.locator('[data-pdf-text] span:not(.markedContent)').filter({ hasText: expected }).first()
  33. await text.waitFor({ state: 'visible' })
  34. await expect.poll(() => text.evaluate(node => getComputedStyle(node).userSelect)).toBe('text')
  35. await text.click({ clickCount: 3 })
  36. await expect.poll(() => page.evaluate(() => window.getSelection()?.toString().trim())).toBe(expected)
  37. await page.context().grantPermissions(['clipboard-read', 'clipboard-write'], { origin: new URL(page.url()).origin })
  38. await page.keyboard.press('ControlOrMeta+C')
  39. await expect.poll(() => page.evaluate(async () => (await navigator.clipboard.readText()).trim())).toBe(expected)
  40. await expect.poll(() => preview.locator('[data-pdf-page]').first().evaluate((node) => {
  41. const canvas = node.querySelector('canvas')!.getBoundingClientRect()
  42. const layer = node.querySelector('.textLayer')!.getBoundingClientRect()
  43. return Math.max(Math.abs(layer.width - canvas.width), Math.abs(layer.height - canvas.height),
  44. Math.abs(layer.left - canvas.left), Math.abs(layer.top - canvas.top))
  45. })).toBeLessThan(1)
  46. }
  47. /** Trigger the document owner's native scroll handler after a real first page overflows. */
  48. async function scrollForNextPage(body: Locator): Promise<void> {
  49. await body.evaluate((node) => {
  50. if (node.scrollHeight <= node.clientHeight) throw new Error('paging fixture does not overflow the document body')
  51. node.scrollTop = node.scrollHeight
  52. })
  53. }
  54. /** Read the solid vector fill away from antialiased page edges. */
  55. async function canvasColor(canvas: Locator): Promise<string> {
  56. return await canvas.evaluate((node) => {
  57. const surface = node as HTMLCanvasElement
  58. const context = surface.getContext('2d')
  59. if (context === null) throw new Error('PDF canvas has no 2D context')
  60. const pixel = context.getImageData(Math.floor(surface.width / 2), Math.floor(surface.height / 2), 1, 1).data
  61. if (Number(pixel[3]) !== 255) return 'transparent'
  62. if (Number(pixel[0]) - Number(pixel[2]) > 150) return 'red'
  63. if (Number(pixel[2]) - Number(pixel[0]) > 150) return 'blue'
  64. return 'other'
  65. })
  66. }
  67. /** Select a workspace file through the Files tab and wait for its preview identity. */
  68. async function openPreviewFile(column: Locator, filesTab: Locator, preview: Locator, name: string): Promise<void> {
  69. await filesTab.click()
  70. await column.locator('[data-files-entry="file"]').getByRole('button', { name, exact: true }).click()
  71. await expect.poll(async () => (await preview.getAttribute('data-textpreview-url'))?.endsWith(`/${name}`)).toBe(true)
  72. }
  73. describe.skipIf(MODE === 'record')('web e2e: document preview through Files', () => {
  74. let scaffold: WebScaffold
  75. let browser: Browser
  76. let page: Page
  77. let tripwire: ReturnType<typeof watchConsole>
  78. let outsideRoot: string | undefined
  79. beforeAll(async () => {
  80. outsideRoot = await mkdtemp(join(tmpdir(), 'dsh-preview-outside-'))
  81. scaffold = await launchWebScaffold({ replayFixture: FIXTURE, paceMs: 5, compareReplaySession: false, extraOverlayPath: PAGING_PATCH })
  82. browser = await chromium.launch()
  83. page = await newEnglishPage(browser)
  84. tripwire = watchConsole(page)
  85. await page.goto(scaffold.authenticatedUrl, { waitUntil: 'load' })
  86. await connectFreshWorkspace(page, scaffold.workspaceCwd)
  87. })
  88. afterAll(async () => {
  89. try {
  90. await browser?.close()
  91. } finally {
  92. try {
  93. await scaffold?.close()
  94. } finally {
  95. if (outsideRoot !== undefined) await rm(outsideRoot, { recursive: true, force: true })
  96. }
  97. }
  98. })
  99. it('opens text, isolated HTML, width-fitted images, and rendered PDF from the Session workspace', async () => {
  100. onTestFailed(async () => {
  101. await mkdir(SHOT_DIR, { recursive: true })
  102. await saveFailureShot(page, `screenshots/0908-document-preview/smoke-${process.pid}`)
  103. })
  104. const settled = scaffold.whenTurnSettled()
  105. const input = page.locator('[data-composer-input]').first()
  106. await input.fill(PROMPT)
  107. await input.press('Enter')
  108. const sessionId = await settled
  109. await page.getByText('LIGHTHOUSE', { exact: true }).waitFor({ timeout: 15_000 })
  110. const cwd = scaffold.ctx.agents.get(sessionId)?.session.header.cwd
  111. if (cwd === undefined) throw new Error('settled Session has no workspace cwd')
  112. if (outsideRoot === undefined) throw new Error('outside fixture directory is unavailable')
  113. const outsideScript = join(outsideRoot, 'outside.js')
  114. const outsideReference = relative(cwd, outsideScript).replace(/\\/g, '/')
  115. const markdownText = [
  116. '# Markdown smoke', '', 'Rendered from the workspace.', '',
  117. ...Array.from({ length: (PAGE_LINES - 4) / 2 }, (_, index) => [`Paragraph ${index + 1}: ${'visible prefix '.repeat(20)}`, '']).flat(),
  118. '# Markdown tail',
  119. ].join('\n')
  120. const codeLines = [
  121. ...Array.from({ length: PAGE_LINES }, (_, index) => index === 0 ? 'const prefix = "CODE_PREFIX";' : `// prefix line ${index + 1}`),
  122. 'const tail = "CODE_TAIL";',
  123. ]
  124. await Promise.all([
  125. writeFile(join(cwd, 'smoke.md'), markdownText),
  126. writeFile(join(cwd, 'pages.ts'), codeLines.join('\n')),
  127. writeFile(join(cwd, 'notes.unknown'), 'UNKNOWN_SUFFIX\nPlain fallback.'),
  128. writeFile(join(cwd, 'smoke.html'), [
  129. '<!doctype html><link rel="stylesheet" href="./local.css">',
  130. '<h1>HTML smoke</h1><p id="result">pending</p><p id="local-result">pending</p><p id="parent-result">pending</p>',
  131. '<p id="outside-result">pending</p>',
  132. '<script>document.getElementById("result").textContent="INLINE_OK";',
  133. 'try{parent.document.documentElement.setAttribute("data-document-preview-escape","true");document.getElementById("parent-result").textContent="parent-accessible"}',
  134. 'catch(error){const result=document.getElementById("parent-result");result.textContent="parent-blocked";result.dataset.error=error.name}</script>',
  135. '<script src="./local.js"></script>',
  136. `<script src="${outsideReference}"></script>`,
  137. ].join('\n')),
  138. writeFile(join(cwd, 'local.js'), 'document.getElementById("local-result").textContent="LOCAL_JS_OK";'),
  139. writeFile(join(cwd, 'local.css'), '#local-result { color: rgb(12, 34, 56); }'),
  140. writeFile(outsideScript, 'document.getElementById("outside-result").textContent="OUTSIDE_JS_OK";'),
  141. writeFile(join(cwd, 'tiny.png'), TINY_PNG),
  142. writeFile(join(cwd, 'large.svg'), [
  143. '<svg xmlns="http://www.w3.org/2000/svg" width="1200" height="1600" viewBox="0 0 1200 1600">',
  144. '<script>parent.document.documentElement.setAttribute("data-image-preview-escape","true")</script>',
  145. '<rect width="1200" height="1600" fill="#2463eb"/>',
  146. '</svg>',
  147. ].join('')),
  148. writeFile(join(cwd, 'smoke.pdf'), pdfFixture()),
  149. writeFile(join(cwd, 'user-unit.pdf'), pdfFixture(2)),
  150. writeFile(join(cwd, 'selection.pdf'), selectionPdfFixture()),
  151. ...['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx'].map(extension => writeFile(join(cwd, `unavailable.${extension}`), Buffer.from('PK\u0003\u0004OFFICE_BINARY_PREVIEW'))),
  152. writeFile(join(cwd, 'clip.mp4'), Buffer.from([0x00, 0x00, 0x00, 0x18, 0x66, 0x74, 0x79, 0x70])),
  153. ])
  154. const column = page.locator('[data-rightbar-col]')
  155. await page.locator('[data-sidebar-right-expand]').click()
  156. await column.locator('[data-sidebar-right-guide-entry="files"]').click()
  157. await column.locator('[data-files-state="tree"]').waitFor({ state: 'visible' })
  158. await column.locator('[data-files-reload]').click()
  159. const filesTab = column.locator('[data-dockkit-tab]').filter({ has: page.getByText('Files', { exact: true }) })
  160. const addTab = column.locator('[data-dockkit-add-tab]')
  161. expect(await column.locator('[data-dockkit-tab]').count()).toBe(1)
  162. const defaultTitle = await filesTab.locator('[data-dockkit-tab-title]').innerText()
  163. const initialFilesClose = await filesTab.locator('[data-dockkit-tab-close]').count()
  164. expect(initialFilesClose).toBe(1)
  165. await filesTab.click({ button: 'right' })
  166. expect(await page.locator('[data-dockkit-tab-menu]:visible').count()).toBe(1)
  167. await page.keyboard.press('Escape')
  168. await addTab.waitFor({ state: 'visible' })
  169. const initialAdd = await addTab.count()
  170. expect(initialAdd).toBe(1)
  171. await addTab.click()
  172. await column.locator('[data-sidebar-right-guide]').waitFor({ state: 'visible' })
  173. await expect.poll(() => column.locator('[data-dockkit-tab]').count()).toBe(2)
  174. const guideTab = column.locator('[data-dockkit-tab]').filter({ hasNot: page.getByText('Files', { exact: true }) })
  175. const guideClose = await guideTab.locator('[data-dockkit-tab-close]').count()
  176. const filesCloseWithGuide = await filesTab.locator('[data-dockkit-tab-close]').count()
  177. expect(guideClose).toBe(1)
  178. expect(filesCloseWithGuide).toBe(1)
  179. await expect.poll(() => addTab.count()).toBe(0)
  180. const addWithGuide = await addTab.count()
  181. await guideTab.hover()
  182. await guideTab.locator('[data-dockkit-tab-close]').click()
  183. await column.locator('[data-files-state="tree"]').waitFor({ state: 'visible' })
  184. await expect.poll(() => column.locator('[data-sidebar-right-guide]').count()).toBe(0)
  185. await expect.poll(() => column.locator('[data-dockkit-tab]').count()).toBe(1)
  186. await addTab.waitFor({ state: 'visible' })
  187. const restoredFilesClose = await filesTab.locator('[data-dockkit-tab-close]').count()
  188. const restoredAdd = await addTab.count()
  189. expect(restoredFilesClose).toBe(1)
  190. expect(restoredAdd).toBe(1)
  191. const preview = column.locator('[data-document-preview]')
  192. const openFile = openPreviewFile.bind(undefined, column, filesTab, preview)
  193. const viewer = preview.locator('[data-document-viewer-menu]')
  194. const body = preview.locator('[data-textpreview-body]')
  195. const sections = ['# Document preview']
  196. sections.push([
  197. '## Sidebar tabs', '',
  198. `- Default tab: ${defaultTitle}`,
  199. `- Files close buttons (alone -> with guide -> restored): ${[initialFilesClose, filesCloseWithGuide, restoredFilesClose].join(' -> ')}`,
  200. `- Manual guide close buttons: ${guideClose}`,
  201. `- Add buttons (Files -> guide -> Files): ${[initialAdd, addWithGuide, restoredAdd].join(' -> ')}`,
  202. ].join('\n'))
  203. await openFile('smoke.md')
  204. await expect.poll(() => viewer.innerText()).toBe('Markdown')
  205. await preview.getByRole('heading', { name: 'Markdown smoke', exact: true }).waitFor({ timeout: 15_000 })
  206. expect(await preview.getByText('Rendered from the workspace.', { exact: true }).isVisible()).toBe(true)
  207. const heading = await preview.getByRole('heading', { name: 'Markdown smoke', exact: true }).innerText()
  208. const markdownTail = preview.getByRole('heading', { name: 'Markdown tail', exact: true })
  209. await expect.poll(() => preview.locator('[data-textpreview-more]').isEnabled()).toBe(true)
  210. expect(await markdownTail.count()).toBe(0)
  211. await scrollForNextPage(body)
  212. await markdownTail.waitFor({ timeout: 15_000 })
  213. await expect.poll(() => preview.locator('[data-textpreview-more]').count()).toBe(0)
  214. expect(await preview.getByRole('heading', { name: heading, exact: true }).count()).toBe(1)
  215. expect(await preview.getByText('Rendered from the workspace.', { exact: true }).count()).toBe(1)
  216. const tailHeading = await markdownTail.innerText()
  217. await preview.getByRole('heading', { name: heading, exact: true }).scrollIntoViewIfNeeded()
  218. await successShot(page, 'markdown')
  219. const markdownTab = column.locator('[data-dockkit-tab]').filter({ has: page.getByText('smoke.md', { exact: true }) })
  220. const markdownTabId = await markdownTab.getAttribute('data-dockkit-tab')
  221. expect(markdownTabId).not.toBeNull()
  222. const tabCount = await column.locator('[data-dockkit-tab]').count()
  223. const markdownViewers = [await viewer.innerText()]
  224. const sourceFonts: Array<{ fontSize: string; lineHeight: string }> = []
  225. for (const label of ['Code', 'Plain text']) {
  226. await viewer.click()
  227. await page.getByRole('menuitem', { name: label, exact: true }).click()
  228. await expect.poll(() => viewer.innerText()).toBe(label)
  229. if (label === 'Code') {
  230. await preview.locator('.shiki').waitFor({ timeout: 15_000 })
  231. expect(await preview.locator('.shiki').textContent()).toBe(markdownText)
  232. } else {
  233. await expect.poll(async () => (await preview.locator('[data-textpreview-line]').first().textContent())?.trim()).toBe('# Markdown smoke')
  234. }
  235. const source = label === 'Code' ? preview.locator('.shiki') : preview.locator('[data-textpreview-line]').first()
  236. sourceFonts.push(await source.evaluate((node) => {
  237. const style = getComputedStyle(node)
  238. return { fontSize: style.fontSize, lineHeight: style.lineHeight }
  239. }))
  240. expect(await markdownTab.getAttribute('data-dockkit-tab')).toBe(markdownTabId)
  241. expect(await column.locator('[data-dockkit-tab]').count()).toBe(tabCount)
  242. markdownViewers.push(await viewer.innerText())
  243. }
  244. expect(sourceFonts[1]).toEqual(sourceFonts[0])
  245. sections.push([
  246. '## Markdown', '',
  247. `- Heading: ${heading}`,
  248. `- Tail loaded by scrolling: ${tailHeading}`,
  249. `- Viewers: ${markdownViewers.join(' -> ')}`,
  250. `- Same tab: ${String(await markdownTab.getAttribute('data-dockkit-tab') === markdownTabId)}`,
  251. ].join('\n'))
  252. await openFile('smoke.html')
  253. await expect.poll(() => viewer.innerText()).toBe('HTML')
  254. const iframe = preview.locator('[data-html-preview]')
  255. await iframe.waitFor({ timeout: 15_000 })
  256. expect(await iframe.getAttribute('sandbox')).toBe('allow-scripts')
  257. expect(await iframe.evaluate((node) => {
  258. const host = node.closest('[data-textpreview-body]')
  259. if (!(host instanceof HTMLElement)) throw new Error('HTML preview body is unavailable')
  260. const outer = host.getBoundingClientRect()
  261. const frame = node.getBoundingClientRect()
  262. return {
  263. top: Math.round(frame.top - outer.top),
  264. right: Math.round(outer.right - frame.right),
  265. bottom: Math.round(outer.bottom - frame.bottom),
  266. left: Math.round(frame.left - outer.left),
  267. }
  268. })).toEqual({ top: 0, right: 0, bottom: 0, left: 0 })
  269. const html = page.frameLocator('[data-html-preview]')
  270. await html.getByRole('heading', { name: 'HTML smoke', exact: true }).waitFor({ timeout: 15_000 })
  271. await expect.poll(() => html.locator('#result').innerText()).toBe('INLINE_OK')
  272. await expect.poll(() => html.locator('#local-result').innerText()).toBe('LOCAL_JS_OK')
  273. await expect.poll(() => html.locator('#outside-result').innerText()).toBe('OUTSIDE_JS_OK')
  274. await expect.poll(() => html.locator('#local-result').evaluate(node => getComputedStyle(node).color)).toBe('rgb(12, 34, 56)')
  275. await expect.poll(() => html.locator('#parent-result').innerText()).toBe('parent-blocked')
  276. expect(await html.locator('#parent-result').getAttribute('data-error')).toBe('SecurityError')
  277. expect(await page.locator('html').getAttribute('data-document-preview-escape')).toBeNull()
  278. await successShot(page, 'html')
  279. sections.push([
  280. '## HTML', '',
  281. `- Viewer: ${await viewer.innerText()}`,
  282. `- Sandbox: ${await iframe.getAttribute('sandbox')}`,
  283. `- Inline script: ${await html.locator('#result').innerText()}`,
  284. `- Local script: ${await html.locator('#local-result').innerText()}`,
  285. `- Outside-workspace script: ${await html.locator('#outside-result').innerText()}`,
  286. `- Local stylesheet: ${await html.locator('#local-result').evaluate(node => getComputedStyle(node).color)}`,
  287. `- Parent access: ${await html.locator('#parent-result').innerText()} (${await html.locator('#parent-result').getAttribute('data-error')})`,
  288. `- Parent unchanged: ${String(await page.locator('html').getAttribute('data-document-preview-escape') === null)}`,
  289. ].join('\n'))
  290. await openFile('smoke.pdf')
  291. const canvas = preview.getByRole('img', { name: 'PDF page 1', exact: true })
  292. await canvas.waitFor({ state: 'visible', timeout: 30_000 })
  293. expect(await viewer.count()).toBe(0)
  294. expect(await preview.locator('[role="toolbar"]').count()).toBe(0)
  295. expect(await preview.locator('[data-pdf-page]').count()).toBe(2)
  296. await expect.poll(() => canvasColor(canvas), { timeout: 30_000 }).toBe('red')
  297. const firstColor = await canvasColor(canvas)
  298. expect(firstColor).toBe('red')
  299. const workerNames = await Promise.all(page.workers().map(worker => worker.evaluate(() => self.name)))
  300. expect(workerNames).toContain('dsh-pdf')
  301. await preview.locator('[data-pdf-page="2"]').scrollIntoViewIfNeeded()
  302. const secondPage = preview.getByRole('img', { name: 'PDF page 2', exact: true })
  303. await secondPage.waitFor({ state: 'visible', timeout: 30_000 })
  304. await expect.poll(() => canvasColor(secondPage), { timeout: 30_000 }).toBe('blue')
  305. const secondColor = await canvasColor(secondPage)
  306. expect(secondColor).toBe('blue')
  307. expect(await body.evaluate(node => node.scrollWidth <= node.clientWidth)).toBe(true)
  308. const pdfTab = column.locator('[data-dockkit-tab]').filter({ has: page.getByText('smoke.pdf', { exact: true }) })
  309. const pdfTabId = await pdfTab.getAttribute('data-dockkit-tab')
  310. expect(pdfTabId).not.toBeNull()
  311. await filesTab.click()
  312. await column.locator('[data-files-state="tree"]').waitFor({ state: 'visible' })
  313. await pdfTab.click()
  314. await preview.locator('[data-pdf-page="2"]').scrollIntoViewIfNeeded()
  315. await secondPage.waitFor({ state: 'visible', timeout: 30_000 })
  316. await expect.poll(() => canvasColor(secondPage), { timeout: 30_000 }).toBe('blue')
  317. const restoredColor = await canvasColor(secondPage)
  318. expect(restoredColor).toBe('blue')
  319. expect(await pdfTab.getAttribute('data-dockkit-tab')).toBe(pdfTabId)
  320. await copyPdfText(page, preview, 'Selectable PDF text')
  321. await successShot(page, 'pdf')
  322. sections.push([
  323. '## PDF', '',
  324. `- Viewer menu hidden: ${String(await viewer.count() === 0)}`,
  325. `- Worker: ${workerNames.find(name => name === 'dsh-pdf')}`,
  326. `- Continuous pages: ${await preview.locator('[data-pdf-page]').count()}`,
  327. `- Horizontal overflow: ${String(await body.evaluate(node => node.scrollWidth > node.clientWidth))}`,
  328. `- Canvas fills: ${[firstColor, secondColor, restoredColor].join(' -> ')}`,
  329. `- Same tab: ${String(await pdfTab.getAttribute('data-dockkit-tab') === pdfTabId)}`,
  330. '- Selected and copied text: Selectable PDF text',
  331. ].join('\n'))
  332. await openFile('user-unit.pdf')
  333. await preview.getByRole('img', { name: 'PDF page 1', exact: true }).waitFor({ state: 'visible' })
  334. await copyPdfText(page, preview, 'Selectable PDF text')
  335. sections.push('## PDF page units\n\n- UserUnit 2: selected and copied text aligns with the canvas')
  336. await openFile('selection.pdf')
  337. await preview.getByRole('img', { name: 'PDF page 1', exact: true }).waitFor({ state: 'visible' })
  338. const selectionLayer = preview.locator('.textLayer')
  339. const selectionText = selectionLayer.locator('span:not(.markedContent)')
  340. const titleText = selectionText.filter({ hasText: 'JOURNAL' })
  341. const priorityText = selectionText.filter({ hasText: 'HIGH / MEDIUM / LOW' }).first()
  342. // The text resize observer aligns the overlay after the canvas becomes visible.
  343. await titleText.waitFor({ state: 'visible' })
  344. await priorityText.waitFor({ state: 'visible' })
  345. const titleBox = await titleText.boundingBox()
  346. const priorityBox = await priorityText.boundingBox()
  347. if (titleBox === null || priorityBox === null) throw new Error('selection fixture text has no bounds')
  348. const start = { x: titleBox.x + 1, y: titleBox.y + titleBox.height / 2 }
  349. const end = { x: priorityBox.x + priorityBox.width / 2, y: priorityBox.y - 3 }
  350. const drag = async (from: typeof start, to: typeof end, through?: typeof end): Promise<string> => {
  351. await page.mouse.move(from.x, from.y)
  352. await page.mouse.down()
  353. try {
  354. if (through !== undefined) await page.mouse.move(through.x, through.y, { steps: 15 })
  355. await page.mouse.move(to.x, to.y, { steps: 15 })
  356. return await page.evaluate(() => window.getSelection()?.toString() ?? '')
  357. } finally { await page.mouse.up() }
  358. }
  359. const priority = { x: end.x, y: priorityBox.y + priorityBox.height / 2 }
  360. const forward = await drag(start, end, priority)
  361. expect(forward).toContain('JOURNAL')
  362. expect(forward).toContain('THREE TASKS')
  363. expect(forward).not.toContain('REFLECTION')
  364. expect(forward).not.toContain('AFTER TABLE')
  365. const backward = await drag(priority, start)
  366. expect(backward).toContain('THREE TASKS')
  367. expect(backward).not.toContain('REFLECTION')
  368. expect(backward).not.toContain('AFTER TABLE')
  369. expect(await selectionLayer.locator('br').first().evaluate(node => getComputedStyle(node, '::selection').backgroundColor))
  370. .toBe('rgba(0, 0, 0, 0)')
  371. await successShot(page, 'pdf-drag-selection')
  372. sections.push('## PDF drag selection\n\n- Table selection: forward and backward drags exclude later sections\n- Line-break highlight: transparent')
  373. await openFile('tiny.png')
  374. const tinyImage = preview.getByRole('img', { name: 'Image preview: tiny.png', exact: true })
  375. await tinyImage.waitFor({ state: 'visible', timeout: 15_000 })
  376. expect(await viewer.count()).toBe(0)
  377. expect(await tinyImage.evaluate(node => ({
  378. width: (node as HTMLImageElement).naturalWidth,
  379. height: (node as HTMLImageElement).naturalHeight,
  380. draggable: (node as HTMLImageElement).draggable,
  381. }))).toEqual({ width: 1, height: 1, draggable: false })
  382. const centering = await tinyImage.evaluate((node) => {
  383. const image = node.getBoundingClientRect()
  384. const scroller = node.closest('[data-textpreview-body]')?.getBoundingClientRect()
  385. if (scroller === undefined) throw new Error('image document scroller is unavailable')
  386. return {
  387. horizontal: Math.abs((image.left + image.width / 2) - (scroller.left + scroller.width / 2)),
  388. vertical: Math.abs((image.top + image.height / 2) - (scroller.top + scroller.height / 2)),
  389. }
  390. })
  391. expect(centering.horizontal).toBeLessThan(10)
  392. expect(centering.vertical).toBeLessThan(10)
  393. await openFile('large.svg')
  394. await expect.poll(() => viewer.innerText()).toBe('Image')
  395. await viewer.click()
  396. await page.getByRole('menuitem', { name: 'Plain text', exact: true }).waitFor({ timeout: 15_000 })
  397. await page.keyboard.press('Escape')
  398. const largeImage = preview.getByRole('img', { name: 'Image preview: large.svg', exact: true })
  399. await largeImage.waitFor({ state: 'visible', timeout: 15_000 })
  400. const fitted = await largeImage.evaluate((node) => {
  401. const image = node as HTMLImageElement
  402. const scroller = image.closest('[data-textpreview-body]')
  403. if (scroller === null) throw new Error('image document scroller is unavailable')
  404. const rect = image.getBoundingClientRect()
  405. return {
  406. naturalWidth: image.naturalWidth,
  407. naturalHeight: image.naturalHeight,
  408. width: rect.width,
  409. height: rect.height,
  410. paneWidth: scroller.clientWidth,
  411. paneHeight: scroller.clientHeight,
  412. }
  413. })
  414. expect(fitted).toMatchObject({ naturalWidth: 1200, naturalHeight: 1600 })
  415. expect(fitted.width).toBeLessThan(1200)
  416. // Width fit: the image fills the frame's 12px-inset box while the aspect ratio holds.
  417. expect(Math.abs((fitted.paneWidth - 24) - fitted.width)).toBeLessThanOrEqual(1)
  418. expect(fitted.height / fitted.width).toBeCloseTo(1600 / 1200, 2)
  419. const scrolled = await body.evaluate((node) => {
  420. node.scrollLeft = node.scrollWidth
  421. return { left: node.scrollLeft, horizontalOverflow: node.scrollWidth > node.clientWidth }
  422. })
  423. expect(scrolled).toEqual({ left: 0, horizontalOverflow: false })
  424. expect(await page.locator('html').getAttribute('data-image-preview-escape')).toBeNull()
  425. const releaseRead = Promise.withResolvers<undefined>()
  426. let waitingForRead = false
  427. const readPage = scaffold.ctx.workspaceFiles.read.bind(scaffold.ctx.workspaceFiles)
  428. const heldRead = vi.spyOn(scaffold.ctx.workspaceFiles, 'read').mockImplementation(async (agent, path, range, signal) => {
  429. if (path === 'pages.ts' && (range.offset ?? 1) === 1) {
  430. waitingForRead = true
  431. await releaseRead.promise
  432. }
  433. return readPage(agent, path, range, signal)
  434. })
  435. let initialReading = false
  436. try {
  437. await openFile('pages.ts')
  438. await expect.poll(() => waitingForRead).toBe(true)
  439. const reading = preview.locator('[data-document-loading]')
  440. initialReading = await reading.isVisible()
  441. expect(initialReading).toBe(true)
  442. expect(await preview.locator('[data-code-preview]').count()).toBe(0)
  443. const indicator = await reading.boundingBox()
  444. const scroller = await body.boundingBox()
  445. if (indicator === null || scroller === null) throw new Error('reading indicator or document body is not rendered')
  446. expect(indicator.y).toBeGreaterThanOrEqual(scroller.y)
  447. expect(indicator.y + indicator.height).toBeLessThanOrEqual(scroller.y + scroller.height)
  448. await successShot(page, 'code-reading')
  449. } finally {
  450. releaseRead.resolve(undefined)
  451. heldRead.mockRestore()
  452. }
  453. await expect.poll(() => viewer.innerText()).toBe('Code')
  454. const highlightedLines = preview.locator('.shiki .line')
  455. await expect.poll(() => highlightedLines.count(), { timeout: 15_000 }).toBe(PAGE_LINES)
  456. const codeBlock = preview.locator('.md-code-block')
  457. const codeScrollport = preview.locator('[data-code-block-content]')
  458. expect(await codeBlock.getAttribute('data-line-numbers')).toBe('true')
  459. await expect.poll(() => highlightedLines.first().evaluate(node => getComputedStyle(node, '::before').content))
  460. .not.toMatch(/^(?:none|normal)$/u)
  461. const numbering = await highlightedLines.first().evaluate((node) => {
  462. const line = getComputedStyle(node)
  463. const before = getComputedStyle(node, '::before')
  464. return {
  465. counterIncrement: line.counterIncrement,
  466. gutterWidth: Number.parseFloat(before.width),
  467. sourceInset: Number.parseFloat(line.paddingInlineStart),
  468. }
  469. })
  470. expect(numbering.counterIncrement).toBe('source-line 1')
  471. expect(numbering.gutterWidth).toBeGreaterThan(0)
  472. expect(numbering.sourceInset).toBeGreaterThan(numbering.gutterWidth)
  473. const prefix = await highlightedLines.allTextContents()
  474. expect(prefix).toEqual(codeLines.slice(0, PAGE_LINES))
  475. await expect.poll(() => preview.locator('[data-textpreview-more]').isEnabled()).toBe(true)
  476. await scrollForNextPage(codeScrollport)
  477. await expect.poll(() => highlightedLines.count(), { timeout: 15_000 }).toBe(codeLines.length)
  478. const completed = await highlightedLines.allTextContents()
  479. expect(completed).toEqual(codeLines)
  480. await expect.poll(() => preview.locator('[data-textpreview-more]').count()).toBe(0)
  481. const scrollTop = await codeScrollport.evaluate((node) => {
  482. const target = Math.floor((node.scrollHeight - node.clientHeight) / 2)
  483. if (target <= 0) throw new Error('code fixture does not overflow the document body')
  484. node.scrollTop = target
  485. return target
  486. })
  487. await expect.poll(() => codeScrollport.evaluate((node) => {
  488. const codeBlock = node.parentElement
  489. const banner = codeBlock?.firstElementChild
  490. const firstLine = node.querySelector('.shiki .line')
  491. if (!(banner instanceof HTMLElement) || firstLine === null) throw new Error('missing rendered code banner or source line')
  492. const bounds = node.getBoundingClientRect()
  493. const clipTop = bounds.top + node.clientTop
  494. const bannerBounds = banner.getBoundingClientRect()
  495. return {
  496. scrollTop: node.scrollTop,
  497. scrollportBelowBanner: Math.abs(bannerBounds.bottom - bounds.top) < 1,
  498. firstLineAbove: firstLine.getBoundingClientRect().top < clipTop,
  499. }
  500. })).toEqual({ scrollTop, scrollportBelowBanner: true, firstLineAbove: true })
  501. await page.context().grantPermissions(['clipboard-read', 'clipboard-write'], { origin: new URL(page.url()).origin })
  502. await page.evaluate(() => navigator.clipboard.writeText(''))
  503. await codeBlock.getByRole('button', { name: 'Copy', exact: true }).click()
  504. await expect.poll(() => page.evaluate(() => navigator.clipboard.readText())).toBe(codeLines.join('\n'))
  505. sections.push([
  506. '## Code paging', '',
  507. `- Viewer: ${await viewer.innerText()}`,
  508. `- Initial reading indicator: ${initialReading}`,
  509. `- Lines: ${prefix.length} -> ${completed.length}`,
  510. `- Prefix retained: ${String(JSON.stringify(completed.slice(0, prefix.length)) === JSON.stringify(prefix))}`,
  511. `- Tail: ${completed.at(-1)}`,
  512. ].join('\n'))
  513. const officeMenus: number[] = []
  514. const configurationGuide = 'Read failed: Office previews are unavailable. Enable the document preview service on the computer running DeepSeek Harness.'
  515. for (const extension of ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx']) {
  516. await openFile(`unavailable.${extension}`)
  517. expect(await preview.locator('[data-document-viewer-menu]').count()).toBe(0)
  518. await preview.getByText(configurationGuide, { exact: true }).waitFor({ timeout: 15_000 })
  519. expect(await preview.locator('[data-textpreview-line]').count()).toBe(0)
  520. expect(await preview.getByText('OFFICE_BINARY_PREVIEW', { exact: false }).count()).toBe(0)
  521. officeMenus.push(await viewer.count())
  522. }
  523. await successShot(page, 'office-unavailable')
  524. sections.push([
  525. '## Office unavailable', '',
  526. `- DOC, DOCX, XLS, XLSX, PPT, PPTX viewer menus: ${officeMenus.join(' | ')}`,
  527. `- Guidance: ${configurationGuide}`,
  528. '- Binary text shown: false',
  529. '- Plain-text option and viewer picker: hidden',
  530. ].join('\n'))
  531. await openFile('notes.unknown')
  532. const plainLines = preview.locator('[data-textpreview-line]')
  533. await expect.poll(() => plainLines.count()).toBe(2)
  534. // Plain text is the only candidate, so no viewer menu renders.
  535. expect(await viewer.count()).toBe(0)
  536. const fallback = (await plainLines.allTextContents()).map(line => line.trim())
  537. expect(fallback).toEqual(['UNKNOWN_SUFFIX', 'Plain fallback.'])
  538. sections.push(['## Unknown suffix', '', `- Viewer menu hidden: ${String(await viewer.count() === 0)}`, `- Text: ${fallback.join(' | ')}`].join('\n'))
  539. await filesTab.click()
  540. await column.locator('[data-files-entry="file"]').getByRole('button', { name: 'clip.mp4', exact: true }).click()
  541. const unsupported = column.locator('[data-textpreview-state="unsupported"]')
  542. await unsupported.waitFor({ timeout: 15_000 })
  543. const unsupportedLine = await unsupported.locator('[data-textpreview-unsupported]').innerText()
  544. expect(unsupportedLine).toContain('Preview is not available for this file type yet.')
  545. expect(await unsupported.locator('[data-textpreview-path]').innerText()).toContain('clip.mp4')
  546. expect(await unsupported.locator('[data-document-viewer-menu]').count()).toBe(0)
  547. expect(await unsupported.locator('[data-textpreview-tool="reload"]').count()).toBe(0)
  548. await successShot(page, 'unsupported')
  549. sections.push(['## Unviewable binary', '', '- State: unsupported', `- Line: ${unsupportedLine.trim()}`].join('\n'))
  550. expect(tripwire.pageErrors).toEqual([])
  551. expect(tripwire.warnings).toEqual([])
  552. await compareOrRefreshGolden(EXPECTED, sections.join('\n\n'), MODE)
  553. await assertFixtureInventory(SNAPSHOT_DIR, ['document.expected.md', 'paging.patch.yml'])
  554. })
  555. })
  556. describe.skipIf(MODE === 'record')('web e2e: Host Office preview', () => {
  557. let scaffold: WebScaffold
  558. let browser: Browser
  559. let page: Page
  560. afterAll(async () => {
  561. try { await browser?.close() } finally { await scaffold?.close() }
  562. })
  563. it('rejects renamed text and renders Chinese Office documents through the PDF worker', async () => {
  564. scaffold = await launchWebScaffold({ replayFixture: FIXTURE, paceMs: 5, compareReplaySession: false,
  565. extraOverlayPath: fileURLToPath(new URL('../../../packages/client/ui-sidebar-documentpreview/tests/fixtures/office-cache.patch.yml', import.meta.url)),
  566. })
  567. browser = await chromium.launch()
  568. page = await newEnglishPage(browser)
  569. const tripwire = watchConsole(page)
  570. await page.goto(scaffold.authenticatedUrl, { waitUntil: 'load' })
  571. await connectFreshWorkspace(page, scaffold.workspaceCwd)
  572. onTestFailed(async () => {
  573. await saveFailureShot(page, `screenshots/0908-document-preview/office-${process.pid}`)
  574. })
  575. const settled = scaffold.whenTurnSettled()
  576. const input = page.locator('[data-composer-input]').first()
  577. await input.fill(PROMPT)
  578. await input.press('Enter')
  579. const sessionId = await settled
  580. const cwd = scaffold.ctx.agents.get(sessionId)?.session.header.cwd
  581. if (cwd === undefined) throw new Error('settled Session has no workspace cwd')
  582. await Promise.all([
  583. writeFile(join(cwd, 'renamed.docx'), 'This is plain text renamed to docx.'),
  584. writeFile(join(cwd, 'chinese.docx'), realOfficeBytes('docx', 'DSH Missing Preview Font')),
  585. writeFile(join(cwd, 'chinese.xlsx'), realOfficeBytes('xlsx')),
  586. writeFile(join(cwd, 'chinese.pptx'), realOfficeBytes('pptx')),
  587. ...(['doc', 'xls', 'ppt'] as const).map(extension => writeFile(join(cwd, `chinese.${extension}`), realOfficeBytes(extension))),
  588. ...['doc', 'xls', 'ppt'].map(extension => writeFile(join(cwd, `renamed.${extension}`), 'Plain text is not a binary Office document.')),
  589. ])
  590. const convert = vi.spyOn(scaffold.ctx.officeToPdf, 'convert')
  591. try {
  592. const column = page.locator('[data-rightbar-col]')
  593. await page.locator('[data-sidebar-right-expand]').click()
  594. await column.locator('[data-sidebar-right-guide-entry="files"]').click()
  595. await column.locator('[data-files-state="tree"]').waitFor({ state: 'visible' })
  596. await column.locator('[data-files-reload]').click()
  597. const filesTab = column.locator('[data-dockkit-tab]').filter({ has: page.getByText('Files', { exact: true }) })
  598. const preview = column.locator('[data-document-preview]')
  599. await column.locator('[data-files-entry="file"]').getByRole('button', { name: 'chinese.docx', exact: true }).click()
  600. expect(await preview.locator('[data-document-viewer-menu]').count()).toBe(0)
  601. const canvas = preview.getByRole('img', { name: 'PDF page 1', exact: true })
  602. await canvas.waitFor({ state: 'visible', timeout: 60_000 })
  603. await expect.poll(() => canvas.evaluate((node) => {
  604. const canvas = node as HTMLCanvasElement
  605. const context = canvas.getContext('2d')
  606. if (context === null) return false
  607. const bytes = context.getImageData(0, 0, canvas.width, canvas.height).data
  608. for (let index = 0; index < bytes.length; index += 4) {
  609. if (bytes[index + 3] === 255 && bytes[index]! < 200 && bytes[index + 1]! < 200 && bytes[index + 2]! < 200) return true
  610. }
  611. return false
  612. }), { timeout: 30_000 }).toBe(true)
  613. const workerNames = await Promise.all(page.workers().map(worker => worker.evaluate(() => self.name)))
  614. expect(workerNames).toContain('dsh-pdf')
  615. expect(workerNames.some(name => /libreoffice|soffice/i.test(name))).toBe(false)
  616. await copyPdfText(page, preview, 'Office preview')
  617. await copyPdfText(page, preview, '中文文档')
  618. expect((await preview.locator('[data-pdf-text]').allTextContents()).join('')).toContain('中文文档')
  619. expect(convert).toHaveBeenCalledTimes(1)
  620. await preview.getByRole('button', { name: 'Read the file again', exact: true }).click()
  621. await canvas.waitFor({ state: 'visible' })
  622. expect(convert).toHaveBeenCalledTimes(1)
  623. const notice = preview.locator('[data-office-font-notice]')
  624. const more = notice.getByRole('button', { name: 'Show more', exact: true })
  625. await more.waitFor({ state: 'visible' })
  626. const before = await canvas.evaluate(node => node.getBoundingClientRect().top)
  627. await more.click()
  628. const details = page.getByRole('dialog', { name: 'Missing fonts', exact: true })
  629. await details.getByText('DSH Missing Preview Font', { exact: true }).waitFor({ state: 'visible' })
  630. await successShot(page, 'office-font-details')
  631. await page.keyboard.press('Escape')
  632. await expect.poll(() => details.count()).toBe(0)
  633. expect(await more.evaluate(node => node === document.activeElement)).toBe(true)
  634. await more.click()
  635. await page.getByRole('button', { name: 'Close font details', exact: true }).click()
  636. expect(await more.isVisible()).toBe(true)
  637. await notice.getByRole('button', { name: 'Dismiss font notice', exact: true }).click()
  638. await expect.poll(() => notice.evaluate(node => node.getBoundingClientRect().height)).toBe(0)
  639. const after = await canvas.evaluate(node => node.getBoundingClientRect().top)
  640. expect(before - after).toBeGreaterThan(40)
  641. const topInset = await preview.evaluate((node) => {
  642. const body = node.querySelector('[data-textpreview-body]')!.getBoundingClientRect()
  643. const canvas = node.querySelector('canvas')!.getBoundingClientRect()
  644. return canvas.top - body.top
  645. })
  646. expect(topInset).toBe(0)
  647. await successShot(page, 'office-font-dismissed')
  648. await compareOrRefreshGolden(fileURLToPath(new URL('./expected/office-font-notice.md', import.meta.url)), [
  649. '# Office font notice', '',
  650. '- Requested absent family is listed: true',
  651. '- Escape restores focus to Show more: true',
  652. '- Closing details preserves the notice: true',
  653. '- Dismissing the notice collapses its occupied height: 0',
  654. `- Document top inset after dismissal: ${topInset}px`,
  655. ].join('\n'), MODE)
  656. await successShot(page, 'office-docx')
  657. for (const extension of ['doc', 'xls', 'xlsx', 'ppt', 'pptx']) {
  658. await openPreviewFile(column, filesTab, preview, `chinese.${extension}`)
  659. await preview.getByRole('img', { name: 'PDF page 1', exact: true }).waitFor({ state: 'visible', timeout: 60_000 })
  660. await expect.poll(async () => (await preview.locator('[data-pdf-text]').allTextContents()).join(''), { timeout: 30_000 }).toContain('中文文档')
  661. await successShot(page, `office-${extension}`)
  662. }
  663. expect(convert).toHaveBeenCalledTimes(6)
  664. await openPreviewFile(column, filesTab, preview, 'chinese.docx')
  665. await preview.getByRole('img', { name: 'PDF page 1', exact: true }).waitFor({ state: 'visible' })
  666. await preview.getByRole('button', { name: 'Read the file again', exact: true }).click()
  667. await expect.poll(() => convert.mock.calls.length).toBe(7)
  668. await preview.getByRole('img', { name: 'PDF page 1', exact: true }).waitFor({ state: 'visible' })
  669. await openPreviewFile(column, filesTab, preview, 'renamed.docx')
  670. await preview.getByText('Read failed: This Office file cannot be previewed. It may be damaged, password protected, or have the wrong extension.', { exact: true }).waitFor({ timeout: 30_000 })
  671. expect(await preview.locator('[data-textpreview-line]').count()).toBe(0)
  672. await successShot(page, 'office-invalid')
  673. expect(convert).toHaveBeenCalledTimes(8)
  674. for (const extension of ['doc', 'xls', 'ppt']) {
  675. await openPreviewFile(column, filesTab, preview, `renamed.${extension}`)
  676. await preview.getByText('Read failed: This Office file cannot be previewed. It may be damaged, password protected, or have the wrong extension.', { exact: true }).waitFor({ timeout: 30_000 })
  677. expect(await preview.locator('[data-textpreview-line]').count()).toBe(0)
  678. }
  679. expect(convert).toHaveBeenCalledTimes(11)
  680. expect(tripwire.pageErrors).toEqual([])
  681. } finally { convert.mockRestore() }
  682. })
  683. })