Files
stack/tests/pfs/test_anchors.py
kert d1d6e4f93d
Some checks failed
CI / lint (push) Successful in 43s
CI / notebooks-smoke (push) Successful in 1m27s
Deploy / notebooks (push) Has been skipped
Deploy / zotero (push) Has been skipped
Deploy / docs (push) Has been skipped
Deploy / api (push) Has been skipped
Deploy / llm (push) Has been skipped
Deploy / mc (push) Has been skipped
Infra CI / notebooks (push) Successful in 59s
Infra CI / zotero (push) Successful in 16s
Infra CI / docs (push) Successful in 1m31s
Infra CI / api (push) Successful in 1m16s
Infra CI / llm (push) Successful in 47s
Infra CI / mc (push) Failing after 12s
Deploy / report (push) Successful in 14s
CI / test (push) Has been cancelled
refactor(pfs,llm,bib): one definition of the chunk-metadata array predicates (GIN-index expression), normalize_key for family keys everywhere, error-isolation docstring on code_cited_sources (refs #705)
2026-09-11 17:01:37 -04:00

106 lines
4.0 KiB
Python

"""pfs.anchors — metadata for chunks: codes, families, and element slugs."""
from pfs.anchors import anchor_metadata, code_family_index
from pfs.families import FAMILIES, Family
def test_codes_families_elements():
md = anchor_metadata(
"Use 99439 in conjunction with 99490; consent; per calendar month"
)
assert md["codes"] == "99439 99490"
assert md["families"] == "CCM"
assert (
"activity=consent" in md["elements"].split()
and "period=calendar-month" in md["elements"].split()
)
assert "relation=addon-of" in md["elements"].split() # detail dropped
def test_empty_and_unknown():
assert anchor_metadata("nothing here") == {
"codes": "",
"families": "",
"elements": "",
}
fams = {"X": Family("X", "X fam", ("12345", "23456"), ("x fam",))}
assert anchor_metadata("code 12345", families=fams)["families"] == "X"
def test_code_index_matches_unindexed_scan():
text = "Use 99439 in conjunction with 99490; consent; per calendar month"
index = code_family_index(FAMILIES)
assert anchor_metadata(text, code_index=index) == anchor_metadata(text)
def test_code_index_keeps_every_family_on_collision():
"""Ruling A6: a code shared by two qualifying families must keep
both in the index — a single-winner index made the indexed path
(restamp) disagree with the un-indexed scan the moment two families
shared a code, which the next slice (CPT headings intersecting a
hand family) will create."""
fams = {
"A": Family("A", "A fam", ("12345", "23456"), ("a fam",)),
"B": Family("B", "B fam", ("12345", "34567"), ("b fam",)),
}
index = code_family_index(fams)
assert index["12345"] == ("A", "B")
def test_code_index_matches_unindexed_scan_on_collision():
"""The indexed and un-indexed paths must write identical 'families'
values for the same text once a code belongs to two families."""
fams = {
"A": Family("A", "A fam", ("12345", "23456"), ("a fam",)),
"B": Family("B", "B fam", ("12345", "34567"), ("b fam",)),
}
text = "code 12345"
index = code_family_index(fams)
assert anchor_metadata(text, families=fams, code_index=index) == anchor_metadata(
text, families=fams
)
assert anchor_metadata(text, families=fams)["families"] == "A B"
def test_single_code_family_never_stamped():
"""A stem-derived family of exactly one code (~17,182 of 17,187
derived keys) is not a grouping worth its own anchor — it would just
echo the code already in 'codes' (Ruling A5)."""
fams = {"SOLO": Family("SOLO", "Solo", ("54321",), ("solo",))}
assert "54321" not in code_family_index(fams)
assert anchor_metadata("code 54321", families=fams)["families"] == ""
index = code_family_index(fams)
assert (
anchor_metadata("code 54321", families=fams, code_index=index)["families"] == ""
)
class TestSharedPredicateSql:
"""#705 item 4: one definition of the metadata-array expressions the
GIN index was built on; every predicate composes from it."""
def test_arrays_match_the_index_expression(self):
from pfs.anchors import codes_array_sql, families_array_sql
assert codes_array_sql("e") == (
"string_to_array(COALESCE(e.cmetadata->>'codes', ''), ' ')"
)
assert families_array_sql("x") == (
"string_to_array(COALESCE(x.cmetadata->>'families', ''), ' ')"
)
def test_predicate_composes_both_sides(self):
from pfs.anchors import code_or_family_predicate
p = code_or_family_predicate("e")
assert "&& CAST(:codes AS text[])" in p and "&& ARRAY[:family_key]" in p
assert p.startswith("(string_to_array(COALESCE(e.cmetadata->>'codes'")
def test_consumers_use_the_shared_text(self):
import llm.evidence as ev
import pfs.reaction as rx
from pfs.anchors import code_or_family_predicate, codes_array_sql
assert codes_array_sql("e") in ev._CITED_SQL
assert code_or_family_predicate("e") in str(rx._DOCKET_COUNTS_SQL)