Files
rea/website/worker.test.mjs
188e2bd684 fix(website): honor validators and restore the implicit design system (#14768)
* fix(website): honor validators and restore the implicit design system

- Route every worker answer that skips Markdown negotiation through the HTML
  fallback, so validators and ranges removed for the probe still apply.
- Replace undefined classes with existing components and remove 27 CSS
  classes no page uses.
- Express article text color as a token so component colors win in both
  themes, and use logical properties for text-flow layout in RTL pages.
- Give an untranslated English fallback notice English direction.
- Start the page navigation closed without JavaScript, dismiss it with Escape
  or an outside click, and keep later copy clicks, failed key imports and
  first animation frames from corrupting demo state.
- Sync the theme bootstrap into the Notes fixture through a declared region.

* fix(website): skip Markdown negotiation for requests with a body

Markdown probing takes ownership of the caller request body. Methods other
than GET and HEAD have no Markdown representation, so they reach the asset
service once with the original body and validators.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(website): describe the homepage menu's closed start

The disclosure has no open attribute, so without JavaScript it starts
closed and the summary reveals the fragment links. The guide now matches
that, including overlay dismissal.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* style(website): wrap the worker precondition assertion

oxfmt's 80-column width is what Static checks enforce. The If-Match
comparison in the non-retrieval test exceeded it.

Co-authored-by: morluto <morluto@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: morluto <morluto@users.noreply.github.com>
2026-11-26 17:04:00 +00:00

208 lines
6.7 KiB
JavaScript

import assert from "node:assert/strict";
import { test } from "node:test";
import worker from "./worker.mjs";
function assets({ markdownStatus = 200 } = {}) {
return {
ASSETS: {
async fetch(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 html = await worker.fetch(
new Request("https://rea.tools/guide/", {
headers: { Accept: "text/markdown" },
}),
assets({ markdownStatus: 404 }),
);
assert.equal(html.status, 200);
assert.equal(html.headers.get("Content-Type"), "text/html");
assert.equal(html.headers.get("ETag"), '"html"');
assert.equal(await html.text(), "<h1>Guide</h1>");
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 response = await worker.fetch(
new Request("https://rea.tools/guide/", {
headers: { Accept: "text/markdown", "If-None-Match": '"html"' },
}),
assets({ markdownStatus: 404 }),
);
assert.equal(response.status, 304);
assert.equal(response.headers.get("ETag"), '"html"');
assert.equal(await response.text(), "");
});
await test("methods other than GET and HEAD skip Markdown negotiation", async () => {
// A probe would take the body. The asset service must see the caller's
// method, body and validators exactly once.
for (const method of ["POST", "PUT", "PATCH", "DELETE"]) {
const calls = [];
const env = {
ASSETS: {
async fetch(request) {
const body = request.body === null ? null : await request.text();
calls.push({
method: request.method,
ifMatch: request.headers.get("If-Match"),
body,
});
const preconditionFailed =
request.headers.get("If-Match") === '"html"';
return new Response(preconditionFailed ? null : body, {
status: preconditionFailed ? 412 : 200,
headers: { ETag: '"html"', Allow: "GET, HEAD" },
});
},
},
};
const response = await worker.fetch(
new Request("https://rea.tools/guide/", {
method,
body: "payload",
headers: {
Accept: "text/markdown",
"If-Match": '"html"',
"Content-Type": "text/plain",
},
}),
env,
);
assert.equal(response.status, 412, method);
assert.equal(response.headers.get("ETag"), '"html"', method);
assert.equal(response.headers.get("Allow"), "GET, HEAD", method);
assert.equal(await response.text(), "");
assert.deepEqual(calls, [{ method, ifMatch: '"html"', body: "payload" }]);
}
});
await test("a Markdown preference keeps validators on responses without a Markdown route", async () => {
// The probe drops validators for the Markdown representation. A response
// that never reaches Markdown negotiation must still honor them.
for (const path of ["/guide/page", "/assets/site.webmanifest"]) {
const response = await worker.fetch(
new Request(`https://rea.tools${path}`, {
headers: { Accept: "text/markdown", "If-None-Match": '"html"' },
}),
assets(),
);
assert.equal(response.status, 304, path);
}
const fresh = await worker.fetch(
new Request("https://rea.tools/guide/page", {
headers: { Accept: "text/markdown" },
}),
assets(),
);
assert.equal(fresh.status, 200);
assert.equal(await fresh.text(), "<h1>Guide</h1>");
});