273 lines
11 KiB
JavaScript
273 lines
11 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, focusPath, 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("><b>&</a>"));
|
||
ok(!h.includes("<b>"));
|
||
ok(h.includes('href="https://example.test/a?b=1&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 <i>b</i> & 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("<y>"));
|
||
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>{"g":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("one source appears once per line, at its first position; the chip lists every fact (S1-P12)", () => {
|
||
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, "B is the only plain link");
|
||
eq(count(h, "group"), 1);
|
||
eq(count(h, "A ×4"), 1);
|
||
ok(h.includes('data-refs="F1 F2 F4 F5"') && h.indexOf("A ×4") < h.indexOf(">B<"));
|
||
});
|
||
|
||
test("a source repeated later in the line is dropped, its fact kept in the first chip", () => {
|
||
const fs = { F1: mk("F1", "A"), F2: mk("F2", "B"), F3: mk("F3", "A") };
|
||
const h = tagged("one [F1] two [F2] three [F3].", fs);
|
||
eq(count(h, 'data-refs="F1 F3"'), 1);
|
||
eq(count(h, ">B<"), 1);
|
||
ok(h.includes("A ×2") && h.endsWith("three ."));
|
||
});
|
||
|
||
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, "<a "), 0);
|
||
eq(count(h, 'data-refs="F1 F3"'), 1);
|
||
eq(count(h, "A ×2"), 1);
|
||
});
|
||
|
||
test("a library key opens the focus pane; the path is not a citation url", () => {
|
||
const cite = {
|
||
kind: "comment",
|
||
label: "Comment 9",
|
||
url: "https://example.test/c?x=1",
|
||
locator: "cmt",
|
||
item_key: "ABCD1234",
|
||
};
|
||
eq(focusPath(cite), "/ui/pdf/ABCD1234?protocol=comment-stance");
|
||
const h = evidenceHtml({ id: "F9", kind: "event", text: "t", cite });
|
||
ok(h.includes('data-focus-src="/ui/pdf/ABCD1234?protocol=comment-stance"'));
|
||
ok(h.includes(">Open in focus pane</button>"));
|
||
ok(h.includes('sandbox="allow-scripts allow-same-origin"'));
|
||
ok(h.includes('class="focus-pane"') && h.includes('title="Focus pane"'));
|
||
ok(h.includes('href="https://example.test/c?x=1"'), "the citation link stays the url the API gave");
|
||
const pane = h.slice(h.indexOf("data-focus-src"));
|
||
ok(!pane.includes("https://") && !pane.includes("example.test"));
|
||
});
|
||
|
||
test("a federal register cite uses the disposition protocol", () => {
|
||
eq(
|
||
focusPath({ kind: "fr_para", label: "p", url: "https://example.test/fr", item_key: "RULEKEY1" }),
|
||
"/ui/pdf/RULEKEY1?protocol=cms-disposition",
|
||
);
|
||
eq(
|
||
focusPath({ kind: "fr_page", label: "p", url: "", item_key: "RULEKEY1" }),
|
||
"/ui/pdf/RULEKEY1?protocol=cms-disposition",
|
||
);
|
||
});
|
||
|
||
test("a licensed book never gets a focus pane, even with a library key", () => {
|
||
const cite = {
|
||
kind: "cpt_book",
|
||
label: "CPT 2019 p. 20",
|
||
url: "https://example.test/book",
|
||
locator: "CPT 2019 p. 20",
|
||
item_key: "BOOKKEY1",
|
||
};
|
||
eq(focusPath(cite), "");
|
||
const h = evidenceHtml({ id: "F2", kind: "formation", text: "t", cite });
|
||
ok(!h.includes("focus-open") && !h.includes("/ui/pdf") && !h.includes("href"));
|
||
});
|
||
|
||
test("a missing, short, or odd key does not open the pane", () => {
|
||
const base = { kind: "study", label: "S", url: "https://example.test/s", item_key: "" };
|
||
eq(focusPath(base), "");
|
||
eq(focusPath({ ...base, item_key: "ABC" }), "");
|
||
eq(focusPath({ ...base, item_key: "../etc/passwd" }), "");
|
||
eq(focusPath({ ...base, item_key: "has space" }), "");
|
||
eq(focusPath(null), "");
|
||
eq(focusPath(undefined), "");
|
||
});
|
||
|
||
test("groupHtml offers the pane for a keyed source and keeps the citation link", () => {
|
||
const cite = { kind: "study", label: "S", url: "https://example.test/s", locator: "", item_key: "STUDYKEY" };
|
||
const h = groupHtml([mk("F1", "S")], cite);
|
||
ok(h.includes('data-focus-src="/ui/pdf/STUDYKEY?protocol=comment-stance"'));
|
||
ok(h.includes('href="https://example.test/s"'));
|
||
ok(!h.slice(h.indexOf("data-focus-src")).includes("example.test"));
|
||
});
|
||
|
||
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 <x>") && !h.includes("<x>"));
|
||
ok(h.includes("data-close") && h.includes('href="https://example.test/A-<x>"'.replace("<x>", "<x>")));
|
||
});
|