221 lines
8.9 KiB
JavaScript
221 lines
8.9 KiB
JavaScript
// lex API client: errors the page can render, and one build per page.
|
|
import { test } from "node:test";
|
|
import { deepEqual as deq, equal as eq, ok, rejects } from "node:assert/strict";
|
|
import { readFileSync } from "node:fs";
|
|
|
|
import {
|
|
CONTENTS_PATH,
|
|
DossierStore,
|
|
NotFound,
|
|
Rebuilt,
|
|
SignedOut,
|
|
Unavailable,
|
|
factPath,
|
|
getJSON,
|
|
sectionPath,
|
|
} from "../../infra/gitea/custom/public/assets/dossier/api.js";
|
|
|
|
const fx = (name) => JSON.parse(readFileSync(new URL(`./fixtures/${name}.json`, import.meta.url), "utf8"));
|
|
|
|
/** A fetch Response stand-in (a real Response cannot be an opaque redirect). */
|
|
function res({ status = 200, type = "basic", ct = "application/json", body = {} } = {}) {
|
|
return {
|
|
status,
|
|
type,
|
|
ok: status >= 200 && status < 300,
|
|
headers: new Headers(ct ? { "content-type": ct } : {}),
|
|
json: async () => (typeof body === "string" ? JSON.parse(body) : body),
|
|
text: async () => (typeof body === "string" ? body : JSON.stringify(body)),
|
|
};
|
|
}
|
|
|
|
/** A fetch that answers from a list per path prefix and records each call. */
|
|
function fake(routes) {
|
|
const calls = [];
|
|
const left = Object.fromEntries(Object.entries(routes).map(([k, v]) => [k, Array.isArray(v) ? [...v] : [v]]));
|
|
const impl = async (path, init) => {
|
|
calls.push({ path, init });
|
|
const key = Object.keys(left).find((k) => path.startsWith(k));
|
|
if (!key) return res({ status: 404, body: { detail: { message: "no route" } } });
|
|
const q = left[key];
|
|
const r = q.length > 1 ? q.shift() : q[0];
|
|
return typeof r === "function" ? r() : r;
|
|
};
|
|
impl.calls = calls;
|
|
return impl;
|
|
}
|
|
|
|
test("paths: section, contents, fact", () => {
|
|
eq(sectionPath("timeline", "A/D"), "/api/policy/dossier/timeline?family=A%2FD");
|
|
eq(CONTENTS_PATH, "/api/policy/contents");
|
|
eq(factPath("A/D", "F12"), "/api/policy/fact?family=A%2FD&id=F12");
|
|
});
|
|
|
|
test("getJSON asks for a manual redirect and returns the body", async () => {
|
|
const f = fake({ "/x": res({ body: { a: 1 } }) });
|
|
deq(await getJSON("/x", f), { a: 1 });
|
|
eq(f.calls[0].init.redirect, "manual");
|
|
});
|
|
|
|
test("opaque redirect is SignedOut", async () => {
|
|
await rejects(getJSON("/x", fake({ "/x": res({ status: 0, type: "opaqueredirect", ct: "" }) })), SignedOut);
|
|
});
|
|
|
|
test("status 0 is SignedOut", async () => {
|
|
await rejects(getJSON("/x", fake({ "/x": res({ status: 0, ct: "" }) })), SignedOut);
|
|
});
|
|
|
|
test("html body is SignedOut", async () => {
|
|
await rejects(getJSON("/x", fake({ "/x": res({ ct: "text/html; charset=utf-8", body: "<html>sign in</html>" }) })), SignedOut);
|
|
});
|
|
|
|
test("401 and 403 are SignedOut", async () => {
|
|
await rejects(getJSON("/x", fake({ "/x": res({ status: 401 }) })), SignedOut);
|
|
await rejects(getJSON("/x", fake({ "/x": res({ status: 403, ct: "text/plain" }) })), SignedOut);
|
|
});
|
|
|
|
test("404 is NotFound with the family's status", async () => {
|
|
const body = { detail: { message: "No dossier is built for CCM yet.", family: "CCM", status: "planned" } };
|
|
const err = await getJSON("/x", fake({ "/x": res({ status: 404, body }) })).catch((e) => e);
|
|
ok(err instanceof NotFound);
|
|
eq(err.status, "planned");
|
|
eq(err.family, "CCM");
|
|
eq(err.message, "No dossier is built for CCM yet.");
|
|
});
|
|
|
|
test("a 404 without a JSON detail is NotFound with no status", async () => {
|
|
const err = await getJSON("/x", fake({ "/x": res({ status: 404, ct: "application/json", body: "{}" }) })).catch((e) => e);
|
|
ok(err instanceof NotFound);
|
|
eq(err.status, "");
|
|
});
|
|
|
|
test("503 is Unavailable, as are 502 and 504 from the proxy", async () => {
|
|
const body = { detail: { message: "The data replica is not available." } };
|
|
const err = await getJSON("/x", fake({ "/x": res({ status: 503, body }) })).catch((e) => e);
|
|
ok(err instanceof Unavailable);
|
|
eq(err.message, "The data replica is not available.");
|
|
await rejects(getJSON("/x", fake({ "/x": res({ status: 502, ct: "text/plain", body: "Bad Gateway" }) })), Unavailable);
|
|
await rejects(getJSON("/x", fake({ "/x": res({ status: 504, ct: "" }) })), Unavailable);
|
|
});
|
|
|
|
test("a network failure is Unavailable", async () => {
|
|
await rejects(getJSON("/x", async () => { throw new TypeError("Failed to fetch"); }), Unavailable);
|
|
});
|
|
|
|
test("another error status is a plain Error naming it", async () => {
|
|
const err = await getJSON("/x", fake({ "/x": res({ status: 400, body: { detail: { message: "invalid family key" } } }) })).catch((e) => e);
|
|
ok(err instanceof Error && !(err instanceof NotFound) && !(err instanceof SignedOut));
|
|
ok(err.message.includes("400"));
|
|
ok(err.message.includes("invalid family key"));
|
|
});
|
|
|
|
const H = fx("header");
|
|
const T = fx("timeline");
|
|
const at = (slice, built_at) => ({ ...slice, built_at });
|
|
|
|
test("the store loads what a view needs, adds the header, and caches", async () => {
|
|
const f = fake({
|
|
"/api/policy/dossier/header": res({ body: H }),
|
|
"/api/policy/dossier/timeline": res({ body: T }),
|
|
"/api/policy/contents": res({ body: fx("contents") }),
|
|
});
|
|
const s = new DossierStore("HOME-VISITS", f);
|
|
const d = await s.load(["timeline", "contents"]);
|
|
eq(d.header.name, H.name);
|
|
eq(d.timeline.events.length, T.events.length);
|
|
eq(d.contents.families[0].family, "HOME-VISITS");
|
|
eq(f.calls.length, 3);
|
|
await s.load(["header", "timeline"]);
|
|
eq(f.calls.length, 3, "a second load is served from the cache");
|
|
ok(f.calls.every((c) => c.init.redirect === "manual"));
|
|
ok(f.calls.some((c) => c.path === "/api/policy/dossier/timeline?family=HOME-VISITS"));
|
|
});
|
|
|
|
test("mixed builds refetch", async () => {
|
|
const f = fake({
|
|
"/api/policy/dossier/header": [res({ body: at(H, "T1") }), res({ body: at(H, "T2") })],
|
|
"/api/policy/dossier/timeline": res({ body: at(T, "T2") }),
|
|
});
|
|
const d = await new DossierStore("HOME-VISITS", f).load(["header", "timeline"]);
|
|
eq(d.header.built_at, "T2");
|
|
eq(d.timeline.built_at, "T2");
|
|
eq(f.calls.filter((c) => c.path.startsWith("/api/policy/dossier/header")).length, 2);
|
|
eq(f.calls.filter((c) => c.path.startsWith("/api/policy/dossier/timeline")).length, 2);
|
|
});
|
|
|
|
test("a slice cached from an older build is refetched", async () => {
|
|
const f = fake({
|
|
"/api/policy/dossier/header": [res({ body: at(H, "T1") }), res({ body: at(H, "T2") })],
|
|
"/api/policy/dossier/timeline": res({ body: at(T, "T2") }),
|
|
});
|
|
const s = new DossierStore("HOME-VISITS", f);
|
|
eq((await s.load(["header"])).header.built_at, "T1");
|
|
const d = await s.load(["timeline"]);
|
|
eq(d.header.built_at, "T2");
|
|
});
|
|
|
|
test("persistent mismatch is Rebuilt", async () => {
|
|
const f = fake({
|
|
"/api/policy/dossier/header": res({ body: at(H, "T1") }),
|
|
"/api/policy/dossier/timeline": res({ body: at(T, "T2") }),
|
|
});
|
|
await rejects(new DossierStore("HOME-VISITS", f).load(["header", "timeline"]), Rebuilt);
|
|
eq(f.calls.length, 4, "one refetch, then Rebuilt");
|
|
});
|
|
|
|
test("contents and the facts index carry no build check (S1-P1)", async () => {
|
|
const f = fake({
|
|
"/api/policy/dossier/header": res({ body: at(H, "T1") }),
|
|
"/api/policy/dossier/facts": res({ body: at(fx("facts"), "T0") }),
|
|
"/api/policy/contents": res({ body: fx("contents") }),
|
|
});
|
|
const d = await new DossierStore("HOME-VISITS", f).load(["header", "facts", "contents"]);
|
|
eq(d.facts.built_at, "T0");
|
|
eq(f.calls.length, 3);
|
|
});
|
|
|
|
test("every per-family slice is checked: sources too", async () => {
|
|
const f = fake({
|
|
"/api/policy/dossier/header": res({ body: at(H, "T1") }),
|
|
"/api/policy/dossier/sources": res({ body: at(fx("sources"), "T2") }),
|
|
});
|
|
await rejects(new DossierStore("HOME-VISITS", f).load(["sources"]), Rebuilt);
|
|
});
|
|
|
|
test("a failed slice is not cached", async () => {
|
|
const f = fake({
|
|
"/api/policy/dossier/header": [res({ status: 503 }), res({ body: H })],
|
|
});
|
|
const s = new DossierStore("HOME-VISITS", f);
|
|
await rejects(s.load(["header"]), Unavailable);
|
|
eq((await s.load(["header"])).header.name, H.name);
|
|
});
|
|
|
|
test("the contents store needs no family", async () => {
|
|
const f = fake({ "/api/policy/contents": res({ body: fx("contents") }) });
|
|
const d = await new DossierStore("", f).load(["contents"]);
|
|
deq(Object.keys(d), ["contents"]);
|
|
});
|
|
|
|
test("fact(id) reads a loaded slice before asking the API", async () => {
|
|
const f = fake({
|
|
"/api/policy/dossier/header": res({ body: H }),
|
|
"/api/policy/dossier/timeline": res({ body: T }),
|
|
"/api/policy/fact": res({ body: { ...T.facts.F3, id: "F99" } }),
|
|
});
|
|
const s = new DossierStore("HOME-VISITS", f);
|
|
await s.load(["timeline"]);
|
|
eq((await s.fact("F3")).text, T.facts.F3.text);
|
|
eq((await s.fact("F2")).note, "model paraphrase, unreviewed");
|
|
eq(f.calls.length, 2);
|
|
eq((await s.fact("F99")).id, "F99");
|
|
eq(f.calls[2].path, "/api/policy/fact?family=HOME-VISITS&id=F99");
|
|
await s.fact("F99");
|
|
eq(f.calls.length, 3, "a fetched fact is cached");
|
|
});
|
|
|
|
test("fact(id) for an unknown id is NotFound", async () => {
|
|
const f = fake({ "/api/policy/fact": res({ status: 404, body: { detail: { message: "unknown fact", family: "HOME-VISITS", id: "F404" } } }) });
|
|
await rejects(new DossierStore("HOME-VISITS", f).fact("F404"), NotFound);
|
|
});
|