ckb/tests/unit/test_lint_dirty.py
Michał Kopeć f6aa0f5b1d Template 2.0.6: frontmatter contract across eight readers, dirty-KB lint test
All eight frontmatter readers close at a `---` line, including one at
end of file (consolidate, dedup, OKF and share missed it; retrieve,
Starlight and mcp stopped at any line starting with `---`).
tests/unit/test_frontmatter_contract.py (T9) and
tests/unit/test_lint_dirty.py (T2: one planted fault per lint check).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 17:38:59 +01:00

104 lines
5.2 KiB
Python

"""Every lint check must fire on its own fault. The golden lint.json of the
clean fixture has every category at zero, which proves only that the checks
stay quiet; this plants one fault per check and asserts exactly which files
each check reports."""
import hashlib
import json
import shutil
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
FIX = ROOT / "tests/fixtures/minimal-kb"
LINT = ROOT / ".agents/skills/ckb-lint/scripts/lint_report.py"
TODAY = "2026-11-11"
SOURCE = "raw/archive/2026-09-01/kickoff-notes.md"
FM = "---\ntype: concept\ntldr: {name}\nlast_updated: 2026-11-01\nfreshness_window_days: 90\n{extra}---\n\n# {name}\n\n{body}\n"
def page(name, extra="", body="Text."):
return FM.format(name=name, extra=extra, body=body)
class DirtyKB(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.tmp = tempfile.TemporaryDirectory()
kb = Path(cls.tmp.name) / "kb"
shutil.copytree(str(FIX), str(kb))
digest = hashlib.sha256((kb / SOURCE).read_bytes()).hexdigest()[:8]
bed = kb / "wiki" / "lintbed"
listed = {
"notype.md": "---\ntldr: no type here\n---\n\n# No type\n",
"stale.md": page("Stale", "").replace("last_updated: 2026-11-01", "last_updated: 2020-01-01"),
"lowconf.md": page("Low confidence", "confidence: 0.1\n"),
"archive.md": page("Archive me", "retention: low\n").replace("last_updated: 2026-11-01", "last_updated: 2020-01-01"),
"0099-undecided.md": "---\ntype: decision\ntldr: x\ndecided_by: A\n---\n\n# D-0099\n",
"drifted.md": page("Drifted", body=f"## Sources\n\n- `{SOURCE}` — sha256:00000000 (checked 2026-09-01)\n"),
"misquoted.md": page("Misquoted", body=f"## Sources\n\n- `{SOURCE}` — sha256:{digest} (checked 2026-09-01)\n\n"
"## Crux\n\n> This sentence was never in the kickoff notes at all.\n— "
f"`{SOURCE}`, under \"Notes\"\n"),
"unreviewed.md": page("Unreviewed", "review: reviewed\nreviewed_on: 2026-11-05\n"),
"contested.md": page("Contested", body="> CONTRADICTION: 3 retries (`a.md`) vs 5 retries (`b.md`) — found 2026-11-01\n"),
"tagged.md": page("Tagged", "tags: made-up-tag\n"),
}
bed.mkdir()
for name, text in listed.items():
(bed / name).write_text(text, encoding="utf-8")
(bed / "orphan.md").write_text(page("Orphan"), encoding="utf-8") # linked from nowhere
(bed / "nested").mkdir()
(bed / "nested" / "child.md").write_text(page("Child"), encoding="utf-8") # folder without index.md
(bed / "index.md").write_text("# Lint bed\n\n" + "".join(f"- [{n}]({n})\n" for n in listed)
+ "- [child](nested/child.md)\n", encoding="utf-8")
with (kb / "wiki" / "index.md").open("a", encoding="utf-8") as fh:
fh.write("\n- [Lint bed](lintbed/index.md)\n")
edges = kb / "wiki/graph/edges.json"
data = json.loads(edges.read_text(encoding="utf-8"))
data["edges"].append({"from": "entities/maria-nowak", "to": "entities/nobody", "type": "knows"})
edges.write_text(json.dumps(data), encoding="utf-8")
r = subprocess.run([sys.executable, str(LINT), "--json", "--today", TODAY], cwd=str(kb),
capture_output=True, text=True, encoding="utf-8")
cls.tree = json.loads(r.stdout)["trees"][0]
@classmethod
def tearDownClass(cls):
cls.tmp.cleanup()
def files(self, category):
return {f.split(":", 1)[0] for f in self.tree[category]}
def test_each_check_reports_exactly_its_planted_files(self):
expected = {
"conformance": {"lintbed/notype.md"},
"freshness": {"lintbed/stale.md", "lintbed/archive.md"}, # an archive candidate is also stale
"confidence": {"lintbed/lowconf.md"},
"retention": {"lintbed/archive.md"},
"decisions": {"lintbed/0099-undecided.md"},
"orphans": {"lintbed/orphan.md"},
"graph": {"graph/edges.json"},
"index_log": {"lintbed/index.md", "lintbed/nested/index.md"}, # lists no orphan; nested has none
"evidence": {"lintbed/drifted.md"},
"crux": {"lintbed/misquoted.md"},
"review": {"lintbed/unreviewed.md"},
"contradictions": {"lintbed/contested.md"},
"tags": {"lintbed/tagged.md"},
}
for category, files in expected.items():
with self.subTest(check=category):
self.assertEqual(self.files(category), files, "\n".join(self.tree[category]))
def test_findings_say_what_is_wrong(self):
text = json.dumps(self.tree, ensure_ascii=False)
for phrase in ("missing or empty `type`", "stale —", "below 0.3", "archive candidate", "no `status`",
"no inbound links", "does not resolve to a page", "does not list lintbed/orphan.md",
"CHANGED since this page was built", "not found verbatim", "no `reviewed_by`",
"open contradiction", "tags.md"):
self.assertIn(phrase, text)
if __name__ == "__main__":
unittest.main()