Skip to content

Commit 5f3a2ff

Browse files
authored
test: hold the printer to Gecko as well as Blink (#22151)
* test: hold the printer to Gecko as well as Blink The equivalence suite asked one engine, so a rewrite only the other reads differently went unseen. `EQUIVALENCE_BROWSER=firefox` points the same corpus at Gecko, which took three harness gaps to reach: BiDi truncates a structure nested past three levels where CDP returns it whole, the media emulation is CDP only, and a defect filed against one engine is not one the other has. * test: load the browser fetcher the way puppeteer itself is loaded `@puppeteer/browsers` is ESM-only, so a `require` of it throws wherever Node does not allow one — before either Firefox path can report a browser problem. Both sites take it by dynamic import now, and the tooling script carries the license header every file under `tooling/` opens with. * test: read the generic families from the table that states them The comparison had its own copy of the list, which is the one table in this change a dataset already answers — `lib/css/data.js` generates it, so it is passed into the page rather than written a second time. * test: keep a condition apart on the dimension nothing sampled Where the engine answers no media emulation, the signature was the viewport bits alone — measured in Firefox 156, `print`, `speech` and both `color-gamut` values all answer `0000`, so a rewrite from one to another read as equivalent. What no viewport varies is carried as text beside the bits now: the media type and every feature but the dimensions, with `min-x: v` written as `x >= v` so the two spellings of one query still meet.
1 parent ed824e2 commit 5f3a2ff

13 files changed

Lines changed: 449 additions & 89 deletions
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Run the printer equivalence suite against Firefox as well as Chrome.

‎eslint.config.mjs‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,9 @@ export default defineConfig([
192192
}
193193
},
194194
{
195-
// puppeteer-core is ESM-only (v25+) and is loaded via dynamic import here
196-
files: ["test/helpers/launchChrome.js"],
195+
// puppeteer-core and @puppeteer/browsers are ESM-only and are loaded via
196+
// dynamic import here
197+
files: ["test/helpers/launchBrowser.js"],
197198
languageOptions: {
198199
ecmaVersion: 2020
199200
}
@@ -246,6 +247,22 @@ export default defineConfig([
246247
"no-console": "off"
247248
}
248249
},
250+
{
251+
// Fetches a browser for the suites that compare against one, from a package
252+
// that is ESM-only — so it runs on modern Node rather than the baseline.
253+
files: ["tooling/install-firefox.js"],
254+
languageOptions: {
255+
ecmaVersion: 2022
256+
},
257+
rules: {
258+
"n/no-unsupported-features/es-syntax": [
259+
"error",
260+
{
261+
ignores: ["dynamic-import"]
262+
}
263+
]
264+
}
265+
},
249266
{
250267
// The tool comparisons run on modern Node, not webpack's runtime baseline:
251268
// they measure with post-10 APIs (fs.promises, zstd, resourceUsage).

‎package.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"benchmark:html-tools:setup": "node tooling/compare-html-tools.js --setup",
5959
"benchmark:js-tools": "node tooling/compare-js-tools.js",
6060
"measure:color-agreement": "node tooling/measure-color-agreement.js",
61+
"setup:firefox": "node tooling/install-firefox.js",
6162
"pretest": "yarn lint",
6263
"test": "yarn test:base",
6364
"test:base": "node --expose-gc --max-old-space-size=4096 --experimental-vm-modules --trace-deprecation node_modules/jest-cli/bin/jest --workerIdleMemoryLimit=512MB --logHeapUsage",
@@ -136,6 +137,7 @@
136137
"@types/graceful-fs": "^4.1.9",
137138
"@types/jest": "^30.0.0",
138139
"@types/mime-db": "^1.43.6",
140+
"@puppeteer/browsers": "^3.2.2",
139141
"@types/node": "^26.2.0",
140142
"@types/xxhashjs": "^0.2.4",
141143
"@webdiscus/pug-loader": "^2.11.1",

‎test/WebpackDevServer.longtest.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const net = require("net");
1616
const os = require("os");
1717
const path = require("path");
1818
const webpack = require("..");
19-
const launchChrome = require("./helpers/launchChrome");
19+
const launchBrowser = require("./helpers/launchBrowser");
2020

2121
// Drives a real webpack-dev-server, added as a compiler plugin, in real Chrome via
2222
// puppeteer-core. It needs Node >= 22.15 and the matrix goes down to Node 10, so
@@ -292,7 +292,7 @@ describe("WebpackDevServer integration in real Chrome", () => {
292292
beforeAll(async () => {
293293
if (!nodeSupported) return;
294294
ensureWebpackSelfLink();
295-
browser = await launchChrome();
295+
browser = await launchBrowser();
296296
}, 120000);
297297

298298
afterAll(async () => {

‎test/helpers/launchBrowser.js‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"use strict";
2+
3+
const os = require("os");
4+
5+
const CACHE_DIR = `${os.homedir()}/.cache/puppeteer`;
6+
7+
/**
8+
* Where an already-downloaded Firefox sits, or `undefined` when none is. The
9+
* suites do not download one themselves: a cold fetch is tens of megabytes and
10+
* would expire the callers' hook budget rather than report a browser problem.
11+
* @returns {Promise<string | undefined>} the executable path
12+
*/
13+
const installedFirefox = async () => {
14+
// ESM-only, like puppeteer-core itself, so it is loaded the same way.
15+
const { Browser, detectBrowserPlatform, getInstalledBrowsers } =
16+
await import("@puppeteer/browsers");
17+
18+
const platform = detectBrowserPlatform();
19+
for (const one of await getInstalledBrowsers({ cacheDir: CACHE_DIR })) {
20+
if (one.browser === Browser.FIREFOX && one.platform === platform) {
21+
return one.executablePath;
22+
}
23+
}
24+
return undefined;
25+
};
26+
27+
/**
28+
* A real browser for the suites that compare against one. A browser that is
29+
* missing or will not launch throws rather than skipping, so no environment can
30+
* report these checks as passing without having run them.
31+
* @param {(import("puppeteer-core").LaunchOptions & { browser?: "chrome" | "firefox" })=} options extra launch options; `browser` picks the engine
32+
* @returns {Promise<import("puppeteer-core").Browser>} the running browser
33+
*/
34+
module.exports = async (options) => {
35+
// require() of puppeteer-core throws under Jest since it is ESM-only (v25+).
36+
const puppeteer = (await import("puppeteer-core")).default;
37+
const engine = (options && options.browser) || "chrome";
38+
const firefox = engine === "firefox";
39+
/** @type {import("puppeteer-core").LaunchOptions} */
40+
const launchOptions = {
41+
headless: true,
42+
// puppeteer's own 30s default expires on a cold runner before the browser
43+
// prints its WS endpoint; this stays under the callers' 120s hook budget
44+
timeout: 100000,
45+
// Chrome's sandbox and GPU flags mean nothing to Firefox, which reads its
46+
// own settings out of `extraPrefsFirefox` instead.
47+
...(firefox
48+
? {}
49+
: { args: ["--no-sandbox", "--disable-gpu", "--disable-dev-shm-usage"] }),
50+
...options
51+
};
52+
if (firefox) {
53+
const executablePath =
54+
process.env.FIREFOX_EXECUTABLE_PATH || (await installedFirefox());
55+
if (executablePath === undefined) {
56+
throw new Error(
57+
"no Firefox to launch: run `yarn setup:firefox`, or point FIREFOX_EXECUTABLE_PATH at one"
58+
);
59+
}
60+
launchOptions.executablePath = executablePath;
61+
} else if (process.env.PUPPETEER_EXECUTABLE_PATH) {
62+
launchOptions.executablePath = process.env.PUPPETEER_EXECUTABLE_PATH;
63+
} else {
64+
launchOptions.channel = "chrome";
65+
}
66+
return puppeteer.launch(launchOptions);
67+
};

‎test/helpers/launchChrome.js‎

Lines changed: 0 additions & 29 deletions
This file was deleted.

‎test/helpers/syntaxEquivalence.js‎

Lines changed: 133 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,11 @@ const benchmarkDocuments = (minify) => {
192192
* inline `<style>` is held to exactly the same standard as a `.css` file.
193193
* @returns {void}
194194
*/
195-
const installHelpers = () => {
195+
/**
196+
* @param {string[]} generics the generic font families, from `lib/css/data.js`
197+
* @returns {void}
198+
*/
199+
const installHelpers = (generics) => {
196200
const NS_HTML = "http://www.w3.org/1999/xhtml";
197201
const NS_SVG = "http://www.w3.org/2000/svg";
198202
const probe = document.createElement("div");
@@ -581,6 +585,25 @@ const installHelpers = () => {
581585
["step-end", "steps(1, end)"]
582586
]);
583587

588+
/**
589+
* A font family list with every name the bare spelling would also name left
590+
* unquoted. CSS Fonts 4 §2.2 makes the two the same family, and the engines
591+
* disagree about which to echo.
592+
* @param {string} list a `font-family` or `font` value
593+
* @returns {string} the same list, quoted once
594+
*/
595+
const unquoteFamilies = (list) =>
596+
list.replace(
597+
/"((?:[A-Za-z_-][\w-]*)(?: [A-Za-z_-][\w-]*)*)"/g,
598+
(quoted, name) =>
599+
GENERIC_FAMILIES.has(name.toLowerCase()) ? quoted : name
600+
);
601+
602+
// A generic family is a keyword, so quoting one names a font of that name
603+
// instead, and the quoting is what tells the two apart. `CssSyntax.unittest`
604+
// holds the printer to the same rule.
605+
const GENERIC_FAMILIES = new Set(generics);
606+
584607
/**
585608
* The one spelling of a value the spec gives several names: an easing keyword
586609
* is the curve it stands for, `jump-start` names the step position `start`
@@ -661,7 +684,17 @@ const installHelpers = () => {
661684
: style.getPropertyValue(property);
662685
// A value that is not itself a color still carries them: `box-shadow`
663686
// keeps the space its color was written in, so each one is painted.
664-
const named = canonical(resolved);
687+
688+
// WHY: Gecko carries the source's quoting into the computed family where
689+
// Blink drops it, so a respelling neither engine can tell apart reads as
690+
// a difference in one of them — measured: `font-family:"Manrope"`
691+
// computes `"Manrope"` in Firefox 156 and `Manrope` in Chrome 147, while
692+
// the bare spelling computes `Manrope` in both.
693+
const named = canonical(
694+
property === "font-family" || property === "font"
695+
? unquoteFamilies(resolved)
696+
: resolved
697+
);
665698
const whole = painted(named);
666699
out.push(
667700
`${property}${bang}:${whole === named ? paintedColors(named) : whole}`
@@ -1395,6 +1428,75 @@ const installHelpers = () => {
13951428
};
13961429
};
13971430

1431+
/** @type {WeakMap<import("puppeteer-core").Page, boolean>} */
1432+
const emulates = new WeakMap();
1433+
1434+
/**
1435+
* Whether the engine behind a page answers the media-emulation calls at all.
1436+
* Asked once per page and remembered, since the answer is a property of the
1437+
* protocol rather than of the page.
1438+
* @param {import("puppeteer-core").Page} page the page to ask
1439+
* @returns {Promise<boolean>} true when it emulates
1440+
*/
1441+
const emulatesMedia = async (page) => {
1442+
const known = emulates.get(page);
1443+
if (known !== undefined) return known;
1444+
let can = true;
1445+
try {
1446+
await page.emulateMediaType(undefined);
1447+
} catch (_error) {
1448+
can = false;
1449+
}
1450+
emulates.set(page, can);
1451+
return can;
1452+
};
1453+
1454+
// The media types a condition can name, which no viewport varies.
1455+
const MEDIA_TYPES = new Set([
1456+
"all",
1457+
"aural",
1458+
"braille",
1459+
"embossed",
1460+
"handheld",
1461+
"print",
1462+
"projection",
1463+
"screen",
1464+
"speech",
1465+
"tty",
1466+
"tv"
1467+
]);
1468+
1469+
/**
1470+
* What a media condition asks that no viewport can answer: its media type and
1471+
* every feature but the dimensions, named once and in order. Built by taking
1472+
* what is left rather than by cutting the rest out, so a query reduces to the
1473+
* same text however its parts were spelled or joined.
1474+
* @param {string} condition the condition as written
1475+
* @returns {string} what the viewport did not sample
1476+
*/
1477+
const unsampledBy = (condition) => {
1478+
const parts = [];
1479+
for (const [word] of condition.toLowerCase().matchAll(/[a-z-]+/g)) {
1480+
if (MEDIA_TYPES.has(word)) parts.push(word);
1481+
}
1482+
for (const [feature] of condition
1483+
.toLowerCase()
1484+
.matchAll(/\([^()]*(?:\([^()]*\))?[^()]*\)/g)) {
1485+
if (/\b(?:min-|max-)?(?:width|height|aspect-ratio)\b/.test(feature)) {
1486+
continue;
1487+
}
1488+
// `min-x: v` and `x >= v` are one query written two ways (Media Queries 4
1489+
// §2.4), so the prefixed spelling is written as the range one.
1490+
parts.push(
1491+
feature
1492+
.replace(/\s+/g, "")
1493+
.replace(/^\(min-([a-z-]+):/, "($1>=")
1494+
.replace(/^\(max-([a-z-]+):/, "($1<=")
1495+
);
1496+
}
1497+
return parts.sort().join("&");
1498+
};
1499+
13981500
/**
13991501
* What the engine makes of every at-rule condition in a set of rules. A
14001502
* condition is not compared as text: `(min-width: 200px)` and
@@ -1501,22 +1603,37 @@ const conditionSignatures = async (page, groups) => {
15011603
[{ name: "prefers-reduced-motion", value: "reduce" }],
15021604
[{ name: "color-gamut", value: "p3" }]
15031605
];
1504-
for (const type of ["screen", "print"]) {
1505-
for (const features of [[], ...featureSets]) {
1506-
await page.emulateMediaType(type);
1507-
await page.emulateMediaFeatures(features);
1508-
const answers = await page.evaluate(
1509-
(conditions) =>
1510-
conditions.map((condition) =>
1511-
matchMedia(condition).matches ? "1" : "0"
1512-
),
1513-
media
1514-
);
1515-
for (const [i, bit] of answers.entries()) bits[i] += bit;
1606+
// WHY: Both calls are CDP, which Gecko's WebDriver BiDi does not answer, and
1607+
// only `prefers-color-scheme` and `prefers-reduced-motion` have a launch
1608+
// preference standing in — set at launch, so not switchable per sample.
1609+
// Measured in Firefox 156: `print`, `speech` and both `color-gamut` values
1610+
// all answer `0000` across the viewports, so a signature of those bits
1611+
// alone would call them one condition. What the viewport cannot vary is
1612+
// carried as text instead, which keeps them apart while the widths still
1613+
// equate `(min-width:200px)` with `(width>=200px)`.
1614+
if (!(await emulatesMedia(page))) {
1615+
for (const [i, condition] of media.entries()) {
1616+
bits[i] += ` ${unsampledBy(condition)}`;
15161617
}
15171618
}
1518-
await page.emulateMediaType(undefined);
1519-
await page.emulateMediaFeatures([]);
1619+
if (await emulatesMedia(page)) {
1620+
for (const type of ["screen", "print"]) {
1621+
for (const features of [[], ...featureSets]) {
1622+
await page.emulateMediaType(type);
1623+
await page.emulateMediaFeatures(features);
1624+
const answers = await page.evaluate(
1625+
(conditions) =>
1626+
conditions.map((condition) =>
1627+
matchMedia(condition).matches ? "1" : "0"
1628+
),
1629+
media
1630+
);
1631+
for (const [i, bit] of answers.entries()) bits[i] += bit;
1632+
}
1633+
}
1634+
await page.emulateMediaType(undefined);
1635+
await page.emulateMediaFeatures([]);
1636+
}
15201637
for (const [i, condition] of media.entries()) {
15211638
signatures.set(`media ${condition}`, bits[i]);
15221639
}

0 commit comments

Comments
 (0)