`;
+
+export function render(data) {
+ const P = data.politics || {};
+ const facts = P.facts || {};
+ const panels = P.panels || [];
+ const all = Object.values(facts).sort(byYearId);
+ const lines = [P.stance_accuracy, ...panels.map((p) => p.accuracy)];
+ const of = (kind) => all.filter((f) => f.kind === kind).map((f) => withAccuracy(f, lines));
+ const shown = new Set(["stance_split", "cms_summary", ...STAKES.map(([k]) => k)]);
+ const other = all.filter((f) => !shown.has(f.kind));
+
+ let h = `
Politics
preliminary
+
Who commented on each classified rule event, by organization type and specialty, with names as posted on regulations.gov. Beside each unit: our stance count, the stance its money stakes would predict, its billing share, its payment change and its RUC seats. CMS's own summaries are listed apart and never merged with our counts.
`;
+ h += `
Stance counts. Read by a local model. ${esc(P.stance_accuracy || NO_ACCURACY)}
${list(other)}`;
+ return h;
}
diff --git a/tests/lex/views-politics.test.mjs b/tests/lex/views-politics.test.mjs
new file mode 100644
index 0000000..fe41b86
--- /dev/null
+++ b/tests/lex/views-politics.test.mjs
@@ -0,0 +1,260 @@
+// lex · Politics view. One test per display rule (global-constraints.md,
+// Content rules), plus the brief's checks.
+import { test } from "node:test";
+import { equal as eq, deepEqual as deq, ok } from "node:assert/strict";
+import { readFileSync } from "node:fs";
+
+import { parseLocation } from "../../infra/gitea/custom/public/assets/dossier/state.js";
+import { esc } from "../../infra/gitea/custom/public/assets/dossier/fmt.js";
+import * as politics from "../../infra/gitea/custom/public/assets/dossier/view-politics.js";
+
+const fx = (name) => JSON.parse(readFileSync(new URL(`./fixtures/${name}.json`, import.meta.url), "utf8"));
+const H = fx("header");
+const POL = fx("politics");
+const P0 = POL.panels[0];
+const st = (url) => parseLocation(new URL(url, "https://lex.test"));
+const count = (s, sub) => s.split(sub).length - 1;
+const page = (pol = POL, url = "/politics?family=HOME-VISITS") => politics.render({ header: H, politics: pol }, st(url));
+
+// the live panel marker and unit fields (Plan A fix): see the live dump
+const IDENTITY = "organization type and specialty: model reading, unreviewed";
+const live = (p) => ({
+ ...p,
+ identity: IDENTITY,
+ units: p.units.map((u, i) => ({ ...u, basis: i ? { regs_gov_fields: 1 } : { letterhead_model: 1 }, extractor: ["model:qwen2.5:14b@identity-v5"] })),
+});
+
+/** Each stance panel's own markup (S1-P4: checks are scoped to it). */
+const panels = (out) => out.split(' s.slice(0, s.indexOf("")));
+/** A section's own markup, by its data-sec name. */
+const section = (out, name) => {
+ const i = out.indexOf(`data-sec="${name}"`);
+ return i < 0 ? "" : out.slice(i, out.indexOf("", i));
+};
+
+test("politics: the brief's checks", () => {
+ const out = page();
+ eq(politics.needs.join(), "header,politics");
+ const [p0] = panels(out);
+ ok(p0.indexOf(P0.accuracy) >= 0 && p0.indexOf(P0.accuracy) < p0.indexOf('class="st support"'));
+ eq((out.match(/1 permanent, 0 rotating/g) || []).length, 1);
+ ok(out.includes("a cut of at least $12,106") && !out.includes("-12105") && !out.includes("-12,106"));
+ ok(out.includes("jane q. DOE") && out.includes("ACME health LLC"));
+ ok(out.indexOf("Our classification") < out.indexOf("CMS's summary"));
+ for (const id of Object.keys(POL.facts)) ok(out.includes(`data-fact="${id}"`), id);
+ const bare = page({ ...POL, panels: [{ ...P0, accuracy: "" }] }, "/politics?family=X");
+ ok(bare.includes(politics.NO_ACCURACY));
+});
+
+test("politics: the page-level accuracy panel comes first and carries stance_accuracy", () => {
+ const out = page();
+ const top = out.indexOf("data-stance-accuracy");
+ ok(top >= 0 && top < out.indexOf('", top)).includes(POL.stance_accuracy));
+ // no page-level line: the generic constant, still before any panel
+ const none = page({ ...POL, stance_accuracy: "" });
+ const t2 = none.indexOf("data-stance-accuracy");
+ ok(none.slice(t2, none.indexOf("", t2)).includes(politics.NO_ACCURACY));
+});
+
+test("rule: stance counts appear only with their accuracy line, in each panel's own container (S1-P4)", () => {
+ const second = { ...P0, event_key: "HV:2023:y", event_text: "second event", accuracy: "Second panel's own line." };
+ const out = page({ ...POL, panels: [P0, second, { ...P0, event_key: "HV:2023:z", accuracy: "" }] });
+ const ps = panels(out);
+ eq(ps.length, 3);
+ const lines = [P0.accuracy, "Second panel's own line.", politics.NO_ACCURACY];
+ ps.forEach((p, i) => {
+ const first = p.indexOf('class="st ');
+ ok(first > 0, `panel ${i} shows counts`);
+ const at = p.indexOf(esc(lines[i]));
+ ok(at >= 0 && at < first, `panel ${i}: accuracy before counts`);
+ });
+ // the page-level line cannot stand in for a panel's: panel 2 has only the constant
+ ok(!ps[2].includes(POL.stance_accuracy));
+ // S1-P3: the JS constant is its own, never the Python sentence
+ ok(politics.NO_ACCURACY.includes("accuracy not yet measured"));
+ ok(!politics.NO_ACCURACY.includes("its accuracy is not yet measured"));
+});
+
+test("rule: a stance count carries its stance class and name, one chip per non-zero entry", () => {
+ const u = { ...P0.units[0], stance: { oppose: 2, support: 3, modify: 0, unclassified: 4 } };
+ const out = page({ ...POL, panels: [{ ...P0, units: [u] }] });
+ const [p] = panels(out);
+ ok(p.includes('3'));
+ ok(p.includes('2'));
+ ok(p.includes('4'));
+ ok(!p.includes('class="st modify"'), "a zero count is not shown");
+ ok(p.indexOf('class="st support"') < p.indexOf('class="st oppose"'), "support, modify, oppose, unclear, unclassified");
+});
+
+test("rule: a stance split without an accuracy line gets the constant (a count never travels alone)", () => {
+ const F = { ...POL.facts.F7, id: "F99", text: "Of 3 commenters we classified, 2 supported and 1 opposed." };
+ delete F.accuracy;
+ const out = page({ ...POL, facts: { ...POL.facts, F99: F } });
+ const li = out.slice(out.indexOf('data-fact="F99"'), out.indexOf("", out.indexOf('data-fact="F99"')));
+ ok(li.includes(politics.NO_ACCURACY));
+ // facts that carry their line (in the text, or as accuracy) are left as they are
+ const f7 = out.slice(out.indexOf('data-fact="F7"'), out.indexOf("", out.indexOf('data-fact="F7"')));
+ ok(!f7.includes(politics.NO_ACCURACY) && f7.includes("it finds 80% of hand-coded positions"));
+ const f8 = out.slice(out.indexOf('data-fact="F8"'), out.indexOf("", out.indexOf('data-fact="F8"')));
+ ok(!f8.includes(politics.NO_ACCURACY) && f8.includes('class="acc"'));
+});
+
+test("rule: the payment change is the stored delta_text; delta_total and share are never formatted", () => {
+ const u = { ...P0.units[0], delta_total: -98765.4321, share: 0.123456, share_text: "5.0%", delta_text: "a cut of at least $12,106" };
+ const up = { ...P0.units[1], delta_total: 804.57, delta_lower_bound: true, delta_text: "an increase of at least $805", share: 0.00008, share_text: "<0.1%" };
+ const out = page({ ...POL, panels: [{ ...P0, units: [u, up, P0.units[2]] }] });
+ ok(out.includes("a cut of at least $12,106") && out.includes("an increase of at least $805"));
+ ok(out.includes("5.0%") && out.includes("<0.1%"));
+ for (const bad of ["98765", "98,765", "-98", "12.3", "0.123", "804.57", "804.6", "0.00008", "$-"]) ok(!out.includes(bad), bad);
+ // an empty delta_text is a dash, never a number made from delta_total
+ const blank = page({ ...POL, panels: [{ ...P0, units: [{ ...u, delta_text: "", share_text: "" }] }] });
+ ok(!blank.includes("98765") && !blank.includes("12.3%"));
+});
+
+test("rule: RUC seats appear once per specialty, one cell spanning that specialty's rows", () => {
+ const out = page();
+ const [p] = panels(out);
+ // CARDIOLOGY has two units (a society and a clinician): one seat cell over both
+ eq(count(p, 'class="seat"'), 2, "one cell per specialty: CARDIOLOGY and NONE");
+ ok(p.includes('
1 permanent, 0 rotating
'));
+ ok(/
<\/td>/.test(p), "a specialty with no seats: an empty cell");
+ // rows sort by specialty, then org type; the society's row comes after the clinician's
+ const rows = p.match(/
]*>/g);
+ deq(rows.map((r) => r.match(/data-unit="([^"]*)"/)[1]), ["CARDIOLOGY|individual_clinician", "CARDIOLOGY|specialty_society", "NONE|health_system"]);
+ // CPT seats join the line only when there are any
+ const seats = { CARDIOLOGY: { ...P0.seats.CARDIOLOGY, ruc_rotating: 1, cpt: 2 } };
+ const cpt = page({ ...POL, panels: [{ ...P0, seats }] });
+ eq(count(cpt, "1 permanent, 1 rotating, 2 CPT"), 1);
+ // two specialties with seats: each line once, the holder named once
+ const two = {
+ ...P0,
+ units: [...P0.units, { ...P0.units[1], org_type: "specialty_society", specialty: "GERIATRIC MEDICINE", names: ["American Geriatrics Society"] }],
+ seats: { ...P0.seats, "GERIATRIC MEDICINE": { ruc_permanent: 1, ruc_rotating: 0, cpt: 0, holders: ["American Geriatrics Society"], item_keys: ["X"] } },
+ };
+ const t = panels(page({ ...POL, panels: [two] }))[0];
+ eq(count(t, "1 permanent, 0 rotating"), 2);
+ eq(count(t, 'class="seat"'), 3);
+});
+
+test("rule: the panel heading, docket link and RUC roster line", () => {
+ const out = page();
+ const [p] = panels(out);
+ ok(p.includes("CY2023 · invented event · "));
+ ok(p.includes('href="https://www.regulations.gov/docket/CMS-0000-0001"'));
+ ok(p.includes("RUC roster 2022") && !p.includes("RUC roster 2022 ()"));
+ const noted = panels(page({ ...POL, panels: [{ ...P0, roster_note: "the RUC that valued CY2023 met in 2022" }] }))[0];
+ ok(noted.includes("RUC roster 2022 (the RUC that valued CY2023 met in 2022)"));
+ const none = panels(page({ ...POL, panels: [{ ...P0, roster_year: null, roster_note: "" }] }))[0];
+ ok(none.includes("RUC roster not captured"));
+ // the line sits before the table, after the accuracy line
+ ok(p.indexOf(P0.accuracy) < p.indexOf("RUC roster") && p.indexOf("RUC roster") < p.indexOf("
{
+ const names = ["jane q. DOE", "McDONALD & sons ", "individual commenter ×2", "Dr. [F6] Smith"];
+ const u = { ...P0.units[1], names };
+ const out = page({ ...POL, panels: [{ ...P0, units: [u] }] });
+ ok(out.includes("jane q. DOE; McDONALD & sons <Clinic>; individual commenter ×2; Dr. [F6] Smith"));
+ ok(!out.includes(""));
+ // a name is never read as prose: "[F6]" stays text, not a citation
+ const [p] = panels(out);
+ ok(!p.includes('data-ref="F6"'));
+ // the header says these are names as posted
+ ok(p.includes("Names as posted"));
+});
+
+test("rule: CMS's summaries are labelled and never merged with our classification", () => {
+ const out = page();
+ const ours = section(out, "ours");
+ const cms = section(out, "cms");
+ ok(ours.includes("
Our classification
"));
+ ok(cms.includes("
CMS's summary
"), "a literal heading, not escaped");
+ ok(/class="pill cms">cms {
+ const out = page();
+ const s = section(out, "stakes");
+ ok(s.includes("
Stakes: money and power
"));
+ for (const id of ["F13", "F14", "F15"]) ok(s.includes(`data-fact="${id}"`), id);
+ // years ascending: the CY2022 seat before the CY2023 money and impact
+ ok(s.indexOf("CY2022") < s.indexOf("CY2023"));
+ ok(s.indexOf('data-fact="F15"') < s.indexOf('data-fact="F14"'));
+ ok(!s.includes('data-fact="F6"') && !s.includes('data-fact="F7"'));
+});
+
+test("rule: a fact's note renders as a flag", () => {
+ const facts = { ...POL.facts, F14: { ...POL.facts.F14, note: "model extract, unreviewed" }, F6: { ...POL.facts.F6, note: "model paraphrase, unreviewed" } };
+ const out = page({ ...POL, facts });
+ const li = (id) => out.slice(out.indexOf(`data-fact="${id}"`), out.indexOf("", out.indexOf(`data-fact="${id}"`)));
+ ok(li("F14").includes('model extract, unreviewed'));
+ ok(li("F6").includes('model paraphrase, unreviewed'));
+});
+
+test("rule: the identity marker flags every org type × specialty panel", () => {
+ const out = page({ ...POL, panels: [live(P0), live({ ...P0, event_key: "HV:2023:y" })] });
+ const ps = panels(out);
+ eq(ps.length, 2);
+ for (const p of ps) {
+ ok(p.includes(`${IDENTITY}`));
+ // how the labels were read: the basis per unit, the extractor once per panel
+ ok(p.includes("letterhead, model 1") && p.includes("regulations.gov fields 1"));
+ eq(count(p, "model:qwen2.5:14b@identity-v5"), 1);
+ }
+ // a panel without the marker is still flagged unreviewed, with the JS's own words
+ const [bare] = panels(page());
+ ok(bare.includes(`${politics.NO_IDENTITY}`));
+ ok(politics.NO_IDENTITY !== IDENTITY && politics.NO_IDENTITY.includes("unreviewed"));
+});
+
+test("rule: write actions are disabled and marked coming · S2", () => {
+ const out = page();
+ const buttons = out.match(/