From 3fea1b0d714c47f99f611a1e4a1291ff6289003b Mon Sep 17 00:00:00 2001 From: Ralph Date: Sat, 25 Jul 2026 13:56:57 -0400 Subject: [PATCH] test: harden build-output assertions The deferral check counted preload=none globally, so a stray string elsewhere could mask an eager video; the .jpg scan covered only the homepage of 106 built pages; an empty poster folder passed; and the frozen byte baseline would falsely fail when a poster is legitimately added. Assertions are now per-video-tag, all-pages, presence-checked, and per-poster (110KB cap), with fileURLToPath for path safety. --- docs/tests/build-output.test.js | 66 +++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 27 deletions(-) diff --git a/docs/tests/build-output.test.js b/docs/tests/build-output.test.js index da59a4b5..37463528 100644 --- a/docs/tests/build-output.test.js +++ b/docs/tests/build-output.test.js @@ -1,53 +1,65 @@ import { test } from "node:test"; import assert from "node:assert/strict"; import { readFileSync, readdirSync, statSync } from "node:fs"; +import { fileURLToPath } from "node:url"; import { join } from "node:path"; -const DIST = new URL("../dist/", import.meta.url).pathname; +const DIST = fileURLToPath(new URL("../dist/", import.meta.url)); export function readDist(relativePath) { return readFileSync(join(DIST, relativePath), "utf8"); } +export function distHtmlFiles(dir = DIST) { + return readdirSync(dir, { withFileTypes: true }).flatMap((entry) => { + const path = join(dir, entry.name); + if (entry.isDirectory()) return distHtmlFiles(path); + return entry.name.endsWith(".html") ? [path] : []; + }); +} + test("feature posters ship as webp, not jpeg", () => { const posters = readdirSync(join(DIST, "features/posters")); const jpegs = posters.filter((f) => f.endsWith(".jpg")); assert.deepEqual(jpegs, [], `expected no .jpg posters, found: ${jpegs}`); + const webps = posters.filter((f) => f.endsWith(".webp")); + assert.ok( + webps.length >= 13, + `expected at least 13 .webp posters, found ${webps.length}`, + ); }); -test("poster payload shrank against the jpeg baseline", () => { +test("every poster stays lean", () => { + // 110 KB per poster: the largest legitimate poster today is 103 KB, and a + // JPEG-sized regression or an unoptimized export blows past this cap. + // Per-file rather than a frozen directory total, so adding posters later + // does not invalidate the guard. + const MAX_POSTER_BYTES = 112640; const dir = join(DIST, "features/posters"); - const total = readdirSync(dir).reduce( - (sum, f) => sum + statSync(join(dir, f)).size, - 0, - ); - // Byte-sum of the 13 jpg posters, measured 2026-07-25. Threshold is 0.75, - // not 0.55: the converted set saves ~51%, but the directory also keeps the - // pre-existing physical-device-location-system-behaviors.webp (105 KB, - // no jpg twin), which this change does not touch. - const BASELINE_BYTES = 856528; - assert.ok( - total <= BASELINE_BYTES * 0.75, - `poster payload ${Math.round(total / 1024)}KB exceeds 75% of baseline`, - ); + for (const file of readdirSync(dir)) { + const size = statSync(join(dir, file)).size; + assert.ok( + size <= MAX_POSTER_BYTES, + `${file} is ${Math.round(size / 1024)}KB, max 110KB`, + ); + } }); test("no rendered page references a .jpg poster", () => { - const html = readDist("index.html"); - assert.equal( - /posters\/[^"']*\.jpg/.test(html), - false, - "homepage still references a .jpg poster", + const offenders = distHtmlFiles().filter((file) => + /posters\/[^"']*\.jpg/.test(readFileSync(file, "utf8")), + ); + assert.deepEqual( + offenders.map((f) => f.slice(DIST.length)), + [], + "pages still reference a .jpg poster", ); }); test("homepage videos stay deferred", () => { const html = readDist("index.html"); - const videoCount = (html.match(/