|
| 1 | +import { spawnSync } from "node:child_process"; |
| 2 | +import { basename } from "node:path"; |
| 3 | +import puppeteer, { type Browser } from "puppeteer-core"; |
| 4 | +import { afterAll, beforeAll, describe, expect, it } from "vitest"; |
| 5 | +import { resolveHeadlessShellPath } from "./browserManager.js"; |
| 6 | +import { beginFrameCapture, forgetBeginFrameCapture } from "./screenshotService.js"; |
| 7 | +import { closeTestChrome } from "./__fixtures__/loopback.js"; |
| 8 | + |
| 9 | +const resolved = resolveHeadlessShellPath(); |
| 10 | +const chromePath = |
| 11 | + process.env.HF_BEGINFRAME_SHELL ?? |
| 12 | + (resolved && basename(resolved).startsWith("chrome-headless-shell") ? resolved : undefined); |
| 13 | +const hasBrowser = chromePath !== undefined && spawnSync(chromePath, ["--version"]).status === 0; |
| 14 | +const INTERVAL = 1000 / 30; |
| 15 | +const options = { width: 160, height: 90, fps: { num: 30, den: 1 }, format: "jpeg" as const }; |
| 16 | + |
| 17 | +describe.skipIf(!hasBrowser)("beginFrameCapture reuse", () => { |
| 18 | + let browser: Browser; |
| 19 | + beforeAll(async () => { |
| 20 | + browser = await puppeteer.launch({ |
| 21 | + executablePath: chromePath, |
| 22 | + headless: true, |
| 23 | + args: [ |
| 24 | + "--no-sandbox", |
| 25 | + "--disable-dev-shm-usage", |
| 26 | + "--deterministic-mode", |
| 27 | + "--enable-begin-frame-control", |
| 28 | + "--disable-new-content-rendering-timeout", |
| 29 | + "--run-all-compositor-stages-before-draw", |
| 30 | + ], |
| 31 | + }); |
| 32 | + }, 30_000); |
| 33 | + afterAll(() => closeTestChrome(browser)); |
| 34 | + |
| 35 | + it("reuses a frame only when the page reports no change, never a stale one", async () => { |
| 36 | + const page = await browser.newPage(); |
| 37 | + await page.setViewport({ width: 160, height: 90 }); |
| 38 | + await page.setContent( |
| 39 | + `<body style="margin:0"><div id="b" style="width:160px;height:90px;background:#f00"></div></body>`, |
| 40 | + ); |
| 41 | + const grab = (tick: number) => beginFrameCapture(page, options, tick * INTERVAL, INTERVAL); |
| 42 | + const paint = (color: string) => |
| 43 | + page.evaluate( |
| 44 | + (c) => ((document.getElementById("b") as HTMLElement).style.background = c), |
| 45 | + color, |
| 46 | + ); |
| 47 | + |
| 48 | + // The first ticks after load may still report paint settling; reuse starts once it ends. |
| 49 | + let tick = 1; |
| 50 | + let last = await grab(tick); |
| 51 | + expect(last.hasDamage).toBe(true); |
| 52 | + let held = await grab(++tick); |
| 53 | + while (held.hasDamage && tick < 8) { |
| 54 | + last = held; |
| 55 | + held = await grab(++tick); |
| 56 | + } |
| 57 | + expect(held.hasDamage).toBe(false); |
| 58 | + expect(held.buffer.equals(last.buffer)).toBe(true); |
| 59 | + const first = held; |
| 60 | + |
| 61 | + await paint("#00f"); |
| 62 | + const changed = await grab(++tick); |
| 63 | + expect(changed.hasDamage).toBe(true); |
| 64 | + expect(changed.buffer.equals(first.buffer)).toBe(false); |
| 65 | + await page.close(); |
| 66 | + }, 30_000); |
| 67 | + |
| 68 | + it("never returns a frame older than a change the page commits across frames", async () => { |
| 69 | + const page = await browser.newPage(); |
| 70 | + await page.setViewport({ width: 160, height: 90 }); |
| 71 | + await page.setContent( |
| 72 | + `<body style="margin:0"><div id="b" style="width:160px;height:90px;background:#f00"></div></body>`, |
| 73 | + ); |
| 74 | + const grab = (tick: number) => beginFrameCapture(page, options, tick * INTERVAL, INTERVAL); |
| 75 | + let tick = 1; |
| 76 | + for (let settled = 0; settled < 3 && tick < 12; tick++) { |
| 77 | + settled = (await grab(tick)).hasDamage ? 0 : settled + 1; |
| 78 | + } |
| 79 | + // Each rAF callback runs in a later frame than the one before it. |
| 80 | + await page.evaluate(() => { |
| 81 | + const paint = (c: string) => |
| 82 | + ((document.getElementById("b") as HTMLElement).style.background = c); |
| 83 | + requestAnimationFrame(() => { |
| 84 | + paint("#0f0"); |
| 85 | + requestAnimationFrame(() => { |
| 86 | + paint("#00f"); |
| 87 | + requestAnimationFrame(() => paint("#ff0")); |
| 88 | + }); |
| 89 | + }); |
| 90 | + }); |
| 91 | + const frames = [await grab(tick++), await grab(tick++), await grab(tick++), await grab(tick++)]; |
| 92 | + forgetBeginFrameCapture(page); |
| 93 | + const fresh = await grab(tick); |
| 94 | + expect(frames.at(-1)?.buffer.equals(fresh.buffer)).toBe(true); |
| 95 | + await page.close(); |
| 96 | + }, 30_000); |
| 97 | +}); |
0 commit comments