89 lines
3.8 KiB
JavaScript
89 lines
3.8 KiB
JavaScript
// lex URL state (D1): the path is the view, ?family= the only query parameter,
|
|
// the fragment holds view-local state. Run: node --test tests/lex/*.test.mjs
|
|
import { test } from "node:test";
|
|
import { deepEqual as deq, equal as eq, ok } from "node:assert/strict";
|
|
|
|
import {
|
|
FAMILY_VIEWS,
|
|
VIEWS,
|
|
formatState,
|
|
parseLocation,
|
|
} from "../../infra/gitea/custom/public/assets/dossier/state.js";
|
|
|
|
const at = (pathname, search = "", hash = "") => parseLocation({ pathname, search, hash });
|
|
|
|
test("views and family views", () => {
|
|
deq(VIEWS, ["contents", "dossier", "timeline", "valuation", "payment", "politics", "sources", "grid", "evaluate", "predict", "draft", "review"]);
|
|
deq(FAMILY_VIEWS, VIEWS.filter((v) => v !== "contents"));
|
|
});
|
|
|
|
test("R98's /?family=KEY#dossier is rewritten to the path form", () => {
|
|
eq(at("/", "?family=HOME-VISITS", "#dossier").canonical, "/dossier?family=HOME-VISITS");
|
|
});
|
|
|
|
test("path view, upper-cased family, fragment code and fact", () => {
|
|
const s = at("/timeline", "?family=home-visits", "#code=99343&fact=F12");
|
|
deq([s.family, s.view, s.code, s.fact], ["HOME-VISITS", "timeline", "99343", "F12"]);
|
|
eq(s.canonical, "/timeline?family=HOME-VISITS#code=99343&fact=F12");
|
|
});
|
|
|
|
test("no family and no view is the contents", () => {
|
|
eq(at("/").canonical, "/contents");
|
|
eq(at("/", "", "").view, "contents");
|
|
});
|
|
|
|
test("an unknown path with a family is the dossier", () => {
|
|
eq(at("/nonsense", "?family=X").view, "dossier");
|
|
eq(at("/nonsense", "?family=X").canonical, "/dossier?family=X");
|
|
});
|
|
|
|
test("a family view without a family falls back to contents", () => {
|
|
eq(at("/", "", "#timeline").view, "contents");
|
|
eq(at("/payment").view, "contents");
|
|
eq(at("/payment").canonical, "/contents");
|
|
});
|
|
|
|
test("the snapshot's bare #view is read when the path names none", () => {
|
|
eq(at("/", "?family=HOME-VISITS", "#payment").view, "payment");
|
|
// the path wins over a legacy fragment
|
|
eq(at("/politics", "?family=HOME-VISITS", "#payment").view, "politics");
|
|
});
|
|
|
|
test("a stray ?code= moves to the fragment", () => {
|
|
eq(at("/payment", "?family=HOME-VISITS&code=99350").canonical, "/payment?family=HOME-VISITS#code=99350");
|
|
// the fragment wins over the query
|
|
eq(at("/payment", "?family=HOME-VISITS&code=99350", "#code=99341").code, "99341");
|
|
});
|
|
|
|
test("the family is trimmed and upper-cased", () => {
|
|
eq(at("/dossier", "?family=%20ccm%20").family, "CCM");
|
|
eq(at("/dossier", "?family=").family, "");
|
|
});
|
|
|
|
test("contents keeps the family it came from", () => {
|
|
const s = at("/contents", "?family=HOME-VISITS");
|
|
deq([s.view, s.family, s.canonical], ["contents", "HOME-VISITS", "/contents?family=HOME-VISITS"]);
|
|
});
|
|
|
|
test("formatState encodes the family and the fragment", () => {
|
|
eq(formatState({ family: "A/D-TX", view: "dossier" }), "/dossier?family=A%2FD-TX");
|
|
eq(formatState({ view: "contents" }), "/contents");
|
|
eq(formatState({ family: "HOME-VISITS", view: "payment", code: "99350" }), "/payment?family=HOME-VISITS#code=99350");
|
|
eq(formatState({ family: "HOME-VISITS", view: "dossier", fact: "F2" }), "/dossier?family=HOME-VISITS#fact=F2");
|
|
eq(formatState({ family: "HOME-VISITS", view: "dossier", code: "", fact: "" }), "/dossier?family=HOME-VISITS");
|
|
});
|
|
|
|
test("a slash family key round-trips", () => {
|
|
const url = formatState({ family: "A/D-TX-PROGRAM-HOUR", view: "timeline", code: "99341" });
|
|
const [path, rest] = url.split("?");
|
|
const [search, hash] = rest.split("#");
|
|
const s = at(path, "?" + search, "#" + hash);
|
|
deq([s.family, s.view, s.code, s.canonical], ["A/D-TX-PROGRAM-HOUR", "timeline", "99341", url]);
|
|
});
|
|
|
|
test("parseLocation accepts a URL object", () => {
|
|
const s = parseLocation(new URL("https://lex.test/valuation?family=HOME-VISITS#fact=F4"));
|
|
deq([s.view, s.family, s.fact], ["valuation", "HOME-VISITS", "F4"]);
|
|
ok(s.canonical.startsWith("/valuation?"));
|
|
});
|