// The `js` CI job must never need a built wasm bundle. See // context/code/clients.md; the split is what keeps it seconds, not ~4m30s. import { test } from "node:test"; import assert from "node:assert/strict"; import { readFileSync, readdirSync } from "node:fs"; import { fileURLToPath } from "node:url"; const dir = fileURLToPath(new URL(".", import.meta.url)); /// Built by concatenation so this file carries no literal of the thing it /// forbids, and is therefore checked by itself rather than exempt from itself. const FORBIDDEN = "p" + "kg"; /// Comments may name the directory — that is what the header above does. Only /// what the code reaches for is a dependency, so strip comments first, exactly /// as render_surface.test.js does. const code = (name) => readFileSync(dir + name, "utf8") .replace(/\/\*[\s\S]*?\*\//g, " ") .replace(/(^|[^:"'`])\/\/.*$/gm, "$1"); test("no test in web/tests/ reaches for a built bundle", () => { const siblings = readdirSync(dir).filter((f) => f.endsWith(".test.js")); assert.ok(siblings.length > 1, `found ${siblings.length} test files — the glob moved`); assert.ok(siblings.includes("bundle_freedom.test.js"), "this file must check itself"); const offenders = siblings.filter((f) => code(f).includes(FORBIDDEN)); assert.deepEqual( offenders, [], `these belong in web/tests/bundle/, which the browser job runs: ${offenders}`, ); }); test("no test in web/tests/ imports a module that reaches the bundle", () => { // The check above is lexical and DIRECT-only: `import "../app.js"` needs the // bundle transitively and names nothing forbidden. app.js and worker.js are // the two entry points that load it; shell.js and serial.js do not. const siblings = readdirSync(dir).filter((f) => f.endsWith(".test.js")); const reaches = /^\s*import\b[^;]*["']\.\.\/(app|worker)\.js["']/m; const offenders = siblings.filter((f) => reaches.test(code(f))); assert.deepEqual( offenders, [], `importing ../app.js or ../worker.js executes the bundle loader: ${offenders}`, ); }); test("the subdirectory that MAY use a bundle is not the one being checked", () => { // Reading it proves the exemption is a real directory rather than a // convention nobody kept, and that this check is scoped to the flat listing. const nested = readdirSync(dir + "bundle").filter((f) => f.endsWith(".test.js")); assert.ok(nested.length > 0, "web/tests/bundle/ is empty — the split lost its other half"); assert.ok( nested.some((f) => readFileSync(dir + "bundle/" + f, "utf8").includes(FORBIDDEN)), "no bundle test reaches for a bundle, so this check has nothing to be scoped against", ); });