Files
stack/tests/lex/cite.test.mjs
kert 0082ec80ce
Some checks failed
CI / lint (push) Successful in 58s
CI / notebooks-smoke (push) Successful in 2m3s
Deploy / changes (push) Successful in 18s
Infra CI / notebooks (push) Successful in 59s
CI / test (push) Failing after 4m32s
Infra CI / zotero (push) Successful in 29s
Infra CI / docs (push) Successful in 38s
Infra CI / api (push) Successful in 1m32s
Infra CI / bcda (push) Successful in 2m29s
Infra CI / llm (push) Successful in 1m26s
Deploy / notebooks (push) Has been skipped
Deploy / zotero (push) Has been skipped
Deploy / docs (push) Has been skipped
Infra CI / mc (push) Successful in 33s
Deploy / api (push) Successful in 1m42s
Deploy / report (push) Has been cancelled
Deploy / mc (push) Has been cancelled
Deploy / bcda (push) Has been cancelled
Deploy / llm (push) Has been cancelled
fix(lex): facts index carries also-cite locators and the probe matches book also-cites exactly (S1-P10); the current tab scrolls into view on phones; book title only on licensed books; a citeless [F#] stays as text (S1)
2026-11-13 03:42:52 -05:00

162 lines
6.1 KiB
JavaScript

// lex citations (R10): the page renders cite.url, or cite.locator when the url
// is empty, exactly as the API gives them; a licensed book never links.
import { test } from "node:test";
import { equal as eq, ok } from "node:assert/strict";
import { readFileSync } from "node:fs";
import { citeHtml, evidenceHtml, factHtml, tagged } from "../../infra/gitea/custom/public/assets/dossier/cite.js";
const fx = (name) => JSON.parse(readFileSync(new URL(`./fixtures/${name}.json`, import.meta.url), "utf8"));
const H = fx("header");
const P = fx("politics");
const facts = { ...H.facts, ...fx("timeline").facts, ...fx("valuation").facts, ...P.facts };
const count = (s, sub) => s.split(sub).length - 1;
test("a cite with a url is a link that opens apart", () => {
const c = facts.F4.cite;
eq(citeHtml(c), `<a class="cite" href="${c.url}" target="_blank" rel="noopener">${c.label}</a>`);
});
test("no url renders a locator span", () => {
const h = citeHtml(H.facts.F2.cite);
ok(!h.includes("href"));
ok(h.startsWith('<span class="cite book"'));
ok(h.includes("CPT 2019 p. 20"));
ok(h.includes('title="Cited by edition and page; text not reproduced"'));
});
test("the label stands in when there is neither url nor locator", () => {
ok(citeHtml({ kind: "comment", label: "Comment 9", url: "", locator: "" }).includes(">Comment 9</span>"));
});
test("a cite with no url that is not a book is a plain locator span, no book title", () => {
const h = citeHtml({ kind: "iom", label: "100-04 ch.26", url: "", locator: "IOM 100-04 ch.26" });
eq(h, '<span class="cite book">IOM 100-04 ch.26</span>');
ok(!h.includes("title") && !h.includes("not reproduced"));
ok(citeHtml({ kind: "iom", label: "x", url: "", locator: "L" }, { ref: "F7" }).includes('data-ref="F7"'));
});
test("a licensed book never links, even when a url is set", () => {
const h = citeHtml({ ...H.facts.F2.cite, url: "https://example.test/book" });
ok(!h.includes("href"));
ok(h.includes("CPT 2019 p. 20"));
});
test("a url that is not http(s) is never put in an href", () => {
const h = citeHtml({ kind: "fr_para", label: "x", url: "javascript:alert(1)", locator: "L 1" });
ok(!h.includes("href"));
ok(!h.includes("javascript"));
ok(h.includes("L 1"));
});
test("label <b>& is escaped, and the url is not rewritten", () => {
const h = citeHtml({ kind: "fr_para", label: "<b>&", url: "https://example.test/a?b=1&c=2#p3", locator: "" });
ok(h.includes(">&lt;b&gt;&amp;</a>"));
ok(!h.includes("<b>"));
ok(h.includes('href="https://example.test/a?b=1&amp;c=2#p3"'));
});
test("an empty cite renders nothing", () => {
eq(citeHtml(null), "");
eq(citeHtml(undefined), "");
});
test("also cites render", () => {
const h = factHtml(H.facts.F1);
eq(count(h, 'class="cite"'), 2);
ok(h.includes('href="https://example.test/statute/9999"'));
});
test("factHtml carries the id, kind, note pill and text", () => {
const h = factHtml(H.facts.F2);
ok(h.startsWith("<li "));
ok(h.includes('data-fact="F2"'));
ok(h.includes('data-kind="formation"'));
ok(h.includes('<span class="pill model">model paraphrase, unreviewed</span>'));
ok(h.includes("Invented formation paraphrase."));
ok(h.endsWith("</li>"));
});
test("factHtml without a note has no pill", () => {
ok(!factHtml(H.facts.F1).includes("pill"));
});
test("factHtml takes another tag", () => {
const h = factHtml(H.facts.F1, { tag: "div" });
ok(h.startsWith("<div ") && h.endsWith("</div>"));
});
test("factHtml carries a stance split's accuracy", () => {
const h = factHtml(P.facts.F8);
ok(h.includes(`<span class="acc">${P.facts.F8.accuracy}</span>`));
ok(!factHtml(P.facts.F7).includes('class="acc"'), "the accuracy is in F7's own text");
});
test("factHtml escapes the text", () => {
ok(factHtml({ ...H.facts.F1, text: "a <i>b</i> & c" }).includes("a &lt;i&gt;b&lt;/i&gt; &amp; c"));
});
test("tagged turns each [F#] into its cite, once per run", () => {
const h = tagged("A [F4]. B [F3][F3].", facts);
eq(count(h, "<a "), 2);
eq(count(h, 'data-ref="F4"'), 1);
eq(count(h, 'data-ref="F3"'), 1);
ok(h.startsWith("A <a "));
ok(!h.includes("[F"));
});
test("tagged keeps an unknown tag as text and escapes the rest", () => {
const h = tagged("x [F99] <y>", facts);
ok(h.includes("[F99]"));
ok(h.includes("&lt;y&gt;"));
ok(!h.includes("data-ref"));
});
test("tagged renders a book fact as its locator span", () => {
const h = tagged("formed [F2].", facts);
ok(h.includes('data-ref="F2"'));
ok(!h.includes("href"));
ok(h.includes("CPT 2019 p. 20"));
});
test("tagged keeps a known tag whose fact has no cite as visible text", () => {
const h = tagged("x [F1] y [F1][F1].", { F1: { id: "F1", kind: "origin", text: "t" } });
eq(h, "x [F1] y [F1][F1].");
eq(tagged("z [F2].", { F2: { id: "F2", cite: null } }), "z [F2].");
});
test("tagged of empty text is empty", () => {
eq(tagged("", facts), "");
eq(tagged(null, facts), "");
});
test("evidenceHtml shows kind, year, codes, text, note, cites and values", () => {
const h = evidenceHtml(facts.F4);
ok(h.includes('data-evidence="F4"'));
ok(h.includes("Valuation round") || h.includes("round"));
ok(h.includes("CY2023"));
ok(h.includes("99341"));
ok(h.includes("Invented valuation round text."));
ok(h.includes('href="https://example.test/fr/PETM225D#p1676"'));
ok(h.includes("<dl"));
ok(h.includes("<dt>final work rvu</dt><dd>1.5</dd>"));
ok(h.includes("data-close"));
});
test("evidenceHtml lists every also cite and the note; empty values are left out", () => {
const h = evidenceHtml(H.facts.F1);
eq(count(h, 'class="cite"'), 2);
ok(!h.includes("<dt>"), "F1 has no values, year and codes go in the head");
const b = evidenceHtml({ ...H.facts.F2, values: { a: "", b: null, c: [], d: 0, e: ["x", "y"], f: { g: 1 } } });
ok(b.includes("model paraphrase, unreviewed"));
ok(!b.includes("href"));
ok(!b.includes("<dt>a</dt>") && !b.includes("<dt>b</dt>") && !b.includes("<dt>c</dt>"));
ok(b.includes("<dt>d</dt><dd>0</dd>"));
ok(b.includes("<dt>e</dt><dd>x, y</dd>"));
ok(b.includes("<dt>f</dt><dd>{&quot;g&quot;:1}</dd>"));
});
test("evidenceHtml carries a stance split's accuracy", () => {
ok(evidenceHtml(P.facts.F8).includes(P.facts.F8.accuracy));
});