import pathlib import re import subprocess import sys import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from repo_checks.test_python_floor import ROOT CITATION = re.compile(r'`?(?:~/\.claude/)?(CLAUDE\.md|rules/[\w-]+\.md)`? → ("[^"]+"(?:,? (?:and )?"[^"]+")*)') HEADING = re.compile(r"^#+ (.+?)(?: \(.*\))?$", re.MULTILINE) LEAD = re.compile(r"^(?:- )?\*\*(.+?)(?:\*\*|[:.](?=\*\*| )| —)", re.MULTILINE) NAME = re.compile(r'"([^"]+)"') STRAY = re.compile(r'(?:CLAUDE|rules/[\w-]+)\.md(?!`? → "|:\d)[^\n"]{1,5}"') class Citations(unittest.TestCase): def test_every_citation_names_a_heading_then_headings_or_bold_leads_of_the_file_it_cites(self): tracked = subprocess.run(["git", "ls-files", "-z"], capture_output=True, check=True, cwd=ROOT, text=True).stdout.split("\0") texts = [(path, (ROOT / path).read_text()) for path in filter(None, tracked)] self.assertEqual([f"{path}: {stray}" for path, text in texts for stray in STRAY.findall(text)], [], 'spell a citation `CLAUDE.md` → "Vocabulary"') cited = [(path, file, NAME.findall(run)) for path, text in texts for file, run in CITATION.findall(text)] self.assertTrue(cited) unresolved = [] for path, file, names in cited: if not (ROOT / file).exists(): unresolved.append(f"{path}: {file}") continue text = (ROOT / file).read_text() headings = set(HEADING.findall(text)) leads = headings | set(LEAD.findall(text)) unresolved += [f"{path}: {file} → {name}" for i, name in enumerate(names) if name not in (headings if i == 0 else leads)] self.assertEqual(unresolved, [], "the first name is a heading of the cited file, each later one a heading or a bold lead")