"""Tests for okfwiki/krepair.py: one fragment repaired or removed, one link record moved or taken out, the helper's task and the measurement of what a repair touched (order 34 session B, contracts K18 and K19), with no model call. The collection is the repair gate's base (gates/repair/: a K1 source root about beekeeping, three docs pages and one release note, and the compiler's fragments for them), built by the gate's own code with the frozen tools, so these tests and the gate stand on the same eight pages. The gate judges the command case by case; these tests pin what the gate leaves to the command's unit tests (gates/REPAIR.md, Not measured) and the parts of the contract the gate cannot see: the task's instructions and its limits, the K17 list, the repairs log, the diff's exit 1, the link commands' other refusals, linkset's groups and the helper's agent file. Every fixture is synthetic (CLAUDE.md rule 3). Run: python3 -m unittest discover -s tests -p 'test_krepair.py' """ from __future__ import annotations import hashlib import io import json import re import shutil import sys import tempfile import unittest from contextlib import redirect_stderr, redirect_stdout from pathlib import Path from typing import Any from unittest import mock ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT)) sys.path.insert(0, str(ROOT / "gates")) import repair_gate as gate # noqa: E402 from okfwiki import claims, kassemble, klink, kpack, krepair, reader # noqa: E402 MANIFEST = ROOT / "gates" / "repair.manifest.json" CASES = ROOT / "gates" / "repair" AGENT = ROOT / "agents" / "okf-wiki-repairer.md" AGENT_COPY = ROOT / ".claude" / "agents" / "okf-wiki-repairer.md" AT = "2026-01-02T00:00:00Z" FRAMES_LIFTED = {"concept_id": "frames", "file": "docs-en-hive.md", "start": 8, "end": 10, "quote": "lifted out one by one", "provenance": "documented"} HIVE_OPEN = {"concept_id": "hive", "file": "docs-en-hive.md", "start": 14, "end": 17, "quote": "only on a warm and calm day", "provenance": "documented"} HONEY_OWN = {"concept_id": "honey-harvest", "file": "docs-en-honey.md", "start": 3, "end": 3, "quote": "when most cells in the frame are capped with wax", "provenance": "documented"} BROOD_QUEEN = {"concept_id": "brood", "file": "docs-en-hive.md", "start": 21, "end": 23, "quote": "The queen lays one egg in each empty cell", "provenance": "documented"} def run(argv: list[str]) -> tuple[int, str]: out = io.StringIO() with redirect_stdout(out), redirect_stderr(out): code = krepair.main(argv) return code, out.getvalue() class Base(unittest.TestCase): """The gate's base, built once; every test works in a fresh copy of it.""" base: gate.Base _class_tmp: tempfile.TemporaryDirectory[str] @classmethod def setUpClass(cls) -> None: cls._class_tmp = tempfile.TemporaryDirectory(prefix="krepair-test-") manifest = gate.load_manifest(MANIFEST) gate.verify_fixtures(manifest, CASES) cls.base = gate.build_base(manifest, CASES, Path(cls._class_tmp.name)) @classmethod def tearDownClass(cls) -> None: cls._class_tmp.cleanup() def setUp(self) -> None: self._tmp = tempfile.TemporaryDirectory(prefix="krepair-case-") self.c = gate.fresh_copy(self.base, Path(self._tmp.name), "case") self.p = self.c.places def tearDown(self) -> None: self._tmp.cleanup() # --- inputs ------------------------------------------------------------------------------------------------ def label(self, key: dict[str, Any]) -> str: return gate.fragment_label(self.c.path("sources"), key) def answers(self, items: list[dict[str, Any]], name: str = "answers.json") -> Path: path = self.c.path("in") / name path.write_text(json.dumps(gate.render_answers(items, self.c.path("sources"))), encoding="utf-8") return path def repair_list(self, spec: dict[str, Any], name: str = "list.json") -> Path: path = self.c.path("in") / name path.write_text(json.dumps(gate.render_list(spec, self.base)), encoding="utf-8") return path def edit_list(self, path: Path, change: Any) -> Path: doc = json.loads(path.read_text(encoding="utf-8")) change(doc) path.write_text(json.dumps(doc), encoding="utf-8") return path def agent(self, text: str | None = None) -> Path: path = self.c.path("in") / "agent.md" if text is None: shutil.copyfile(CASES / "agent.md", path) else: path.write_text(text, encoding="utf-8") return path # --- commands ---------------------------------------------------------------------------------------------- def task(self, lst: Path, agent: Path | None = None, out: Path | None = None) -> tuple[int, str]: return run(["task", "--list", str(lst), "--claims", self.p["claims"], "--pairs", self.p["pairs"], "--work", self.p["work"], "--blocks", self.p["blocks"], "--sources", self.p["sources"], "--agent", str(agent or self.agent()), "--out-dir", str(out or self.c.path("out"))]) def fix(self, answers: Path, bundle: str | None = None) -> tuple[int, str]: return run(["fix", "--answers", str(answers), "--bundle", bundle or self.p["wiki"], "--work", self.p["work"], "--blocks", self.p["blocks"], "--sources", self.p["sources"], "--links", self.p["links"]]) def build(self, links: bool = True) -> int: argv = ["build", "--work", self.p["work"], "--sources", self.p["sources"], "--out", self.p["wiki"], "--replace", "--at", AT] + (["--links", self.p["links"]] if links else []) code, _ = gate.quiet(kassemble.main, argv) return code def diff(self) -> tuple[int, str]: return run(["diff", "--old", self.p["wiki_old"], "--new", self.p["wiki"], "--sources", self.p["sources"], "--repairs", str(self.repairs_path()), "--out", self.p["diff"]]) # --- state ------------------------------------------------------------------------------------------------- def accepted(self) -> dict[str, Any]: doc: dict[str, Any] = json.loads((self.c.path("work") / "K-work" / "accepted.json").read_text(encoding="utf-8")) return doc def repairs_path(self) -> Path: return self.c.path("work") / "K-work" / "repairs.json" def repairs(self) -> list[dict[str, Any]]: doc = json.loads(self.repairs_path().read_text(encoding="utf-8")) self.assertEqual("okf-wiki/repairs/1", doc["schema"]) entries: list[dict[str, Any]] = doc["entries"] return entries def links(self) -> list[dict[str, Any]]: records: list[dict[str, Any]] = json.loads(self.c.path("links").read_text(encoding="utf-8"))["links"] return records def trees(self) -> dict[str, dict[str, str]]: return self.c.trees("work", "wiki", "links", "out") class HeaderTest(unittest.TestCase): def test_the_header_states_every_subcommand_and_every_refusal_code(self) -> None: doc = krepair.__doc__ or "" for sub in ("list", "task", "fix", "unlink", "relink", "linkset", "diff"): self.assertIn(f"python3 -m okfwiki.krepair {sub}", doc) for code in krepair.REFUSALS: self.assertIn(code, doc) def test_every_refusal_code_of_the_gate_is_a_code_of_the_command(self) -> None: codes = json.loads(MANIFEST.read_text(encoding="utf-8"))["refusal_codes"] self.assertEqual([], sorted(set(codes) - set(krepair.REFUSALS))) def test_the_hard_check_s_reasons_are_refusal_codes(self) -> None: validate_gives = ("malformed", "anchor-outside-pack", "anchor-crosses-blocks", "sentence-count", "source-missing", "quote-missing", "quote-not-in-span", "provenance-missing", "provenance-unknown", "tests-missing") self.assertEqual([], [r for r in validate_gives if r not in kpack.REASONS]) for reason in validate_gives: self.assertIn(reason, krepair.REFUSALS) class ListTest(Base): """list: the repair list from the reader's verdicts, in the form task reads.""" def claim_ids(self) -> list[str]: return [str(c["id"]) for c in json.loads(Path(self.p["claims"]).read_text(encoding="utf-8"))["claims"]] def verdicts(self, given: dict[str, str]) -> Path: path = self.c.path("in") / "verdicts.json" path.write_text(json.dumps({"reader": "r", "models": ["m"], "verdicts": [ {"id": i, "verdict": v} for i, v in given.items()]}), encoding="utf-8") return path def make(self, verdicts: Path, *more: str) -> tuple[int, str, Path]: out = self.c.path("in") / "list.json" code, said = run(["list", "--claims", self.p["claims"], "--verdicts", str(verdicts), "--out", str(out), *more]) return code, said, out def test_the_list_names_the_claims_the_reader_did_not_support_and_task_reads_it(self) -> None: ids = self.claim_ids() bad = next(i for i in ids if i.startswith("frames.md#")) code, said, out = self.make(self.verdicts({i: "not_supported" if i == bad else "supported" for i in ids})) self.assertEqual(0, code, said) doc = json.loads(out.read_text(encoding="utf-8")) self.assertEqual(set(krepair.LIST_KEYS), set(doc)) self.assertEqual(hashlib.sha256(Path(self.p["claims"]).read_bytes()).hexdigest(), doc["claims_sha256"]) self.assertEqual([bad], [c["id"] for c in doc["claims"]]) self.assertEqual(["not-supported"], doc["claims"][0]["reasons"]) self.assertIn(f"1 of {len(ids)}", said) code, said = self.task(out) self.assertEqual(0, code, said) def test_a_claim_read_without_a_verdict_is_listed_and_a_claim_not_read_is_not(self) -> None: ids = self.claim_ids() read = self.c.path("in") / "ids.json" read.write_text(json.dumps({"ids": ids[:3]}), encoding="utf-8") code, said, out = self.make(self.verdicts({ids[0]: "supported", ids[1]: "not_supported"}), "--ids", str(read)) self.assertEqual(0, code, said) rows = json.loads(out.read_text(encoding="utf-8"))["claims"] self.assertEqual({ids[1]: ["not-supported"], ids[2]: ["no-verdict"]}, {c["id"]: c["reasons"] for c in rows}) def test_two_verdicts_for_one_claim_do_not_support_it(self) -> None: ids = self.claim_ids() path = self.c.path("in") / "verdicts.json" path.write_text(json.dumps({"verdicts": [{"id": i, "verdict": "supported"} for i in ids + ids[:1]]}), encoding="utf-8") code, said, out = self.make(path) self.assertEqual(0, code, said) self.assertEqual([ids[0]], [c["id"] for c in json.loads(out.read_text(encoding="utf-8"))["claims"]]) def test_a_verdict_for_a_claim_the_claim_list_lacks_cannot_run_and_writes_nothing(self) -> None: ids = self.claim_ids() code, said, out = self.make(self.verdicts({**{i: "supported" for i in ids}, "gone.md#0123456789abcdef-0": "not_supported"})) self.assertEqual(2, code, said) self.assertIn("gone.md#0123456789abcdef-0", said) self.assertFalse(out.exists()) def test_a_list_already_written_is_never_overwritten(self) -> None: ids = self.claim_ids() v = self.verdicts({i: "supported" for i in ids}) self.assertEqual(0, self.make(v)[0]) code, said, _ = self.make(v) self.assertEqual(2, code, said) class TaskTest(Base): def test_a_task_holds_the_instructions_and_every_part_of_each_fragment(self) -> None: lst = self.repair_list({"claims": [{"page": "frames.md", "text": "Frames can be lifted out one by one."}, {"page": "brood.md", "text": "The queen lays one egg in each empty cell."}]}) before = self.trees() code, said = self.task(lst) self.assertEqual(0, code, said) after = self.trees() self.assertEqual([before[n] for n in ("work", "wiki", "links")], [after[n] for n in ("work", "wiki", "links")]) tasks = sorted(self.c.path("out").glob("*.parts.json")) self.assertEqual(2, len(tasks)) # two pages, two tasks answers = [] for t in tasks: doc = json.loads(t.read_text(encoding="utf-8")) self.assertEqual("okf-wiki/repair-parts/1", doc["schema"]) parts = [Path(p) for p in doc["parts"]] self.assertTrue(all(p.is_absolute() and p.is_file() for p in parts)) self.assertTrue(Path(doc["answer"]).is_absolute()) self.assertFalse(Path(doc["answer"]).exists()) answers.append(doc["answer"]) first = parts[0].read_text(encoding="utf-8") self.assertIn(krepair.INSTRUCTIONS, first) for p in parts: self.assertLessEqual(len(p.read_text(encoding="utf-8")), kpack.PART_CHARS) self.assertEqual(len(answers), len(set(answers))) text = "\n".join(p.read_text(encoding="utf-8") for p in sorted(self.c.path("out").glob("*.md"))) frames = self.label(FRAMES_LIFTED) self.assertIn(frames, text) self.assertIn("`frames`", text) self.assertIn(" 9 | Each frame holds wax comb where bees store honey and raise brood.", text) self.assertIn("the reader did not accept it", text) def test_the_sentences_of_one_fragment_stand_together_in_one_task(self) -> None: lst = self.repair_list({"claims": [ {"page": "queen-cells.md", "text": "Workers build queen cells along the lower edge of the comb."}, {"page": "queen-cells.md", "text": "A queen cell looks like a peanut shell."}]}) code, said = self.task(lst) self.assertEqual(0, code, said) tasks = sorted(self.c.path("out").glob("*.parts.json")) self.assertEqual(1, len(tasks)) doc = json.loads(tasks[0].read_text(encoding="utf-8")) self.assertEqual(1, len(doc["fragments"])) self.assertIn("2 claims", said) def test_a_task_holds_at_most_ten_fragments_of_one_page(self) -> None: lst = self.repair_list({"claims": [{"page": "frames.md", "text": "Frames can be lifted out one by one."}, {"page": "frames.md", "text": "Each frame holds comb for honey and brood."}, {"page": "frames.md", "text": "Bees store honey and raise brood in the comb of a frame."}]}) self.assertEqual(10, krepair.TASK_FRAGMENTS) with mock.patch.object(krepair, "TASK_FRAGMENTS", 2): code, said = self.task(lst) self.assertEqual(0, code, said) counts = sorted(len(json.loads(t.read_text(encoding="utf-8"))["fragments"]) for t in self.c.path("out").glob("*.parts.json")) self.assertEqual([1, 2], counts) def test_a_long_task_is_split_into_parts_under_the_limit(self) -> None: lst = self.repair_list({"claims": [{"page": "hive.md", "text": "A hive is the box where a bee colony lives."}, {"page": "hive.md", "text": "Light the smoker before you lift the lid."}]}) limit = len(krepair.INSTRUCTIONS) + 200 # the instructions alone fill part 1 with mock.patch.object(kpack, "PART_CHARS", limit): code, said = self.task(lst) self.assertEqual(0, code, said) (t,) = self.c.path("out").glob("*.parts.json") parts = [Path(p) for p in json.loads(t.read_text(encoding="utf-8"))["parts"]] self.assertGreater(len(parts), 1) self.assertTrue(all(len(p.read_text(encoding="utf-8")) <= limit for p in parts)) def test_a_fragment_too_large_for_one_part_cannot_run_and_is_named(self) -> None: lst = self.repair_list({"claims": [{"page": "frames.md", "text": "Frames can be lifted out one by one."}]}) with mock.patch.object(kpack, "PART_CHARS", 400): code, said = self.task(lst) self.assertEqual(2, code) self.assertIn(self.label(FRAMES_LIFTED), said) self.assertEqual([], list(self.c.path("out").iterdir())) def test_instructions_that_share_eight_words_with_a_block_are_refused(self) -> None: lst = self.repair_list({"claims": [{"page": "frames.md", "text": "Frames can be lifted out one by one."}]}) planted = krepair.INSTRUCTIONS + "\nA frame that is full of capped honey is heavy.\n" with mock.patch.object(krepair, "INSTRUCTIONS", planted): code, said = self.task(lst) self.assertEqual(1, code) self.assertRegex(said, r"(?m)^refused: shared-words \S") self.assertEqual([], list(self.c.path("out").iterdir())) def test_the_instructions_share_no_eight_words_with_the_base(self) -> None: words = reader.WORD.findall(krepair.INSTRUCTIONS.lower()) self.assertGreater(len(words), 100) source_text = " ".join(p.read_text(encoding="utf-8") for p in sorted((self.c.path("sources") / "kilde").glob("*"))) runs = {tuple(words[i:i + 8]) for i in range(len(words) - 7)} theirs = reader.WORD.findall(source_text.lower()) self.assertEqual(set(), runs & {tuple(theirs[i:i + 8]) for i in range(len(theirs) - 7)}) self.assertNotIn("bee", krepair.INSTRUCTIONS.lower()) def test_a_list_with_an_unknown_top_level_key_is_refused(self) -> None: lst = self.repair_list({"claims": [{"page": "frames.md", "text": "Frames can be lifted out one by one."}]}) self.edit_list(lst, lambda d: d.update({"extra": []})) code, said = self.task(lst) self.assertEqual(1, code) self.assertRegex(said, r"(?m)^refused: stale-list \S") self.assertEqual([], list(self.c.path("out").iterdir())) def test_a_claim_whose_excerpts_changed_since_the_list_is_refused(self) -> None: lst = self.repair_list({"claims": [{"page": "frames.md", "text": "Frames can be lifted out one by one."}]}) self.edit_list(lst, lambda d: d["claims"][0].update({"excerpt_sha256": "0" * 64})) code, said = self.task(lst) self.assertEqual(1, code) self.assertRegex(said, r"(?m)^refused: stale-list frames\.md#") def test_a_k17_list_with_the_operator_s_reasons_and_every_part_filled_is_read(self) -> None: lst = self.repair_list({"claims": [{"page": "frames.md", "text": "Frames can be lifted out one by one.", "reasons": ["not-supported", "too broad"]}]}) def fill(doc: dict[str, Any]) -> None: doc["claims"][0]["origins"] = ["o30", "o32"] doc["pages"] = [{"page": "frames.md", "origin": "o32", "reason": "too broad"}] doc["pairs"] = [{"a": "frames.md", "b": "brood.md", "origin": "o32"}] self.edit_list(lst, fill) code, said = self.task(lst) self.assertEqual(0, code, said) text = "\n".join(p.read_text(encoding="utf-8") for p in self.c.path("out").glob("*.md")) self.assertIn("the reader did not accept it", text) self.assertIn("the operator: too broad", text) def test_a_task_already_written_is_never_overwritten(self) -> None: lst = self.repair_list({"claims": [{"page": "frames.md", "text": "Frames can be lifted out one by one."}]}) self.assertEqual(0, self.task(lst)[0]) before = self.c.trees("out") code, said = self.task(lst) self.assertEqual(2, code) self.assertEqual(before, self.c.trees("out")) class ExcerptTest(Base): """Order 35 A2: a fragment whose block is too large for one part is shown with the lines around its quote.""" OPEN_CLAIM = {"page": "hive.md", "text": "Open the hive only on a warm and calm day during an inspection."} SHELL_CLAIM = {"page": "queen-cells.md", "text": "A queen cell looks like a peanut shell."} PAD = 36_000 # two padded lines are over the room of a part of kpack.PART_CHARS, one is not def pad(self, file: str, lines: list[int], chars: int = PAD) -> None: """Lengthens lines of a source in this copy only, so that its block is too large for one part.""" path = self.c.path("sources") / "kilde" / file text = path.read_text(encoding="utf-8").split("\n") for n in lines: text[n - 1] += " " + "w" * chars path.write_text("\n".join(text), encoding="utf-8") def task_text(self, spec: list[dict[str, str]]) -> tuple[int, str, str]: code, said = self.task(self.repair_list({"claims": spec})) text = "".join(p.read_text(encoding="utf-8") for p in sorted(self.c.path("out").glob("*.md"))) return code, said, text def test_the_lines_around_a_quote_are_forty(self) -> None: self.assertEqual(40, krepair.EXCERPT_LINES) # measured in order 35 A2 on the two fragments of the large block def test_a_fragment_whose_block_fits_is_shown_as_before_byte_for_byte(self) -> None: code, said, _ = self.task_text([self.OPEN_CLAIM, self.SHELL_CLAIM]) self.assertEqual(0, code, said) got = {p.name: hashlib.sha256(p.read_bytes()).hexdigest() for p in sorted(self.c.path("out").glob("*.md"))} self.assertEqual({ # the parts as krepair at 652231d wrote them "r001-1.md": "ad2182ca81e560e8846189b08d179153261ce6ef89de5ba3a4317f5f83129fb0", "r002-1.md": "d1733cfe63b5c32c55c97d6c5ba6828c70707161c2dcc52246e079cb4ca9127a"}, got) def test_a_block_over_the_room_is_shown_around_the_quote_with_its_line_numbers(self) -> None: self.pad("docs-en-hive.md", [16, 17]) with mock.patch.object(krepair, "EXCERPT_LINES", 1): code, said, text = self.task_text([self.OPEN_CLAIM]) self.assertEqual(0, code, said) self.assertIn("Block: docs-en-hive.md › Hive › Inspections, lines 14-17:\n" "The block is shown only in part: lines 14-15 of lines 14-17 (4 lines). Judge the fragment from " "the lines shown.\n\n" " 14 | Open the hive only on a warm and calm day.\n" " 15 | Move slowly so the bees stay calm.\n", text) self.assertNotIn(" 16 | ", text) self.assertNotIn(" 17 | ", text) self.assertIn("Quote: `only on a warm and calm day` (docs-en-hive.md, line 14)", text) self.assertIn("Anchor: docs-en-hive.md, lines 14-17.", text) for p in self.c.path("out").glob("*.md"): self.assertLessEqual(len(p.read_text(encoding="utf-8")), kpack.PART_CHARS) def test_a_context_quote_outside_the_lines_shown_stands_on_its_own_with_its_line(self) -> None: self.pad("docs-en-hive.md", [16, 17]) with mock.patch.object(krepair, "EXCERPT_LINES", 1): code, said, text = self.task_text([self.OPEN_CLAIM]) self.assertEqual(0, code, said) self.assertIn("Context quote lines outside the lines shown:\n\n 12 | ## Inspections\n", text) def test_a_context_quote_inside_the_lines_shown_stands_only_there(self) -> None: self.pad("docs-en-swarm.md", [9], 2 * self.PAD) with mock.patch.object(krepair, "EXCERPT_LINES", 1): code, said, text = self.task_text([self.SHELL_CLAIM]) self.assertEqual(0, code, said) self.assertIn("lines 7-8 of lines 7-9 (3 lines)", text) self.assertEqual(1, text.count(" 8 | A queen cell looks like a peanut shell hanging from the comb.\n")) self.assertNotIn("Context quote lines outside", text) self.assertNotIn(" 9 | ", text) def test_the_eight_word_refusal_still_reads_the_whole_block(self) -> None: self.pad("docs-en-hive.md", [16, 17]) planted = krepair.INSTRUCTIONS + "\nLook for signs that the colony is getting ready.\n" # line 17, not shown with mock.patch.object(krepair, "EXCERPT_LINES", 1), mock.patch.object(krepair, "INSTRUCTIONS", planted): code, said, _ = self.task_text([self.OPEN_CLAIM]) self.assertEqual(1, code) self.assertRegex(said, r"(?m)^refused: shared-words \S") self.assertEqual([], list(self.c.path("out").iterdir())) def test_a_fragment_too_large_even_in_part_cannot_run_and_is_named(self) -> None: self.pad("docs-en-hive.md", [14], 2 * self.PAD) # the quote's own line code, said, _ = self.task_text([self.OPEN_CLAIM]) self.assertEqual(2, code) self.assertIn(self.label(HIVE_OPEN), said) self.assertEqual([], list(self.c.path("out").iterdir())) class FixTest(Base): def replace_lifted(self, text: str = "Frames are lifted out one at a time.") -> dict[str, Any]: return {"key": FRAMES_LIFTED, "action": "replace", "fragments": [ {"text": text, "quote": "lifted out one by one", "anchor": {"file": "docs-en-hive.md", "start": 8, "end": 10}, "provenance": "documented"}]} def test_a_new_fragment_takes_the_old_record_s_concept_pack_position_place_and_outcome(self) -> None: acc = self.accepted() old_label = self.label(FRAMES_LIFTED) i = next(n for n, r in enumerate(acc["fragments"]) if kassemble.label(r) == old_label) acc["fragments"][i]["outcome"] = "new" # the record that made the concept: still no tests asked (self.c.path("work") / "K-work" / "accepted.json").write_text(kpack.dump(acc), encoding="utf-8") code, said = self.fix(self.answers([self.replace_lifted()])) self.assertEqual(0, code, said) new = self.accepted()["fragments"] self.assertEqual(len(acc["fragments"]), len(new)) r = new[i] self.assertEqual(("frames", acc["fragments"][i]["pack"], acc["fragments"][i]["position"], "new"), (r["concept_id"], r["pack"], r["position"], r["outcome"])) self.assertEqual("Frames are lifted out one at a time.", r["text"]) self.assertNotIn("tests", r) self.assertEqual([x for n, x in enumerate(acc["fragments"]) if n != i], [x for n, x in enumerate(new) if n != i]) self.assertTrue((self.c.path("work") / "K-work" / "backup" / "accepted.json").is_file()) def test_every_write_is_appended_to_the_repairs_log(self) -> None: self.assertEqual(0, self.fix(self.answers([self.replace_lifted()]))[0]) first = self.repairs() self.assertEqual(1, len(first)) e = first[0] self.assertEqual("fix", e["command"]) self.assertEqual(1, e["n"]) self.assertEqual([{"concept_id": "frames", "before": self.label(FRAMES_LIFTED), "action": "replace", "after": [kassemble.label(r) for r in self.accepted()["fragments"] if r["text"] == "Frames are lifted out one at a time."]}], e["fragments"]) self.assertEqual(["frames.md"], e["expected"]["pages"]) self.assertEqual(1, len(e["expected"]["claims"])) self.assertEqual(1, len(e["expected"]["claims_gone"])) self.assertEqual(krepair.tree_sha256(self.c.path("wiki")), e["bundle_sha256"]) answer = {"key": HIVE_OPEN, "action": "remove"} self.assertEqual(0, self.fix(self.answers([answer], "second.json"))[0]) entries = self.repairs() self.assertEqual(2, len(entries)) self.assertEqual(first[0], entries[0]) self.assertEqual(2, entries[1]["n"]) def test_the_log_names_the_links_moved_and_taken_out(self) -> None: answer = {"key": BROOD_QUEEN, "action": "replace", "fragments": [ {"text": "The queen lays a single egg in each empty cell.", "quote": "lays one egg in each empty cell", "anchor": {"file": "docs-en-hive.md", "start": 21, "end": 23}, "provenance": "documented"}]} code, said = self.fix(self.answers([answer])) self.assertEqual(0, code, said) (e,) = self.repairs() (moved,) = e["links"]["moved"] self.assertEqual(("brood.md", "queen", "queen-cells.md"), (moved["after"]["page"], moved["after"]["anchor"], moved["after"]["target"])) self.assertEqual(gate.sentence_key("The queen lays a single egg in each empty cell."), moved["after"]["sentence"]) self.assertEqual([], e["links"]["removed"]) self.assertIn(moved["after"], self.links()) self.assertNotIn(moved["before"], self.links()) def test_a_link_into_a_page_that_goes_is_taken_out_and_listed(self) -> None: answer = {"key": {"concept_id": "smoker", "file": "docs-en-hive.md", "start": 27, "end": 27, "quote": "makes cool smoke that calms the bees", "provenance": "documented"}, "action": "remove"} code, said = self.fix(self.answers([answer])) self.assertEqual(0, code, said) (e,) = self.repairs() self.assertEqual(["smoker.md"], e["expected"]["pages_gone"]) (gone,) = e["links"]["removed"] self.assertEqual(("hive.md", "smoker.md"), (gone["record"]["page"], gone["record"]["target"])) self.assertIn("taken out", said) self.assertNotIn("smoker.md", [r["target"] for r in self.links()]) def test_context_the_answer_leaves_out_is_kept_or_listed(self) -> None: # the old record cites the heading "Inspections"; an anchor in the Frames section cannot keep it answer = {"key": HIVE_OPEN, "action": "replace", "fragments": [ {"text": "A hive holds frames that can be lifted out.", "quote": "lifted out one by one", "anchor": {"file": "docs-en-hive.md", "start": 8, "end": 10}, "provenance": "documented"}]} code, said = self.fix(self.answers([answer])) self.assertEqual(0, code, said) self.assertIn("context not kept", said) (e,) = self.repairs() self.assertEqual([{"concept_id": "hive", "footnote": self.label(HIVE_OPEN), "quote": "Inspections"}], e["listed"]["context_not_kept"]) r = next(r for r in self.accepted()["fragments"] if r["text"] == "A hive holds frames that can be lifted out.") self.assertNotIn("context", r) def test_a_page_left_without_a_home_and_a_main_page_without_its_source_are_listed(self) -> None: code, said = self.fix(self.answers([{"key": HONEY_OWN, "action": "remove"}])) self.assertEqual(0, code, said) (e,) = self.repairs() self.assertEqual([{"concept_id": "honey-harvest", "source": "docs-en-honey.md"}], e["listed"]["main_page_without_source"]) self.assertEqual(["honey-harvest"], e["listed"]["without_home"]) self.assertIn("main page without its source: honey-harvest (docs-en-honey.md)", said) def test_a_replacement_with_the_footnote_of_another_fragment_of_the_concept_is_refused(self) -> None: # frames has two fragments quoting line 9; the second one's quote and anchor give the first one's footnote key = {"concept_id": "frames", "file": "docs-en-hive.md", "start": 8, "end": 10, "quote": "holds wax comb where bees store honey and raise brood", "provenance": "documented"} taken = {"concept_id": "frames", "file": "docs-en-hive.md", "start": 8, "end": 10, "quote": "lifted out one by one", "provenance": "documented"} answer = {"key": key, "action": "replace", "fragments": [ {"text": "Frames can be lifted out.", "quote": "lifted out one by one", "anchor": {"file": "docs-en-hive.md", "start": 8, "end": 10}, "provenance": "documented"}]} before = self.trees() code, said = self.fix(self.answers([answer])) self.assertEqual(1, code) self.assertRegex(said, rf"(?m)^refused: label-taken frames {self.label(key)}") self.assertIn(self.label(taken), said) self.assertEqual(before, self.trees()) def test_more_than_three_new_fragments_are_refused(self) -> None: one = self.replace_lifted()["fragments"][0] answer = {"key": FRAMES_LIFTED, "action": "replace", "fragments": [one] * 4} code, said = self.fix(self.answers([answer])) self.assertEqual(1, code) self.assertRegex(said, r"(?m)^refused: malformed frames ") def test_a_read_only_collection_can_be_repaired_against(self) -> None: # a locked collection (measured 21.10.2026: wiki-18a's folder and pages are read-only): the trial build copies # it, and the copy must take a build wiki = self.c.path("wiki") paths = [wiki, *wiki.iterdir()] for p in paths: p.chmod(0o555 if p.is_dir() else 0o444) try: code, said = self.fix(self.answers([self.replace_lifted()])) finally: for p in paths: p.chmod(0o755 if p.is_dir() else 0o644) self.assertEqual(0, code, said) (e,) = self.repairs() self.assertEqual(["frames.md"], e["expected"]["pages"]) def test_an_unfinished_links_write_stops_the_next_fix(self) -> None: leftover = krepair.links_tmp(self.c.path("links")) leftover.write_text("{}", encoding="utf-8") code, said = self.fix(self.answers([self.replace_lifted()])) self.assertEqual(2, code) self.assertIn(str(leftover), said) def test_a_marker_of_an_unfinished_kpack_write_stops_the_fix(self) -> None: (self.c.path("work") / "K-work" / "replace-x.pending").write_text( json.dumps({"command": "replace-x", "restore": [], "delete": []}), encoding="utf-8") self.assertEqual(2, self.fix(self.answers([self.replace_lifted()]))[0]) def test_two_fixes_against_one_collection_both_stand_and_the_diff_takes_both(self) -> None: self.assertEqual(0, self.fix(self.answers([self.replace_lifted()]))[0]) self.assertEqual(0, self.fix(self.answers([{"key": HIVE_OPEN, "action": "remove"}], "second.json"))[0]) self.assertEqual(0, self.build()) code, said = self.diff() self.assertEqual(0, code, said) pages = json.loads((self.c.path("diff") / "sider.json").read_text(encoding="utf-8"))["pages"] self.assertEqual(["frames.md", "hive.md"], pages) class DiffTest(Base): def replaced(self) -> None: answer = {"key": BROOD_QUEEN, "action": "replace", "fragments": [ {"text": "The queen lays a single egg in each empty cell.", "quote": "lays one egg in each empty cell", "anchor": {"file": "docs-en-hive.md", "start": 21, "end": 23}, "provenance": "documented"}]} code, said = self.fix(self.answers([answer])) self.assertEqual(0, code, said) def test_ids_json_is_read_by_claims_calls(self) -> None: self.replaced() self.assertEqual(0, self.build()) code, said = self.diff() self.assertEqual(0, code, said) ids = json.loads((self.c.path("diff") / "ids.json").read_text(encoding="utf-8")) self.assertEqual("okf-wiki/repair-ids/1", ids["schema"]) self.assertEqual([gate.claim_id("brood.md", "The queen lays a single egg in each empty cell.")], ids["ids"]) pages = json.loads((self.c.path("diff") / "sider.json").read_text(encoding="utf-8")) self.assertEqual(("okf-wiki/repair-pages/1", ["brood.md"], []), (pages["schema"], pages["pages"], pages["gone"])) new_claims, new_pairs, calls = (self.c.root / n for n in ("new-claims.json", "new-pairs.json", "calls.json")) gate.quiet(claims.main, ["list", self.p["wiki"], self.p["sources"], "--out", str(new_claims), "--pairs", str(new_pairs)]) code, said = gate.quiet(claims.main, ["calls", "--claims", str(new_claims), "--pairs", str(new_pairs), "--out", str(calls), "--ids", str(self.c.path("diff") / "ids.json")]) self.assertEqual(0, code, said) layout = json.loads(calls.read_text(encoding="utf-8")) self.assertEqual(ids["ids"], [i for call in layout["calls"] for i in call["ids"]]) def test_a_build_that_is_not_the_logged_one_exits_1(self) -> None: self.replaced() self.assertEqual(0, self.build(links=False)) # every link left out: more pages change than were logged code, said = self.diff() self.assertEqual(1, code) self.assertIn("differs", said) self.assertTrue((self.c.path("diff") / "sider.json").is_file()) def test_a_change_with_no_repair_logged_against_the_old_collection_exits_1(self) -> None: self.replaced() self.assertEqual(0, self.build()) with (self.c.path("wiki_old") / "log.md").open("a", encoding="utf-8") as f: f.write("* another build\n") # an old collection no repair was checked against code, said = self.diff() self.assertEqual(1, code) self.assertIn("no repair is logged against", said) def test_the_identity_logs_nothing_to_change_and_the_diff_agrees(self) -> None: answer = {"key": HIVE_OPEN, "action": "replace", "fragments": [ {"text": "Open the hive only on a warm and calm day during an inspection.", "quote": "only on a warm and calm day", "anchor": {"file": "docs-en-hive.md", "start": 14, "end": 17}, "provenance": "documented", "context": ["Inspections"]}]} self.assertEqual(0, self.fix(self.answers([answer]))[0]) (e,) = self.repairs() self.assertEqual({"pages": [], "pages_gone": [], "claims": [], "claims_gone": []}, e["expected"]) self.assertEqual(0, self.build()) self.assertEqual(0, self.diff()[0]) class LinkTest(Base): SMOKER = ["--page", "hive.md", "--sentence", gate.sentence_key("Light the smoker before you lift the lid."), "--anchor", "smoker", "--at", "10"] def test_unlink_takes_out_exactly_one_record(self) -> None: before = self.links() code, said = run(["unlink", "--links", self.p["links"], *self.SMOKER]) self.assertEqual(0, code, said) after = self.links() self.assertEqual([r for r in before if r["target"] != "smoker.md"], after) self.assertFalse(self.repairs_path().exists()) def test_unlink_with_a_work_folder_is_logged_and_the_diff_agrees(self) -> None: code, said = run(["unlink", "--links", self.p["links"], *self.SMOKER, "--work", self.p["work"], "--bundle", self.p["wiki"]]) self.assertEqual(0, code, said) (e,) = self.repairs() self.assertEqual(("unlink", ["hive.md"]), (e["command"], e["expected"]["pages"])) self.assertEqual(0, self.build()) code, said = self.diff() self.assertEqual(0, code, said) def test_unlink_with_a_work_folder_needs_the_bundle(self) -> None: code, _ = run(["unlink", "--links", self.p["links"], *self.SMOKER, "--work", self.p["work"]]) self.assertEqual(2, code) def test_relink_to_its_own_page_or_to_a_target_the_sentence_has_is_refused(self) -> None: before = self.c.trees("links") code, said = run(["relink", "--links", self.p["links"], *self.SMOKER, "--to", "hive.md", "--bundle", self.p["wiki"]]) self.assertEqual(1, code) self.assertRegex(said, r"(?m)^refused: self-link hive\.md ") code, said = run(["relink", "--links", self.p["links"], *self.SMOKER, "--to", "smoker.md", "--bundle", self.p["wiki"]]) self.assertEqual(1, code) self.assertRegex(said, r"(?m)^refused: duplicate-link hive\.md ") self.assertEqual(before, self.c.trees("links")) def test_relink_changes_only_the_target(self) -> None: code, said = run(["relink", "--links", self.p["links"], *self.SMOKER, "--to", "frames.md", "--bundle", self.p["wiki"]]) self.assertEqual(0, code, said) moved = [r for r in self.links() if r["sentence"] == self.SMOKER[3]] self.assertEqual([{"page": "hive.md", "sentence": self.SMOKER[3], "anchor": "smoker", "at": 10, "target": "frames.md"}], moved) class LinksetTest(Base): CANDIDATES = {"links": [{"page": "hive.md", "sentence": "Light the smoker before you lift the lid.", "anchor": "smoker", "target": "smoker.md"}, {"page": "water.md", "sentence": "Bees need a water source near the hive.", "n": 1, "anchor": "hive", "target": "hive.md"}]} def linkset(self, lst: Path, *extra: str) -> tuple[int, str]: return run(["linkset", "--list", str(lst), "--bundle", self.p["wiki"], "--out-dir", self.p["out"], *extra]) def test_the_catalogue_holds_the_target_and_the_pages_that_share_a_source(self) -> None: code, said = self.linkset(self.repair_list(self.CANDIDATES)) self.assertEqual(0, code, said) self.assertIn("1 group", said) ids = klink.read_ids(self.c.path("out") / "group-1.catalogue") self.assertEqual("smoker", ids[0]) # the current target first for cid in ("frames", "brood", "water", "smoker", "hive", "honey-harvest"): self.assertIn(cid, ids) # hive.md and water.md cite docs-en-hive.md and docs-en-honey.md self.assertNotIn("swarming", ids) def test_groups_are_split_where_the_catalogues_would_pass_the_limit(self) -> None: with mock.patch.object(klink, "CATALOGUE_CHARS", 1): code, said = self.linkset(self.repair_list(self.CANDIDATES)) self.assertEqual(0, code, said) self.assertIn("2 groups", said) cats = sorted(klink.read_ids(p) for p in self.c.path("out").glob("group-*.catalogue")) self.assertEqual([["hive"], ["smoker"]], cats) # cut to the current target, and the cut is counted self.assertIn("cut", said) def test_with_the_claim_list_a_stale_list_is_refused(self) -> None: lst = self.repair_list({**self.CANDIDATES, "stale": True}) code, said = self.linkset(lst, "--claims", self.p["claims"]) self.assertEqual(1, code) self.assertRegex(said, r"(?m)^refused: stale-list \S") self.assertEqual(0, self.linkset(self.repair_list(self.CANDIDATES, "fresh.json"), "--claims", self.p["claims"])[0]) def test_a_candidate_whose_sentence_is_not_on_the_page_is_refused(self) -> None: lst = self.repair_list(self.CANDIDATES) self.edit_list(lst, lambda d: d["links"][0].update({"sentence": "0000000000000000-0"})) code, said = self.linkset(lst) self.assertEqual(1, code) self.assertRegex(said, r"(?m)^refused: stale-list hive\.md ") self.assertEqual([], list(self.c.path("out").iterdir())) class AgentTest(unittest.TestCase): def test_the_two_copies_are_byte_identical(self) -> None: self.assertEqual(AGENT.read_bytes(), AGENT_COPY.read_bytes()) def test_the_agent_file_is_thin_like_the_reader_s(self) -> None: text = AGENT.read_text(encoding="utf-8") m = re.match(r"---\n(.*?)\n---\n(.*)", text, re.S) assert m is not None head = dict(line.split(": ", 1) for line in m.group(1).splitlines()) self.assertEqual({"name": "okf-wiki-repairer", "tools": "Read, Write", "model": "opus", "effort": "xhigh", "omitClaudeMd": "true"}, {k: v for k, v in head.items() if k != "description"}) body = m.group(2) for need in ("read nothing else", "one JSON file", "done"): self.assertIn(need, body) self.assertLess(len(body), 1_000) self.assertNotIn("bee", text.lower()) def test_the_agent_file_shares_no_eight_words_with_the_gate_s_blocks(self) -> None: mine = reader.WORD.findall(AGENT.read_text(encoding="utf-8").lower()) source = " ".join(p.read_text(encoding="utf-8") for p in sorted((CASES / "kilder" / "kilde").glob("*"))) theirs = reader.WORD.findall(source.lower()) runs = {tuple(theirs[i:i + 8]) for i in range(len(theirs) - 7)} self.assertEqual(set(), {tuple(mine[i:i + 8]) for i in range(len(mine) - 7)} & runs) if __name__ == "__main__": unittest.main()