| 1 | import type { Page } from 'playwright-chromium' |
| 2 | import type { RasterRequest } from './normalize' |
| 3 | import { Buffer } from 'node:buffer' |
| 4 | import { describe, expect, it } from 'vitest' |
| 5 | import { capture, isUsableDataUri, shootClip } from './capture' |
| 6 | |
| 7 | /** |
| 8 | * `capture.ts` needs a live browser, so only its pure predicates are unit |
| 9 | * tested here. This one guards a real failure: pptxgenjs answers a data URI it |
| 10 | * cannot parse by printing to stderr and writing no picture at all, so the |
| 11 | * image is missing and the export still reports success. |
| 12 | */ |
| 13 | describe('isUsableDataUri', () => { |
| 14 | it('accepts a base64 raster', () => { |
| 15 | expect(isUsableDataUri('data:image/png;base64,iVBORw0KGgo=')).toBe(true) |
| 16 | expect(isUsableDataUri('data:image/jpeg;base64,/9j/4AAQ')).toBe(true) |
| 17 | }) |
| 18 | |
| 19 | it('rejects a URL-encoded data URI, which is how inline SVG is usually written', () => { |
| 20 | expect(isUsableDataUri('data:image/svg+xml,%3c!--%20icon%20--%3e')).toBe(false) |
| 21 | }) |
| 22 | |
| 23 | it('rejects SVG even when it is base64', () => { |
| 24 | // pptxgenjs writes a broken raster fallback for SVG from Node, so these |
| 25 | // are screenshotted rather than embedded. |
| 26 | expect(isUsableDataUri('data:image/svg+xml;base64,PHN2Zz4=')).toBe(false) |
| 27 | }) |
| 28 | }) |
| 29 | |
| 30 | /** |
| 31 | * A page just real enough for the two things that geometry depends on: how far |
| 32 | * it actually scrolled, and how tall its viewport is. |
| 33 | */ |
| 34 | function fakePage(options: { documentHeight: number, viewportHeight: number, box?: { x: number, y: number, width: number, height: number } }) { |
| 35 | const calls: { |
| 36 | clips: { x: number, y: number, width: number, height: number }[] |
| 37 | viewports: number[] |
| 38 | locators: string[] |
| 39 | elementShots: number |
| 40 | } = { |
| 41 | clips: [], |
| 42 | viewports: [], |
| 43 | locators: [], |
| 44 | elementShots: 0, |
| 45 | } |
| 46 | let scrollY = 0 |
| 47 | let viewportHeight = options.viewportHeight |
| 48 | const page = { |
| 49 | viewportSize: () => ({ width: 980, height: viewportHeight }), |
| 50 | async setViewportSize({ height }: { height: number }) { |
| 51 | viewportHeight = height |
| 52 | calls.viewports.push(height) |
| 53 | }, |
| 54 | async evaluate(fn: any, arg: any) { |
| 55 | // Only scroll calls reach here, and they are told apart by their source |
| 56 | // because all three are no-argument functions of `window`, which this |
| 57 | // stands in for. |
| 58 | if (typeof arg === 'number') { |
| 59 | scrollY = Math.max(0, Math.min(arg, options.documentHeight - viewportHeight)) |
| 60 | return scrollY |
| 61 | } |
| 62 | if (String(fn).includes('scrollTo')) { |
| 63 | scrollY = 0 |
| 64 | return undefined |
| 65 | } |
| 66 | return scrollY |
| 67 | }, |
| 68 | async screenshot({ clip }: any) { |
| 69 | calls.clips.push(clip) |
| 70 | return Buffer.from('png') |
| 71 | }, |
| 72 | locator(selector: string) { |
| 73 | calls.locators.push(selector) |
| 74 | return { |
| 75 | first: () => ({ |
| 76 | count: async () => (options.box ? 1 : 0), |
| 77 | // Viewport-relative, exactly as Playwright reports it. |
| 78 | boundingBox: async () => (options.box |
| 79 | ? { ...options.box, y: options.box.y - scrollY } |
| 80 | : null), |
| 81 | screenshot: async () => { |
| 82 | calls.elementShots++ |
| 83 | return Buffer.from('png') |
| 84 | }, |
| 85 | }), |
| 86 | } |
| 87 | }, |
| 88 | } |
| 89 | return { |
| 90 | page: page as unknown as Page, |
| 91 | calls, |
| 92 | scrollY: () => scrollY, |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | describe('a clip is taken relative to the scroll the page actually reached', () => { |
| 97 | it('rebases the clip onto the real scroll position', () => { |
| 98 | // `clip` is in DOCUMENT coordinates while Playwright reads it as |
| 99 | // viewport-relative, so the difference has to come from where the browser |
| 100 | // ended up, not from where it was asked to go. |
| 101 | const { page, calls } = fakePage({ documentHeight: 28152, viewportHeight: 2000 }) |
| 102 | return shootClip(page, { x: 100, y: 20000, w: 300, h: 150 }).then(() => { |
| 103 | expect(calls.clips).toEqual([ |
| 104 | { |
| 105 | x: 100, |
| 106 | y: 1, |
| 107 | width: 300, |
| 108 | height: 150, |
| 109 | }, |
| 110 | ]) |
| 111 | }) |
| 112 | }) |
| 113 | |
| 114 | it('does not scroll past the end of the document', () => { |
| 115 | // At the very bottom the browser stops short of the requested offset. A |
| 116 | // clip rebased on the REQUESTED offset lands above the region and |
| 117 | // Playwright answers "clipped area is empty", which the caller swallows, |
| 118 | // so the picture silently goes missing. |
| 119 | const { page, calls } = fakePage({ documentHeight: 28152, viewportHeight: 2000 }) |
| 120 | return shootClip(page, { x: 0, y: 28100, w: 100, h: 50 }).then(() => { |
| 121 | // Scroll stops at 26152, so the region is 1948 down the viewport. |
| 122 | expect(calls.clips).toEqual([ |
| 123 | { |
| 124 | x: 0, |
| 125 | y: 1948, |
| 126 | width: 100, |
| 127 | height: 50, |
| 128 | }, |
| 129 | ]) |
| 130 | }) |
| 131 | }) |
| 132 | }) |
| 133 | |
| 134 | describe('clip captures run through a shortened viewport', () => { |
| 135 | const request = (sourceId: number, clip?: { x: number, y: number, w: number, h: number }): RasterRequest => |
| 136 | ({ |
| 137 | sourceId, |
| 138 | isolate: false, |
| 139 | hideDescendants: false, |
| 140 | ...(clip |
| 141 | ? { clip } |
| 142 | : {}), |
| 143 | }) |
| 144 | |
| 145 | it('shortens the viewport for the clips and puts it back', async () => { |
| 146 | // The print route sizes its viewport to the WHOLE deck, and past roughly |
| 147 | // twenty thousand pixels Chromium truncates the capture surface, so every |
| 148 | // clip in the last third of a long deck failed and its picture vanished |
| 149 | // with nothing in the log to say so. |
| 150 | const { page, calls } = fakePage({ documentHeight: 28152, viewportHeight: 28152 }) |
| 151 | const slides = [ |
| 152 | { |
| 153 | no: 1, |
| 154 | clickIndex: 0, |
| 155 | containerId: '001-01', |
| 156 | size: { w: 980, h: 552 }, |
| 157 | nodes: [ |
| 158 | { |
| 159 | kind: 'raster' as const, |
| 160 | sourceId: 1, |
| 161 | rect: { x: 0, y: 0, w: 10, h: 10 }, |
| 162 | data: '', |
| 163 | reason: 'svg' as const, |
| 164 | isolate: false, |
| 165 | hideDescendants: false, |
| 166 | }, |
| 167 | ], |
| 168 | }, |
| 169 | ] |
| 170 | const report = await capture(page, slides as any, [ |
| 171 | request(1, { x: 0, y: 20000, w: 10, h: 10 }), |
| 172 | ]) |
| 173 | expect(calls.viewports).toEqual([2000, 28152]) |
| 174 | expect(report.rastersCaptured).toBe(1) |
| 175 | expect(report.rastersFailed).toBe(0) |
| 176 | }) |
| 177 | |
| 178 | it('leaves the viewport alone when nothing needs a clip', async () => { |
| 179 | const { page, calls } = fakePage({ documentHeight: 28152, viewportHeight: 28152 }) |
| 180 | await capture(page, [], []) |
| 181 | expect(calls.viewports).toEqual([]) |
| 182 | }) |
| 183 | }) |
| 184 | |
| 185 | describe('an element is captured by clipping the page at its box', () => { |
| 186 | const request = (sourceId: number): RasterRequest => ({ sourceId, isolate: false, hideDescendants: false }) |
| 187 | const slide = (sourceId: number) => ({ |
| 188 | no: 1, |
| 189 | clickIndex: 0, |
| 190 | containerId: '001-01', |
| 191 | size: { w: 980, h: 552 }, |
| 192 | nodes: [ |
| 193 | { |
| 194 | kind: 'raster' as const, |
| 195 | sourceId, |
| 196 | rect: { x: 0, y: 0, w: 10, h: 10 }, |
| 197 | data: '', |
| 198 | reason: 'svg' as const, |
| 199 | isolate: false, |
| 200 | hideDescendants: false, |
| 201 | }, |
| 202 | ], |
| 203 | }) |
| 204 | |
| 205 | it('clips rather than calling locator.screenshot', async () => { |
| 206 | // `locator.screenshot()` is the obvious call and returns a region from |
| 207 | // somewhere else entirely on a print page far taller than its viewport: |
| 208 | // Slidev's own starter deck came back with a picture of slide one pasted |
| 209 | // into slide four. |
| 210 | const { page, calls } = fakePage({ |
| 211 | documentHeight: 28152, |
| 212 | viewportHeight: 28152, |
| 213 | box: { |
| 214 | x: 100, |
| 215 | y: 2049, |
| 216 | width: 320, |
| 217 | height: 194, |
| 218 | }, |
| 219 | }) |
| 220 | const report = await capture(page, [ |
| 221 | slide(166), |
| 222 | ] as any, [ |
| 223 | request(166), |
| 224 | ]) |
| 225 | expect(calls.elementShots).toBe(0) |
| 226 | expect(calls.clips).toEqual([ |
| 227 | { |
| 228 | x: 100, |
| 229 | y: 1, |
| 230 | width: 320, |
| 231 | height: 194, |
| 232 | }, |
| 233 | ]) |
| 234 | expect(report.rastersCaptured).toBe(1) |
| 235 | }) |
| 236 | |
| 237 | it('reports a failure when the element is not there', async () => { |
| 238 | const { page } = fakePage({ documentHeight: 28152, viewportHeight: 28152 }) |
| 239 | const report = await capture(page, [ |
| 240 | slide(166), |
| 241 | ] as any, [ |
| 242 | request(166), |
| 243 | ]) |
| 244 | expect(report.rastersFailed).toBe(1) |
| 245 | }) |
| 246 | }) |
| 247 | |
| 248 | describe('shootClip', () => { |
| 249 | it('clamps a box that starts left of the page', async () => { |
| 250 | // An absolutely positioned decoration can hang off the left edge, and |
| 251 | // Chromium cannot capture a negative origin: it rejects the whole |
| 252 | // screenshot, so the picture went missing rather than being trimmed. |
| 253 | const { page, calls } = fakePage({ documentHeight: 5000, viewportHeight: 2000 }) |
| 254 | await shootClip(page, { x: -28, y: 100, w: 320, h: 194 }) |
| 255 | expect(calls.clips).toEqual([ |
| 256 | { x: 0, y: 1, width: 292, height: 194 }, |
| 257 | ]) |
| 258 | }) |
| 259 | |
| 260 | it('gives up on a box with nothing left to capture', async () => { |
| 261 | const { page, calls } = fakePage({ documentHeight: 5000, viewportHeight: 2000 }) |
| 262 | expect(await shootClip(page, { x: -400, y: 100, w: 320, h: 194 })).toBeUndefined() |
| 263 | expect(calls.clips).toEqual([]) |
| 264 | }) |
| 265 | }) |
| 266 |