Files
stack/tests/lex/shell.test.mjs

194 lines
8.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// lex shell: tabs, the book rail, masthead status, error panels; fmt helpers;
// the view modules' contract.
import { test } from "node:test";
import { deepEqual as deq, equal as eq, ok } from "node:assert/strict";
import { readFileSync } from "node:fs";
import { NotFound, Rebuilt, SignedOut, Unavailable } from "../../infra/gitea/custom/public/assets/dossier/api.js";
import { builtText, compact, esc, int, money, n2, uniq } from "../../infra/gitea/custom/public/assets/dossier/fmt.js";
import { errorHtml, headHtml, mastHtml, railHtml, tabsHtml } from "../../infra/gitea/custom/public/assets/dossier/shell.js";
import { VIEWS, VIEW_MODULE, parseLocation } from "../../infra/gitea/custom/public/assets/dossier/state.js";
const fx = (name) => JSON.parse(readFileSync(new URL(`./fixtures/${name}.json`, import.meta.url), "utf8"));
const C = fx("contents");
const H = fx("header");
const st = (url) => parseLocation(new URL(url, "https://lex.test"));
test("tabsHtml marks the current tab and links each view", () => {
const h = tabsHtml(st("/timeline?family=HOME-VISITS"));
ok(/<a href="\/timeline\?family=HOME-VISITS"[^>]*class="on"[^>]*aria-current="page"/.test(h), h);
eq((h.match(/aria-current/g) || []).length, 1);
ok(h.includes('href="/dossier?family=HOME-VISITS"'));
ok(h.includes('href="/contents?family=HOME-VISITS"'));
ok(h.includes('<span class="grp">HOME-VISITS</span>'));
});
test("coming tabs are class soon", () => {
const h = tabsHtml(st("/dossier?family=HOME-VISITS"));
for (const v of ["grid", "evaluate", "predict", "draft", "review"]) {
ok(new RegExp(`<a href="/${v}\\?family=HOME-VISITS"[^>]*class="soon"`).test(h), v);
}
for (const v of ["dossier", "timeline", "valuation", "payment", "politics", "sources"]) {
ok(!new RegExp(`<a href="/${v}\\?family=HOME-VISITS"[^>]*soon`).test(h), v);
}
ok(/class="soon on"[^>]*aria-current/.test(tabsHtml(st("/review?family=HOME-VISITS"))));
});
test("without a family only Contents is a tab", () => {
const h = tabsHtml(st("/contents"));
ok(/<a href="\/contents"[^>]*class="on"/.test(h));
eq((h.match(/<a /g) || []).length, 1);
});
test("the family key is encoded in tab links and escaped as the group label", () => {
const h = tabsHtml(st("/dossier?family=A%2FD%3CX"));
ok(h.includes('href="/timeline?family=A%2FD%3CX"'));
ok(h.includes('<span class="grp">A/D&lt;X</span>'));
});
test("signed-out panel links to the current URL", () => {
const href = "https://lex.test/timeline?family=HOME-VISITS#code=99343&fact=F12";
const h = errorHtml(new SignedOut(), st(href), href);
ok(h.includes("Signed out"));
ok(h.includes(`href="${esc(href)}"`));
ok(h.includes('role="alert"'));
ok(h.includes('class="panel err"'));
});
test("not-found panel names the status", () => {
const err = new NotFound("No dossier is built for CCM yet.", { family: "CCM", status: "planned" });
const h = errorHtml(err, st("/dossier?family=CCM"), "https://lex.test/dossier?family=CCM");
ok(h.includes("planned"));
ok(h.includes("CCM"));
ok(h.includes('href="/contents"'));
for (const status of ["curated", "derived", "unknown", ""]) {
const x = errorHtml(new NotFound("m", { family: "X", status }), st("/dossier?family=X"), "u");
ok(x.includes('href="/contents"'), status);
ok(x.includes(status || "No dossier"), status);
}
});
test("unavailable panel", () => {
const href = "https://lex.test/payment?family=HOME-VISITS";
const h = errorHtml(new Unavailable("The data replica is not available."), st(href), href);
ok(h.includes("Unavailable"));
ok(h.includes("not available"));
ok(h.includes(`href="${href}"`));
});
test("rebuilt panel asks for a reload", () => {
const href = "https://lex.test/payment?family=HOME-VISITS";
const h = errorHtml(new Rebuilt(), st(href), href);
ok(h.includes("rebuilt"));
ok(h.includes(`href="${href}"`));
});
test("any other error is a panel with its message escaped, never a blank pane", () => {
const h = errorHtml(new Error("boom <x>"), st("/dossier?family=X"), "u");
ok(h.includes("boom &lt;x&gt;"));
ok(h.includes('href="u"'));
});
test("railHtml links built families and marks the current one", () => {
const h = railHtml(C, st("/payment?family=HOME-VISITS"));
ok(/<li class="cur"[^>]*><a href="\/dossier\?family=HOME-VISITS"/.test(h), h);
ok(h.includes("Home or Residence Services"));
});
test("railHtml: planned rows have no <a>", () => {
const h = railHtml(C, st("/contents"));
const planned = h.match(/<li class="planned[^"]*"[^>]*>.*?<\/li>/g) || [];
eq(planned.length, C.planned.length);
ok(planned.every((li) => !li.includes("<a ")));
ok(!h.includes('class="cur"'), "no current family on the contents");
});
test("railHtml search names cover family, name and codes", () => {
const h = railHtml(C, st("/contents"));
ok(h.includes('data-name="home-visits home or residence services 99341 and 99343"'));
ok(h.includes('data-name="ccm chronic care management (ccm) 99490'));
});
test("railHtml ends with the machine-tier note", () => {
const h = railHtml(C, st("/contents"));
ok(h.trimEnd().endsWith("</p>"));
ok(h.includes('<p class="note">'));
ok(h.includes(`${C.machine.derived.toLocaleString("en-US")} derived code families`));
ok(railHtml({ ...C, machine: null }, st("/contents")).includes("machine-built"));
});
test("railHtml marks a machine-tier family and a planned current family", () => {
const fam = { ...C.families[0], family: "X-1", name: "Derived X", tier: "machine" };
const h = railHtml({ ...C, families: [...C.families, fam] }, st("/dossier?family=CCM"));
ok(/<li[^>]*><a href="\/dossier\?family=X-1">Derived X<\/a><span class="tier">Machine<\/span>/.test(h), h);
ok(/<li class="planned cur"[^>]*><span>Chronic care management \(CCM\)<\/span>/.test(h), h);
});
test("mastHtml: live status of the family's build", () => {
const h = mastHtml(st("/dossier?family=HOME-VISITS"), H, C);
ok(h.includes("Live"));
ok(h.includes("HOME-VISITS dossier built 2026-11-13 05:23 UTC"));
ok(h.includes("citation check passed"));
ok(h.includes("health-ok"));
const bad = mastHtml(st("/dossier?family=HOME-VISITS"), { ...H, check_passed: false, tier: "machine" }, C);
ok(bad.includes("citation check failed") && bad.includes("health-warn"));
ok(bad.includes("machine-built, not reviewed"));
});
test("mastHtml: the book's status on the contents, and while loading", () => {
const h = mastHtml(st("/contents"), null, C);
ok(h.includes("Live"));
ok(h.includes("1 dossier built"));
ok(h.includes("2026-11-13 05:23 UTC"));
ok(mastHtml(st("/contents"), null, null).includes("health-unknown"));
});
test("headHtml: family header and contents header", () => {
const h = headHtml(st("/dossier?family=HOME-VISITS"), H);
ok(h.includes("<h2>Home or Residence Services</h2>"));
ok(h.includes("HOME-VISITS"));
ok(h.includes("Reviewed dossier"));
ok(h.includes("phase 1"));
ok(h.includes("Active 99341 · deleted 99343 · linked G0318 · years 2007–2027"));
ok(headHtml(st("/dossier?family=X"), { ...H, code_groups: { active: ["99341", "99342", "99343", "99345"] } }).includes("Active 99341–99343, 99345"));
ok(headHtml(st("/dossier?family=HOME-VISITS"), { ...H, tier: "machine", phase: null }).includes("Machine-built"));
ok(headHtml(st("/contents"), null).includes("<h2>Contents</h2>"));
});
test("fmt: esc, compact, money, n2, int, uniq", () => {
eq(esc(`<a href="x">'&'</a>`), "&lt;a href=&quot;x&quot;&gt;&#39;&amp;&#39;&lt;/a&gt;");
eq(esc(null), "");
eq(esc(0), "0");
eq(compact(["99343", "99341", "99342", "99344", "G0318", "99350", "99341"]), "99341–99344, 99350, G0318");
eq(compact(["99341", "99342"]), "99341, 99342");
eq(compact([]), "");
eq(money(12.5), "$12.50");
eq(money(null), "—");
eq(n2(1.5), "1.50");
eq(n2(undefined), "—");
eq(int(12106.4), "12,106");
eq(int(null), "—");
deq(uniq(["a", "b", "a"]), ["a", "b"]);
});
test("fmt: builtText", () => {
eq(builtText("2026-11-13T05:23:37"), "2026-11-13 05:23 UTC");
eq(builtText("2026-11-13T05:23:37.123456"), "2026-11-13 05:23 UTC");
eq(builtText("2026-11-13T00:23:37-05:00"), "2026-11-13 05:23 UTC");
eq(builtText("2026-11-13T05:23:37Z"), "2026-11-13 05:23 UTC");
eq(builtText(""), "");
eq(builtText(null), "");
});
test("every view has a module with needs and render", async () => {
deq(Object.keys(VIEW_MODULE), VIEWS);
deq(uniq(Object.values(VIEW_MODULE)).sort(), ["coming", "contents", "dossier", "payment", "politics", "review", "sources", "timeline", "valuation"]);
for (const name of uniq(Object.values(VIEW_MODULE))) {
const m = await import(`../../infra/gitea/custom/public/assets/dossier/view-${name}.js`);
ok(Array.isArray(m.needs), name);
ok(m.needs.every((n) => ["contents", "header", "timeline", "valuation", "payment", "politics", "sources", "facts"].includes(n)), name);
eq(typeof m.render, "function", name);
if (m.mount) eq(typeof m.mount, "function", name);
}
});