Files
stack/tests/pfs/test_anchors.py
kert 8a0eef60b7 perf(pfs): code→family index for anchor_metadata (refs #688)
code_family_index(families) builds a code -> family key map once so a
restamp run of ~1.15M chunks doesn't rescan every family per code.
anchor_metadata gains an optional code_index kwarg that looks family
membership up through it instead.

Ruling A5: families with fewer than two codes are excluded from the
index and from the default unindexed scan — the derived registry is
~17,187 keys and all but a handful are single-code stem families,
where a 'families' anchor would just echo the code already in
'codes'. Only real groupings (today: ACP, CCM, PCM, TCM, APCM) get a
families anchor.
2026-09-09 16:01:40 -04:00

56 lines
2.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_first_family_on_collision():
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"
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 "SOLO" 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"] == ""
)