iterate.com / docs

git clone https://git.iterate.com/repos/docs

docs / tests / notes.test.mjsmain at 8463f41

// notes.test.mjs — `node --test tests/notes.test.mjs`. The docs app's notes route (notes.ts): which paths
// it answers, the headers that keep a note from acting as the member, and what a missing file gets.
import { test } from "node:test";
import assert from "node:assert/strict";
import { NOTE_HEADERS, isNotePath, noteResponse } from "../notes.ts";

const files = { "/notes/onboarding-map-2026-11-17.html": "<!doctype html><title>map</title><a href=\"#a\">a</a>" };
const asked = [];
const read = async (path) => {
  asked.push(path);
  if (!(path in files)) throw new Error(`files: nothing at ${JSON.stringify(path)}`);
  return new TextEncoder().encode(files[path]);
};
const get = (path, init) => noteResponse(new Request(`https://docs.example.com${path}`, init), read);

test("a stored note is answered inline as HTML, with the headers that fence it", async () => {
  const answer = await get("/notes/onboarding-map-2026-11-17.html");
  assert.equal(answer.status, 200);
  assert.equal(await answer.text(), files["/notes/onboarding-map-2026-11-17.html"]);
  assert.equal(answer.headers.get("content-type"), "text/html; charset=utf-8");
  assert.equal(answer.headers.get("content-disposition"), null);
  const policy = answer.headers.get("content-security-policy");
  assert.match(policy, /^sandbox;/);
  assert.doesNotMatch(policy, /allow-scripts|allow-same-origin|script-src/);
  assert.match(policy, /default-src 'none'/);
  assert.equal(answer.headers.get("x-content-type-options"), "nosniff");
  assert.equal(answer.headers.get("cache-control"), "private, no-store");
  assert.match(answer.headers.get("x-robots-tag"), /noindex/);
});

test("a HEAD has the headers and no body", async () => {
  const answer = await get("/notes/onboarding-map-2026-11-17.html", { method: "HEAD" });
  assert.equal(answer.status, 200);
  assert.equal(await answer.text(), "");
});

test("only a strict name under /notes/ that ends in .html is read", async () => {
  for (const path of [
    "/notes/",
    "/notes/a.md",
    "/notes/a.html.md",
    "/notes/A.html",
    "/notes/a_b.html",
    "/notes/a.b.html",
    "/notes/-a.html",
    "/notes/sub/a.html",
    "/notes/%2e%2e/secrets.html",
    "/notes/..%2fsecrets.html",
    "/notes/a.html/",
    `/notes/${"a".repeat(101)}.html`,
  ]) {
    asked.length = 0;
    const answer = await get(path);
    assert.equal(answer.status, 404, path);
    assert.deepEqual(asked, [], `${path} must not reach the files`);
    assert.equal(answer.headers.get("content-type"), "text/plain; charset=utf-8");
  }
});

test("a note that is not there is a 404, and another fault is thrown on", async () => {
  assert.equal((await get("/notes/no-such-note.html")).status, 404);
  await assert.rejects(
    noteResponse(new Request("https://docs.example.com/notes/a.html"), async () => {
      throw new Error("the files did not answer");
    }),
    /did not answer/,
  );
});

test("a write to a note's address reads nothing", async () => {
  asked.length = 0;
  const answer = await get("/notes/onboarding-map-2026-11-17.html", { method: "POST", body: "x" });
  assert.equal(answer.status, 404);
  assert.deepEqual(asked, []);
});

test("the docs app asks the route for every path under /notes/ and for no other", () => {
  assert.equal(isNotePath("/notes/a.html"), true);
  assert.equal(isNotePath("/notes/../x"), true);
  assert.equal(isNotePath("/notes"), false);
  assert.equal(isNotePath("/"), false);
  assert.equal(isNotePath("/itx"), false);
  assert.equal(Object.keys(NOTE_HEADERS).length, 6);
});