Files
reverse-engineering-anything/website/worker.test.mjs
T
morlutoand吴佳浩 1b36fc2d50 fix(scripts): validate package-map ownership and preserve HTML fallback (#10097)
* fix(website): keep the HTML validator when the Markdown route is absent

A caller that prefers Markdown gets the HTML probe with its conditional headers
deleted, so the Markdown route can be evaluated on its own. When the Markdown
sidecar is missing the worker answers with that probe, which never saw the
caller's `If-None-Match`, so a matching HTML ETag returned 200 instead of 304.

Ask the asset service again with the caller's own request before answering with
HTML. The decision moves into `htmlFallback` so the request handler keeps its
complexity below the repository bound.

Verified by executing `node --test website/worker.test.mjs`: without the change
the new case fails with 200 instead of 304, and all four cases pass with it.
`oxlint` reports no findings and `oxfmt --check` passes. The website worker test
is not part of a CI workflow, so this suite runs locally only.

* fix(scripts): resolve package-map imports in the module-boundary guard

The guard resolved only relative, absolute and `file:` specifiers, so every
`#`-prefixed package-map import returned no target and was skipped. A pure-layer
file importing `#javascript-analysis-worker` passed, while the same import
written relatively failed with a `pure-layer` diagnostic: the gate failed open
on the spelling the package map exists to encourage.

Resolve a package-map specifier to the source file that produces its built
output, then apply the same ownership rules. The package map is read once per
repository root.

Verified with the new `scripts/lib/module-boundaries.test.mjs` (2 pass; the
package-map case failed before the change) and by running the complete guard on
this checkout, which still reports 4138 files with no findings, so the new rule
adds no false positive. `oxfmt --check` passes; `oxlint` reports the same single
pre-existing `failedBoundary` complexity warning it reported before this change.
The script tests are not part of a CI workflow, so this suite runs locally only.

* fix(scripts): normalize package targets as file URLs

---------

Co-authored-by: 吴佳浩 <wujiahao@supremind.com>
2026-11-11 08:27:39 -05:00

143 lines
4.5 KiB
JavaScript

import assert from "node:assert/strict";
import { test } from "node:test";
import worker from "./worker.mjs";
function assets({ markdownStatus = 200, requests = [] } = {}) {
return {
ASSETS: {
async fetch(request) {
requests.push(request);
const url = new URL(request.url);
const markdown = url.pathname.endsWith("index.md");
if (url.pathname === "/missing/")
return new Response("Missing", { status: 404 });
if (url.pathname === "/guide")
return Response.redirect(`${url.origin}/guide/`, 307);
const headers = {
"Content-Type": markdown ? "text/plain" : "text/html",
ETag: markdown ? '"markdown"' : '"html"',
Link: '<https://rea.tools/.well-known/ard.json>; rel="ard"',
};
const status =
request.headers.get("If-None-Match") === headers.ETag
? 304
: markdown
? markdownStatus
: 200;
return new Response(
request.method === "HEAD" || status === 304
? null
: markdown
? "# Guide"
: "<h1>Guide</h1>",
{ headers, status },
);
},
},
};
}
await test("HTML is the default and explicit Accept preferences select Markdown", async () => {
for (const [accept, markdown] of [
[null, false],
["*/*", false],
["text/html", false],
["text/markdown", true],
["text/markdown;q=0", false],
["text/markdown;q=0.5,text/html", false],
["text/markdown,text/html;q=0.5", true],
["text/markdown,text/html", false],
["TEXT/MARKDOWN; q=1,text/*;q=0.5", true],
["text/markdown;q=bad", false],
["text/markdown;q=0,*/*;q=1", false],
]) {
const response = await worker.fetch(
new Request("https://rea.tools/guide/", {
headers: accept === null ? {} : { Accept: accept },
}),
assets(),
);
assert.equal(
response.headers.get("Content-Type"),
markdown ? "text/markdown; charset=utf-8" : "text/html",
accept,
);
assert.equal(
await response.text(),
markdown ? "# Guide" : "<h1>Guide</h1>",
accept,
);
assert.match(response.headers.get("Vary"), /Accept/);
assert.match(response.headers.get("Link"), /rel="ard"/);
assert.match(
response.headers.get("Link"),
/<\/guide\/index.md>; rel="alternate"/,
);
}
});
await test("HEAD and validators use the selected representation", async () => {
for (const method of ["GET", "HEAD"]) {
const response = await worker.fetch(
new Request("https://rea.tools/guide/", {
method,
headers: { Accept: "text/markdown", "If-None-Match": '"html"' },
}),
assets(),
);
assert.equal(response.status, 200);
assert.equal(response.headers.get("ETag"), '"markdown"');
assert.equal(await response.text(), method === "HEAD" ? "" : "# Guide");
}
for (const [accept, tag] of [
["text/html", '"html"'],
["text/markdown", '"markdown"'],
]) {
const response = await worker.fetch(
new Request("https://rea.tools/guide/", {
headers: { Accept: accept, "If-None-Match": tag },
}),
assets(),
);
assert.equal(response.status, 304);
assert.equal(await response.text(), "");
assert.match(response.headers.get("Vary"), /Accept/);
}
});
await test("missing Markdown keeps HTML, and asset redirects and missing routes survive", async () => {
const requests = [];
const html = await worker.fetch(
new Request("https://rea.tools/guide/", {
headers: { Accept: "text/markdown" },
}),
assets({ markdownStatus: 404, requests }),
);
assert.equal(await html.text(), "<h1>Guide</h1>");
assert.equal(requests.length, 2, "reuse the unchanged HTML probe");
for (const [path, status] of [
["/guide", 307],
["/missing/", 404],
]) {
const response = await worker.fetch(
new Request(`https://rea.tools${path}`, {
headers: { Accept: "text/markdown" },
}),
assets(),
);
assert.equal(response.status, status);
}
});
await test("a Markdown-preferring client keeps the HTML validator when the sidecar is missing", async () => {
const requests = [];
const response = await worker.fetch(
new Request("https://rea.tools/guide/", {
headers: { Accept: "text/markdown", "If-None-Match": '"html"' },
}),
assets({ markdownStatus: 404, requests }),
);
assert.equal(response.status, 304);
assert.equal(requests.length, 3, "reevaluate the original HTML validator");
});