document-preview.e2e.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 { pdfFixture } from '../../../packages/client/ui-sidebar-documentpreview/tests/pdf-fixture.ts'
  10. import { assertFixtureInventory, compareOrRefreshGolden, launchWebScaffold, watchConsole, webSnapshotMode, type WebScaffold } from './scaffold.ts'
  11. import { connectFreshWorkspace, newEnglishPage, saveFailureShot } from './support.ts'
  12. const FIXTURE = fileURLToPath(new URL('../../../snapshots/web/lifecycle-chrome/session.v3.jsonl', import.meta.url))
  13. const SNAPSHOT_DIR = fileURLToPath(new URL('../../../snapshots/web/document-preview', import.meta.url))
  14. const EXPECTED = join(SNAPSHOT_DIR, 'document.expected.md')
  15. const PAGING_PATCH = join(SNAPSHOT_DIR, 'paging.patch.yml')
  16. const PAGE_LINES = 64
  17. const SHOT_DIR = fileURLToPath(new URL('../../../.artifacts/screenshots/0908-document-preview', import.meta.url))
  18. const PROMPT = 'Reply with the single word LIGHTHOUSE and stop.'
  19. const MODE = webSnapshotMode()
  20. const TINY_PNG = Buffer.from(
  21. 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=',
  22. 'base64',
  23. )
  24. /** Successful render evidence stays outside the committed snapshot inventory. */
  25. async function successShot(page: Page, name: string): Promise<void> {
  26. await mkdir(SHOT_DIR, { recursive: true })
  27. await page.screenshot({ path: join(SHOT_DIR, `${name}-${MODE}-${process.pid}.png`), fullPage: true })
  28. }
  29. /** Trigger the document owner's native scroll handler after a real first page overflows. */
  30. async function scrollForNextPage(body: Locator): Promise<void> {
  31. await body.evaluate((node) => {
  32. if (node.scrollHeight <= node.clientHeight) throw new Error('paging fixture does not overflow the document body')
  33. node.scrollTop = node.scrollHeight
  34. })
  35. }
  36. /** Read the solid vector fill away from antialiased page edges. */
  37. async function canvasColor(canvas: Locator): Promise<string> {
  38. return await canvas.evaluate((node) => {
  39. const surface = node as HTMLCanvasElement
  40. const context = surface.getContext('2d')
  41. if (context === null) throw new Error('PDF canvas has no 2D context')
  42. const pixel = context.getImageData(Math.floor(surface.width / 2), Math.floor(surface.height / 2), 1, 1).data
  43. if (Number(pixel[3]) !== 255) return 'transparent'
  44. if (Number(pixel[0]) - Number(pixel[2]) > 150) return 'red'
  45. if (Number(pixel[2]) - Number(pixel[0]) > 150) return 'blue'
  46. return 'other'
  47. })
  48. }
  49. describe.skipIf(MODE === 'record')('web e2e: document preview through Files', () => {
  50. let scaffold: WebScaffold
  51. let browser: Browser
  52. let page: Page
  53. let tripwire: ReturnType<typeof watchConsole>
  54. let outsideRoot: string | undefined
  55. beforeAll(async () => {
  56. outsideRoot = await mkdtemp(join(tmpdir(), 'dsh-preview-outside-'))
  57. scaffold = await launchWebScaffold({ replayFixture: FIXTURE, paceMs: 5, compareReplaySession: false, extraOverlayPath: PAGING_PATCH })
  58. browser = await chromium.launch()
  59. page = await newEnglishPage(browser)
  60. tripwire = watchConsole(page)
  61. await page.goto(scaffold.authenticatedUrl, { waitUntil: 'load' })
  62. await connectFreshWorkspace(page, scaffold.workspaceCwd)
  63. })
  64. afterAll(async () => {
  65. try {
  66. await browser?.close()
  67. } finally {
  68. try {
  69. await scaffold?.close()
  70. } finally {
  71. if (outsideRoot !== undefined) await rm(outsideRoot, { recursive: true, force: true })
  72. }
  73. }
  74. })
  75. it('opens text, isolated HTML, intrinsic images, and rendered PDF from the Session workspace', async () => {
  76. onTestFailed(async () => {
  77. await mkdir(SHOT_DIR, { recursive: true })
  78. await saveFailureShot(page, `screenshots/0908-document-preview/smoke-${process.pid}`)
  79. })
  80. const settled = scaffold.whenTurnSettled()
  81. const input = page.locator('[data-composer-input]').first()
  82. await input.fill(PROMPT)
  83. await input.press('Enter')
  84. const sessionId = await settled
  85. await page.getByText('LIGHTHOUSE', { exact: true }).waitFor({ timeout: 15_000 })
  86. const cwd = scaffold.ctx.agents.get(sessionId)?.session.header.cwd
  87. if (cwd === undefined) throw new Error('settled Session has no workspace cwd')
  88. if (outsideRoot === undefined) throw new Error('outside fixture directory is unavailable')
  89. const outsideScript = join(outsideRoot, 'outside.js')
  90. const outsideReference = relative(cwd, outsideScript).replace(/\\/g, '/')
  91. const markdownText = [
  92. '# Markdown smoke', '', 'Rendered from the workspace.', '',
  93. ...Array.from({ length: (PAGE_LINES - 4) / 2 }, (_, index) => [`Paragraph ${index + 1}: ${'visible prefix '.repeat(20)}`, '']).flat(),
  94. '# Markdown tail',
  95. ].join('\n')
  96. const codeLines = [
  97. ...Array.from({ length: PAGE_LINES }, (_, index) => index === 0 ? 'const prefix = "CODE_PREFIX";' : `// prefix line ${index + 1}`),
  98. 'const tail = "CODE_TAIL";',
  99. ]
  100. await Promise.all([
  101. writeFile(join(cwd, 'smoke.md'), markdownText),
  102. writeFile(join(cwd, 'pages.ts'), codeLines.join('\n')),
  103. writeFile(join(cwd, 'notes.unknown'), 'UNKNOWN_SUFFIX\nPlain fallback.'),
  104. writeFile(join(cwd, 'smoke.html'), [
  105. '<!doctype html><link rel="stylesheet" href="./local.css">',
  106. '<h1>HTML smoke</h1><p id="result">pending</p><p id="local-result">pending</p><p id="parent-result">pending</p>',
  107. '<p id="outside-result">pending</p>',
  108. '<script>document.getElementById("result").textContent="INLINE_OK";',
  109. 'try{parent.document.documentElement.setAttribute("data-document-preview-escape","true");document.getElementById("parent-result").textContent="parent-accessible"}',
  110. 'catch(error){const result=document.getElementById("parent-result");result.textContent="parent-blocked";result.dataset.error=error.name}</script>',
  111. '<script src="./local.js"></script>',
  112. `<script src="${outsideReference}"></script>`,
  113. ].join('\n')),
  114. writeFile(join(cwd, 'local.js'), 'document.getElementById("local-result").textContent="LOCAL_JS_OK";'),
  115. writeFile(join(cwd, 'local.css'), '#local-result { color: rgb(12, 34, 56); }'),
  116. writeFile(outsideScript, 'document.getElementById("outside-result").textContent="OUTSIDE_JS_OK";'),
  117. writeFile(join(cwd, 'tiny.png'), TINY_PNG),
  118. writeFile(join(cwd, 'large.svg'), [
  119. '<svg xmlns="http://www.w3.org/2000/svg" width="1200" height="1600" viewBox="0 0 1200 1600">',
  120. '<script>parent.document.documentElement.setAttribute("data-image-preview-escape","true")</script>',
  121. '<rect width="1200" height="1600" fill="#2463eb"/>',
  122. '</svg>',
  123. ].join('')),
  124. writeFile(join(cwd, 'smoke.pdf'), pdfFixture()),
  125. ])
  126. const column = page.locator('[data-rightbar-col]')
  127. await page.locator('[data-sidebar-right-expand]').click()
  128. await column.locator('[data-files-state="tree"]').waitFor({ state: 'visible' })
  129. await column.locator('[data-files-reload]').click()
  130. const filesTab = column.locator('[data-dockkit-tab]').filter({ has: page.getByText('Files', { exact: true }) })
  131. const addTab = column.locator('[data-dockkit-add-tab]')
  132. expect(await column.locator('[data-dockkit-tab]').count()).toBe(1)
  133. const defaultTitle = await filesTab.locator('[data-dockkit-tab-title]').innerText()
  134. const initialFilesClose = await filesTab.locator('[data-dockkit-tab-close]').count()
  135. expect(initialFilesClose).toBe(1)
  136. await filesTab.click({ button: 'right' })
  137. expect(await page.locator('[data-dockkit-tab-menu]:visible').count()).toBe(1)
  138. await page.keyboard.press('Escape')
  139. await addTab.waitFor({ state: 'visible' })
  140. const initialAdd = await addTab.count()
  141. expect(initialAdd).toBe(1)
  142. await addTab.click()
  143. await column.locator('[data-sidebar-right-guide]').waitFor({ state: 'visible' })
  144. await expect.poll(() => column.locator('[data-dockkit-tab]').count()).toBe(2)
  145. const guideTab = column.locator('[data-dockkit-tab]').filter({ hasNot: page.getByText('Files', { exact: true }) })
  146. const guideClose = await guideTab.locator('[data-dockkit-tab-close]').count()
  147. const filesCloseWithGuide = await filesTab.locator('[data-dockkit-tab-close]').count()
  148. expect(guideClose).toBe(1)
  149. expect(filesCloseWithGuide).toBe(1)
  150. await expect.poll(() => addTab.count()).toBe(0)
  151. const addWithGuide = await addTab.count()
  152. await guideTab.hover()
  153. await guideTab.locator('[data-dockkit-tab-close]').click()
  154. await column.locator('[data-files-state="tree"]').waitFor({ state: 'visible' })
  155. await expect.poll(() => column.locator('[data-sidebar-right-guide]').count()).toBe(0)
  156. await expect.poll(() => column.locator('[data-dockkit-tab]').count()).toBe(1)
  157. await addTab.waitFor({ state: 'visible' })
  158. const restoredFilesClose = await filesTab.locator('[data-dockkit-tab-close]').count()
  159. const restoredAdd = await addTab.count()
  160. expect(restoredFilesClose).toBe(1)
  161. expect(restoredAdd).toBe(1)
  162. const preview = column.locator('[data-document-preview]')
  163. const openFile = async (name: string): Promise<void> => {
  164. await filesTab.click()
  165. await column.locator('[data-files-entry="file"]').getByRole('button', { name, exact: true }).click()
  166. await expect.poll(async () => (await preview.getAttribute('data-textpreview-url'))?.endsWith(`/${name}`)).toBe(true)
  167. }
  168. const viewer = preview.locator('[data-document-viewer-menu]')
  169. const body = preview.locator('[data-textpreview-body]')
  170. const sections = ['# Document preview']
  171. sections.push([
  172. '## Sidebar tabs', '',
  173. `- Default tab: ${defaultTitle}`,
  174. `- Files close buttons (alone -> with guide -> restored): ${[initialFilesClose, filesCloseWithGuide, restoredFilesClose].join(' -> ')}`,
  175. `- Manual guide close buttons: ${guideClose}`,
  176. `- Add buttons (Files -> guide -> Files): ${[initialAdd, addWithGuide, restoredAdd].join(' -> ')}`,
  177. ].join('\n'))
  178. await openFile('smoke.md')
  179. await expect.poll(() => viewer.innerText()).toBe('Markdown')
  180. await preview.getByRole('heading', { name: 'Markdown smoke', exact: true }).waitFor({ timeout: 15_000 })
  181. expect(await preview.getByText('Rendered from the workspace.', { exact: true }).isVisible()).toBe(true)
  182. const heading = await preview.getByRole('heading', { name: 'Markdown smoke', exact: true }).innerText()
  183. const markdownTail = preview.getByRole('heading', { name: 'Markdown tail', exact: true })
  184. await expect.poll(() => preview.locator('[data-textpreview-more]').isEnabled()).toBe(true)
  185. expect(await markdownTail.count()).toBe(0)
  186. await scrollForNextPage(body)
  187. await markdownTail.waitFor({ timeout: 15_000 })
  188. await expect.poll(() => preview.locator('[data-textpreview-more]').count()).toBe(0)
  189. expect(await preview.getByRole('heading', { name: heading, exact: true }).count()).toBe(1)
  190. expect(await preview.getByText('Rendered from the workspace.', { exact: true }).count()).toBe(1)
  191. const tailHeading = await markdownTail.innerText()
  192. await preview.getByRole('heading', { name: heading, exact: true }).scrollIntoViewIfNeeded()
  193. await successShot(page, 'markdown')
  194. const markdownTab = column.locator('[data-dockkit-tab]').filter({ has: page.getByText('smoke.md', { exact: true }) })
  195. const markdownTabId = await markdownTab.getAttribute('data-dockkit-tab')
  196. expect(markdownTabId).not.toBeNull()
  197. const tabCount = await column.locator('[data-dockkit-tab]').count()
  198. const markdownViewers = [await viewer.innerText()]
  199. const sourceFonts: Array<{ fontSize: string; lineHeight: string }> = []
  200. for (const label of ['Code', 'Plain text']) {
  201. await viewer.click()
  202. await page.getByRole('menuitem', { name: label, exact: true }).click()
  203. await expect.poll(() => viewer.innerText()).toBe(label)
  204. if (label === 'Code') {
  205. await preview.locator('.shiki').waitFor({ timeout: 15_000 })
  206. expect(await preview.locator('.shiki').textContent()).toBe(markdownText)
  207. } else {
  208. await expect.poll(async () => (await preview.locator('[data-textpreview-line]').first().textContent())?.trim()).toBe('# Markdown smoke')
  209. }
  210. const source = label === 'Code' ? preview.locator('.shiki') : preview.locator('[data-textpreview-line]').first()
  211. sourceFonts.push(await source.evaluate((node) => {
  212. const style = getComputedStyle(node)
  213. return { fontSize: style.fontSize, lineHeight: style.lineHeight }
  214. }))
  215. expect(await markdownTab.getAttribute('data-dockkit-tab')).toBe(markdownTabId)
  216. expect(await column.locator('[data-dockkit-tab]').count()).toBe(tabCount)
  217. markdownViewers.push(await viewer.innerText())
  218. }
  219. expect(sourceFonts[1]).toEqual(sourceFonts[0])
  220. sections.push([
  221. '## Markdown', '',
  222. `- Heading: ${heading}`,
  223. `- Tail loaded by scrolling: ${tailHeading}`,
  224. `- Viewers: ${markdownViewers.join(' -> ')}`,
  225. `- Same tab: ${String(await markdownTab.getAttribute('data-dockkit-tab') === markdownTabId)}`,
  226. ].join('\n'))
  227. await openFile('smoke.html')
  228. await expect.poll(() => viewer.innerText()).toBe('HTML')
  229. const iframe = preview.locator('[data-html-preview]')
  230. await iframe.waitFor({ timeout: 15_000 })
  231. expect(await iframe.getAttribute('sandbox')).toBe('allow-scripts')
  232. expect(await iframe.evaluate((node) => {
  233. const host = node.closest('[data-textpreview-body]')
  234. if (!(host instanceof HTMLElement)) throw new Error('HTML preview body is unavailable')
  235. const outer = host.getBoundingClientRect()
  236. const frame = node.getBoundingClientRect()
  237. return {
  238. top: Math.round(frame.top - outer.top),
  239. right: Math.round(outer.right - frame.right),
  240. bottom: Math.round(outer.bottom - frame.bottom),
  241. left: Math.round(frame.left - outer.left),
  242. }
  243. })).toEqual({ top: 0, right: 0, bottom: 0, left: 0 })
  244. const html = page.frameLocator('[data-html-preview]')
  245. await html.getByRole('heading', { name: 'HTML smoke', exact: true }).waitFor({ timeout: 15_000 })
  246. await expect.poll(() => html.locator('#result').innerText()).toBe('INLINE_OK')
  247. await expect.poll(() => html.locator('#local-result').innerText()).toBe('LOCAL_JS_OK')
  248. await expect.poll(() => html.locator('#outside-result').innerText()).toBe('OUTSIDE_JS_OK')
  249. await expect.poll(() => html.locator('#local-result').evaluate(node => getComputedStyle(node).color)).toBe('rgb(12, 34, 56)')
  250. await expect.poll(() => html.locator('#parent-result').innerText()).toBe('parent-blocked')
  251. expect(await html.locator('#parent-result').getAttribute('data-error')).toBe('SecurityError')
  252. expect(await page.locator('html').getAttribute('data-document-preview-escape')).toBeNull()
  253. await successShot(page, 'html')
  254. sections.push([
  255. '## HTML', '',
  256. `- Viewer: ${await viewer.innerText()}`,
  257. `- Sandbox: ${await iframe.getAttribute('sandbox')}`,
  258. `- Inline script: ${await html.locator('#result').innerText()}`,
  259. `- Local script: ${await html.locator('#local-result').innerText()}`,
  260. `- Outside-workspace script: ${await html.locator('#outside-result').innerText()}`,
  261. `- Local stylesheet: ${await html.locator('#local-result').evaluate(node => getComputedStyle(node).color)}`,
  262. `- Parent access: ${await html.locator('#parent-result').innerText()} (${await html.locator('#parent-result').getAttribute('data-error')})`,
  263. `- Parent unchanged: ${String(await page.locator('html').getAttribute('data-document-preview-escape') === null)}`,
  264. ].join('\n'))
  265. await openFile('smoke.pdf')
  266. await expect.poll(() => viewer.innerText()).toBe('PDF')
  267. const canvas = preview.getByRole('img', { name: 'PDF page 1', exact: true })
  268. await canvas.waitFor({ state: 'visible', timeout: 30_000 })
  269. expect(await preview.locator('[role="toolbar"]').count()).toBe(0)
  270. expect(await preview.locator('[data-pdf-page]').count()).toBe(2)
  271. await expect.poll(() => canvasColor(canvas), { timeout: 30_000 }).toBe('red')
  272. const firstColor = await canvasColor(canvas)
  273. expect(firstColor).toBe('red')
  274. const workerNames = await Promise.all(page.workers().map(worker => worker.evaluate(() => self.name)))
  275. expect(workerNames).toContain('dsh-pdf')
  276. await preview.locator('[data-pdf-page="2"]').scrollIntoViewIfNeeded()
  277. const secondPage = preview.getByRole('img', { name: 'PDF page 2', exact: true })
  278. await secondPage.waitFor({ state: 'visible', timeout: 30_000 })
  279. await expect.poll(() => canvasColor(secondPage), { timeout: 30_000 }).toBe('blue')
  280. const secondColor = await canvasColor(secondPage)
  281. expect(secondColor).toBe('blue')
  282. expect(await body.evaluate(node => node.scrollWidth <= node.clientWidth)).toBe(true)
  283. const pdfTab = column.locator('[data-dockkit-tab]').filter({ has: page.getByText('smoke.pdf', { exact: true }) })
  284. const pdfTabId = await pdfTab.getAttribute('data-dockkit-tab')
  285. expect(pdfTabId).not.toBeNull()
  286. await filesTab.click()
  287. await column.locator('[data-files-state="tree"]').waitFor({ state: 'visible' })
  288. await pdfTab.click()
  289. await preview.locator('[data-pdf-page="2"]').scrollIntoViewIfNeeded()
  290. await secondPage.waitFor({ state: 'visible', timeout: 30_000 })
  291. await expect.poll(() => canvasColor(secondPage), { timeout: 30_000 }).toBe('blue')
  292. const restoredColor = await canvasColor(secondPage)
  293. expect(restoredColor).toBe('blue')
  294. expect(await pdfTab.getAttribute('data-dockkit-tab')).toBe(pdfTabId)
  295. await successShot(page, 'pdf')
  296. sections.push([
  297. '## PDF', '',
  298. `- Viewer: ${await viewer.innerText()}`,
  299. `- Worker: ${workerNames.find(name => name === 'dsh-pdf')}`,
  300. `- Continuous pages: ${await preview.locator('[data-pdf-page]').count()}`,
  301. `- Horizontal overflow: ${String(await body.evaluate(node => node.scrollWidth > node.clientWidth))}`,
  302. `- Canvas fills: ${[firstColor, secondColor, restoredColor].join(' -> ')}`,
  303. `- Same tab: ${String(await pdfTab.getAttribute('data-dockkit-tab') === pdfTabId)}`,
  304. ].join('\n'))
  305. await openFile('tiny.png')
  306. await expect.poll(() => viewer.innerText()).toBe('Image')
  307. const tinyImage = preview.getByRole('img', { name: 'Image preview: tiny.png', exact: true })
  308. await tinyImage.waitFor({ state: 'visible', timeout: 15_000 })
  309. expect(await tinyImage.evaluate(node => ({
  310. width: (node as HTMLImageElement).naturalWidth,
  311. height: (node as HTMLImageElement).naturalHeight,
  312. draggable: (node as HTMLImageElement).draggable,
  313. }))).toEqual({ width: 1, height: 1, draggable: false })
  314. const centering = await tinyImage.evaluate((node) => {
  315. const image = node.getBoundingClientRect()
  316. const scroller = node.closest('[data-textpreview-body]')?.getBoundingClientRect()
  317. if (scroller === undefined) throw new Error('image document scroller is unavailable')
  318. return {
  319. horizontal: Math.abs((image.left + image.width / 2) - (scroller.left + scroller.width / 2)),
  320. vertical: Math.abs((image.top + image.height / 2) - (scroller.top + scroller.height / 2)),
  321. }
  322. })
  323. expect(centering.horizontal).toBeLessThan(10)
  324. expect(centering.vertical).toBeLessThan(10)
  325. await openFile('large.svg')
  326. await expect.poll(() => viewer.innerText()).toBe('Image')
  327. const largeImage = preview.getByRole('img', { name: 'Image preview: large.svg', exact: true })
  328. await largeImage.waitFor({ state: 'visible', timeout: 15_000 })
  329. expect(await largeImage.evaluate(node => ({
  330. naturalWidth: (node as HTMLImageElement).naturalWidth,
  331. naturalHeight: (node as HTMLImageElement).naturalHeight,
  332. width: getComputedStyle(node).width,
  333. height: getComputedStyle(node).height,
  334. }))).toEqual({ naturalWidth: 1200, naturalHeight: 1600, width: '1200px', height: '1600px' })
  335. expect(await body.evaluate(node => ({
  336. horizontal: node.scrollWidth > node.clientWidth,
  337. vertical: node.scrollHeight > node.clientHeight,
  338. }))).toEqual({ horizontal: true, vertical: true })
  339. const scrolled = await body.evaluate((node) => {
  340. node.scrollLeft = node.scrollWidth
  341. node.scrollTop = node.scrollHeight
  342. return { left: node.scrollLeft, top: node.scrollTop }
  343. })
  344. expect(scrolled.left).toBeGreaterThan(0)
  345. expect(scrolled.top).toBeGreaterThan(0)
  346. expect(await page.locator('html').getAttribute('data-image-preview-escape')).toBeNull()
  347. const releaseRead = Promise.withResolvers<undefined>()
  348. let waitingForRead = false
  349. const readPage = scaffold.ctx.workspaceFiles.read.bind(scaffold.ctx.workspaceFiles)
  350. const heldRead = vi.spyOn(scaffold.ctx.workspaceFiles, 'read').mockImplementation(async (agent, path, range, signal) => {
  351. if (path === 'pages.ts' && (range.offset ?? 1) === 1) {
  352. waitingForRead = true
  353. await releaseRead.promise
  354. }
  355. return readPage(agent, path, range, signal)
  356. })
  357. let initialReading = false
  358. try {
  359. await openFile('pages.ts')
  360. await expect.poll(() => waitingForRead).toBe(true)
  361. const reading = preview.locator('[data-document-loading]')
  362. initialReading = await reading.isVisible()
  363. expect(initialReading).toBe(true)
  364. expect(await preview.locator('[data-code-preview]').count()).toBe(0)
  365. const indicator = await reading.boundingBox()
  366. const scroller = await body.boundingBox()
  367. if (indicator === null || scroller === null) throw new Error('reading indicator or document body is not rendered')
  368. expect(indicator.y).toBeGreaterThanOrEqual(scroller.y)
  369. expect(indicator.y + indicator.height).toBeLessThanOrEqual(scroller.y + scroller.height)
  370. await successShot(page, 'code-reading')
  371. } finally {
  372. releaseRead.resolve(undefined)
  373. heldRead.mockRestore()
  374. }
  375. await expect.poll(() => viewer.innerText()).toBe('Code')
  376. const highlightedLines = preview.locator('.shiki .line')
  377. await expect.poll(() => highlightedLines.count(), { timeout: 15_000 }).toBe(PAGE_LINES)
  378. const codeBlock = preview.locator('.md-code-block')
  379. const codeScrollport = preview.locator('[data-code-block-content]')
  380. expect(await codeBlock.getAttribute('data-line-numbers')).toBe('true')
  381. await expect.poll(() => highlightedLines.first().evaluate(node => getComputedStyle(node, '::before').content))
  382. .not.toMatch(/^(?:none|normal)$/u)
  383. const numbering = await highlightedLines.first().evaluate((node) => {
  384. const line = getComputedStyle(node)
  385. const before = getComputedStyle(node, '::before')
  386. return {
  387. counterIncrement: line.counterIncrement,
  388. gutterWidth: Number.parseFloat(before.width),
  389. sourceInset: Number.parseFloat(line.paddingInlineStart),
  390. }
  391. })
  392. expect(numbering.counterIncrement).toBe('source-line 1')
  393. expect(numbering.gutterWidth).toBeGreaterThan(0)
  394. expect(numbering.sourceInset).toBeGreaterThan(numbering.gutterWidth)
  395. const prefix = await highlightedLines.allTextContents()
  396. expect(prefix).toEqual(codeLines.slice(0, PAGE_LINES))
  397. await expect.poll(() => preview.locator('[data-textpreview-more]').isEnabled()).toBe(true)
  398. await scrollForNextPage(codeScrollport)
  399. await expect.poll(() => highlightedLines.count(), { timeout: 15_000 }).toBe(codeLines.length)
  400. const completed = await highlightedLines.allTextContents()
  401. expect(completed).toEqual(codeLines)
  402. await expect.poll(() => preview.locator('[data-textpreview-more]').count()).toBe(0)
  403. const scrollTop = await codeScrollport.evaluate((node) => {
  404. const target = Math.floor((node.scrollHeight - node.clientHeight) / 2)
  405. if (target <= 0) throw new Error('code fixture does not overflow the document body')
  406. node.scrollTop = target
  407. return target
  408. })
  409. await expect.poll(() => codeScrollport.evaluate((node) => {
  410. const codeBlock = node.parentElement
  411. const banner = codeBlock?.firstElementChild
  412. const firstLine = node.querySelector('.shiki .line')
  413. if (!(banner instanceof HTMLElement) || firstLine === null) throw new Error('missing rendered code banner or source line')
  414. const bounds = node.getBoundingClientRect()
  415. const clipTop = bounds.top + node.clientTop
  416. const bannerBounds = banner.getBoundingClientRect()
  417. return {
  418. scrollTop: node.scrollTop,
  419. scrollportBelowBanner: Math.abs(bannerBounds.bottom - bounds.top) < 1,
  420. firstLineAbove: firstLine.getBoundingClientRect().top < clipTop,
  421. }
  422. })).toEqual({ scrollTop, scrollportBelowBanner: true, firstLineAbove: true })
  423. await page.context().grantPermissions(['clipboard-read', 'clipboard-write'], { origin: new URL(page.url()).origin })
  424. await page.evaluate(() => navigator.clipboard.writeText(''))
  425. await codeBlock.getByRole('button', { name: 'Copy', exact: true }).click()
  426. await expect.poll(() => page.evaluate(() => navigator.clipboard.readText())).toBe(codeLines.join('\n'))
  427. sections.push([
  428. '## Code paging', '',
  429. `- Viewer: ${await viewer.innerText()}`,
  430. `- Initial reading indicator: ${initialReading}`,
  431. `- Lines: ${prefix.length} -> ${completed.length}`,
  432. `- Prefix retained: ${String(JSON.stringify(completed.slice(0, prefix.length)) === JSON.stringify(prefix))}`,
  433. `- Tail: ${completed.at(-1)}`,
  434. ].join('\n'))
  435. await openFile('notes.unknown')
  436. await expect.poll(() => viewer.innerText()).toBe('Plain text')
  437. const plainLines = preview.locator('[data-textpreview-line]')
  438. await expect.poll(() => plainLines.count()).toBe(2)
  439. const fallback = (await plainLines.allTextContents()).map(line => line.trim())
  440. expect(fallback).toEqual(['UNKNOWN_SUFFIX', 'Plain fallback.'])
  441. sections.push(['## Unknown suffix', '', `- Viewer: ${await viewer.innerText()}`, `- Text: ${fallback.join(' | ')}`].join('\n'))
  442. expect(tripwire.pageErrors).toEqual([])
  443. expect(tripwire.warnings).toEqual([])
  444. await compareOrRefreshGolden(EXPECTED, sections.join('\n\n'), MODE)
  445. await assertFixtureInventory(SNAPSHOT_DIR, ['document.expected.md', 'paging.patch.yml'])
  446. })
  447. })