Files
stack/tests/lex/cite.test.mjs
kert 55bc6bda63
Some checks failed
CI / lint (push) Successful in 1m8s
CI / notebooks-smoke (push) Successful in 2m8s
Deploy / changes (push) Successful in 18s
CI / test (push) Failing after 5m36s
Infra CI / notebooks (push) Successful in 2m24s
Infra CI / zotero (push) Successful in 34s
Infra CI / docs (push) Successful in 1m49s
Infra CI / api (push) Successful in 1m54s
Infra CI / llm (push) Successful in 1m34s
Deploy / report (push) Has been cancelled
Deploy / notebooks (push) Has been cancelled
Deploy / zotero (push) Has been cancelled
Deploy / docs (push) Has been cancelled
Deploy / api (push) Has been cancelled
Deploy / bcda (push) Has been cancelled
Deploy / llm (push) Has been cancelled
Deploy / mc (push) Has been cancelled
Infra CI / bcda (push) Has been cancelled
Infra CI / mc (push) Has been cancelled
fix(lex): consecutive identical cites collapse to one counted chip with a drawer list (S1-P11); held review items show text, kind and CMS's summary year; live cite-object fixture
2026-11-13 03:53:56 -05:00

203 lines
8.0 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 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, groupHtml, 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));
});
const mk = (id, label, extra = {}) => ({ id, kind: "event", year: 2008, text: "t" + id, cite: { kind: "fr_para", label, url: "https://example.test/" + label.replace(/ /g, "-"), locator: "" }, ...extra });
test("tagged collapses a run of identical cites into one counted chip (S1-P11)", () => {
const fs = {};
let txt = "Run ";
for (let i = 1; i <= 14; i++) {
fs["F" + i] = mk("F" + i, "CY2008 PFS final 72 FR 66361 Table 14");
txt += `[F${i}]`;
}
const h = tagged(txt + " end.", fs);
eq(count(h, 'class="cite group"'), 1);
ok(h.includes("CY2008 PFS final 72 FR 66361 Table 14 ×14"));
ok(h.includes('data-refs="F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 F13 F14"'));
ok(!h.includes("<a ") && !h.includes("[F"));
});
test("tagged keeps distinct cites in order and a different cite ends a run", () => {
const fs = { F1: mk("F1", "A"), F2: mk("F2", "A"), F3: mk("F3", "B"), F4: mk("F4", "A"), F5: mk("F5", "A") };
const h = tagged("x [F1][F2][F3][F4][F5].", fs);
eq(count(h, "<a "), 1);
eq(count(h, "group"), 2);
ok(h.indexOf("A ×2") < h.indexOf(">B<") && h.indexOf(">B<") < h.lastIndexOf("A ×2"));
eq(count(h, "A ×2"), 2);
});
test("an uncited tag in a run stays text and ends the run", () => {
const fs = { F1: mk("F1", "A"), F2: { id: "F2", kind: "event", text: "n" }, F3: mk("F3", "A") };
const h = tagged("[F1][F2][F3]", fs);
ok(h.includes("[F2]"));
eq(count(h, "group"), 0);
eq(count(h, "<a "), 2);
});
test("groupHtml lists the collapsed facts, each opening its own evidence", () => {
const f1 = mk("F1", "A <x>");
const h = groupHtml([f1, mk("F2", "A <x>")], f1.cite);
ok(h.includes("2 facts cite this source") && h.includes('data-ref="F1"') && h.includes('data-ref="F2"'));
ok(h.includes("tF1") && h.includes("A &lt;x&gt;") && !h.includes("<x>"));
ok(h.includes("data-close") && h.includes('href="https://example.test/A-<x>"'.replace("<x>", "&lt;x&gt;")));
});