|
| 1 | +// fails without the fix: a screenshot verdict chose the next seek, so the page could read pixels it cannot see. |
| 2 | +import { existsSync } from "node:fs"; |
| 3 | +import type { Browser, Page } from "puppeteer-core"; |
| 4 | +import { afterAll, beforeAll, describe, expect, it } from "vitest"; |
| 5 | +import { findSystemBrowser } from "../browser/manager.js"; |
| 6 | +import { createPageDriver } from "./checkBrowser.js"; |
| 7 | +import { DEFAULT_CHECK_OPTIONS, runAuditGrid } from "./checkPipeline.js"; |
| 8 | + |
| 9 | +function resolveExecutable(): string | undefined { |
| 10 | + for (const name of ["HYPERFRAMES_BROWSER_PATH", "PUPPETEER_EXECUTABLE_PATH"]) { |
| 11 | + const candidate = process.env[name]; |
| 12 | + if (candidate && existsSync(candidate)) return candidate; |
| 13 | + } |
| 14 | + return findSystemBrowser()?.executablePath; |
| 15 | +} |
| 16 | + |
| 17 | +const executablePath = resolveExecutable(); |
| 18 | +const RUNS_CHROMIUM = |
| 19 | + !!executablePath && |
| 20 | + (process.platform !== "win32" || process.env.HYPERFRAMES_BROWSER_TESTS === "1"); |
| 21 | +const BROWSER_LAUNCH_TIMEOUT_MS = 120_000; |
| 22 | +const CASE_TIMEOUT_MS = 90_000; |
| 23 | + |
| 24 | +const TITLE = "position:absolute;left:80px;top:140px;margin:0;color:#fff;font:600 48px sans-serif"; |
| 25 | + |
| 26 | +function film(body: string, draw: string): string { |
| 27 | + return `<!doctype html><html><head><style> |
| 28 | + html, body { margin: 0; width: 640px; height: 360px; overflow: hidden; background: #0a0a0a; } |
| 29 | + </style></head><body> |
| 30 | + <div id="root" data-composition-id="main" data-duration="6" data-width="640" data-height="360"> |
| 31 | + <h1 id="a" style="${TITLE}">Before</h1><h1 id="b" style="${TITLE}">After</h1>${body} |
| 32 | + </div> |
| 33 | + <script> |
| 34 | + const show = (id, on) => { document.getElementById(id).style.display = on ? "" : "none"; }; |
| 35 | + const draw = (t) => { ${draw} }; |
| 36 | + window.__hf = { duration: 6, seek: draw }; |
| 37 | + draw(0); |
| 38 | + </script> |
| 39 | + </body></html>`; |
| 40 | +} |
| 41 | + |
| 42 | +// A blank stretch between two cuts; paint outside the composition root is the only difference, and only in pixels. |
| 43 | +const blankBetweenCuts = (outside: string) => |
| 44 | + film("", `show("a", t < 2); show("b", t >= 3.5);`).replace( |
| 45 | + "<script>", |
| 46 | + `<div style="position:fixed;left:0;top:0;width:80px;height:80px;background:${outside}"></div><script>`, |
| 47 | + ); |
| 48 | + |
| 49 | +// The same DOM at every seek; only what the canvas draws, which the DOM signature cannot read, moves or not. |
| 50 | +const drawnLine = (grows: boolean) => |
| 51 | + film( |
| 52 | + `<canvas id="c" width="640" height="360" style="position:absolute;inset:0"></canvas>`, |
| 53 | + `show("a", true); show("b", false); |
| 54 | + const ctx = document.getElementById("c").getContext("2d"); |
| 55 | + ctx.clearRect(0, 0, 640, 360); ctx.strokeStyle = "#4ade80"; ctx.lineWidth = 1; ctx.beginPath(); |
| 56 | + ctx.moveTo(40, 180); ctx.lineTo(${grows ? "40 + t * 90" : "220"}, 180); ctx.stroke();`, |
| 57 | + ); |
| 58 | + |
| 59 | +/** Every call the check makes on the page, with its arguments: what the composition could observe. */ |
| 60 | +function recorded(page: Page, calls: string[]): Page { |
| 61 | + return new Proxy(page, { |
| 62 | + get(target, key) { |
| 63 | + const value = Reflect.get(target, key, target); |
| 64 | + if (typeof value !== "function") return value; |
| 65 | + return (...args: unknown[]) => { |
| 66 | + const shown = args.map((arg) => |
| 67 | + typeof arg === "function" ? `fn:${arg.name || String(arg).slice(0, 160)}` : arg, |
| 68 | + ); |
| 69 | + calls.push(`${String(key)} ${JSON.stringify(shown)}`); |
| 70 | + return Reflect.apply(value, target, args); |
| 71 | + }; |
| 72 | + }, |
| 73 | + }); |
| 74 | +} |
| 75 | + |
| 76 | +describe.skipIf(!RUNS_CHROMIUM)("pixel verdicts and what the page is asked", () => { |
| 77 | + let browser: Browser; |
| 78 | + |
| 79 | + beforeAll(async () => { |
| 80 | + const puppeteer = await import("puppeteer-core"); |
| 81 | + browser = await puppeteer.default.launch({ |
| 82 | + headless: true, |
| 83 | + executablePath, |
| 84 | + args: ["--no-sandbox", "--disable-dev-shm-usage", "--disable-gpu"], |
| 85 | + }); |
| 86 | + }, BROWSER_LAUNCH_TIMEOUT_MS); |
| 87 | + |
| 88 | + afterAll(async () => { |
| 89 | + await browser?.close(); |
| 90 | + }); |
| 91 | + |
| 92 | + async function check(html: string) { |
| 93 | + const page = await browser.newPage(); |
| 94 | + try { |
| 95 | + await page.setViewport({ width: 640, height: 360 }); |
| 96 | + await page.setContent(html); |
| 97 | + const calls: string[] = []; |
| 98 | + const result = await runAuditGrid( |
| 99 | + createPageDriver(recorded(page, calls)), |
| 100 | + { ...DEFAULT_CHECK_OPTIONS, contrast: false }, |
| 101 | + { kind: "none" }, |
| 102 | + ); |
| 103 | + const codes = [...result.layoutIssues, ...result.craft.findings].map(({ code }) => code); |
| 104 | + return { calls, codes }; |
| 105 | + } finally { |
| 106 | + await page.close(); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + it( |
| 111 | + "asks the page the same for a blank stretch whatever the stage's pixels show", |
| 112 | + async () => { |
| 113 | + const blank = await check(blankBetweenCuts("#0a0a0a")); |
| 114 | + const painted = await check(blankBetweenCuts("#fff")); |
| 115 | + |
| 116 | + expect(blank.codes).toContain("blank_span"); |
| 117 | + expect(painted.codes).not.toContain("blank_span"); |
| 118 | + expect(painted.calls).toEqual(blank.calls); |
| 119 | + }, |
| 120 | + CASE_TIMEOUT_MS, |
| 121 | + ); |
| 122 | + |
| 123 | + it( |
| 124 | + "asks the page the same for a sweep whatever its frames show", |
| 125 | + async () => { |
| 126 | + const moving = await check(drawnLine(true)); |
| 127 | + const still = await check(drawnLine(false)); |
| 128 | + |
| 129 | + expect(moving.codes).not.toContain("sweep_static"); |
| 130 | + expect(still.codes).toContain("sweep_static"); |
| 131 | + expect(still.calls).toEqual(moving.calls); |
| 132 | + }, |
| 133 | + CASE_TIMEOUT_MS, |
| 134 | + ); |
| 135 | + |
| 136 | + it( |
| 137 | + "reads the stage colour before it takes the frame compared with it", |
| 138 | + async () => { |
| 139 | + const { calls } = await check(blankBetweenCuts("#0a0a0a")); |
| 140 | + const reads = calls.flatMap((call, index) => |
| 141 | + call.startsWith("evaluate ") && call.includes("__hyperframesEmptyStageColor") |
| 142 | + ? [index] |
| 143 | + : [], |
| 144 | + ); |
| 145 | + |
| 146 | + expect(reads.length).toBeGreaterThan(1); |
| 147 | + for (const index of reads) expect(calls[index + 1]).toMatch(/^screenshot /); |
| 148 | + }, |
| 149 | + CASE_TIMEOUT_MS, |
| 150 | + ); |
| 151 | +}); |
0 commit comments