Skip to content

Commit 6f27ee6

Browse files
fix(cli): check asks the page the same whatever its screenshots show (#9103)
* fix(cli): check asks the page the same whatever its screenshots show * test(cli): a blank stretch with paint at one of its times stays quiet
1 parent e60261a commit 6f27ee6

6 files changed

Lines changed: 179 additions & 11 deletions

File tree

‎packages/cli/src/utils/blankSpan.chromium.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,16 @@ describe.skipIf(!RUNS_CHROMIUM)("blank_span in Chromium", () => {
163163
`<div id="outside" style="position: fixed; left: 0; top: 0; width: 80px; height: 80px; background: #fff"></div><script>`,
164164
),
165165
],
166+
[
167+
"paint outside the composition root at one time of the stretch",
168+
film(
169+
"",
170+
`${CUTS(2, 3.5)} document.getElementById("outside").style.display = t >= 2.5 && t < 2.625 ? "" : "none";`,
171+
).replace(
172+
"<script>",
173+
`<div id="outside" style="position: fixed; left: 0; top: 0; width: 80px; height: 80px; background: #fff"></div><script>`,
174+
),
175+
],
166176
["a film with no opaque stage", film("", CUTS(2, 3.5), { stage: "html, body { margin: 0; }" })],
167177
[
168178
"a blank marked deliberate",

‎packages/cli/src/utils/blankSpan.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ async function isBlankBetweenCuts(
7171
if (before.presence < BLANK_SPAN.cutPresence) return false;
7272
const after = await frameAt(stretch.end + BLANK_SPAN.edgeProbeSec);
7373
if (after.presence < BLANK_SPAN.cutPresence) return false;
74-
for (const time of stretch.times) if (!(await stageIsEmpty(time))) return false;
75-
return true;
74+
const verdicts: boolean[] = [];
75+
for (const time of stretch.times) verdicts.push(await stageIsEmpty(time));
76+
return verdicts.every(Boolean);
7677
}
7778

7879
function blankSpanFinding(stretch: Stretch): CraftFinding {

‎packages/cli/src/utils/checkBrowser.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,13 +1146,13 @@ export function createPageDriver(
11461146
collectContrast(page, time, annotations, skipInMotion),
11471147
collectCraftFrame: (time) => collectCraftFrame(page, time),
11481148
collectCraftFilm: () => collectCraftFilm(page),
1149-
stageIsEmpty: async (frame) => {
1149+
stageIsEmpty: async (capture) => {
11501150
const color = rgbaValue(
11511151
await page.evaluate(() =>
11521152
Reflect.apply(Reflect.get(window, "__hyperframesEmptyStageColor"), window, []),
11531153
),
11541154
);
1155-
return color !== null && isOneColor(framePixels(frame), color);
1155+
return color !== null && isOneColor(framePixels(await capture()), color);
11561156
},
11571157
});
11581158
}

‎packages/cli/src/utils/checkPipeline.ts‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -923,15 +923,18 @@ function detectSweepStatic(
923923
}
924924

925925
// The signature reads DOM channels, which miss paint such as SVG mask and clip contents, stroke dashes and
926-
// canvas or WebGL pixels. Before calling a sweep still, the rendered frames get the last word. A change counts
926+
// canvas or WebGL pixels, so frames get the last word, every one taken before any is compared. A change counts
927927
// only if the first time still renders its first frame: wall-clock paint (an unregistered rAF loop) is no seek.
928928
async function framesMoved(driver: CheckAuditDriver, times: number[]): Promise<boolean> {
929929
const [firstTime, ...rest] = times;
930-
if (firstTime === undefined) return false;
930+
if (firstTime === undefined || rest.length === 0) return false;
931931
const first = await frameAt(driver, firstTime);
932-
for (const time of rest) {
933-
if (!(await driver.framesDiffer(first, await frameAt(driver, time)))) continue;
934-
return !(await driver.framesDiffer(first, await frameAt(driver, firstTime)));
932+
const later: string[] = [];
933+
for (const time of rest) later.push(await frameAt(driver, time));
934+
const firstAgain = await frameAt(driver, firstTime);
935+
for (const frame of later) {
936+
if (await driver.framesDiffer(first, frame))
937+
return !(await driver.framesDiffer(first, firstAgain));
935938
}
936939
return false;
937940
}
@@ -1594,7 +1597,10 @@ async function craftFilmFacts(
15941597
function stageProbe(driver: CheckAuditDriver): StageProbe | undefined {
15951598
const { stageIsEmpty } = driver;
15961599
if (!stageIsEmpty) return undefined;
1597-
return async (time) => stageIsEmpty(await frameAt(driver, time));
1600+
return async (time) => {
1601+
await driver.seek(time, true);
1602+
return stageIsEmpty(() => driver.captureFrame());
1603+
};
15981604
}
15991605

16001606
function disabledCraft(): CraftResult {

‎packages/cli/src/utils/checkTypes.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ export interface CheckAuditDriver {
335335
/** Craft section: text, stillness and paint at the current seek. A driver without it skips the section. */
336336
collectCraftFrame?(time: number): Promise<CraftFrame>;
337337
collectCraftFilm?(): Promise<CraftFilm>;
338-
stageIsEmpty?(frame: string): Promise<boolean>;
338+
stageIsEmpty?(captureOnceColourIsRead: () => Promise<string>): Promise<boolean>;
339339
}
340340

341341
export interface CheckScreenshot {
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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

Comments
 (0)