// Shooting an element from the window, against a fake page: a window of a // set size, an element at a set box, scrolls and resizes that move it. No // browser. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test social/viewportShot.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import type { PageLike } from "./playwrightRuntime"; import { shootFromViewport, TOP_ROOM, unionBox, VIEW_SCRIPT_MARKER, viewScript, type ElementView } from "./viewportShot"; const PNG = new Uint8Array([0x89, 0x50, 0x4e, 0x47]); type Box = { x: number; y: number; width: number; height: number }; type FakeWindow = PageLike & { shots: unknown[]; sizes: { width: number; height: number }[]; scrolls: number[]; }; // A window `vw`×`vh` holding the element at `box` (viewport coordinates). A // scroll moves it; a resize changes the window. `views` overrides what the // measure returns, in turn (to fake a page still laying out). function fakeWindow(opts: { box: Box; vw?: number; vh?: number; resizable?: boolean; views?: (ElementView | null)[]; onResize?: (box: Box) => Box; }): FakeWindow { let box = { ...opts.box }; let vw = opts.vw ?? 1280; let vh = opts.vh ?? 720; const views = opts.views ? [...opts.views] : undefined; const shots: unknown[] = []; const sizes: { width: number; height: number }[] = []; const scrolls: number[] = []; const page: FakeWindow = { shots, sizes, scrolls, goto: async () => undefined, waitForSelector: async () => undefined, waitForTimeout: async () => undefined, on: () => {}, evaluate: async (script: string) => { if (script.includes(VIEW_SCRIPT_MARKER)) { if (views?.length) return views.length > 1 ? views.shift() : views[0]; return { ...box, vw, vh }; } const m = /window\.scrollBy\(0, (-?\d+)\)/.exec(script); if (m) { scrolls.push(Number(m[1])); box = { ...box, y: box.y - Number(m[1]) }; } return undefined; }, screenshot: async (o?: unknown) => { shots.push(o); return PNG; }, }; if (opts.resizable !== false) { page.setViewportSize = async (s) => { sizes.push(s); vw = s.width; vh = s.height; if (opts.onResize) box = opts.onResize(box); }; } return page; } const SEL = "[data-archilyzer-capture]"; test("an element inside the window is shot from it: a viewport clip, no full page, no resize", async () => { const p = fakeWindow({ box: { x: 283.5, y: 43, width: 598, height: 668.75 } }); const res = await shootFromViewport(p, { selector: SEL, settleMs: 0 }); assert.deepEqual(res, { png: PNG, trimmed: false }); assert.deepEqual(p.shots, [{ type: "png", clip: { x: 283, y: 43, width: 598, height: 669 } }]); assert.deepEqual(p.sizes, []); assert.deepEqual(p.scrolls, []); }); test("an element taller than the window grows the window's height (never its width), then puts it back", async () => { const p = fakeWindow({ box: { x: 283.5, y: 53, width: 598, height: 1500 } }); const res = await shootFromViewport(p, { selector: SEL, settleMs: 0 }); assert.ok(res && "png" in res); assert.deepEqual(p.sizes, [ { width: 1280, height: 53 + 1500 + 16 }, { width: 1280, height: 720 }, ]); assert.deepEqual(p.shots, [{ type: "png", clip: { x: 283, y: 53, width: 598, height: 1500 } }]); }); test("an element above the window is scrolled into it, below the sticky header", async () => { const p = fakeWindow({ box: { x: 283, y: -400, width: 598, height: 300 } }); const res = await shootFromViewport(p, { selector: SEL, settleMs: 0 }); assert.ok(res && "png" in res); assert.deepEqual(p.scrolls, [-400 - TOP_ROOM]); assert.deepEqual(p.shots, [{ type: "png", clip: { x: 283, y: TOP_ROOM, width: 598, height: 300 } }]); assert.deepEqual(p.sizes, []); }); test("maxHeight shoots the element's top and says it trimmed", async () => { const p = fakeWindow({ box: { x: 0, y: 64, width: 600, height: 5000 } }); const res = await shootFromViewport(p, { selector: SEL, settleMs: 0, maxHeight: 2000 }); assert.deepEqual(res, { png: PNG, trimmed: true }); assert.deepEqual(p.shots, [{ type: "png", clip: { x: 0, y: 64, width: 600, height: 2000 } }]); }); test("a page that cannot grow its window, or an element past the grow limit, falls back (null)", async () => { const fixed = fakeWindow({ box: { x: 283, y: 53, width: 598, height: 1500 }, resizable: false }); assert.equal(await shootFromViewport(fixed, { selector: SEL, settleMs: 0 }), null); assert.deepEqual(fixed.shots, []); const huge = fakeWindow({ box: { x: 283, y: 53, width: 598, height: 40_000 } }); assert.equal(await shootFromViewport(huge, { selector: SEL, settleMs: 0 }), null); assert.deepEqual(huge.sizes, []); }); test("a page that answers no box (a fake, a missing element) falls back; one wider than the window too", async () => { const none = fakeWindow({ box: { x: 0, y: 0, width: 1, height: 1 }, views: [null] }); assert.equal(await shootFromViewport(none, { selector: SEL, settleMs: 0 }), null); const wide = fakeWindow({ box: { x: 0, y: 53, width: 1400, height: 300 } }); assert.equal(await shootFromViewport(wide, { selector: SEL, settleMs: 0 }), null); assert.deepEqual(wide.shots, []); }); test("a box that moved during the shot is shot again; one that never settles is an error", async () => { const at = (x: number): ElementView => ({ x, y: 53, width: 598, height: 300, vw: 1280, vh: 720 }); // first, loop measure, shot, after (moved) → loop measure, shot, after (steady). const settling = fakeWindow({ box: at(0), views: [at(283), at(283), at(180), at(283), at(283)] }); const ok = await shootFromViewport(settling, { selector: SEL, settleMs: 0 }); assert.ok(ok && "png" in ok); assert.equal(settling.shots.length, 2); let n = 0; const restless = fakeWindow({ box: at(0), views: [at(283)] }); const measure = restless.evaluate; restless.evaluate = async (s: string) => (s.includes(VIEW_SCRIPT_MARKER) ? at(283 + 50 * n++) : measure(s)); const bad = await shootFromViewport(restless, { selector: SEL, settleMs: 0 }); assert.ok(bad && "error" in bad, JSON.stringify(bad)); assert.equal(restless.shots.length, 2); }); test("the window is put back even when the shot throws", async () => { const p = fakeWindow({ box: { x: 283, y: 53, width: 598, height: 1500 } }); p.screenshot = async () => { throw new Error("Target closed"); }; await assert.rejects(shootFromViewport(p, { selector: SEL, settleMs: 0 }), /Target closed/); assert.deepEqual(p.sizes.at(-1), { width: 1280, height: 720 }); }); test("a selector list is measured as ONE box, their union, by the page's own script", () => { const rects: Record = { "[p]": { left: 283, top: -200, right: 881, bottom: 20 }, "[o]": { left: 283, top: 20, right: 881, bottom: 300 }, }; const g = globalThis as Record; const saved = { document: g.document, window: g.window }; g.document = { querySelector: (s: string) => rects[s] ? { getBoundingClientRect: () => ({ ...rects[s], width: rects[s].right - rects[s].left, height: rects[s].bottom - rects[s].top }) } : null, }; g.window = { innerWidth: 1280, innerHeight: 720 }; try { assert.deepEqual((0, eval)(viewScript(["[p]", "[o]"])), { x: 283, y: -200, width: 598, height: 500, vw: 1280, vh: 720 }); assert.deepEqual((0, eval)(viewScript("[o]")), { x: 283, y: 20, width: 598, height: 280, vw: 1280, vh: 720 }); // One of them missing: nothing to shoot as the union. assert.equal((0, eval)(viewScript(["[p]", "[gone]"])), null); } finally { g.document = saved.document; g.window = saved.window; } assert.deepEqual( unionBox([ { x: 283, y: -200, width: 598, height: 220 }, { x: 283, y: 20, width: 598, height: 280 }, ]), { x: 283, y: -200, width: 598, height: 500 }, ); });