Files
stack/tests/pfs/test_cite.py
kert 481cd3f2e5 feat(pfs): dossier assembly — cited facts, computed gaps, model prose behind a sentence-level citation check
pfs.cite (Cite + builders; rule_label moved from llm.lineage and re-exported),
pfs.citecheck (sentence split, number normalization, check_prose; plus
grounding_problems: words, codes and CMS-vs-ours attribution of model prose),
pfs.dossier (facts with resolved cites from every Task 1-12 table, timeline,
stakeholder panels with names from pfs.commenter, stance accuracy beside every
count, computed coverage gaps, ledgered model prose with template fallback,
pfs.dossier table), llm.search item_keys filter, `stack pfs dossier`.
Lineage rows reach the dossier only when a final rule's own plain sentence
states them. HOME-VISITS live: 558 facts, 28 timeline events, check passed,
prose model:qwen2.5:14b+template (8 of 29 units on templates).
2026-11-12 23:07:03 -05:00

189 lines
5.3 KiB
Python

"""pfs.cite — one citation per dossier fact (dossier task 13)."""
from __future__ import annotations
import pytest
from pfs.cite import (
KINDS,
Cite,
book_cite,
comment_cite,
dataset_cite,
fr_page_cite,
fr_para_cite,
guidance_cite,
rule_label,
rvu_cite,
ssa_section_url,
)
from pfs.codetables import GuidanceRow
PETM = "https://www.federalregister.gov/documents/2022/11/18/2022-23873/medicare-and-medicaid-programs-cy-2023-payment-policies-under-the-physician-fee-schedule-and-other"
def test_kinds():
assert KINDS == (
"fr_para",
"fr_page",
"comment",
"cms_file",
"cpt_book",
"iom",
"cfr",
"mln",
"statute",
"study",
"rvu_file",
"dataset",
)
def test_rule_label_moved_verbatim():
assert (
rule_label("Medicare Program; CY 2015 PFS Final Rule", "2014-11-13", 1251)
== "CY2015 PFS final ¶1251"
)
assert (
rule_label(
"CY 2021 PFS", "2020-12-28", 1578, vol="85", page=84639, kind="final"
)
== "CY2021 PFS final 85 FR 84639 ¶1578"
)
def test_fr_para_cite(home_fixture):
_con, store, _fam, _ask = home_fixture
c = fr_para_cite(store, "PETM225D", 1672)
assert c == Cite(
kind="fr_para",
label="CY2023 PFS final 87 FR 69608 ¶1672",
url=f"{PETM}#p-1672",
item_key="PETM225D",
p_id=1672,
page=69608,
locator="87 FR 69608",
)
def test_fr_para_cite_unresolved_keeps_a_locator(home_fixture):
_con, store, _fam, _ask = home_fixture
c = fr_para_cite(store, "PETM225D", 99999) # no such paragraph
assert c.url == ""
assert c.label == "CY2023 PFS final ¶99999"
assert c.locator == "PETM225D ¶99999"
c = fr_para_cite(store, "NOSUCH", 3) # no such item
assert (c.url, c.label, c.locator) == ("", "NOSUCH ¶3", "NOSUCH ¶3")
def test_fr_page_cite(home_fixture):
_con, store, _fam, _ask = home_fixture
c = fr_page_cite(store, "54GTKR5G", 66361, table="Table 14")
assert c.label == "CY2008 PFS final 72 FR 66361 Table 14"
assert c.url.endswith("#page-66361")
assert (c.kind, c.item_key, c.page, c.locator) == (
"fr_page",
"54GTKR5G",
66361,
"72 FR 66361",
)
assert (
fr_page_cite(store, "54GTKR5G", 66361).label == "CY2008 PFS final 72 FR 66361"
)
bad = fr_page_cite(store, "54GTKR5G", 1) # outside the rule's pages
assert bad.url == "" and bad.locator == "72 FR 1"
none = fr_page_cite(store, "NOSUCH", 5)
assert (none.url, none.label) == ("", "NOSUCH p. 5")
def test_comment_cite():
c = comment_cite("CMS-2022-0113-0001")
assert c.label == "Comment CMS-2022-0113-0001"
assert c.url == "https://www.regulations.gov/comment/CMS-2022-0113-0001"
assert c.kind == "comment"
def test_rvu_cite():
assert rvu_cite(2023).label == "PFS CY2023 RVU file"
assert rvu_cite(2027, proposed=True).label == "PFS CY2027 NPRM RVU file"
assert rvu_cite(2023).url.startswith("https://www.cms.gov/")
assert rvu_cite(2023).kind == "rvu_file"
def test_book_cite():
c = book_cite("3ANRT854", "CPT Changes 2023", "49")
assert (c.kind, c.label, c.item_key, c.url) == (
"cpt_book",
"CPT Changes 2023 p. 49",
"3ANRT854",
"",
)
assert c.locator == "CPT Changes 2023 p. 49"
assert book_cite("K", "CPT 2024", "p. 29").label == "CPT 2024 p. 29"
assert (
book_cite("F7", "Panel summary", "tab 10, PDF p. 4").label
== "Panel summary, tab 10, PDF p. 4"
)
def test_dataset_cite():
c = dataset_cite("K1", "PSPS CY2022", "https://data.cms.gov/x")
assert c == Cite(
"dataset", "PSPS CY2022", "https://data.cms.gov/x", "K1", 0, 0, "PSPS CY2022"
)
def test_guidance_cite():
cfr = guidance_cite(
GuidanceRow(
"F", "99341", "cfr", "42 CFR 425.400(c)", "", "PETM225D", 3607, 69823
)
)
assert cfr.kind == "cfr"
assert cfr.label == "42 CFR 425.400(c)"
assert (
cfr.url == "https://www.ecfr.gov/current/title-42/section-425.400#p-425.400(c)"
)
assert (cfr.item_key, cfr.p_id, cfr.locator) == (
"PETM225D",
3607,
"42 CFR 425.400(c)",
)
iom = guidance_cite(
GuidanceRow(
"F", "99341", "iom", "100-04 ch.26", "IOMKEY", "PETM225D", 3607, 0, 4
)
)
assert (iom.kind, iom.url, iom.item_key, iom.page) == ("iom", "", "IOMKEY", 4)
junk = guidance_cite(GuidanceRow("F", "x", "cfr", "not a cite", "", "", 0, 0))
assert junk.url == "" and junk.locator == "not a cite"
@pytest.mark.parametrize(
"ref,url",
[
(
"section 1834(m) of the Act",
"https://www.ssa.gov/OP_Home/ssact/title18/1834.htm",
),
(
"section 1899(c) of the Act",
"https://www.ssa.gov/OP_Home/ssact/title18/1899.htm",
),
(
"section 1866E of the Social Security Act",
"https://www.ssa.gov/OP_Home/ssact/title18/1866E.htm",
),
("section 5501(a) of the Affordable Care Act", ""),
("42 CFR 410.78", ""),
("section 1902 of the Act", ""),
],
)
def test_ssa_section_url(ref, url):
assert ssa_section_url(ref) == url
def test_cite_round_trips_through_a_dict():
c = comment_cite("X-1")
assert Cite(**c.__dict__) == c