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: '; 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" : "

Guide

", { 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" : "

Guide

", 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(), "

Guide

"); 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(), "

Guide

"); });