First public release, published as one snapshot. What changed between versions is in CHANGELOG.md. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2290 lines
133 KiB
Python
2290 lines
133 KiB
Python
"""Tests for okfwiki/kpack.py: skeleton, packs, input parts, the one hard check at writing, concept identity, and one
|
||
source's contribution replaced or removed alone. No model call.
|
||
|
||
Ported from the trial's tests (evals/tests/test_p1_build.py, KSkeletonTest, KIdentityTest, KPackTest, KCliTest) with
|
||
rule E and the provenance mark, plus rev 4 Step 11: a pack number outside 1..N exits 2, a pending marker blocks every
|
||
command with exit 2, the id-mismatch count is reported and never rejects. The rule itself (F1-F6, P1-P4) is in
|
||
tests/test_fragment_rule.py. The fixtures are synthetic text about gardening (CLAUDE.md rule 3).
|
||
|
||
Wave 2, order 10 (Step 10 of the v1 plan, K2 point 17): K1 at the sources root decides a source's kind and main topic;
|
||
the compiler names the main page of each docs source and a home for every page that has none; accept records the main
|
||
page per source, which replace and remove swap with the source, and the home per page, which stays with its page; a new
|
||
concept from a release note or a history page needs a home like any other page (without one it is homeless and the
|
||
guard asks, never rejected); and run-before / run-after chain the steps without a model around the compiler, with the hard check's
|
||
one retry, the auditor on a subset root of the packs built so far, the guard, the breach stop and the stop line. The dry
|
||
run at the end drives three synthetic packs about bread through the chain the way the skill tells the orchestrator to,
|
||
with the model steps written by the test and the auditor of Step 7 stood in for by a stub that writes K2 findings in the
|
||
auditor's JSON form (order 7 builds the real one; at the merge the dry run runs against it).
|
||
Run: python3 -m unittest discover -s tests -p 'test_kpack.py'
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import io
|
||
import json
|
||
import os
|
||
import re
|
||
import sys
|
||
import tempfile
|
||
import unittest
|
||
from collections import Counter
|
||
from contextlib import redirect_stderr, redirect_stdout
|
||
from dataclasses import asdict
|
||
from pathlib import Path
|
||
from typing import Any, Callable
|
||
from unittest import mock
|
||
|
||
ROOT = Path(__file__).resolve().parents[1]
|
||
sys.path.insert(0, str(ROOT))
|
||
|
||
from okfwiki import audit, cost, kblocks, klink, kpack, sources # noqa: E402
|
||
from okfwiki.common import RunError # noqa: E402
|
||
|
||
DOC_TOMATO = """# Growing tomatoes
|
||
Source: https://example.org/garden/tomatoes
|
||
|
||
How to grow tomatoes in a small garden.
|
||
|
||
## Soil
|
||
|
||
Tomatoes need deep, loose soil with `compost` mixed in.
|
||
Water the soil every morning.
|
||
|
||
* **Sun**: full sun for eight hours.
|
||
* **Space**: forty centimetres between plants,
|
||
more in a wet summer.
|
||
|
||
```bash
|
||
# not a heading
|
||
mix compost
|
||
```
|
||
|
||
| Month | Task |
|
||
|---|---|
|
||
| May | plant |
|
||
|
||
## Overview
|
||
|
||
The Kitchen Garden keeps tomatoes and basil together.
|
||
|
||
### Pruning
|
||
|
||
Remove side shoots once a week.
|
||
"""
|
||
|
||
DOC_BASIL = """# Growing basil
|
||
Source: https://example.org/garden/basil
|
||
|
||
## Soil
|
||
|
||
Basil likes warm soil and the Kitchen Garden.
|
||
Plant Sweet Genovese early.
|
||
|
||
## Watering
|
||
|
||
Water basil with `compost` tea when the leaves droop.
|
||
"""
|
||
|
||
REL_1 = """## 1.2.3
|
||
|
||
- Added the `compost` command to the Kitchen Garden planner.
|
||
- Fixed watering reminders.
|
||
"""
|
||
|
||
REL_2 = """## 1.10.0
|
||
|
||
- The Kitchen Garden planner now shows `compost` levels.
|
||
"""
|
||
|
||
REL_3 = """## 1.9.2
|
||
|
||
- Basil seedlings get their own tray.
|
||
"""
|
||
|
||
TOMATO, BASIL, R123 = "docs-en-tomatoes.md", "docs-en-basil.md", "release-v1.2.3.md"
|
||
|
||
|
||
def entry(eid: str, title: str, aliases: tuple[str, ...] = ()) -> dict[str, Any]:
|
||
return {"id": eid, "title": title, "aliases": [title, *aliases], "kind": "skeleton", "merge_of": None,
|
||
"origin": None}
|
||
|
||
|
||
def frag(concept: str, file: str, start: int, end: int, text: str, quote: str | None, status: str = "existing",
|
||
eid: str | None = None, provenance: str | None = "documented", tests: bool = False) -> dict[str, Any]:
|
||
f: dict[str, Any] = {"concept": concept, "status": status, "text": text,
|
||
"anchor": {"file": file, "start": start, "end": end}}
|
||
if eid is not None:
|
||
f["id"] = eid
|
||
if quote is not None:
|
||
f["quote"] = quote
|
||
if provenance is not None:
|
||
f["provenance"] = provenance
|
||
if tests:
|
||
f["tests"] = {"topic_shape": "a named planner", "citation": "See the reference"}
|
||
return f
|
||
|
||
|
||
PACK_1 = [
|
||
frag("Soil", TOMATO, 8, 9, "Tomatoes need deep, loose soil.", "deep, loose soil", eid="soil"),
|
||
frag("Kitchen Garden", TOMATO, 26, 26, "The Kitchen Garden keeps tomatoes and basil.", "The Kitchen Garden keeps",
|
||
status="new", tests=True),
|
||
frag("Kitchen Garden", R123, 3, 3, "The planner gained a command.", "Kitchen Garden planner", provenance="inferred"),
|
||
frag("Watering reminders", R123, 4, 4, "Reminders were fixed.", "Fixed watering reminders", status="new",
|
||
tests=True),
|
||
]
|
||
|
||
|
||
class Fixture(unittest.TestCase):
|
||
def setUp(self) -> None:
|
||
self._tmp = tempfile.TemporaryDirectory()
|
||
self.tmp = Path(self._tmp.name)
|
||
self.root = self.tmp / "sources"
|
||
self.kilde = self.root / "kilde"
|
||
self.kilde.mkdir(parents=True)
|
||
files = {TOMATO: DOC_TOMATO, BASIL: DOC_BASIL, R123: REL_1, "release-v1.10.0.md": REL_2,
|
||
"release-v1.9.2.md": REL_3}
|
||
for name, text in files.items():
|
||
(self.kilde / name).write_text(text, encoding="utf-8")
|
||
self.work = self.tmp / "work"
|
||
self.kw = self.work / "K-work"
|
||
self.blocks = self.tmp / "blocks.json"
|
||
doc = kblocks.blocks_document(self.kilde, kblocks.order(sorted(files)))
|
||
self.blocks.write_text(json.dumps(doc), encoding="utf-8")
|
||
|
||
def tearDown(self) -> None:
|
||
self._tmp.cleanup()
|
||
|
||
def doc(self) -> dict[str, Any]:
|
||
data: dict[str, Any] = json.loads(self.blocks.read_text(encoding="utf-8"))
|
||
return data
|
||
|
||
def cli(self, args: list[str]) -> int:
|
||
with redirect_stdout(io.StringIO()), redirect_stderr(io.StringIO()):
|
||
return kpack.main(args)
|
||
|
||
def skeleton(self) -> None:
|
||
self.assertEqual(self.cli(["skeleton", "--sources", str(self.root), "--work", str(self.work)]), 0)
|
||
|
||
def common(self) -> list[str]:
|
||
return ["--blocks", str(self.blocks), "--sources", str(self.root), "--work", str(self.work)]
|
||
|
||
def write_pack(self, n: int, fragments: list[dict[str, Any]]) -> None:
|
||
path = self.work / "K-fragments" / f"pack-{n}.json"
|
||
path.parent.mkdir(parents=True, exist_ok=True)
|
||
path.write_text(json.dumps({"fragments": fragments}), encoding="utf-8")
|
||
|
||
def accepted(self) -> list[dict[str, Any]]:
|
||
records: list[dict[str, Any]] = json.loads((self.kw / "accepted.json").read_text(encoding="utf-8"))["fragments"]
|
||
return records
|
||
|
||
def log(self) -> dict[str, Any]:
|
||
data: dict[str, Any] = json.loads((self.kw / "packs.json").read_text(encoding="utf-8"))
|
||
return data
|
||
|
||
|
||
# ---------------------------------------------------------------------------------------------- skeleton and identity
|
||
|
||
|
||
class KSkeletonTest(Fixture):
|
||
def test_the_skeleton_is_an_index_of_docs_headings_that_pass_test_2(self) -> None:
|
||
index, collisions = kpack.skeleton(self.kilde)
|
||
titles = {e["title"] for e in index["entries"]}
|
||
self.assertIn("Growing tomatoes", titles)
|
||
self.assertIn("Pruning", titles)
|
||
self.assertIn("Soil", titles)
|
||
self.assertNotIn("Overview", titles)
|
||
soil = [e for e in index["entries"] if e["title"] == "Soil"]
|
||
self.assertEqual(len(soil), 1) # the same heading in two pages is one entry
|
||
self.assertEqual(soil[0]["origin"]["file"], BASIL) # pages in name order, first one wins
|
||
self.assertEqual(collisions, 1) # "Soil" in the tomato page collides with the basil page's
|
||
|
||
def test_the_cli_writes_the_index_once_and_reports_the_collisions(self) -> None:
|
||
with redirect_stdout(io.StringIO()) as printed:
|
||
self.assertEqual(kpack.main(["skeleton", "--sources", str(self.root), "--work", str(self.work)]), 0)
|
||
self.assertIn("1 headings collide across files", printed.getvalue())
|
||
self.assertTrue((self.kw / "index.json").is_file())
|
||
self.assertEqual(self.cli(["skeleton", "--sources", str(self.root), "--work", str(self.work)]), 2)
|
||
|
||
def test_no_concept_gets_a_reserved_file_name(self) -> None:
|
||
(self.kilde / "docs-en-zz.md").write_text("# Zz\n\n## Index\n\nA.\n\n## Log\n\nB.\n", encoding="utf-8")
|
||
index, _ = kpack.skeleton(self.kilde)
|
||
ids = {e["id"] for e in index["entries"]}
|
||
self.assertTrue({"index-2", "log-2"} <= ids)
|
||
self.assertFalse({"index", "log"} & ids)
|
||
|
||
def test_norm(self) -> None:
|
||
self.assertEqual(kpack.norm("The `Compost` bins"), "compost bin")
|
||
self.assertEqual(kpack.norm("settings.json"), "settings.json")
|
||
self.assertEqual(kpack.norm(" Kitchen Garden! "), "kitchen garden")
|
||
self.assertEqual(kpack.norm("CLI flags"), "cli flag")
|
||
|
||
|
||
class RerunTextTest(unittest.TestCase):
|
||
"""The texts say what hard_check does: a rejected fragment is dropped and counted; only an output left unwritten
|
||
runs the pack once more (exit 3), and a second time is STOP (exit 6)."""
|
||
|
||
def test_the_help_text_names_only_an_unwritten_output_as_the_cause_of_a_rerun(self) -> None:
|
||
doc = " ".join((kpack.__doc__ or "").split())
|
||
self.assertNotIn("a fragment rejected at writing, or an output left unwritten", doc)
|
||
self.assertNotIn("the pack was rejected at writing twice", doc)
|
||
self.assertIn("a fragment rejected at writing is dropped and counted", doc)
|
||
self.assertIn("an output left unwritten sets the pack's outputs aside and exits 3", doc)
|
||
self.assertIn("6 = STOP: an output of the pack was left unwritten twice", doc)
|
||
|
||
def test_both_compiler_prompts_say_a_rejected_fragment_is_dropped(self) -> None:
|
||
texts = [(ROOT / rel).read_text(encoding="utf-8")
|
||
for rel in ("agents/okf-wiki-compiler.md", ".claude/agents/okf-wiki-compiler.md")]
|
||
self.assertEqual(texts[0], texts[1])
|
||
text = " ".join(texts[0].split())
|
||
self.assertNotIn("one rejected fragment makes the whole pack run again", text)
|
||
self.assertIn("A rejected fragment is dropped and counted; the pack does not run again for it.", text)
|
||
|
||
|
||
class KIdentityTest(unittest.TestCase):
|
||
def setUp(self) -> None:
|
||
self.index = {"entries": [entry("kitchen-garden-planner", "Kitchen Garden planner"),
|
||
entry("compost", "compost", ("compost heap",))]}
|
||
|
||
def frag(self, concept: str, status: str = "new", file: str = TOMATO, eid: str | None = None) -> dict[str, Any]:
|
||
f: dict[str, Any] = {"concept": concept, "status": status, "anchor": {"file": file, "start": 8, "end": 9}}
|
||
if eid:
|
||
f["id"] = eid
|
||
return f
|
||
|
||
def test_outcomes(self) -> None:
|
||
r = kpack.resolve
|
||
self.assertEqual(r(self.frag("x", "existing", eid="compost"), self.index), ("existing", "compost"))
|
||
self.assertEqual(r(self.frag("Kitchen Garden planners"), self.index), ("existing", "kitchen-garden-planner"))
|
||
self.assertEqual(r(self.frag("Compost heap"), self.index), ("existing", "compost"))
|
||
self.assertEqual(r(self.frag("Kitchen Garden planner tool"), self.index),
|
||
("merge-candidate", "kitchen-garden-planner"))
|
||
self.assertEqual(r(self.frag("Garden hose"), self.index), ("new", None))
|
||
self.assertEqual(r(self.frag("Garden hose", file=R123), self.index), ("rejected", "release-note-new"))
|
||
self.assertEqual(r(self.frag("Kitchen Garden planner tool", file=R123), self.index),
|
||
("rejected", "release-note-new"))
|
||
self.assertEqual(r(self.frag("Troubleshooting"), self.index), ("rejected", "meta"))
|
||
|
||
def test_a_concept_given_as_an_index_id_without_an_id_field_is_that_concept(self) -> None:
|
||
# order 11, pack 1: the compiler put the index id in `concept` and wrote no `id`; main_pages and homes read the
|
||
# same value as an id (lookup), so the fragments must too, or the main page names a page no fragment is on
|
||
r = kpack.resolve
|
||
for status in ("existing", "new"):
|
||
self.assertEqual(r(self.frag("kitchen-garden-planner", status), self.index),
|
||
("existing", "kitchen-garden-planner"))
|
||
self.assertEqual(r(self.frag("kitchen-garden-planner", "existing", file=R123), self.index),
|
||
("existing", "kitchen-garden-planner"))
|
||
for e in self.index["entries"]:
|
||
self.assertEqual(r(self.frag(e["id"], "existing"), self.index), ("existing", kpack.lookup(self.index, e["id"])))
|
||
self.assertFalse(kpack.id_mismatch(self.frag("kitchen-garden-planner", "existing"), self.index))
|
||
self.assertTrue(kpack.id_mismatch(self.frag("Garden hose", "existing"), self.index))
|
||
|
||
def test_relevant_lines_by_phrase(self) -> None:
|
||
got = kpack.relevant_lines(self.index, "We keep the compost heaps behind the shed.")
|
||
self.assertEqual([e["id"] for e in got], ["compost"])
|
||
self.assertEqual(kpack.relevant_lines(self.index, "Nothing about gardens here."), [])
|
||
|
||
def test_an_id_mismatch_is_an_existing_fragment_whose_entry_does_not_match_its_name(self) -> None:
|
||
m = kpack.id_mismatch
|
||
self.assertFalse(m(self.frag("Compost heap", "existing", eid="compost"), self.index))
|
||
self.assertFalse(m(self.frag("Kitchen Garden planner tool", "existing", eid="kitchen-garden-planner"),
|
||
self.index)) # Jaccard 0.75 >= 0.6
|
||
self.assertTrue(m(self.frag("Garden hose", "existing", eid="compost"), self.index))
|
||
self.assertTrue(m(self.frag("Compost", "existing", eid="no-such-id"), self.index))
|
||
self.assertFalse(m(self.frag("Garden hose", "new"), self.index))
|
||
|
||
|
||
# ---------------------------------------------------------------------------------------------- packs and the check
|
||
|
||
|
||
class KPackTest(Fixture):
|
||
def test_packs_of_five(self) -> None:
|
||
self.assertEqual([len(p) for p in kpack.packs([f"n{i}" for i in range(19)], 5)], [5, 5, 5, 4])
|
||
|
||
def test_a_large_pack_is_split_into_parts_under_the_read_cap(self) -> None:
|
||
big = "".join(f"Line {i} says the tomatoes are fine today and tomorrow, and the basil too.\n\n"
|
||
for i in range(1110))
|
||
(self.kilde / "docs-en-big.md").write_text("# Big\n\n" + big, encoding="utf-8")
|
||
doc = kblocks.blocks_document(self.kilde, ["docs-en-big.md"])
|
||
parts = kpack.render_input(1, ["docs-en-big.md"], doc, [], self.kilde)
|
||
self.assertGreater(sum(len(p) for p in parts), 130_000)
|
||
self.assertEqual(len(parts), 3)
|
||
self.assertTrue(all(len(p) <= kpack.PART_CHARS for p in parts))
|
||
self.assertIn("docs-en-big.md › Big › 3–3", parts[0])
|
||
|
||
def test_the_contract_asks_for_a_quote_and_a_provenance_mark_on_every_fragment(self) -> None:
|
||
parts = kpack.render_input(1, [TOMATO], self.doc(), [], self.kilde)
|
||
text = "".join(parts)
|
||
self.assertIn(kpack.CONTRACT, text)
|
||
for word in ("quote", "provenance", *kpack.PROVENANCE):
|
||
self.assertIn(f'"{word}"' if word in kpack.PROVENANCE else word, kpack.CONTRACT, word)
|
||
|
||
def test_validate_gives_one_reason_per_rejected_fragment(self) -> None:
|
||
pack = [TOMATO, R123, "docs-en-gone.md"]
|
||
doc = self.doc()
|
||
doc["blocks"].append({"file": "docs-en-gone.md", "start": 1, "end": 1, "kind": "paragraph", "section": "",
|
||
"version": None})
|
||
good = frag("Soil", TOMATO, 8, 9, "Tomatoes need deep, loose soil.", "deep, loose soil", eid="soil")
|
||
cases: dict[str, dict[str, Any]] = {
|
||
"anchor-crosses-blocks": {**good, "anchor": {"file": TOMATO, "start": 8, "end": 11}},
|
||
"quote-missing": {k: v for k, v in good.items() if k != "quote"},
|
||
"quote-not-in-span": {**good, "quote": "forty centimetres"},
|
||
"sentence-count": {**good, "text": "One. Two. Three. Four."},
|
||
"tests-missing": {**good, "status": "new", "concept": "Loose soil"},
|
||
"anchor-outside-pack": {**good, "anchor": {"file": BASIL, "start": 6, "end": 6}},
|
||
"malformed": {"concept": "Soil"},
|
||
"provenance-missing": {k: v for k, v in good.items() if k != "provenance"},
|
||
"provenance-unknown": {**good, "provenance": "likely"},
|
||
"source-missing": {**good, "anchor": {"file": "docs-en-gone.md", "start": 1, "end": 1}, "quote": "x"},
|
||
}
|
||
accepted, rejected = kpack.validate([good, {**good, "text": ""}, *cases.values()], pack, doc, self.kilde)
|
||
self.assertEqual(len(accepted), 1)
|
||
self.assertEqual(rejected, Counter({**{reason: 1 for reason in cases}, "sentence-count": 2}))
|
||
self.assertTrue(set(rejected) <= set(kpack.REASONS))
|
||
self.assertEqual((accepted[0]["quote_line"], accepted[0]["position"], accepted[0]["provenance"]),
|
||
(8, 1, "documented"))
|
||
|
||
def test_an_id_that_is_not_a_string_is_malformed_not_a_crash(self) -> None:
|
||
bad = [frag("Soil", TOMATO, 8, 9, "Tomatoes need deep, loose soil.", "deep, loose soil", eid="soil")
|
||
for _ in range(2)]
|
||
bad[0]["id"], bad[1]["id"] = ["soil"], {"id": "soil"}
|
||
accepted, rejected = kpack.validate(bad, [TOMATO], self.doc(), self.kilde)
|
||
self.assertEqual((accepted, rejected), ([], Counter({"malformed": 2})))
|
||
|
||
def test_the_quote_line_is_the_line_of_the_span_that_holds_the_quote(self) -> None:
|
||
f = frag("Soil", TOMATO, 12, 13, "Plants stand forty centimetres apart.", "more in a wet summer")
|
||
accepted, rejected = kpack.validate([f], [TOMATO], self.doc(), self.kilde)
|
||
self.assertEqual((len(accepted), sum(rejected.values())), (1, 0))
|
||
self.assertEqual(accepted[0]["quote_line"], 13)
|
||
|
||
|
||
class KCliTest(Fixture):
|
||
def test_skeleton_pack_and_accept(self) -> None:
|
||
self.skeleton()
|
||
self.assertEqual(self.cli(["pack", "1", *self.common()]), 0)
|
||
self.assertTrue((self.work / "K-oppgaver" / "pack-1-part-1.md").is_file())
|
||
self.write_pack(1, PACK_1)
|
||
self.assertEqual(self.cli(["accept", "1", *self.common()]), 0)
|
||
acc = self.accepted()
|
||
self.assertEqual([a["outcome"] for a in acc], ["existing", "new", "existing", "new"])
|
||
self.assertEqual(acc[2]["concept_id"], acc[1]["concept_id"]) # the release note enriches the new concept
|
||
self.assertEqual(acc[2]["version"], "1.2.3")
|
||
self.assertEqual([a["provenance"] for a in acc], ["documented", "documented", "inferred", "documented"])
|
||
self.assertTrue({"pack", "text", "concept_id", "file", "quote", "quote_line"} <= set(acc[0])) # klink reads
|
||
log = self.log()["packs"][0]
|
||
# order 10: a new concept from a release note without a home is kept and homeless (the guard asks), not rejected
|
||
self.assertEqual(log["rejected"], {})
|
||
self.assertIn(acc[3]["concept_id"], log["homes"]["homeless"])
|
||
self.assertEqual(log["proposed"], 4)
|
||
self.assertEqual(log["id_mismatch"], 1) # the third is "existing" without an id: counted, still accepted
|
||
# PM 02.10: the ids of the index lines sent, and the accepted fragments whose concept was not among them
|
||
self.assertIn("soil", log["index_ids"])
|
||
self.assertNotIn(acc[1]["concept_id"], log["index_ids"])
|
||
self.assertEqual(log["not_sent"], {"total": 3, "by_outcome": {"existing": 1, "new": 2}})
|
||
|
||
def test_an_id_mismatch_is_counted_and_never_rejected(self) -> None:
|
||
self.skeleton()
|
||
self.write_pack(1, [frag("Garden hose", TOMATO, 8, 9, "Water the soil every morning.", "every morning",
|
||
eid="soil")])
|
||
with redirect_stdout(io.StringIO()) as printed:
|
||
self.assertEqual(kpack.main(["accept", "1", *self.common()]), 0)
|
||
self.assertEqual([a["concept_id"] for a in self.accepted()], ["soil"])
|
||
self.assertEqual(self.log()["packs"][0]["id_mismatch"], 1)
|
||
self.assertIn("id mismatches (counted, not rejected): 1", printed.getvalue())
|
||
|
||
|
||
class ContextTest(Fixture):
|
||
"""Wave 4 order 22, correction O+L: order 20 coded 128 of 344 unsupported claims as drawing on a heading above the
|
||
quote (O 54) or another line near it (L 74). A fragment may name up to MAX_CONTEXT context quotes; code places each
|
||
on a heading of the anchor's heading path or a line of the anchor's section, never on the quote's own line, and
|
||
drops the rest, counted. Synthetic fixture (the tomato page above)."""
|
||
|
||
SUN = {"file": TOMATO, "start": 11, "end": 11, "kind": "item", "section": "Growing tomatoes › Soil",
|
||
"version": None}
|
||
|
||
def lines(self) -> list[str]:
|
||
return DOC_TOMATO.split("\n")
|
||
|
||
def test_a_heading_of_the_path_and_a_line_of_the_section_are_kept_the_rest_dropped(self) -> None:
|
||
wanted = ["Soil", "Water the soil every morning", "Remove side shoots", "full sun for eight", "Growing tomatoes"]
|
||
kept, dropped = kpack.context_of(self.lines(), self.SUN, 11, wanted)
|
||
self.assertEqual(kept, [{"line": 6, "quote": "Soil", "section": "Growing tomatoes › Soil"},
|
||
{"line": 9, "quote": "Water the soil every morning",
|
||
"section": "Growing tomatoes › Soil"}])
|
||
self.assertEqual(dropped, 3) # another section, the quote's own line, and one over MAX_CONTEXT
|
||
self.assertEqual(kpack.MAX_CONTEXT, 2)
|
||
|
||
def test_the_title_heading_is_on_the_path(self) -> None:
|
||
kept, dropped = kpack.context_of(self.lines(), self.SUN, 11, ["Growing tomatoes"])
|
||
self.assertEqual((kept, dropped), ([{"line": 1, "quote": "Growing tomatoes", "section": "Growing tomatoes"}], 0))
|
||
|
||
def test_a_context_that_is_not_a_list_of_text_is_dropped(self) -> None:
|
||
self.assertEqual(kpack.context_of(self.lines(), self.SUN, 11, None), ([], 0))
|
||
self.assertEqual(kpack.context_of(self.lines(), self.SUN, 11, "Soil"), ([], 1))
|
||
self.assertEqual(kpack.context_of(self.lines(), self.SUN, 11, [3, "", "Soil\nSun"]), ([], 3))
|
||
|
||
def test_accept_keeps_the_context_on_the_record_and_counts_what_it_dropped(self) -> None:
|
||
self.skeleton()
|
||
f = frag("Soil", TOMATO, 11, 11, "Tomatoes in this soil need full sun.", "full sun", eid="soil")
|
||
f["context"] = ["Soil", "Remove side shoots"]
|
||
self.write_pack(1, [f, frag("Soil", TOMATO, 8, 9, "Tomatoes need deep soil.", "deep, loose soil", eid="soil")])
|
||
with redirect_stdout(io.StringIO()) as printed:
|
||
self.assertEqual(kpack.main(["accept", "1", *self.common()]), 0)
|
||
acc = self.accepted()
|
||
self.assertEqual(acc[0]["context"], [{"line": 6, "quote": "Soil", "section": "Growing tomatoes › Soil"}])
|
||
self.assertNotIn("context", acc[1])
|
||
self.assertEqual(self.log()["packs"][0]["context"], {"kept": 1, "dropped": 1})
|
||
self.assertIn("context quotes: kept 1, dropped 1", printed.getvalue())
|
||
|
||
|
||
class MergedAnchorTest(Fixture):
|
||
"""Wave 4 order 25 (order 24 on round 1): all 20 claims marked merged were unsupported, all 20 coded L (what was
|
||
missing stood on another line), 19 of them with no context quote, every one on an anchor of 5 to 47 lines. A merged
|
||
fragment combines what several lines of its anchor say; when it names no context quote, code cites the other lines
|
||
of its anchor, nearest the quote first, at most MERGED_LINES. Synthetic fixture (the tomato page above)."""
|
||
|
||
def accept(self, fragments: list[dict[str, Any]]) -> tuple[list[dict[str, Any]], str]:
|
||
self.skeleton()
|
||
self.write_pack(1, fragments)
|
||
with redirect_stdout(io.StringIO()) as printed:
|
||
self.assertEqual(kpack.main(["accept", "1", *self.common()]), 0)
|
||
return self.accepted(), printed.getvalue()
|
||
|
||
def test_a_merged_fragment_without_context_cites_the_other_lines_of_its_anchor(self) -> None:
|
||
acc, printed = self.accept([frag("Soil", TOMATO, 8, 9, "Tomatoes need deep soil and daily water.",
|
||
"deep, loose soil", eid="soil", provenance="merged")])
|
||
self.assertEqual(acc[0]["context"], [{"line": 9, "quote": "Water the soil every morning.",
|
||
"section": "Growing tomatoes › Soil"}])
|
||
self.assertEqual(self.log()["packs"][0]["context"], {"kept": 0, "dropped": 0, "anchored": 1,
|
||
"anchor_lines": 1})
|
||
self.assertIn("merged fragments cited by their anchor: 1 (1 lines)", printed)
|
||
|
||
def test_its_own_context_a_documented_mark_or_a_one_line_anchor_is_never_widened(self) -> None:
|
||
own = frag("Soil", TOMATO, 8, 9, "Soil tomatoes need deep soil.", "deep, loose soil", eid="soil",
|
||
provenance="merged")
|
||
own["context"] = ["Soil"]
|
||
acc, _ = self.accept([
|
||
own,
|
||
frag("Soil", TOMATO, 8, 9, "Tomatoes need deep soil.", "deep, loose soil", eid="soil"),
|
||
frag("Soil", TOMATO, 11, 11, "Tomatoes need full sun.", "full sun", eid="soil", provenance="merged")])
|
||
self.assertEqual(acc[0]["context"], [{"line": 6, "quote": "Soil", "section": "Growing tomatoes › Soil"}])
|
||
self.assertNotIn("context", acc[1])
|
||
self.assertNotIn("context", acc[2])
|
||
self.assertEqual(self.log()["packs"][0]["context"], {"kept": 1, "dropped": 0})
|
||
|
||
def test_at_most_merged_lines_nearest_first_in_line_order_without_fences_or_rules(self) -> None:
|
||
lines = ["| Plant | Month |", "|---|---|", *[f"| Plant {i} | May |" for i in range(1, 15)], "```", ""]
|
||
kept = kpack.anchor_context(lines, {"start": 1, "end": 18}, 9, "Planting")
|
||
self.assertEqual(kpack.MERGED_LINES, 8)
|
||
self.assertEqual([k["line"] for k in kept], [5, 6, 7, 8, 10, 11, 12, 13])
|
||
self.assertEqual(kept[0], {"line": 5, "quote": "| Plant 3 | May |", "section": "Planting"})
|
||
self.assertEqual(kpack.anchor_context(lines, {"start": 1, "end": 3}, 3, "Planting"),
|
||
[{"line": 1, "quote": "| Plant | Month |", "section": "Planting"}])
|
||
|
||
|
||
class AcceptSafetyTest(Fixture):
|
||
def setUp(self) -> None:
|
||
super().setUp()
|
||
self.skeleton()
|
||
self.write_pack(1, PACK_1)
|
||
|
||
def test_a_pack_number_outside_1_to_n_exits_2_and_writes_nothing(self) -> None:
|
||
for n in ("0", "-1", "2"): # five sources in packs of five: one pack
|
||
with self.subTest(n=n):
|
||
self.assertEqual(self.cli(["accept", n, *self.common()]), 2)
|
||
self.assertEqual(self.cli(["pack", n, *self.common()]), 2)
|
||
self.assertFalse((self.kw / "accepted.json").exists())
|
||
|
||
def test_a_pack_is_accepted_once(self) -> None:
|
||
self.assertEqual(self.cli(["accept", "1", *self.common()]), 0)
|
||
self.assertEqual(self.cli(["accept", "1", *self.common()]), 2)
|
||
self.assertEqual(len(self.accepted()), 4) # the release note's new concept is kept, homeless (order 10)
|
||
|
||
def test_a_successful_accept_leaves_no_marker_and_a_backup_of_what_it_replaced(self) -> None:
|
||
before = (self.kw / "index.json").read_bytes()
|
||
self.assertEqual(self.cli(["accept", "1", *self.common()]), 0)
|
||
self.assertEqual(list(self.kw.glob("*.pending")), [])
|
||
self.assertEqual((self.kw / "backup" / "index.json").read_bytes(), before)
|
||
self.assertNotEqual((self.kw / "index.json").read_bytes(), before)
|
||
|
||
def test_a_write_that_did_not_finish_leaves_its_marker_and_says_what_to_undo(self) -> None:
|
||
(self.kw / "accepted.json").mkdir() # the write of accepted.json fails after the marker is set
|
||
self.assertEqual(self.cli(["accept", "1", *self.common()]), 2)
|
||
self.assertEqual([p.name for p in self.kw.glob("*.pending")], ["accept-1.pending"])
|
||
(self.kw / "accepted.json").rmdir()
|
||
with redirect_stderr(io.StringIO()) as err:
|
||
self.assertEqual(kpack.main(["accept", "1", *self.common()]), 2)
|
||
self.assertIn("accept-1.pending", err.getvalue())
|
||
self.assertIn("copy index.json back from", err.getvalue())
|
||
self.assertIn("delete accepted.json, packs.json", err.getvalue()) # they did not exist before
|
||
|
||
def test_a_marker_stops_every_command_and_only_the_marker(self) -> None:
|
||
self.assertEqual(self.cli(["accept", "1", *self.common()]), 0)
|
||
commands = {
|
||
"pack": ["pack", "1", *self.common()],
|
||
"replace": ["replace", TOMATO, "--fragments", str(self.work / "K-fragments" / "pack-1.json"),
|
||
*self.common()],
|
||
"remove": ["remove", TOMATO, "--work", str(self.work)],
|
||
"skeleton": ["skeleton", "--sources", str(self.root), "--work", str(self.tmp / "fresh")],
|
||
}
|
||
(self.tmp / "fresh" / "K-work").mkdir(parents=True)
|
||
(self.tmp / "fresh" / "K-work" / "skeleton.pending").write_text("{}\n", encoding="utf-8")
|
||
(self.kw / "accept-7.pending").write_text("{}\n", encoding="utf-8")
|
||
for name, args in commands.items():
|
||
with self.subTest(command=name, marker=True):
|
||
self.assertEqual(self.cli(args), 2)
|
||
(self.kw / "accept-7.pending").unlink()
|
||
(self.tmp / "fresh" / "K-work" / "skeleton.pending").unlink()
|
||
for name, args in commands.items(): # the control: without the marker each command runs
|
||
with self.subTest(command=name, marker=False):
|
||
self.assertEqual(self.cli(args), 0)
|
||
|
||
def test_the_task_parts_of_a_pack_are_written_under_the_marker(self) -> None:
|
||
(self.kw / ".packs.json.tmp").mkdir() # the write of packs.json fails after the parts are written
|
||
self.assertEqual(self.cli(["pack", "1", *self.common()]), 2)
|
||
part = self.work / "K-oppgaver" / "pack-1-part-1.md"
|
||
self.assertTrue(part.is_file())
|
||
(self.kw / ".packs.json.tmp").rmdir()
|
||
with redirect_stderr(io.StringIO()) as err:
|
||
self.assertEqual(kpack.main(["pack", "1", *self.common()]), 2)
|
||
self.assertIn("pack-1.pending", err.getvalue())
|
||
self.assertIn(str(part), err.getvalue())
|
||
self.assertNotIn("index.json", err.getvalue())
|
||
|
||
def test_a_registered_pack_keeps_its_size_and_a_new_pack_takes_the_size_given(self) -> None:
|
||
# order 27 A2 replaces "another size is refused": the size is set per new pack and registered with it
|
||
self.assertEqual(self.cli(["pack", "1", *self.common(), "--size", "2"]), 0)
|
||
self.assertEqual(self.log()["size"], 2)
|
||
self.assertEqual(self.cli(["pack", "2", *self.common(), "--size", "3"]), 0)
|
||
self.assertEqual({p["pack"]: (len(p["sources"]), p["size"]) for p in self.log()["packs"]}, {1: (2, 2), 2: (3, 3)})
|
||
self.assertEqual(self.log()["size"], 2) # the first size stays the default for a new pack
|
||
self.assertEqual(self.cli(["pack", "3", *self.common()]), 2) # the five sources are in packs 1 and 2
|
||
|
||
|
||
# ---------------------------------------------------------------------------------------------- replace and remove
|
||
|
||
|
||
class ReplaceTest(Fixture):
|
||
def setUp(self) -> None:
|
||
super().setUp()
|
||
self.skeleton()
|
||
self.write_pack(1, PACK_1)
|
||
self.assertEqual(self.cli(["accept", "1", *self.common()]), 0)
|
||
self.before = self.accepted()
|
||
|
||
def replacement(self, fragments: list[dict[str, Any]]) -> list[str]:
|
||
path = self.work / "K-fragments" / "replace.json"
|
||
path.write_text(json.dumps({"fragments": fragments}), encoding="utf-8")
|
||
return ["--fragments", str(path), *self.common()]
|
||
|
||
def test_replacing_one_source_keeps_every_other_record_unchanged_and_in_place(self) -> None:
|
||
new = [frag("Soil", TOMATO, 8, 9, "Water tomatoes every morning.", "every morning", eid="soil"),
|
||
frag("Pruning", TOMATO, 30, 30, "Remove side shoots weekly.", "side shoots", eid="pruning")]
|
||
self.assertEqual(self.cli(["replace", TOMATO, *self.replacement(new)]), 0)
|
||
after = self.accepted()
|
||
self.assertEqual([a for a in after if a["file"] != TOMATO], [b for b in self.before if b["file"] != TOMATO])
|
||
self.assertEqual([a["text"] for a in after[:2]], ["Water tomatoes every morning.", "Remove side shoots weekly."])
|
||
self.assertEqual({(a["pack"], a["position"]) for a in after if a["file"] == TOMATO}, {(1, 2)})
|
||
self.assertEqual(self.log()["packs"][0]["replaced"],
|
||
[{"source": TOMATO, "removed": 2, "proposed": 2, "accepted": 2, "rejected": {}}])
|
||
|
||
def test_replace_keeps_the_place_the_source_had_when_its_pack_was_accepted(self) -> None:
|
||
(self.kilde / "docs-en-alpha.md").write_text("# Alpha\n\nAlpha beds come first.\n", encoding="utf-8")
|
||
names = kblocks.order(sorted(p.name for p in self.kilde.glob("*.md")))
|
||
self.assertEqual(names.index(TOMATO), 2) # the new page moves tomatoes from position 2 to 3
|
||
self.blocks.write_text(json.dumps(kblocks.blocks_document(self.kilde, names)), encoding="utf-8")
|
||
new = [frag("Soil", TOMATO, 8, 9, "Water tomatoes every morning.", "every morning", eid="soil")]
|
||
self.assertEqual(self.cli(["replace", TOMATO, *self.replacement(new)]), 0)
|
||
self.assertEqual({(a["pack"], a["position"]) for a in self.accepted() if a["file"] == TOMATO}, {(1, 2)})
|
||
self.assertEqual([r["pack"] for r in self.log()["packs"] if "replaced" in r], [1])
|
||
|
||
def test_a_source_in_no_accepted_pack_cannot_be_replaced(self) -> None:
|
||
(self.kilde / "docs-en-alpha.md").write_text("# Alpha\n\nAlpha beds come first.\n", encoding="utf-8")
|
||
names = kblocks.order(sorted(p.name for p in self.kilde.glob("*.md")))
|
||
self.blocks.write_text(json.dumps(kblocks.blocks_document(self.kilde, names)), encoding="utf-8")
|
||
new = [frag("Alpha", "docs-en-alpha.md", 3, 3, "Alpha beds come first.", "Alpha beds", status="new",
|
||
tests=True)]
|
||
self.assertEqual(self.cli(["replace", "docs-en-alpha.md", *self.replacement(new)]), 2)
|
||
|
||
def test_a_replacement_is_checked_against_its_own_source_only(self) -> None:
|
||
new = [frag("Soil", BASIL, 6, 6, "Basil likes warm soil.", "warm soil", eid="soil"),
|
||
frag("Soil", TOMATO, 8, 9, "Tomatoes need loose soil.", None)]
|
||
self.assertEqual(self.cli(["replace", "kilde/" + TOMATO, *self.replacement(new)]), 0) # a K1 path works too
|
||
self.assertEqual([a for a in self.accepted() if a["file"] == TOMATO], [])
|
||
self.assertEqual(self.log()["packs"][0]["replaced"][0]["rejected"],
|
||
{"anchor-outside-pack": 1, "quote-missing": 1})
|
||
|
||
def test_replace_cannot_run_for_an_unknown_source_or_a_pack_not_yet_accepted(self) -> None:
|
||
self.assertEqual(self.cli(["replace", "docs-en-gone.md", *self.replacement([])]), 2)
|
||
self.assertEqual(self.cli(["pack", "1", *self.common()]), 0)
|
||
log = self.log()
|
||
log["packs"][0].pop("accepted")
|
||
(self.kw / "packs.json").write_text(json.dumps(log), encoding="utf-8")
|
||
self.assertEqual(self.cli(["replace", TOMATO, *self.replacement([])]), 2)
|
||
self.assertEqual(self.accepted(), self.before)
|
||
|
||
def test_removing_one_source_keeps_every_other_record(self) -> None:
|
||
self.assertEqual(self.cli(["remove", TOMATO, "--work", str(self.work)]), 0)
|
||
self.assertEqual(self.accepted(), [b for b in self.before if b["file"] != TOMATO])
|
||
self.assertEqual(self.log()["packs"][0]["removed"], [{"source": TOMATO, "fragments": 2}])
|
||
self.assertEqual(self.cli(["remove", TOMATO, "--work", str(self.work)]), 2) # nothing left to remove
|
||
|
||
|
||
# ---------------------------------------------------------------------------------------------- wave 2: K1 and homes
|
||
|
||
A, B, C, D = "docs-en-a.md", "docs-en-b.md", "docs-en-c.md", "docs-en-d.md"
|
||
W, R = "docs-en-whats-new-2026-w14.md", "release-v1.2.0.md"
|
||
BREAD = {
|
||
A: "# Sourdough\n\nSourdough bread rises with wild yeast instead of packet yeast.\n\n## Starter\n\n"
|
||
"A starter is a mix of flour and water that ferments for about a week.\n\n## Flour\n\n"
|
||
"Rye flour makes a starter more lively than white flour.\n",
|
||
B: "# Proofing\n\nProofing is the last rise before the loaf goes into the oven.\n\n## Cold proofing\n\n"
|
||
"A night in the fridge slows the rise and deepens the taste.\n",
|
||
C: "# Scoring\n\nScoring is a shallow cut across the top of the loaf after proofing.\n"
|
||
"Bakers score with a razor blade on a handle, called a lame.\n",
|
||
D: "# Oven\n\nA hot oven gives the crust its colour.\nA baking stone holds heat under the loaf.\n\n## Steam\n\n"
|
||
"Steam in the first minutes lets the loaf open along its cuts.\n\n## Crumb\n\nAn open crumb has large, uneven holes.\n",
|
||
W: "# Week 14 · Bakery notes\n\n- The oven guide now explains preheating for an hour.\n"
|
||
"- A new page describes an open crumb.\n",
|
||
R: "## 1.2.0\n\n- The starter guide now lists rye flour.\n- Added a rye starter recipe to the starter guide.\n"
|
||
"- Added a bake timer.\n",
|
||
}
|
||
HISTORY = {W} # K1 main_topic false for a docs page: the weekly What's New pages (K1 rule 4, changed 08.10)
|
||
NEW = {"topic_shape": "a named thing a baker uses", "citation": "See the reference for it"}
|
||
|
||
|
||
def quiet(fn: Callable[[list[str]], int], args: list[str]) -> tuple[int, str]:
|
||
out, err = io.StringIO(), io.StringIO()
|
||
with redirect_stdout(out), redirect_stderr(err):
|
||
code = fn(args)
|
||
return code, out.getvalue() + err.getvalue()
|
||
|
||
|
||
def freeze_root(tmp: Path, files: dict[str, str], history: set[str], name: str = "root") -> Path:
|
||
"""A K1 root made by the K1 code itself (okfwiki.sources freeze)."""
|
||
staging = tmp / f"{name}-staging"
|
||
staging.mkdir()
|
||
meta: dict[str, Any] = {"collection": "bakery", "schema": "okf-wiki/source-meta/1", "sources": {}}
|
||
for file, text in files.items():
|
||
(staging / file).write_text(text, encoding="utf-8")
|
||
if file.startswith("release-"):
|
||
meta["sources"][file] = {"kind": "release-notes", "main_topic": False, "version": file[9:-3],
|
||
"url": f"https://example.org/bakery/changelog#{file[9:-3]}"}
|
||
else:
|
||
meta["sources"][file] = {"kind": "docs", "main_topic": file not in history,
|
||
"url": f"https://example.org/bakery/{file}"}
|
||
(tmp / f"{name}-meta.json").write_text(json.dumps(meta), encoding="utf-8")
|
||
root = tmp / name
|
||
code, out = quiet(sources.main, ["freeze", str(staging), "--meta", str(tmp / f"{name}-meta.json"), "--out",
|
||
str(root), "--frozen-at", "2026-10-08T06:00:00Z"])
|
||
assert code == 0, out
|
||
return root
|
||
|
||
|
||
REAL_RUN_AUDIT = kpack.run_audit
|
||
|
||
# The classes k2_audit computes itself: since order 7 merged, audit() gives them too, and the stub must not count twice.
|
||
STOOD_IN_FOR = ("link-rule", "main-page-missing", "main-page-duplicate", "main-page-invalid", "no-main-page-link")
|
||
|
||
|
||
def k2_audit(bundle: Path, root: Path) -> dict[str, Any]:
|
||
"""A stand-in for the auditor of Step 7 (order 7 builds it): today's findings without link-rule, which Step 7
|
||
retires, plus main-page-missing and no-main-page-link computed from the bundle and K1 at ROOT (K2 points 1-5, 14),
|
||
in the auditor's JSON form. main-page-missing names the K1 path as its file."""
|
||
found = [asdict(f) for f in audit.audit(bundle, root) if f.check not in STOOD_IN_FOR]
|
||
k1 = json.loads((root / "source-manifest.json").read_text(encoding="utf-8"))
|
||
main_topic = {"/" + str(e["path"]) for e in k1["sources"] if e["main_topic"]}
|
||
b = audit.load_bundle(bundle, root)
|
||
marked: dict[str, list[str]] = {}
|
||
mains: set[str] = set()
|
||
for d in b.concepts:
|
||
fm = d.frontmatter or {}
|
||
srcs = fm.get("sources")
|
||
own = {s.get("resource") for s in srcs if isinstance(s, dict)} if isinstance(srcs, list) else set()
|
||
marks = fm.get("x-main-for")
|
||
valid = [v for v in marks if isinstance(v, str) and v in own and v in main_topic] if isinstance(marks, list) else []
|
||
for v in valid:
|
||
marked.setdefault(v, []).append(d.rel)
|
||
if valid:
|
||
mains.add(d.rel)
|
||
for path in sorted(main_topic):
|
||
if not marked.get(path):
|
||
found.append({"check": "main-page-missing", "group": "links", "file": path[1:], "line": None,
|
||
"message": f"main-topic source {path} has no main page"})
|
||
linked: set[str] = set()
|
||
for d in b.concepts:
|
||
if d.rel in mains:
|
||
for _, target in audit.body_links(d):
|
||
rel = audit.resolve(d.rel, target.split("#", 1)[0])
|
||
if rel and rel != d.rel:
|
||
linked.add(rel)
|
||
for d in sorted(b.concepts, key=lambda x: x.rel):
|
||
if d.rel not in mains and d.rel not in linked:
|
||
found.append({"check": "no-main-page-link", "group": "links", "file": d.rel, "line": None,
|
||
"message": "no link from a main page"})
|
||
pages = len(b.concepts) - len(mains)
|
||
line = (f"main pages: {len(main_topic) - sum(1 for p in main_topic if not marked.get(p))} of {len(main_topic)} "
|
||
f"sources · linked from a main page: {pages - sum(f['check'] == 'no-main-page-link' for f in found)} of "
|
||
f"{pages} pages")
|
||
return {"findings": found, "summary": {}, "link_rule": {"line": line}}
|
||
|
||
|
||
class BakeryK1(unittest.TestCase):
|
||
"""Six bread sources in a K1 root, in packs of two: [A, B], [C, D], [W, R]; W is a history page by K1."""
|
||
|
||
def setUp(self) -> None:
|
||
self._tmp = tempfile.TemporaryDirectory()
|
||
self.tmp = Path(self._tmp.name)
|
||
self.root = freeze_root(self.tmp, BREAD, HISTORY)
|
||
self.work = self.tmp / "work"
|
||
self.work.mkdir()
|
||
self.blocks = self.work / "blocks.json"
|
||
doc = kblocks.blocks_document(self.root / "kilde", kblocks.order(sorted(BREAD)))
|
||
self.blocks.write_text(json.dumps(doc), encoding="utf-8")
|
||
self.assertEqual(self.kp(["skeleton", "--sources", str(self.root), "--work", str(self.work)])[0], 0)
|
||
|
||
def tearDown(self) -> None:
|
||
self._tmp.cleanup()
|
||
|
||
def kp(self, args: list[str]) -> tuple[int, str]:
|
||
return quiet(kpack.main, args)
|
||
|
||
def common(self) -> list[str]:
|
||
return ["--blocks", str(self.blocks), "--sources", str(self.root), "--work", str(self.work), "--size", "2"]
|
||
|
||
def frag(self, concept: str, file: str, quote: str, text: str, new: bool = False,
|
||
provenance: str = "documented") -> dict[str, Any]:
|
||
"""A fragment anchored on the block whose line holds the quote, as the compiler would write it."""
|
||
line = next(n for n, raw in enumerate(BREAD[file].split("\n"), 1) if quote in raw)
|
||
doc = json.loads(self.blocks.read_text(encoding="utf-8"))
|
||
block = next(b for b in doc["blocks"] if b["file"] == file and b["start"] <= line <= b["end"])
|
||
f: dict[str, Any] = {"concept": concept, "status": "new" if new else "existing", "text": text,
|
||
"anchor": {"file": file, "start": block["start"], "end": block["end"]}, "quote": quote,
|
||
"provenance": provenance}
|
||
if new:
|
||
f["tests"] = NEW
|
||
return f
|
||
|
||
def compiled(self, fragments: list[dict[str, Any]], main_pages: list[tuple[str, str]] = (), # type: ignore[assignment]
|
||
homes: list[tuple[str, str]] = ()) -> dict[str, Any]: # type: ignore[assignment]
|
||
return {"fragments": fragments, "main_pages": [{"source": s, "concept": c} for s, c in main_pages],
|
||
"homes": [{"concept": c, "home": h} for c, h in homes]}
|
||
|
||
def write(self, n: int, doc: dict[str, Any]) -> Path:
|
||
path = self.work / "K-fragments" / f"pack-{n}.json"
|
||
path.parent.mkdir(parents=True, exist_ok=True)
|
||
path.write_text(json.dumps(doc), encoding="utf-8")
|
||
return path
|
||
|
||
def accept(self, n: int, doc: dict[str, Any]) -> str:
|
||
self.write(n, doc)
|
||
code, out = self.kp(["accept", str(n), *self.common()])
|
||
self.assertEqual(code, 0, out)
|
||
return out
|
||
|
||
def acc(self) -> dict[str, Any]:
|
||
data: dict[str, Any] = json.loads((self.work / "K-work" / "accepted.json").read_text(encoding="utf-8"))
|
||
return data
|
||
|
||
def log(self, n: int) -> dict[str, Any]:
|
||
data = json.loads((self.work / "K-work" / "packs.json").read_text(encoding="utf-8"))
|
||
return next(dict(r) for r in data["packs"] if r["pack"] == n)
|
||
|
||
# the compiler's output for the three packs, as the dry run uses it
|
||
|
||
def pack_1(self) -> dict[str, Any]:
|
||
return self.compiled([
|
||
self.frag("Sourdough", A, "rises with wild yeast", "Sourdough bread rises with wild yeast."),
|
||
self.frag("Starter", A, "ferments for about a week", "A starter is flour and water that ferments for about a week."),
|
||
self.frag("Flour", A, "more lively than white flour", "Rye flour makes a starter more lively than white flour."),
|
||
self.frag("Proofing", B, "the last rise before the loaf goes into the oven",
|
||
"Proofing is the last rise before the loaf goes into the oven."),
|
||
self.frag("Cold proofing", B, "slows the rise", "A night in the fridge slows the rise."),
|
||
], main_pages=[(A, "Sourdough"), (B, "Proofing")], homes=[("Starter", "sourdough"), ("Flour", "Sourdough")])
|
||
|
||
def pack_2(self, bad: bool = False) -> dict[str, Any]:
|
||
scoring = self.frag("Scoring", C, "a shallow cut across the top of the loaf",
|
||
"Scoring is a shallow cut across the top of the loaf.")
|
||
steam = self.frag("Steam", D, "lets the loaf open along its cuts", "Steam lets the loaf open along its cuts.")
|
||
if bad:
|
||
steam["quote"] = "lets the loaf rise" # not on any line of its anchor: rejected at writing
|
||
return self.compiled([
|
||
scoring,
|
||
self.frag("Proofing", C, "after proofing", "Bakers score the loaf after proofing.", provenance="inferred"),
|
||
self.frag("Oven", D, "gives the crust its colour", "A hot oven gives the crust its colour."),
|
||
self.frag("Baking stone", D, "holds heat under the loaf", "A baking stone holds heat under the loaf.", new=True),
|
||
steam,
|
||
], main_pages=[(D, "Oven")], homes=[("Baking stone", "oven"), ("Steam", "Oven")]) # C names no main page
|
||
|
||
def pack_3(self) -> dict[str, Any]:
|
||
return self.compiled([
|
||
self.frag("Oven", W, "explains preheating for an hour", "The oven guide explains preheating for an hour."),
|
||
self.frag("Crumb", W, "describes an open crumb", "A page describes an open crumb."), # no home declared
|
||
self.frag("Starter", R, "lists rye flour", "The starter guide lists rye flour."),
|
||
self.frag("Rye starter", R, "a rye starter recipe", "The starter guide has a rye starter recipe.", new=True),
|
||
self.frag("Bake timer", R, "Added a bake timer", "There is a bake timer.", new=True), # new, no home
|
||
], homes=[("Rye starter", "sourdough")])
|
||
|
||
|
||
class SourceFactsTest(BakeryK1):
|
||
def test_k1_decides_the_kind_and_the_main_topic(self) -> None:
|
||
facts = kpack.source_facts(self.root, sorted(BREAD))
|
||
self.assertTrue(facts[A].main_topic)
|
||
self.assertFalse(facts[A].history)
|
||
self.assertTrue(facts[W].history) # a docs page by its name, a history page by K1
|
||
self.assertFalse(facts[W].main_topic)
|
||
self.assertTrue(facts[R].history)
|
||
self.assertEqual((facts[R].kind, facts[W].kind), ("release-notes", "docs"))
|
||
|
||
def test_without_k1_the_file_name_decides_as_today(self) -> None:
|
||
plain = self.tmp / "plain"
|
||
(plain / "kilde").mkdir(parents=True)
|
||
for name, text in BREAD.items():
|
||
(plain / "kilde" / name).write_text(text, encoding="utf-8")
|
||
facts = kpack.source_facts(plain, sorted(BREAD))
|
||
self.assertTrue(facts[W].main_topic) # by name a docs page
|
||
self.assertTrue(facts[R].history)
|
||
|
||
def test_an_invalid_k1_or_a_source_it_does_not_list_cannot_run(self) -> None:
|
||
with self.assertRaises(RunError):
|
||
kpack.source_facts(self.root, [*sorted(BREAD), "docs-en-zz.md"])
|
||
manifest = self.root / "source-manifest.json"
|
||
good = manifest.read_text(encoding="utf-8")
|
||
manifest.chmod(0o644)
|
||
for broken in ("{", json.dumps({"schema": "other", "sources": []}),
|
||
good.replace('"main_topic": true', '"main_topic": "yes"', 1)):
|
||
with self.subTest(broken=broken[:20]):
|
||
manifest.write_text(broken, encoding="utf-8")
|
||
with self.assertRaises(RunError):
|
||
kpack.source_facts(self.root, sorted(BREAD))
|
||
|
||
def test_the_week_of_a_weekly_page_comes_from_its_name(self) -> None:
|
||
self.assertEqual(kpack.week_of(W), "2026-w14")
|
||
self.assertIsNone(kpack.week_of(A))
|
||
self.assertIsNone(kpack.week_of(R))
|
||
|
||
def test_the_skeleton_leaves_out_history_pages(self) -> None:
|
||
titles = {e["title"] for e in kpack.read_index(self.work)["entries"]}
|
||
self.assertIn("Cold proofing", titles)
|
||
self.assertIn("Crumb", titles)
|
||
self.assertFalse(any("Week 14" in t for t in titles)) # history adds to concepts and sets no structure
|
||
|
||
|
||
class ContractTest(BakeryK1):
|
||
def test_the_contract_asks_for_main_pages_and_homes(self) -> None:
|
||
for word in ('"main_pages"', '"homes"', '"home"', "main page", "Main pages"):
|
||
self.assertIn(word, kpack.CONTRACT, word)
|
||
self.assertNotIn("never from a release note", kpack.CONTRACT)
|
||
self.assertNotIn("kept only with a home", kpack.CONTRACT) # without a home it is homeless, never dropped
|
||
|
||
def test_the_contract_names_the_context_field_and_its_limit(self) -> None:
|
||
# order 22, correction O+L: the field accept reads (context_of) is the one the compiler is asked for
|
||
self.assertIn("`context`", kpack.CONTRACT)
|
||
self.assertEqual(kpack.MAX_CONTEXT, 2)
|
||
self.assertIn("at most two", kpack.CONTRACT)
|
||
self.assertIn("whole line", kpack.CONTRACT)
|
||
|
||
def test_the_contract_names_several_outputs_one_per_turn(self) -> None:
|
||
# order 22 A2: the outputs run-before names for a pack of more than two parts, as run-after reads them
|
||
flat = " ".join(kpack.CONTRACT.split())
|
||
self.assertIn("each output path named in your prompt", flat)
|
||
self.assertIn("each in its own turn", flat)
|
||
self.assertIn("the parts it names", flat)
|
||
|
||
def test_the_pack_input_marks_each_source_and_index_line_and_lists_the_main_pages(self) -> None:
|
||
self.accept(1, self.pack_1())
|
||
self.assertEqual(self.kp(["pack", "2", *self.common()])[0], 0)
|
||
text = "".join(p.read_text(encoding="utf-8") for p in sorted((self.work / "K-oppgaver").glob("pack-2-*.md")))
|
||
self.assertIn(f"- {C}: docs page; name its main page", text)
|
||
self.assertIn("- id: proofing | Proofing | main page", text)
|
||
main = text.split("## Main pages", 1)[1]
|
||
self.assertIn("- sourdough | Sourdough", main)
|
||
self.assertIn("- proofing | Proofing", main)
|
||
self.assertNotIn("starter |", main) # a page with a home is not a main page
|
||
self.assertEqual(self.kp(["pack", "3", *self.common()])[0], 0)
|
||
text = "".join(p.read_text(encoding="utf-8") for p in sorted((self.work / "K-oppgaver").glob("pack-3-*.md")))
|
||
self.assertIn(f"- {W}: history (week 2026-w14); no main page", text)
|
||
self.assertIn(f"- {R}: history (version 1.2.0); no main page", text)
|
||
self.assertIn("- id: starter | Starter | has a home", text)
|
||
self.assertIn("- id: crumb | Crumb\n", text) # a concept without a page: neither mark
|
||
|
||
|
||
class MainPageHomeTest(BakeryK1):
|
||
def test_the_main_page_of_each_docs_source_and_the_declared_homes_are_recorded_per_source(self) -> None:
|
||
out = self.accept(1, self.pack_1())
|
||
acc = self.acc()
|
||
self.assertEqual(acc["main_pages"], [{"source": A, "concept_id": "sourdough", "pack": 1, "by": "compiler"},
|
||
{"source": B, "concept_id": "proofing", "pack": 1, "by": "compiler"}])
|
||
self.assertEqual(acc["homes"], [{"source": A, "concept_id": "starter", "home": "sourdough", "pack": 1,
|
||
"by": "compiler"},
|
||
{"source": A, "concept_id": "flour", "home": "sourdough", "pack": 1,
|
||
"by": "compiler"}])
|
||
self.assertEqual(self.log(1)["main_pages"], {"named": 2, "valid": 2, "missing": []})
|
||
self.assertIn("main pages: 2 of 2 docs sources", out)
|
||
|
||
def test_a_page_without_a_declared_home_is_accepted_and_listed_homeless_never_rejected(self) -> None:
|
||
out = self.accept(1, self.pack_1())
|
||
self.assertIn("cold-proofing", {r["concept_id"] for r in self.acc()["fragments"]})
|
||
self.assertEqual(self.log(1)["homes"]["homeless"], ["cold-proofing"])
|
||
self.assertEqual(self.log(1)["rejected"], {})
|
||
self.assertIn("without a home: cold-proofing", out)
|
||
|
||
def test_a_main_page_that_does_not_cite_its_source_or_a_home_that_is_no_main_page_is_not_recorded(self) -> None:
|
||
doc = self.pack_1()
|
||
doc["main_pages"] = [{"source": A, "concept": "Sourdough"}, {"source": B, "concept": "Sourdough"},
|
||
{"source": C, "concept": "Scoring"}]
|
||
doc["homes"] = [{"concept": "Starter", "home": "flour"}, {"concept": "Flour", "home": "sourdough"}]
|
||
self.accept(1, doc)
|
||
self.assertEqual([m["source"] for m in self.acc()["main_pages"]], [A])
|
||
self.assertEqual(self.log(1)["main_pages"], {"named": 3, "valid": 1, "missing": [B]})
|
||
self.assertEqual([h["concept_id"] for h in self.acc()["homes"]], ["flour"])
|
||
# B has no main page, so proofing is a page like any other and needs a home too
|
||
self.assertEqual(self.log(1)["homes"]["homeless"], ["starter", "proofing", "cold-proofing"])
|
||
self.assertEqual(self.log(1)["homes"]["invalid"], 1)
|
||
|
||
def test_the_last_valid_home_declared_for_a_page_wins_and_an_earlier_invalid_one_is_counted(self) -> None:
|
||
doc = self.pack_1()
|
||
doc["homes"] = [{"concept": "Starter", "home": "flour"}, # flour is no main page: invalid
|
||
{"concept": "Starter", "home": "proofing"}, # valid, but a newer choice follows
|
||
{"concept": "Starter", "home": "sourdough"},
|
||
{"concept": "Flour", "home": "sourdough"}]
|
||
self.accept(1, doc)
|
||
self.assertEqual([(h["concept_id"], h["home"]) for h in self.acc()["homes"]],
|
||
[("starter", "sourdough"), ("flour", "sourdough")])
|
||
homes = self.log(1)["homes"]
|
||
self.assertEqual(homes["homeless"], ["cold-proofing"])
|
||
self.assertEqual((homes["given"], homes["invalid"], homes["not_needed"], homes["unknown"]), (2, 1, 0, 0))
|
||
|
||
def test_a_home_is_recorded_only_for_a_page_that_has_none(self) -> None:
|
||
self.accept(1, self.pack_1())
|
||
doc = self.pack_2()
|
||
doc["homes"].append({"concept": "Proofing", "home": "oven"}) # a main page needs no home
|
||
doc["fragments"].append(self.frag("Starter", C, "a razor blade on a handle", "Bakers use a razor blade."))
|
||
doc["homes"].append({"concept": "Starter", "home": "oven"}) # starter has its home from pack 1
|
||
self.accept(2, doc)
|
||
self.assertEqual([(h["concept_id"], h["home"]) for h in self.acc()["homes"]],
|
||
[("starter", "sourdough"), ("flour", "sourdough"), ("baking-stone", "oven"), ("steam", "oven")])
|
||
self.assertEqual(self.log(2)["homes"]["not_needed"], 2)
|
||
|
||
def test_a_new_concept_from_a_release_note_or_a_history_page_without_a_home_is_homeless_not_rejected(self) -> None:
|
||
self.accept(1, self.pack_1())
|
||
self.accept(2, self.pack_2())
|
||
doc = self.pack_3()
|
||
doc["fragments"].append(self.frag("Preheating", W, "explains preheating for an hour",
|
||
"The oven guide explains preheating.", new=True))
|
||
doc["homes"].append({"concept": "Preheating", "home": "rye-starter"}) # not a main page: no valid home
|
||
doc["main_pages"] = [{"source": W, "concept": "Oven"}] # a history page has no main page
|
||
out = self.accept(3, doc)
|
||
ids = {r["concept_id"] for r in self.acc()["fragments"]}
|
||
self.assertIn("rye-starter", ids) # new from a release note, with a home on a docs main page
|
||
self.assertIn("bake-timer", ids) # new from a release note, no home: kept, and the guard asks for one
|
||
self.assertIn("preheating", ids) # new from a weekly page, a home that is no main page: the same
|
||
self.assertEqual(self.log(3)["rejected"], {})
|
||
self.assertEqual(self.log(3)["homes"]["homeless"], ["crumb", "bake-timer", "preheating"]) # pack order
|
||
self.assertEqual(self.log(3)["homes"]["invalid"], 1)
|
||
self.assertEqual(self.log(3)["main_pages"], {"named": 1, "valid": 0, "missing": []})
|
||
self.assertIn(("rye-starter", "sourdough", R), [(h["concept_id"], h["home"], h["source"])
|
||
for h in self.acc()["homes"]])
|
||
self.assertNotIn("release-note-new", out)
|
||
weekly = [r for r in self.acc()["fragments"] if r["file"] == W]
|
||
self.assertEqual([r["concept_id"] for r in weekly], ["oven", "crumb", "preheating"]) # existing ones first
|
||
|
||
|
||
class ReplaceHomesTest(BakeryK1):
|
||
def setUp(self) -> None:
|
||
super().setUp()
|
||
doc = self.pack_1()
|
||
doc["homes"].append({"concept": "Cold proofing", "home": "sourdough"}) # B's page, homed on A's main page
|
||
self.accept(1, doc)
|
||
|
||
def replacement(self, doc: dict[str, Any]) -> list[str]:
|
||
path = self.work / "K-fragments" / "replace.json"
|
||
path.write_text(json.dumps(doc), encoding="utf-8")
|
||
return ["--fragments", str(path), *self.common()]
|
||
|
||
def test_replace_swaps_the_main_page_records_of_that_source_and_homes_stay_with_their_pages(self) -> None:
|
||
doc = self.compiled([
|
||
self.frag("Sourdough", A, "rises with wild yeast", "Sourdough rises with wild yeast."),
|
||
self.frag("Starter", A, "ferments for about a week", "A starter ferments for about a week."),
|
||
self.frag("Flour", A, "more lively than white flour", "Rye flour makes a lively starter."),
|
||
], main_pages=[(A, "Sourdough")], homes=[("Starter", "sourdough")])
|
||
code, out = self.kp(["replace", A, *self.replacement(doc)])
|
||
self.assertEqual(code, 0, out)
|
||
acc = self.acc()
|
||
self.assertEqual([(m["source"], m["concept_id"]) for m in acc["main_pages"]],
|
||
[(B, "proofing"), (A, "sourdough")])
|
||
self.assertEqual(sorted((h["source"], h["concept_id"], h["home"], h["pack"]) for h in acc["homes"]),
|
||
[(A, "flour", "sourdough", 1), (A, "starter", "sourdough", 1), (B, "cold-proofing", "sourdough", 1)])
|
||
self.assertNotIn("without a home", out) # flour keeps the home it had: a home belongs to its page
|
||
self.assertIn("kept the home they had: 1", out) # starter's home is the one the replacement chose
|
||
|
||
def test_remove_drops_the_sources_records_and_every_record_that_points_to_a_page_now_gone(self) -> None:
|
||
code, out = self.kp(["remove", A, "--work", str(self.work)])
|
||
self.assertEqual(code, 0, out)
|
||
acc = self.acc()
|
||
self.assertEqual([m["source"] for m in acc["main_pages"]], [B])
|
||
self.assertEqual(acc["homes"], []) # starter and flour are gone; cold proofing's home sourdough is gone
|
||
self.assertIn("dropped 3 home records", out)
|
||
self.assertIn("without a home now: cold-proofing", out) # B's page, homed on A's main page
|
||
self.assertEqual(self.log(1)["removed"], [{"source": A, "fragments": 3, "main_pages": 1, "dropped": 3}])
|
||
|
||
|
||
def test_the_model_s_newest_home_wins_on_replace_and_no_home_it_dropped_comes_back(self) -> None:
|
||
def homes() -> list[tuple[str, str]]:
|
||
return sorted((h["concept_id"], h["home"]) for h in self.acc()["homes"])
|
||
|
||
frags = [self.frag("Sourdough", A, "rises with wild yeast", "Sourdough rises with wild yeast."),
|
||
self.frag("Starter", A, "ferments for about a week", "A starter ferments for about a week."),
|
||
self.frag("Flour", A, "more lively than white flour", "Rye flour makes a lively starter.")]
|
||
doc = self.compiled(frags, main_pages=[(A, "Flour")], homes=[("Sourdough", "flour"), ("Starter", "proofing")])
|
||
code, out = self.kp(["replace", A, *self.replacement(doc)])
|
||
self.assertEqual(code, 0, out)
|
||
# cold proofing's home sourdough is no main page any more: the record goes and the page is homeless
|
||
self.assertEqual(homes(), [("sourdough", "flour"), ("starter", "proofing")])
|
||
doc = self.compiled(frags, main_pages=[(A, "Sourdough")], homes=[("Starter", "proofing"), ("Flour", "proofing")])
|
||
code, out = self.kp(["replace", A, *self.replacement(doc)])
|
||
self.assertEqual(code, 0, out)
|
||
self.assertEqual(homes(), [("flour", "proofing"), ("starter", "proofing")]) # the newest choice, once per page
|
||
self.assertIn("homes: 2 of 2 pages that need one", out)
|
||
|
||
|
||
class HomeFollowsPageTest(BakeryK1):
|
||
"""A home belongs to its page: replacing or removing the source that brought a page that stays (another source
|
||
cites it too) neither takes its home nor changes the main page that lists it."""
|
||
|
||
def setUp(self) -> None:
|
||
super().setUp()
|
||
doc = self.pack_1()
|
||
doc["homes"] = [{"concept": "Starter", "home": "proofing"}, {"concept": "Flour", "home": "sourdough"}]
|
||
self.accept(1, doc)
|
||
doc = self.pack_2()
|
||
doc["fragments"].append(self.frag("Starter", C, "a razor blade on a handle", "Bakers use a razor blade."))
|
||
self.accept(2, doc)
|
||
|
||
def homes(self) -> list[tuple[str, str, str]]:
|
||
return [(h["source"], h["concept_id"], h["home"]) for h in self.acc()["homes"]]
|
||
|
||
def test_removing_the_source_that_brought_a_page_that_stays_keeps_its_home(self) -> None:
|
||
self.assertIn((A, "starter", "proofing"), self.homes())
|
||
code, out = self.kp(["remove", A, "--work", str(self.work)])
|
||
self.assertEqual(code, 0, out)
|
||
self.assertIn("starter", kpack.pages_of(self.acc()))
|
||
self.assertIn((C, "starter", "proofing"), self.homes()) # passed to the source that now brings the page
|
||
self.assertNotIn("flour", [c for _, c, _ in self.homes()]) # flour's page is gone with A
|
||
self.assertIn("passed 1 home record to the source that now brings its page", out)
|
||
self.assertIn("dropped 1 home record", out)
|
||
|
||
def test_replacing_that_source_without_the_page_keeps_its_home(self) -> None:
|
||
doc = self.compiled([
|
||
self.frag("Sourdough", A, "rises with wild yeast", "Sourdough rises with wild yeast."),
|
||
self.frag("Flour", A, "more lively than white flour", "Rye flour makes a lively starter."),
|
||
], main_pages=[(A, "Sourdough")])
|
||
path = self.work / "K-fragments" / "replace.json"
|
||
path.write_text(json.dumps(doc), encoding="utf-8")
|
||
code, out = self.kp(["replace", A, "--fragments", str(path), *self.common()])
|
||
self.assertEqual(code, 0, out)
|
||
self.assertIn((C, "starter", "proofing"), self.homes())
|
||
self.assertIn((A, "flour", "sourdough"), self.homes())
|
||
self.assertNotIn("without a home", out)
|
||
|
||
|
||
class MainNowTest(BakeryK1):
|
||
def test_accept_drops_the_home_of_a_page_the_pack_makes_a_main_page(self) -> None:
|
||
doc = self.pack_1()
|
||
doc["homes"].append({"concept": "Cold proofing", "home": "sourdough"})
|
||
self.accept(1, doc)
|
||
doc = self.pack_2()
|
||
doc["fragments"].append(self.frag("Cold proofing", C, "called a lame", "Cold proofing comes before the lame."))
|
||
doc["main_pages"].append({"source": C, "concept": "Cold proofing"})
|
||
out = self.accept(2, doc)
|
||
self.assertNotIn("cold-proofing", [h["concept_id"] for h in self.acc()["homes"]])
|
||
self.assertEqual(self.log(2)["dropped"], 1)
|
||
self.assertIn("dropped 1 home record", out)
|
||
|
||
|
||
class NamingTest(BakeryK1):
|
||
"""Every page a replace or a remove leaves without a home or a main role is named, not only counted."""
|
||
|
||
def setUp(self) -> None:
|
||
super().setUp()
|
||
self.accept(1, self.pack_1()) # starter and flour homed on sourdough, A's main page
|
||
doc = self.pack_2() # C writes proofing too
|
||
doc["fragments"].append(self.frag("Starter", C, "a razor blade on a handle", "Bakers use a razor blade."))
|
||
self.accept(2, doc)
|
||
|
||
def test_replace_names_a_page_it_wrote_to_before_and_leaves_without_a_home(self) -> None:
|
||
doc = self.compiled([
|
||
self.frag("Sourdough", A, "rises with wild yeast", "Sourdough rises with wild yeast."),
|
||
self.frag("Flour", A, "more lively than white flour", "Rye flour makes a lively starter."),
|
||
], main_pages=[(A, "Flour")])
|
||
path = self.work / "K-fragments" / "replace.json"
|
||
path.write_text(json.dumps(doc), encoding="utf-8")
|
||
code, out = self.kp(["replace", A, "--fragments", str(path), *self.common()])
|
||
self.assertEqual(code, 0, out)
|
||
self.assertIn("starter", kpack.pages_of(self.acc())) # C keeps the page; its home sourdough is no main page
|
||
self.assertIn("without a home now: starter", out)
|
||
|
||
def test_remove_names_a_page_that_stays_and_loses_its_main_role(self) -> None:
|
||
code, out = self.kp(["remove", B, "--work", str(self.work)])
|
||
self.assertEqual(code, 0, out)
|
||
self.assertIn("proofing", kpack.pages_of(self.acc())) # C keeps the page; B named it its main page
|
||
self.assertIn("without a home now: proofing", out)
|
||
|
||
|
||
class HomeSourceTest(BakeryK1):
|
||
"""A home record names the source that brings its page (the page's first fragment in build order), also when a
|
||
later pack gives the home, so a replace or remove of another source passes nothing."""
|
||
|
||
def test_a_home_given_in_a_later_pack_names_the_page_s_first_source(self) -> None:
|
||
self.accept(1, self.pack_1()) # cold proofing, brought by B, is homeless
|
||
doc = self.pack_2()
|
||
doc["fragments"].append(self.frag("Cold proofing", D, "An open crumb has large", "Cold proofing opens the crumb."))
|
||
doc["homes"].append({"concept": "Cold proofing", "home": "oven"})
|
||
self.accept(2, doc)
|
||
self.assertIn((B, "cold-proofing", "oven"), [(h["source"], h["concept_id"], h["home"]) for h in self.acc()["homes"]])
|
||
code, out = self.kp(["remove", C, "--work", str(self.work)])
|
||
self.assertEqual(code, 0, out)
|
||
self.assertNotIn("passed", out)
|
||
|
||
|
||
class DropDanglingTest(unittest.TestCase):
|
||
"""The validity rule for main page and home records, applied after every write: one valid home per page."""
|
||
|
||
def test_every_home_that_is_no_longer_valid_goes_and_the_newest_of_a_page_stays(self) -> None:
|
||
def frag(cid: str) -> dict[str, Any]:
|
||
return {"concept_id": cid, "pack": 1, "position": 1, "start": 1, "file": "a.md"}
|
||
|
||
def home(c: str, h: str) -> dict[str, Any]:
|
||
return {"source": "a.md", "concept_id": c, "home": h, "pack": 1, "by": "compiler"}
|
||
|
||
acc: dict[str, Any] = {
|
||
"fragments": [frag(c) for c in ("m1", "m2", "p", "q", "r", "s")],
|
||
"main_pages": [{"source": "a.md", "concept_id": "m1"}, {"source": "b.md", "concept_id": "m2"},
|
||
{"source": "c.md", "concept_id": "gone"}],
|
||
"homes": [home("p", "m1"), home("p", "m2"), # two for p: the newest stays
|
||
home("q", "x"), # its home is gone
|
||
home("r", "q"), # its home is no main page
|
||
home("s", "s"), # itself
|
||
home("m2", "m1"), # its page is a main page now
|
||
home("gone", "m1")]} # its page is gone
|
||
dropped = kpack.drop_dangling(acc)
|
||
self.assertEqual([(h["concept_id"], h["home"]) for h in acc["homes"]], [("p", "m2")])
|
||
self.assertEqual(dropped, (1, 6, ["q", "r", "s"])) # the pages left without a home are named
|
||
|
||
|
||
class MainCatalogueTest(unittest.TestCase):
|
||
def test_main_pages_rank_recent_packs_first_then_the_most_cited_and_stop_at_the_budget(self) -> None:
|
||
index = {"entries": [{"id": i, "title": i.title(), "aliases": [i.title()]}
|
||
for i in ("alpha", "beta", "gamma", "delta", "omega")]}
|
||
|
||
def frag(cid: str, pack: int, file: str) -> dict[str, Any]:
|
||
return {"concept_id": cid, "pack": pack, "file": file}
|
||
|
||
acc = {"fragments": [frag("alpha", 1, "a.md"), frag("alpha", 2, "b.md"), frag("alpha", 3, "c.md"),
|
||
frag("beta", 1, "a.md"), frag("gamma", 5, "e.md"), frag("delta", 6, "f.md"),
|
||
frag("omega", 7, "g.md")],
|
||
"main_pages": [{"source": f, "concept_id": c, "pack": p, "by": "compiler"}
|
||
for c, p, f in (("alpha", 1, "a.md"), ("beta", 1, "a.md"), ("gamma", 5, "e.md"),
|
||
("delta", 6, "f.md"))],
|
||
"homes": []}
|
||
got = [e["id"] for e in kpack.main_catalogue(index, acc, 7, budget=10_000)]
|
||
self.assertEqual(got, ["delta", "gamma", "alpha", "beta"]) # packs 6 and 5 are recent; alpha cites 3 files
|
||
self.assertNotIn("omega", got) # a page, not a main page
|
||
budget = len("- delta | Delta\n") + len("- gamma | Gamma\n")
|
||
self.assertEqual([e["id"] for e in kpack.main_catalogue(index, acc, 7, budget=budget)], ["delta", "gamma"])
|
||
|
||
|
||
# ---------------------------------------------------------------------------------------------- the chain
|
||
|
||
|
||
class Chain(BakeryK1):
|
||
"""run-before and run-after with the auditor stood in for by k2_audit; every call's root is kept."""
|
||
|
||
def setUp(self) -> None:
|
||
super().setUp()
|
||
self.wiki = self.tmp / "wiki"
|
||
self.kost = self.work / "kost.json"
|
||
self.transcripts = self.tmp / "transcripts"
|
||
(self.transcripts / "s1" / "subagents").mkdir(parents=True)
|
||
(self.transcripts / "s1.jsonl").write_text(json.dumps({"type": "assistant", "message": {
|
||
"id": "m1", "stop_reason": "end_turn", "usage": {"input_tokens": 10, "output_tokens": 5}}}) + "\n",
|
||
encoding="utf-8")
|
||
self.okter = self.tmp / "okter.txt"
|
||
self.okter.write_text("s1 build\n", encoding="utf-8")
|
||
self.total = len(json.loads(self.blocks.read_text(encoding="utf-8"))["blocks"])
|
||
self.cap = 10**9
|
||
self.minute = 0
|
||
self.roots: list[list[str]] = []
|
||
self.audits: list[dict[str, Any]] = []
|
||
patcher = mock.patch.object(kpack, "run_audit", side_effect=self.stub_audit)
|
||
patcher.start()
|
||
self.addCleanup(patcher.stop)
|
||
|
||
def stub_audit(self, bundle: Path, root: Path) -> dict[str, Any]:
|
||
self.roots.append(sorted(p.name for p in (root / "kilde").iterdir()))
|
||
doc = k2_audit(bundle, root)
|
||
self.audits.append(doc)
|
||
return doc
|
||
|
||
def after_args(self) -> list[str]:
|
||
self.minute += 1
|
||
links = self.work / "links.json"
|
||
return [*self.common(), "--out", str(self.wiki), *(["--links", str(links)] if links.is_file() else []),
|
||
"--at", f"2026-10-09T10:{self.minute:02d}:00Z", "--transcripts", str(self.transcripts),
|
||
"--okter", str(self.okter), "--kost", str(self.kost), "--cap", str(self.cap), "--total",
|
||
str(self.total)]
|
||
|
||
def run_after(self, n: int) -> tuple[int, str]:
|
||
return self.kp(["run-after", str(n), *self.after_args()])
|
||
|
||
|
||
class ChainTest(Chain):
|
||
def test_run_before_renders_the_parts_of_an_interval_and_names_each_output(self) -> None:
|
||
code, out = self.kp(["run-before", "1", "--to", "2", *self.common()])
|
||
self.assertEqual(code, 0, out)
|
||
for n in (1, 2):
|
||
self.assertIn(f"output: {self.work / 'K-fragments' / f'pack-{n}.json'}", out)
|
||
self.assertTrue((self.work / "K-oppgaver" / f"pack-{n}-part-1.md").is_file())
|
||
|
||
def test_a_fragment_rejected_at_writing_is_dropped_and_counted_without_running_the_pack_again(self) -> None:
|
||
# the hard check stops the sentence, not the pack (order 28, B2: pack 26 ran again, 4.7 M, for 1 of 574)
|
||
self.kp(["run-before", "2", *self.common()])
|
||
self.accept(1, self.pack_1())
|
||
self.write(2, self.pack_2(bad=True))
|
||
code, out = self.run_after(2)
|
||
self.assertNotIn(code, (kpack.EXIT_RETRY, kpack.EXIT_REJECTED_TWICE), out)
|
||
self.assertIn("rejected quote-not-in-span: 1", out)
|
||
self.assertEqual(self.log(2)["rejected"], {"quote-not-in-span": 1})
|
||
self.assertNotIn("attempts", self.log(2))
|
||
self.assertFalse((self.work / "K-fragments" / "pack-2.attempt-1.json").exists())
|
||
self.assertEqual(self.log(2)["proposed"], 5)
|
||
self.assertEqual(self.log(2)["accepted"], self.log(2)["proposed"] - 1)
|
||
|
||
def test_run_after_needs_k1_at_the_sources_root(self) -> None:
|
||
plain = self.tmp / "plain"
|
||
(plain / "kilde").mkdir(parents=True)
|
||
for name, text in BREAD.items():
|
||
(plain / "kilde" / name).write_text(text, encoding="utf-8")
|
||
self.write(1, self.pack_1())
|
||
args = self.after_args()
|
||
args[args.index("--sources") + 1] = str(plain)
|
||
code, out = self.kp(["run-after", "1", *args])
|
||
self.assertEqual(code, 2, out)
|
||
self.assertIn("source-manifest.json", out)
|
||
|
||
def test_a_finding_the_guard_cannot_mend_stops_without_asking_it(self) -> None:
|
||
self.write(1, self.pack_1())
|
||
broken = {"check": "broken-link", "group": "links", "file": "sourdough.md", "line": 9, "message": "x"}
|
||
with mock.patch.object(kpack, "run_audit", side_effect=lambda b, r: {**k2_audit(b, r), "findings": [broken]}):
|
||
code, out = self.run_after(1)
|
||
self.assertEqual(code, kpack.EXIT_BREACH, out)
|
||
self.assertIn("broken-link sourdough.md", out)
|
||
self.assertFalse((self.work / "K-guard").exists())
|
||
|
||
def test_the_stop_listing_names_the_source_of_a_main_page_missing_finding_from_its_message(self) -> None:
|
||
self.write(1, self.pack_1())
|
||
found = [{"check": "broken-link", "group": "links", "file": "sourdough.md", "line": 9, "message": "x"},
|
||
{"check": "main-page-missing", "group": "links", "file": "", "line": None,
|
||
"message": f"main-topic source /kilde/{B} has no main page"}]
|
||
with mock.patch.object(kpack, "run_audit", side_effect=lambda b, r: {**k2_audit(b, r), "findings": found}):
|
||
code, out = self.run_after(1)
|
||
self.assertEqual(code, kpack.EXIT_BREACH, out)
|
||
self.assertIn(f"main-page-missing kilde/{B}", out)
|
||
|
||
def test_a_cost_that_cannot_be_measured_stops_with_its_own_reason(self) -> None:
|
||
self.write(1, self.pack_1())
|
||
self.assertEqual(self.run_after(1)[0], kpack.EXIT_GUARD)
|
||
(self.work / "K-guard" / "p1" / "answer.json").write_text(json.dumps(
|
||
{"homes": [{"page": "cold-proofing", "home": "proofing"}], "none": [], "main_pages": []}), encoding="utf-8")
|
||
real = cost.main
|
||
with mock.patch.object(cost, "main", side_effect=lambda argv: 1 if argv[0] == "harvest" else real(argv)):
|
||
code, out = self.run_after(1)
|
||
self.assertEqual(code, kpack.EXIT_STOP, out)
|
||
self.assertIn("STOP: the cost could not be measured", out)
|
||
self.assertNotIn("over the cost stop", out)
|
||
|
||
def test_run_after_gives_the_stop_line_the_order_s_command(self) -> None:
|
||
self.write(1, self.pack_1())
|
||
self.assertEqual(self.run_after(1)[0], kpack.EXIT_GUARD)
|
||
(self.work / "K-guard" / "p1" / "answer.json").write_text(json.dumps(
|
||
{"homes": [{"page": "cold-proofing", "home": "proofing"}], "none": [], "main_pages": []}), encoding="utf-8")
|
||
with mock.patch.object(cost, "main", wraps=cost.main) as calls:
|
||
code, out = self.run_after(1)
|
||
self.assertEqual(code, 0, out)
|
||
stop = [c.args[0] for c in calls.call_args_list if c.args[0][0] == "stop"]
|
||
done = self.log(1)["blocks"]
|
||
doc = json.loads(self.blocks.read_text(encoding="utf-8"))
|
||
names = kpack.packs([str(x) for x in doc["order"]], int(json.loads(
|
||
(self.work / "K-work" / "packs.json").read_text(encoding="utf-8"))["size"]))[1]
|
||
following = sum(b["file"] in names for b in doc["blocks"]) # pack 2's blocks: what the next pack is expected to cost
|
||
chars = sum(b["chars"] for b in doc["blocks"] if b["file"] in names) # and their characters (motor B17)
|
||
self.assertEqual(stop, [["stop", "--lines", "build,link,orchestration-build", "--cap", str(self.cap), "--unit",
|
||
"blocks", "--done", str(done), "--total", str(self.total), "--kost", str(self.kost),
|
||
"--blocks", str(self.work / "K-work" / "packs.json"), "--next", str(following),
|
||
"--next-chars", str(chars)]])
|
||
first = set(self.log(1)["sources"])
|
||
self.assertEqual(self.log(1)["block_chars"], sum(b["chars"] for b in doc["blocks"] if b["file"] in first))
|
||
self.assertLess(done, self.total)
|
||
self.assertGreater(following, 0)
|
||
|
||
def test_the_stop_line_stops_on_measured_cost_not_on_the_projection(self) -> None:
|
||
self.write(1, self.pack_1())
|
||
self.assertEqual(self.run_after(1)[0], kpack.EXIT_GUARD)
|
||
(self.work / "K-guard" / "p1" / "answer.json").write_text(json.dumps(
|
||
{"homes": [{"page": "cold-proofing", "home": "proofing"}], "none": [], "main_pages": []}), encoding="utf-8")
|
||
self.assertEqual(self.run_after(1)[0], 0)
|
||
row = json.loads(self.kost.read_text(encoding="utf-8"))["stops"]["build,link,orchestration-build"]
|
||
self.assertGreater(row["projected_with_margin"], row["measured_next_with_margin"])
|
||
self.cap = row["measured_next_with_margin"] # under the projection: the operator 12.10.2026, it never stops
|
||
code, out = self.run_after(1)
|
||
self.assertEqual(code, 0, out)
|
||
self.cap -= 1
|
||
self.assertEqual(self.run_after(1)[0], 1)
|
||
|
||
def test_the_auditor_sees_only_the_sources_of_accepted_packs_up_to_the_last(self) -> None:
|
||
log = {"packs": [{"pack": 1, "accepted": 5, "sources": [A, B]}, {"pack": 2, "sources": [C, D]},
|
||
{"pack": 3, "accepted": 5, "sources": [W, R]}]}
|
||
root = kpack.audit_root(self.root, self.work, log, 2)
|
||
self.assertEqual(sorted(p.name for p in (root / "kilde").iterdir()), [A, B])
|
||
root = kpack.audit_root(self.root, self.work, log, 3)
|
||
self.assertEqual(sorted(p.name for p in (root / "kilde").iterdir()), sorted([A, B, W, R]))
|
||
|
||
def test_the_guard_naming_a_homed_page_as_main_page_drops_its_home_and_logs_it(self) -> None:
|
||
self.kp(["run-before", "2", *self.common()])
|
||
self.write(1, self.pack_1())
|
||
self.assertEqual(self.run_after(1)[0], kpack.EXIT_GUARD)
|
||
(self.work / "K-guard" / "p1" / "answer.json").write_text(json.dumps(
|
||
{"homes": [{"page": "cold-proofing", "home": "proofing"}], "none": [], "main_pages": []}), encoding="utf-8")
|
||
self.assertEqual(self.run_after(1)[0], 0)
|
||
doc = self.pack_2()
|
||
doc["homes"].append({"concept": "Scoring", "home": "oven"}) # C names no main page; scoring is homed
|
||
self.write(2, doc)
|
||
code, out = self.run_after(2)
|
||
self.assertEqual(code, kpack.EXIT_GUARD, out)
|
||
self.assertIn("scoring", [h["concept_id"] for h in self.acc()["homes"]])
|
||
(self.work / "K-guard" / "p2" / "answer.json").write_text(json.dumps(
|
||
{"homes": [], "none": [], "main_pages": [{"source": f"kilde/{C}", "page": "scoring"}]}), encoding="utf-8")
|
||
code, out = self.run_after(2)
|
||
self.assertEqual(code, 0, out)
|
||
self.assertNotIn("scoring", [h["concept_id"] for h in self.acc()["homes"]])
|
||
self.assertIn("dropped 1 home record", out)
|
||
self.assertEqual(self.log(2)["guard"]["answered"]["dropped"], 1)
|
||
|
||
def test_a_stop_line_that_cannot_run_says_so(self) -> None:
|
||
self.write(1, self.pack_1())
|
||
self.assertEqual(self.run_after(1)[0], kpack.EXIT_GUARD)
|
||
(self.work / "K-guard" / "p1" / "answer.json").write_text(json.dumps(
|
||
{"homes": [{"page": "cold-proofing", "home": "proofing"}], "none": [], "main_pages": []}), encoding="utf-8")
|
||
real = cost.main
|
||
with mock.patch.object(cost, "main", side_effect=lambda argv: 2 if argv[0] == "stop" else real(argv)):
|
||
code, out = self.run_after(1)
|
||
self.assertEqual(code, 2, out)
|
||
self.assertIn("STOP: the stop line could not run", out)
|
||
self.assertNotIn("over the cost stop", out)
|
||
|
||
def test_the_stop_line_stops_the_chain(self) -> None:
|
||
self.write(1, self.pack_1())
|
||
code, out = self.run_after(1)
|
||
self.assertEqual(code, kpack.EXIT_GUARD, out)
|
||
(self.work / "K-guard" / "p1" / "answer.json").write_text(json.dumps(
|
||
{"homes": [{"page": "cold-proofing", "home": "proofing"}], "none": [], "main_pages": []}), encoding="utf-8")
|
||
self.cap = 10
|
||
code, out = self.run_after(1)
|
||
self.assertEqual(code, 1, out)
|
||
self.assertIn("STOP", out)
|
||
self.assertIn("cost stop: exit 1", out)
|
||
|
||
|
||
class OutputsTest(Chain):
|
||
"""Order 22 A2: one answer of the compiler holds only so much. In round 1 the compiler for pack 1 (seven parts)
|
||
hit the limit on one answer's output, was told by Claude Code to break the work into smaller pieces, and wrote one
|
||
file with 84 fragments where wave 3 wrote 447 (measured with code, no text read). A pack of more than two task
|
||
parts is written as several outputs, one per turn: the first holds part 1 alone, because the first answer also
|
||
holds the thinking (610 s before the cut in round 1, 49 s after it), and every later one two parts; run-after
|
||
reads them all, and an output left unwritten runs the pack once more, as a rejection at writing does."""
|
||
|
||
def setUp(self) -> None:
|
||
super().setUp()
|
||
patcher = mock.patch.object(kpack, "PART_CHARS", 600) # pack 1 of the bread fixture in three parts
|
||
patcher.start()
|
||
self.addCleanup(patcher.stop)
|
||
|
||
def outputs(self, out: str) -> list[Path]:
|
||
return [Path(m.group(1)) for m in re.finditer(r"^output: (\S+)", out, re.M)]
|
||
|
||
def spread(self, doc: dict[str, Any], k: int) -> list[dict[str, Any]]:
|
||
"""The compiler's output over k files: the fragments in turn, the main pages in the first, the homes in the
|
||
last."""
|
||
files: list[dict[str, Any]] = [{"fragments": doc["fragments"][j::k]} for j in range(k)]
|
||
files[0]["main_pages"] = doc["main_pages"]
|
||
files[-1]["homes"] = doc["homes"]
|
||
return files
|
||
|
||
def test_a_pack_of_more_than_two_parts_names_part_1_alone_then_one_output_for_every_two(self) -> None:
|
||
code, out = self.kp(["run-before", "1", *self.common()])
|
||
self.assertEqual(code, 0, out)
|
||
parts = re.findall(r"^part: (\S+)", out, re.M)
|
||
self.assertEqual(len(parts), 3, out)
|
||
self.assertEqual(self.outputs(out), [self.work / "K-fragments" / "pack-1-out-1.json",
|
||
self.work / "K-fragments" / "pack-1-out-2.json"])
|
||
self.assertIn(f"output: {self.work / 'K-fragments' / 'pack-1-out-1.json'} (part 1)", out)
|
||
self.assertIn(f"output: {self.work / 'K-fragments' / 'pack-1-out-2.json'} (parts 2-3)", out)
|
||
self.assertEqual(self.log(1).get("outputs"), 2)
|
||
|
||
def test_the_outputs_of_seven_parts_and_of_four(self) -> None:
|
||
# round 1's pack 1 has seven parts: 1, 2-3, 4-5, 6-7; a pack of four: 1, 2-3, 4
|
||
self.assertEqual([label for _, label in kpack.output_paths(self.work, 1, 7)],
|
||
["part 1", "parts 2-3", "parts 4-5", "parts 6-7"])
|
||
self.assertEqual([label for _, label in kpack.output_paths(self.work, 1, 4)], ["part 1", "parts 2-3", "part 4"])
|
||
|
||
def test_a_pack_of_two_parts_or_fewer_keeps_its_one_output(self) -> None:
|
||
with mock.patch.object(kpack, "PART_CHARS", 60_000):
|
||
code, out = self.kp(["run-before", "1", *self.common()])
|
||
self.assertEqual(code, 0, out)
|
||
self.assertEqual(self.outputs(out), [self.work / "K-fragments" / "pack-1.json"])
|
||
self.assertNotIn("(part", out)
|
||
self.assertEqual(self.log(1).get("outputs"), 1)
|
||
|
||
def test_run_after_reads_every_output_of_a_pack(self) -> None:
|
||
code, out = self.kp(["run-before", "1", *self.common()])
|
||
outs = self.outputs(out)
|
||
self.assertEqual(len(outs), 2, out)
|
||
for path, doc in zip(outs, self.spread(self.pack_1(), len(outs))):
|
||
path.write_text(json.dumps(doc), encoding="utf-8")
|
||
code, out = self.run_after(1)
|
||
self.assertEqual(code, kpack.EXIT_GUARD, out) # cold proofing has no home, as with one output
|
||
self.assertIn("pack 1: accepted 5 of 5", out)
|
||
self.assertEqual(self.log(1)["main_pages"]["valid"], 2)
|
||
self.assertEqual(self.log(1)["homes"]["given"], 2)
|
||
|
||
def test_a_set_aside_that_stops_halfway_leaves_its_marker_with_the_move_to_undo(self) -> None:
|
||
code, out = self.kp(["run-before", "1", *self.common()])
|
||
outs = self.outputs(out)
|
||
self.assertEqual(len(outs), 2, out)
|
||
outs[0].write_text(json.dumps(self.spread(self.pack_1(), 2)[0]), encoding="utf-8") # the second never written
|
||
real, calls = os.replace, []
|
||
|
||
def flaky(a: Any, b: Any) -> None: # the set-aside moves, then the log's write fails
|
||
calls.append(a)
|
||
if len(calls) == 2:
|
||
raise OSError("disk full")
|
||
real(a, b)
|
||
|
||
with mock.patch.object(os, "replace", side_effect=flaky):
|
||
self.assertEqual(self.run_after(1)[0], 2)
|
||
code, out = self.run_after(1)
|
||
self.assertEqual(code, 2, out) # the marker stops every command and says what to undo
|
||
self.assertIn(f"move {self.work / 'K-fragments' / 'pack-1-out-1.attempt-1.json'} back to "
|
||
f"{self.work / 'K-fragments' / 'pack-1-out-1.json'}", out)
|
||
self.assertNotIn("attempts", self.log(1))
|
||
|
||
def test_an_output_left_unwritten_runs_the_pack_once_more_and_then_stops(self) -> None:
|
||
code, out = self.kp(["run-before", "1", *self.common()])
|
||
outs = self.outputs(out)
|
||
self.assertEqual(len(outs), 2, out)
|
||
first = self.spread(self.pack_1(), 2)[0]
|
||
outs[0].write_text(json.dumps(first), encoding="utf-8") # the second turn's output never written
|
||
code, out = self.run_after(1)
|
||
self.assertEqual(code, kpack.EXIT_RETRY, out)
|
||
self.assertIn("output-missing 1", out)
|
||
self.assertTrue((self.work / "K-fragments" / "pack-1-out-1.attempt-1.json").is_file())
|
||
self.assertFalse(outs[0].exists())
|
||
self.assertNotIn("accepted", self.log(1))
|
||
outs[0].write_text(json.dumps(first), encoding="utf-8")
|
||
code, out = self.run_after(1)
|
||
self.assertEqual(code, kpack.EXIT_REJECTED_TWICE, out)
|
||
self.assertIn("STOP: pack 1 left an output unwritten twice", out) # the cause the operator decides on
|
||
self.assertEqual(len(self.log(1)["attempts"]), 2)
|
||
self.assertFalse((self.work / "K-work" / "accepted.json").exists()) # no pack was accepted
|
||
|
||
def test_accept_reads_every_output_of_a_pack(self) -> None:
|
||
code, out = self.kp(["run-before", "1", *self.common()])
|
||
outs = self.outputs(out)
|
||
self.assertEqual(len(outs), 2, out)
|
||
for path, doc in zip(outs, self.spread(self.pack_1(), len(outs))):
|
||
path.write_text(json.dumps(doc), encoding="utf-8")
|
||
code, out = self.kp(["accept", "1", *self.common()])
|
||
self.assertEqual(code, 0, out)
|
||
self.assertIn("pack 1: accepted 5 of 5", out)
|
||
|
||
|
||
def kp_exit(args: list[str]) -> tuple[int, str]:
|
||
"""kpack's exit and output, with an argument its parser refuses read as exit 2 instead of raised."""
|
||
try:
|
||
return quiet(kpack.main, args)
|
||
except SystemExit as exc:
|
||
return int(exc.code or 0), ""
|
||
|
||
|
||
class FloorTest(Chain):
|
||
"""Order 22 A2: the floor after every pack. Condition (b) of the wave's goal (operator 11.10.2026): every source
|
||
cites at least as many source blocks as it did in wave 3. In round 1 it was found only after pack 1 had cost 3.6 M
|
||
of 16 M; checked after every pack, a source below its floor stops the build before the next pack (exit 7)."""
|
||
|
||
def cited(self) -> dict[str, int]:
|
||
"""The distinct blocks that hold an accepted fragment's anchor, per source, counted here from the fixture."""
|
||
doc = json.loads(self.blocks.read_text(encoding="utf-8"))
|
||
seen: dict[str, set[tuple[int, int]]] = {}
|
||
for r in self.acc()["fragments"]:
|
||
b = next(b for b in doc["blocks"] if b["file"] == r["file"] and b["start"] <= r["start"] <= b["end"])
|
||
seen.setdefault(r["file"], set()).add((b["start"], b["end"]))
|
||
return {f: len(s) for f, s in seen.items()}
|
||
|
||
def guarded(self) -> None:
|
||
"""Pack 1 written and run up to its guard, and the guard's answer written: the next run-after ends the pack."""
|
||
self.write(1, self.pack_1())
|
||
self.assertEqual(self.run_after(1)[0], kpack.EXIT_GUARD)
|
||
(self.work / "K-guard" / "p1" / "answer.json").write_text(json.dumps(
|
||
{"homes": [{"page": "cold-proofing", "home": "proofing"}], "none": [], "main_pages": []}), encoding="utf-8")
|
||
|
||
def floor(self, counts: dict[str, int]) -> Path:
|
||
path = self.tmp / "floor.json"
|
||
path.write_text(json.dumps({"schema": "okf-wiki/cited-blocks/1", "sources": counts}), encoding="utf-8")
|
||
return path
|
||
|
||
def test_cited_counts_the_blocks_each_source_cites(self) -> None:
|
||
self.accept(1, self.pack_1())
|
||
out_path = self.tmp / "cited.json"
|
||
code, out = kp_exit(["cited", "--work", str(self.work), "--blocks", str(self.blocks), "--out", str(out_path)])
|
||
self.assertEqual(code, 0, out)
|
||
data = json.loads(out_path.read_text(encoding="utf-8"))
|
||
self.assertEqual(data["schema"], "okf-wiki/cited-blocks/1")
|
||
self.assertEqual(data["sources"], self.cited())
|
||
self.assertEqual(sorted(data["sources"]), [A, B])
|
||
|
||
def test_a_source_below_its_floor_stops_the_build_after_its_pack(self) -> None:
|
||
self.guarded()
|
||
floor = self.floor({A: 99, B: 0})
|
||
code, out = kp_exit(["run-after", "1", *self.after_args(), "--floor", str(floor)])
|
||
self.assertEqual(code, 7, out)
|
||
self.assertIn("STOP", out)
|
||
self.assertIn(f"floor: {A} cites {self.cited()[A]} anchor blocks of 99", out)
|
||
self.assertIn("cost stop: exit 0", out) # the cost is measured first, as after every pack
|
||
self.assertEqual(self.log(1)["cited"], self.cited())
|
||
|
||
def test_a_source_at_its_floor_goes_on(self) -> None:
|
||
self.guarded()
|
||
code, out = kp_exit(["run-after", "1", *self.after_args(), "--floor", str(self.tmp / "none.json")])
|
||
self.assertEqual(code, 2, out) # a floor file that cannot be read cannot run
|
||
acc_before = self.cited()
|
||
floor = self.floor(acc_before)
|
||
code, out = kp_exit(["run-after", "1", *self.after_args(), "--floor", str(floor)])
|
||
self.assertEqual(code, 0, out)
|
||
self.assertIn("floor: 2 of 2 sources at or over", out)
|
||
|
||
def test_the_floor_counts_what_is_accepted_now_after_a_replace(self) -> None:
|
||
self.guarded()
|
||
floor = self.floor(self.cited())
|
||
code, out = kp_exit(["run-after", "1", *self.after_args(), "--floor", str(floor)])
|
||
self.assertEqual(code, 0, out)
|
||
before = self.cited()[A]
|
||
path = self.work / "K-fragments" / "replace.json" # A again, without the flour fragment and its block
|
||
path.write_text(json.dumps(self.compiled(self.pack_1()["fragments"][:2], main_pages=[(A, "Sourdough")],
|
||
homes=[("Starter", "sourdough")])), encoding="utf-8")
|
||
code, out = self.kp(["replace", A, "--fragments", str(path), *self.common()])
|
||
self.assertEqual(code, 0, out)
|
||
self.assertLess(self.cited()[A], before)
|
||
code, out = kp_exit(["run-after", "1", *self.after_args(), "--floor", str(floor)])
|
||
self.assertEqual(code, kpack.EXIT_THIN, out)
|
||
self.assertIn(f"floor: {A} cites {self.cited()[A]} anchor blocks of {before}", out)
|
||
|
||
def test_a_source_the_floor_does_not_name_is_not_held_to_one(self) -> None:
|
||
self.guarded()
|
||
code, out = kp_exit(["run-after", "1", *self.after_args(), "--floor", str(self.floor({B: 1}))])
|
||
self.assertEqual(code, 0, out)
|
||
self.assertIn("floor: 1 of 1 sources at or over", out)
|
||
|
||
|
||
STEPS_THEN = "# Garden steps\n\nIntro sentence one.\n\n## Planting\n\nDig a hole.\n\nWater the seedling.\n\n" \
|
||
"Mulch the bed.\n\nStake the plant.\n"
|
||
STEPS_NOW = "# Garden steps\n\nIntro sentence one.\n\n## Planting\n\n**1. Dig**\n\nDig a hole.\n\n**2. Water**\n\n" \
|
||
"Water the seedling.\n\n***\n\nMulch the bed.\n\n**Note:** stake the plant when it is tall.\n"
|
||
PLAIN_THEN = "# Beds\n\n" + "".join(f"Bed fact {i}.\n\n" for i in range(4))
|
||
PLAIN_NOW = "# Beds\n\n" + "".join(f"Bed fact {i}.\n\n" for i in range(7)) + "```text\n***\n```\n"
|
||
|
||
|
||
class FloorFromEarlierBuildTest(unittest.TestCase):
|
||
"""c1, pack 16 (09.11.2026): the floor for a new fetch scaled each source's cited blocks by all its blocks now,
|
||
so a page that split its step headings into blocks of their own (common-workflows: 10 such blocks before, 46 now)
|
||
got a floor its unchanged prose could not reach (38 of 40). A thematic break or a paragraph that is only bold text
|
||
carries no claim (wiki-38: 0 of 433 and 0 of 23 cited; c1 packs 1-16: 0 of 261 and 0 of 10), so `kpack floor`
|
||
scales by the blocks that can carry one."""
|
||
|
||
def setUp(self) -> None:
|
||
self._tmp = tempfile.TemporaryDirectory()
|
||
self.tmp = Path(self._tmp.name)
|
||
self.cited = self.tmp / "cited.json"
|
||
self.cited.write_text(json.dumps({"schema": "okf-wiki/cited-blocks/1", "sources": {
|
||
"docs-steps.md": 4, "docs-plain.md": 4, "release-v1.0.0.md": 3, "docs-gone.md": 2}}), encoding="utf-8")
|
||
self.then = self.root("then", {"docs-steps.md": STEPS_THEN, "docs-plain.md": PLAIN_THEN, "docs-gone.md": "# Gone\n\nA.\n",
|
||
"release-v1.0.0.md": "# 1.0.0\n\n* Added beds\n"})
|
||
self.now = self.root("now", {"docs-steps.md": STEPS_NOW, "docs-plain.md": PLAIN_NOW,
|
||
"release-v1.0.0.md": "# 1.0.0\n\n* Added beds\n"})
|
||
self.out = self.tmp / "floor.json"
|
||
|
||
def tearDown(self) -> None:
|
||
self._tmp.cleanup()
|
||
|
||
def root(self, name: str, files: dict[str, str]) -> Path:
|
||
root = self.tmp / name
|
||
(root / "kilde").mkdir(parents=True)
|
||
for f, text in files.items():
|
||
(root / "kilde" / f).write_text(text, encoding="utf-8")
|
||
doc = kblocks.blocks_document(root / "kilde", sorted(files))
|
||
(root / "blocks.json").write_text(json.dumps(doc), encoding="utf-8")
|
||
return root
|
||
|
||
def run_floor(self, *extra: str) -> tuple[int, str]:
|
||
return kp_exit(["floor", "--cited", str(self.cited), "--blocks-then", str(self.then / "blocks.json"),
|
||
"--sources-then", str(self.then), "--blocks", str(self.now / "blocks.json"),
|
||
"--sources", str(self.now), "--out", str(self.out), *extra])
|
||
|
||
def test_blocks_that_carry_no_claim_do_not_raise_the_floor(self) -> None:
|
||
code, out = self.run_floor()
|
||
self.assertEqual(code, 0, out)
|
||
floor = kpack.read_floor(self.out)
|
||
# docs-steps: 5 blocks that can carry a claim then and now; the 2 step headings and the break are left out
|
||
# (all 8 blocks would give int(4 / 5 x 8 / 2) = 3). docs-plain: 4 then, 8 now (the code block counts) -> 4.
|
||
self.assertEqual(floor, {"docs-steps.md": 2, "docs-plain.md": 4})
|
||
self.assertIsNone(kpack.read_history_rate(self.out))
|
||
|
||
def test_a_history_source_and_a_source_gone_get_no_floor_and_the_rate_is_kept(self) -> None:
|
||
code, out = self.run_floor("--history-rate", "0.15")
|
||
self.assertEqual(code, 0, out)
|
||
self.assertNotIn("release-v1.0.0.md", kpack.read_floor(self.out))
|
||
self.assertEqual(kpack.read_history_rate(self.out), 0.15)
|
||
self.assertIn("docs-gone.md", out) # named: it has no blocks now
|
||
self.assertIn("left out: then 0, now 3", out)
|
||
|
||
def test_an_existing_floor_file_is_never_overwritten(self) -> None:
|
||
self.out.write_text("{}", encoding="utf-8")
|
||
code, out = self.run_floor()
|
||
self.assertEqual(code, 2, out)
|
||
self.assertEqual(self.out.read_text(encoding="utf-8"), "{}")
|
||
|
||
|
||
class ThreePacks(Chain):
|
||
"""Three packs through the chain as the skill tells the orchestrator, no model: the compiler, the guard and the
|
||
linker are the test. Pack 1: a page without a home that the guard homes. Pack 2: one fragment rejected at writing
|
||
(one more run), a docs source without a named main page that the guard names. Pack 3 (history: a weekly page and
|
||
a release note): a new concept from the release note with a home, one without (homeless: the guard homes it), and
|
||
a page the guard answers "none" for, which stops the run with the page listed."""
|
||
|
||
GUARD = {1: {"homes": [{"page": "cold-proofing", "home": "proofing"}], "none": [], "main_pages": []},
|
||
2: {"homes": [], "none": [], "main_pages": [{"source": f"kilde/{C}", "page": "scoring"}]},
|
||
3: {"homes": [{"page": "bake-timer", "home": "oven"}], "none": ["crumb"], "main_pages": []}}
|
||
|
||
def written_path(self, out: str, marker: str) -> Path:
|
||
m = re.search(rf"{marker}(\S+)", out)
|
||
assert m is not None, out
|
||
return Path(m.group(1))
|
||
|
||
def run_pack(self, n: int, attempts: list[dict[str, Any]]) -> tuple[int, dict[str, int], str]:
|
||
counts = {"commands": 0, "model_steps": 0}
|
||
code, out = self.kp(["run-before", str(n), *self.common()])
|
||
counts["commands"] += 1
|
||
self.assertEqual(code, 0, out)
|
||
output = self.written_path(out, "output: ")
|
||
log = ""
|
||
tries = iter(attempts)
|
||
output.write_text(json.dumps(next(tries)), encoding="utf-8") # the compiler
|
||
counts["model_steps"] += 1
|
||
while True:
|
||
code, out = self.run_after(n)
|
||
counts["commands"] += 1
|
||
log += out
|
||
if code == kpack.EXIT_RETRY:
|
||
output.write_text(json.dumps(next(tries)), encoding="utf-8") # the compiler, once more
|
||
counts["model_steps"] += 1
|
||
elif code == kpack.EXIT_GUARD:
|
||
self.written_path(out, "answer: ").write_text(json.dumps(self.GUARD[n]), encoding="utf-8") # the guard
|
||
counts["model_steps"] += 1
|
||
else:
|
||
break
|
||
if code != 0:
|
||
return code, counts, log
|
||
done = re.search(r"done: (\d+) of \d+ blocks", log)
|
||
assert done is not None, log
|
||
pass_dir = self.work / "K-links" / f"p{n}"
|
||
code, out = quiet(klink.main, ["pass-before", "--accepted", str(self.work / "K-work" / "accepted.json"),
|
||
"--pack", str(n), "--bundle", str(self.wiki), "--out", str(pass_dir)])
|
||
counts["commands"] += 1
|
||
self.assertEqual(code, 0, out)
|
||
for m in re.finditer(r"-> (\S+proposals-\d+\.json)", out):
|
||
Path(m.group(1)).write_text(json.dumps({"links": [], "none": []}), encoding="utf-8") # the linker
|
||
counts["model_steps"] += 1
|
||
code, out = quiet(klink.main, [
|
||
"pass-after", "--pass-dir", str(pass_dir), "--bundle", str(self.wiki), "--links",
|
||
str(self.work / "links.json"), "--linked", str(self.work / "K-linked"), "--sources", str(self.root),
|
||
"--transcripts", str(self.transcripts), "--okter", str(self.okter), "--work", str(self.work), "--kost",
|
||
str(self.kost), "--lines", "build,link,orchestration-build", "--cap", str(self.cap), "--unit", "blocks",
|
||
"--done", done.group(1), "--total", str(self.total), "--blocks", str(self.work / "K-work" / "packs.json")])
|
||
counts["commands"] += 1
|
||
self.assertEqual(code, 0, out)
|
||
return 0, counts, log
|
||
|
||
def page(self, name: str) -> audit.Document:
|
||
return audit.read_document(self.wiki / name, name)
|
||
|
||
def main_for(self, name: str) -> list[str]:
|
||
fm = self.page(name).frontmatter or {}
|
||
value = fm.get("x-main-for", [])
|
||
return list(value) if isinstance(value, list) else []
|
||
|
||
|
||
class DryRunTest(ThreePacks):
|
||
__doc__ = ThreePacks.__doc__
|
||
|
||
def test_three_packs_give_every_added_page_a_link_from_a_main_page_or_a_stop(self) -> None:
|
||
code1, counts1, log1 = self.run_pack(1, [self.pack_1()])
|
||
self.assertEqual(code1, 0, log1)
|
||
self.assertEqual(self.roots[0], [A, B]) # the auditor after pack 1 does not see pack 2's sources
|
||
code2, counts2, log2 = self.run_pack(2, [self.pack_2()])
|
||
self.assertEqual(code2, 0, log2)
|
||
code3, counts3, log3 = self.run_pack(3, [self.pack_3()])
|
||
self.assertEqual(code3, kpack.EXIT_BREACH, log3)
|
||
self.assertIn("no-main-page-link crumb.md", log3)
|
||
self.assertEqual(self.roots[-1], sorted(BREAD))
|
||
# what the stub auditor sees at the stop: one page without a home, every docs source with its main page
|
||
final = [f for f in self.audits[-1]["findings"]]
|
||
self.assertEqual([(f["check"], f["file"]) for f in final], [("no-main-page-link", "crumb.md")])
|
||
self.assertEqual(self.main_for("sourdough.md"), [f"/kilde/{A}"])
|
||
self.assertEqual(self.main_for("proofing.md"), [f"/kilde/{B}"])
|
||
self.assertEqual(self.main_for("scoring.md"), [f"/kilde/{C}"]) # named by the guard
|
||
self.assertEqual(self.main_for("oven.md"), [f"/kilde/{D}"])
|
||
homes = {"starter.md": "sourdough.md", "flour.md": "sourdough.md", "rye-starter.md": "sourdough.md",
|
||
"cold-proofing.md": "proofing.md", "baking-stone.md": "oven.md", "steam.md": "oven.md",
|
||
"bake-timer.md": "oven.md"} # bake timer: new from a release note without a home, homed by the guard
|
||
for page, home in homes.items():
|
||
with self.subTest(page=page):
|
||
self.assertIn(page, [t for _, t in audit.body_links(self.page(home))])
|
||
self.assertEqual(self.audits[-2]["findings"][0]["check"], "no-main-page-link")
|
||
self.assertEqual(sorted(f["file"] for f in self.audits[-2]["findings"]), ["bake-timer.md", "crumb.md"])
|
||
oven = self.page("oven.md")
|
||
self.assertIn("## History", oven.body)
|
||
week = [s for s in (oven.frontmatter or {})["sources"] if s.get("title") == "2026-w14"]
|
||
self.assertEqual(len(week), 1)
|
||
self.assertIn("## History", self.page("starter.md").body)
|
||
# orchestrator commands and model steps per pack (input to the price of Step 11)
|
||
self.assertEqual(counts1, {"commands": 5, "model_steps": 3}) # + the guard
|
||
self.assertEqual(counts2, {"commands": 5, "model_steps": 3}) # + the guard
|
||
self.assertEqual(counts3, {"commands": 3, "model_steps": 2}) # stopped before the link pass
|
||
|
||
def test_a_fragment_rejected_at_writing_never_reaches_a_page_and_the_compiler_runs_once(self) -> None:
|
||
self.assertEqual(self.run_pack(1, [self.pack_1()])[0], 0)
|
||
code, counts, log = self.run_pack(2, [self.pack_2(bad=True)])
|
||
self.assertEqual(code, 0, log)
|
||
self.assertNotIn("RETRY", log)
|
||
self.assertEqual(counts["model_steps"], 3, log) # the compiler once, the guard, the linker
|
||
self.assertTrue((self.wiki / "scoring.md").is_file())
|
||
self.assertFalse((self.wiki / "steam.md").exists())
|
||
|
||
|
||
|
||
class RestRulesTest(ThreePacks):
|
||
"""Order 28, the rules for building the rest of a collection. A5: a page without a home after the guard stops the
|
||
build as before (exit 5, again on every run); the operator approves it as a reported exception on a list in the
|
||
work folder that only he changes, and the build goes on from the stop, the page never given a forced link. A4: a
|
||
history source (a release note or a weekly page) has no floor of its own; the history sources of a pack cite at
|
||
least floor(their blocks x rate / 2) anchor blocks together, the rate set in the floor file. A2: run-after gives
|
||
the stop line the next pack as the cap and the groups shape it."""
|
||
|
||
CRUMB = {"page": "crumb.md", "pack": 3, "date": "2026-10-15", "operator": "Ja, varslet unntak (Anbefalt)"}
|
||
|
||
def to_the_stop(self) -> None:
|
||
self.assertEqual(self.run_pack(1, [self.pack_1()])[0], 0)
|
||
self.assertEqual(self.run_pack(2, [self.pack_2(bad=True), self.pack_2()])[0], 0)
|
||
code, _, log = self.run_pack(3, [self.pack_3()])
|
||
self.assertEqual(code, kpack.EXIT_BREACH, log)
|
||
self.assertIn("no-main-page-link crumb.md", log)
|
||
|
||
def approve(self, *entries: dict[str, Any]) -> Path:
|
||
path = self.work / kpack.HOMELESS_APPROVED
|
||
path.write_text(json.dumps({"schema": kpack.HOMELESS_SCHEMA, "pages": list(entries)}), encoding="utf-8")
|
||
return path
|
||
|
||
def blocks_of(self) -> Counter[str]:
|
||
doc = json.loads(self.blocks.read_text(encoding="utf-8"))
|
||
return Counter(str(b["file"]) for b in doc["blocks"])
|
||
|
||
def rate_floor(self, rate: Any) -> Path:
|
||
path = self.tmp / "floor-rate.json"
|
||
path.write_text(json.dumps({"schema": kpack.CITED_SCHEMA, "sources": {}, "history_rate": rate}),
|
||
encoding="utf-8")
|
||
return path
|
||
|
||
def test_a_page_the_operator_approved_no_longer_stops_and_the_list_is_never_changed(self) -> None:
|
||
self.to_the_stop()
|
||
self.assertEqual(self.run_after(3)[0], kpack.EXIT_BREACH) # the same stop each time, until he decides
|
||
path = self.approve(self.CRUMB)
|
||
before = path.read_bytes()
|
||
code, out = self.run_after(3)
|
||
self.assertEqual(code, 0, out)
|
||
self.assertIn("approved without a home (the operator's list): crumb.md", out)
|
||
self.assertIn("cost stop: exit 0", out)
|
||
self.assertEqual(path.read_bytes(), before)
|
||
# the auditor still reports the page; only the stop is lifted
|
||
self.assertEqual([(f["check"], f["file"]) for f in self.audits[-1]["findings"]],
|
||
[("no-main-page-link", "crumb.md")])
|
||
self.assertNotIn("crumb.md", [t for _, t in audit.body_links(self.page("oven.md"))])
|
||
|
||
def test_a_page_not_on_the_list_still_stops(self) -> None:
|
||
self.to_the_stop()
|
||
self.approve({**self.CRUMB, "page": "steam.md"})
|
||
code, out = self.run_after(3)
|
||
self.assertEqual(code, kpack.EXIT_BREACH, out)
|
||
self.assertIn("no-main-page-link crumb.md", out)
|
||
|
||
def test_a_page_on_the_list_does_not_excuse_another_finding(self) -> None:
|
||
self.to_the_stop()
|
||
self.approve(self.CRUMB)
|
||
broken = {"check": "broken-link", "group": "links", "file": "sourdough.md", "line": 9, "message": "x"}
|
||
|
||
def with_broken(b: Path, r: Path) -> dict[str, Any]:
|
||
doc = k2_audit(b, r)
|
||
return {**doc, "findings": [*doc["findings"], broken]}
|
||
|
||
with mock.patch.object(kpack, "run_audit", side_effect=with_broken):
|
||
code, out = self.run_after(3)
|
||
self.assertEqual(code, kpack.EXIT_BREACH, out)
|
||
self.assertIn("broken-link sourdough.md", out)
|
||
|
||
def test_a_list_entry_without_its_page_pack_date_or_the_operator_s_words_cannot_run(self) -> None:
|
||
self.to_the_stop()
|
||
for key in ("page", "pack", "date", "operator"):
|
||
with self.subTest(key=key):
|
||
self.approve({k: v for k, v in self.CRUMB.items() if k != key})
|
||
code, out = self.run_after(3)
|
||
self.assertEqual(code, 2, out)
|
||
self.assertIn(kpack.HOMELESS_APPROVED, out)
|
||
|
||
# A main-topic source no page cites (a document with no readable content): no fragment, so no main page.
|
||
BLANK = {"source": "kilde/docs-en-e.md", "pack": 3, "date": "2026-11-13", "operator": "Ja, varslet unntak"}
|
||
|
||
def approve_both(self, pages: list[dict[str, Any]], srcs: list[Any]) -> Path:
|
||
path = self.work / kpack.HOMELESS_APPROVED
|
||
path.write_text(json.dumps({"schema": kpack.HOMELESS_SCHEMA, "pages": pages, "sources": srcs}),
|
||
encoding="utf-8")
|
||
return path
|
||
|
||
def plant_blank(self, in_message_only: bool = False) -> Any:
|
||
"""The auditor also reports kilde/docs-en-e.md without a main page, naming it in `file` or only in `message`."""
|
||
blank = {"check": "main-page-missing", "group": "links", "file": "" if in_message_only else "kilde/docs-en-e.md",
|
||
"line": None, "message": "main-topic source /kilde/docs-en-e.md has no main page"}
|
||
|
||
def with_blank(b: Path, r: Path) -> dict[str, Any]:
|
||
doc = self.stub_audit(b, r)
|
||
return {**doc, "findings": [*doc["findings"], blank]}
|
||
|
||
return mock.patch.object(kpack, "run_audit", side_effect=with_blank)
|
||
|
||
def run_after_blank(self, in_message_only: bool = False) -> tuple[int, str]:
|
||
with self.plant_blank(in_message_only):
|
||
return self.run_after(3)
|
||
|
||
def test_read_approved_reads_sources_beside_pages_and_a_list_of_pages_alone_as_before(self) -> None:
|
||
self.approve(self.CRUMB)
|
||
self.assertEqual(kpack.read_approved(self.work), {"crumb.md"})
|
||
self.approve_both([], [self.BLANK])
|
||
self.assertEqual(kpack.read_approved(self.work), {"kilde/docs-en-e.md"})
|
||
(self.work / kpack.HOMELESS_APPROVED).write_text(json.dumps({"schema": kpack.HOMELESS_SCHEMA,
|
||
"sources": [self.BLANK]}), encoding="utf-8")
|
||
self.assertEqual(kpack.read_approved(self.work), {"kilde/docs-en-e.md"})
|
||
self.approve_both([self.CRUMB], [self.BLANK])
|
||
approved = kpack.read_approved(self.work)
|
||
blank = {"check": "main-page-missing", "file": "kilde/docs-en-e.md", "message": "x"}
|
||
self.assertTrue(kpack.is_approved(blank, approved))
|
||
self.assertTrue(kpack.is_approved({**blank, "file": "", "message": "source /kilde/docs-en-e.md"}, approved))
|
||
self.assertFalse(kpack.is_approved({**blank, "file": "kilde/docs-en-f.md"}, approved))
|
||
self.assertFalse(kpack.is_approved({**blank, "check": "broken-link"}, approved))
|
||
self.assertTrue(kpack.is_approved({"check": "no-main-page-link", "file": "crumb.md"}, approved))
|
||
|
||
def test_a_source_on_the_list_may_be_written_with_or_without_kilde_and_a_leading_slash(self) -> None:
|
||
for name in ("kilde/docs-en-e.md", "/kilde/docs-en-e.md", "docs-en-e.md", " docs-en-e.md "):
|
||
with self.subTest(name=name):
|
||
self.approve_both([], [{**self.BLANK, "source": name}])
|
||
self.assertEqual(kpack.read_approved(self.work), {"kilde/docs-en-e.md"})
|
||
|
||
def test_a_bad_list_of_sources_cannot_run_and_names_the_entry(self) -> None:
|
||
bad: list[Any] = [*({k: v for k, v in self.BLANK.items() if k != key} for key in self.BLANK),
|
||
*({**self.BLANK, k: " "} for k in ("source", "date", "operator")),
|
||
{**self.BLANK, "pack": "3"}, {**self.BLANK, "source": "kilde/"}, "kilde/docs-en-e.md"]
|
||
for entry in bad:
|
||
with self.subTest(entry=entry):
|
||
self.approve_both([self.CRUMB], [entry])
|
||
with self.assertRaises(RunError) as cm:
|
||
kpack.read_approved(self.work)
|
||
self.assertIn(kpack.HOMELESS_APPROVED, str(cm.exception))
|
||
self.assertIn(repr(entry), str(cm.exception))
|
||
for doc in ({"schema": kpack.HOMELESS_SCHEMA, "pages": [self.CRUMB], "sources": {"x": 1}},
|
||
{"schema": kpack.HOMELESS_SCHEMA, "sources": None}, {"schema": kpack.HOMELESS_SCHEMA}):
|
||
with self.subTest(doc=doc):
|
||
(self.work / kpack.HOMELESS_APPROVED).write_text(json.dumps(doc), encoding="utf-8")
|
||
with self.assertRaises(RunError):
|
||
kpack.read_approved(self.work)
|
||
|
||
def test_a_source_the_operator_approved_without_a_main_page_no_longer_stops_and_is_still_reported(self) -> None:
|
||
self.to_the_stop()
|
||
self.approve(self.CRUMB)
|
||
code, out = self.run_after_blank()
|
||
self.assertEqual(code, kpack.EXIT_BREACH, out) # not on the list: the stop names its source
|
||
self.assertIn("main-page-missing kilde/docs-en-e.md", out)
|
||
path = self.approve_both([self.CRUMB], [self.BLANK])
|
||
before = path.read_bytes()
|
||
for in_message_only in (False, True):
|
||
with self.subTest(in_message_only=in_message_only):
|
||
code, out = self.run_after_blank(in_message_only)
|
||
self.assertEqual(code, 0, out)
|
||
self.assertIn("approved without a home (the operator's list): crumb.md", out)
|
||
self.assertIn("approved without a main page (the operator's list): kilde/docs-en-e.md", out)
|
||
self.assertIn("cost stop: exit 0", out)
|
||
saved = re.search(r"-> (\S+\.json)", out)
|
||
assert saved is not None, out
|
||
found = json.loads(Path(saved.group(1)).read_text(encoding="utf-8"))["findings"]
|
||
self.assertIn("main-page-missing", [f["check"] for f in found]) # the auditor's JSON keeps it
|
||
self.assertEqual(path.read_bytes(), before)
|
||
|
||
def test_a_source_on_the_list_does_not_excuse_another_source_or_page(self) -> None:
|
||
self.to_the_stop()
|
||
self.approve_both([], [self.BLANK])
|
||
code, out = self.run_after_blank()
|
||
self.assertEqual(code, kpack.EXIT_BREACH, out)
|
||
self.assertIn("no-main-page-link crumb.md", out)
|
||
self.approve_both([self.CRUMB], [{**self.BLANK, "source": "kilde/docs-en-f.md"}])
|
||
code, out = self.run_after_blank()
|
||
self.assertEqual(code, kpack.EXIT_BREACH, out)
|
||
self.assertIn("main-page-missing kilde/docs-en-e.md", out)
|
||
|
||
def test_a_source_on_the_list_without_a_finding_matches_nothing(self) -> None:
|
||
self.to_the_stop()
|
||
self.approve_both([self.CRUMB], [{**self.BLANK, "source": f"kilde/{D}"}]) # D has its main page
|
||
code, out = self.run_after(3)
|
||
self.assertEqual(code, 0, out)
|
||
self.assertNotIn("approved without a main page", out)
|
||
|
||
def test_a_source_on_the_list_is_not_asked_of_the_guard(self) -> None:
|
||
self.assertEqual(self.run_pack(1, [self.pack_1()])[0], 0)
|
||
self.assertEqual(self.run_pack(2, [self.pack_2(bad=True), self.pack_2()])[0], 0)
|
||
self.approve_both([self.CRUMB], [self.BLANK])
|
||
self.GUARD = {**ThreePacks.GUARD, 3: {"homes": [{"page": "bake-timer", "home": "oven"}], "none": [],
|
||
"main_pages": []}}
|
||
with self.plant_blank():
|
||
code, _, log = self.run_pack(3, [self.pack_3()])
|
||
self.assertEqual(code, 0, log)
|
||
self.assertEqual(self.log(3)["guard"]["asked"]["sources"], [])
|
||
self.assertEqual(self.log(3)["guard"]["asked"]["pages"], ["bake-timer.md"])
|
||
|
||
def test_a_page_on_the_list_is_not_asked_of_the_guard(self) -> None:
|
||
self.assertEqual(self.run_pack(1, [self.pack_1()])[0], 0)
|
||
self.assertEqual(self.run_pack(2, [self.pack_2(bad=True), self.pack_2()])[0], 0)
|
||
self.approve(self.CRUMB)
|
||
self.GUARD = {**ThreePacks.GUARD, 3: {"homes": [{"page": "bake-timer", "home": "oven"}], "none": [],
|
||
"main_pages": []}}
|
||
code, _, log = self.run_pack(3, [self.pack_3()])
|
||
self.assertEqual(code, 0, log)
|
||
self.assertEqual(self.log(3)["guard"]["asked"]["pages"], ["bake-timer.md"])
|
||
|
||
def test_the_history_sources_of_a_pack_cite_at_least_their_floor_together(self) -> None:
|
||
self.to_the_stop()
|
||
self.approve(self.CRUMB)
|
||
b = self.blocks_of()
|
||
hist = b[W] + b[R]
|
||
cited = sum(self.log(3)["cited"].get(x, 0) for x in (W, R))
|
||
need = int(hist * 4.0 / 2)
|
||
self.assertGreater(need, cited)
|
||
code, out = kp_exit(["run-after", "3", *self.after_args(), "--floor", str(self.rate_floor(4.0))])
|
||
self.assertEqual(code, kpack.EXIT_THIN, out)
|
||
self.assertIn(f"floor: pack 3's 2 history sources cite {cited} anchor blocks of {hist}; the floor is {need}",
|
||
out)
|
||
self.assertIn("STOP", out)
|
||
code, out = kp_exit(["run-after", "3", *self.after_args(), "--floor", str(self.rate_floor(0.146))])
|
||
self.assertEqual(code, 0, out)
|
||
self.assertIn(f"floor: pack 3's 2 history sources cite {cited} anchor blocks of {hist}; the floor is "
|
||
f"{int(hist * 0.146 / 2)}", out)
|
||
|
||
def test_the_history_floor_counts_what_is_accepted_now_after_a_remove(self) -> None:
|
||
self.to_the_stop()
|
||
self.approve(self.CRUMB)
|
||
b = self.blocks_of()
|
||
need = sum(self.log(3)["cited"].get(x, 0) for x in (W, R)) # a rate whose floor is what they cite now
|
||
floor = self.rate_floor((2 * need + 0.5) / (b[W] + b[R]))
|
||
code, out = kp_exit(["run-after", "3", *self.after_args(), "--floor", str(floor)])
|
||
self.assertEqual(code, 0, out)
|
||
self.assertIn(f"the floor is {need}", out)
|
||
code, out = self.kp(["remove", R, "--work", str(self.work)])
|
||
self.assertEqual(code, 0, out)
|
||
left = len({(r["start"]) for r in self.acc()["fragments"] if r["file"] == W})
|
||
self.assertLess(left, need)
|
||
code, out = kp_exit(["run-after", "3", *self.after_args(), "--floor", str(floor)])
|
||
self.assertEqual(code, kpack.EXIT_THIN, out)
|
||
self.assertIn(f"floor: pack 3's 2 history sources cite {left} anchor blocks of {b[W] + b[R]}", out)
|
||
|
||
def test_a_pack_without_history_sources_has_no_pack_floor_and_a_rate_must_be_a_number(self) -> None:
|
||
self.write(1, self.pack_1())
|
||
self.assertEqual(self.run_after(1)[0], kpack.EXIT_GUARD)
|
||
(self.work / "K-guard" / "p1" / "answer.json").write_text(json.dumps(
|
||
{"homes": [{"page": "cold-proofing", "home": "proofing"}], "none": [], "main_pages": []}), encoding="utf-8")
|
||
code, out = kp_exit(["run-after", "1", *self.after_args(), "--floor", str(self.rate_floor("half"))])
|
||
self.assertEqual(code, 2, out)
|
||
code, out = kp_exit(["run-after", "1", *self.after_args(), "--floor", str(self.rate_floor(4.0))])
|
||
self.assertEqual(code, 0, out)
|
||
self.assertNotIn("history sources", out)
|
||
|
||
def test_run_after_gives_the_stop_line_the_next_pack_under_the_cap_and_apart(self) -> None:
|
||
self.write(1, self.pack_1())
|
||
self.assertEqual(self.run_after(1)[0], kpack.EXIT_GUARD)
|
||
(self.work / "K-guard" / "p1" / "answer.json").write_text(json.dumps(
|
||
{"homes": [{"page": "cold-proofing", "home": "proofing"}], "none": [], "main_pages": []}), encoding="utf-8")
|
||
b = self.blocks_of()
|
||
with mock.patch.object(cost, "main", wraps=cost.main) as calls:
|
||
code, out = self.kp(["run-after", "1", *self.after_args(), "--max-blocks", str(b[C]), "--groups-apart"])
|
||
self.assertEqual(code, 0, out)
|
||
stop = [c.args[0] for c in calls.call_args_list if c.args[0][0] == "stop"]
|
||
self.assertEqual(stop[0][stop[0].index("--next") + 1], str(b[C])) # pack 2 is C alone under the cap
|
||
self.assertEqual(self.log(1)["sources"], [A, B])
|
||
|
||
|
||
class DryRunRealAuditorTest(DryRunTest):
|
||
"""The same three packs against the real auditor of Step 7 (order 7, merged before order 10): the contract K2 as
|
||
the skill's chain reads it, not as the stub writes it. Every assertion of the dry run holds unchanged."""
|
||
|
||
def stub_audit(self, bundle: Path, root: Path) -> dict[str, Any]:
|
||
self.roots.append(sorted(p.name for p in (root / "kilde").iterdir()))
|
||
doc = REAL_RUN_AUDIT(bundle, root)
|
||
self.audits.append(doc)
|
||
return doc
|
||
|
||
def test_the_real_auditor_writes_the_report_line_for_the_stop(self) -> None:
|
||
self.assertEqual(self.run_pack(1, [self.pack_1()])[0], 0)
|
||
self.assertEqual(self.run_pack(2, [self.pack_2(bad=True), self.pack_2()])[0], 0)
|
||
self.assertEqual(self.run_pack(3, [self.pack_3()])[0], kpack.EXIT_BREACH)
|
||
line = self.audits[-1]["link_rule"]["line"]
|
||
m = re.fullmatch(r"main pages: (\d+) of \1 sources · linked from a main page: (\d+) of (\d+) pages", line)
|
||
assert m is not None, line
|
||
self.assertEqual(int(m.group(3)) - int(m.group(2)), 1) # crumb.md, the page the guard answered "none" for
|
||
|
||
|
||
# ---------------------------------------------------------------------------------------------- a built collection continued
|
||
|
||
|
||
class ContinueTest(Chain):
|
||
"""Order 27 A2: a collection built in packs is continued with new sources. Measured 14.10.2026 on a copy of round
|
||
2's work folder with the trial's subset root: the packs were worked out again from the order and the size, so all
|
||
eight packs moved (the new pages sort in among the built ones, and run-before 9 rendered four built sources and one
|
||
new), another --size was refused, and the skeleton refused to run. Here an earlier build of B and D in one pack of
|
||
two is continued with A, C, the weekly page W and the release note R."""
|
||
|
||
def setUp(self) -> None:
|
||
super().setUp()
|
||
doc = json.loads(self.blocks.read_text(encoding="utf-8"))
|
||
self.blocks_of = Counter(str(b["file"]) for b in doc["blocks"])
|
||
self.packs_file = self.work / "K-work" / "packs.json"
|
||
self.packs_file.write_text(json.dumps({"schema": kpack.PACKS_SCHEMA, "size": 2, "packs": [
|
||
{"pack": 1, "sources": [B, D], "blocks": self.blocks_of[B] + self.blocks_of[D], "accepted": 4,
|
||
"size": 2}]}), encoding="utf-8")
|
||
|
||
def packs_log(self) -> dict[str, Any]:
|
||
data: dict[str, Any] = json.loads(self.packs_file.read_text(encoding="utf-8"))
|
||
return data
|
||
|
||
def test_registered_packs_keep_their_sources_and_new_sources_follow_in_new_packs(self) -> None:
|
||
order = kblocks.order(sorted(BREAD))
|
||
self.assertEqual(kpack.packs(order, 2)[0], [A, B]) # today's re-packing would move pack 1
|
||
self.assertEqual(kpack.layout(order, self.packs_log(), 2), [[B, D], [A, C], [W, R]])
|
||
code, out = self.kp(["run-before", "2", *self.common()])
|
||
self.assertEqual(code, 0, out)
|
||
self.assertEqual(self.log(2)["sources"], [A, C])
|
||
self.assertEqual(self.log(1)["sources"], [B, D])
|
||
|
||
def test_new_sources_come_docs_first_each_group_in_k_order(self) -> None:
|
||
order = [R, C, W, B, A, D] # an order that is not K's: the new sources still follow in K's order
|
||
self.assertEqual(kpack.layout(order, self.packs_log(), 3), [[B, D], [A, C, W], [R]])
|
||
|
||
def test_a_registered_source_the_order_lacks_or_one_registered_twice_cannot_run(self) -> None:
|
||
with self.assertRaises(RunError):
|
||
kpack.layout([A, C], self.packs_log(), 2)
|
||
log = self.packs_log()
|
||
log["packs"].append({"pack": 2, "sources": [D, A]})
|
||
with self.assertRaises(RunError):
|
||
kpack.layout(kblocks.order(sorted(BREAD)), log, 2)
|
||
|
||
def test_the_size_can_be_set_for_a_new_pack_and_is_registered_with_it(self) -> None:
|
||
code, out = self.kp(["run-before", "2", *self.common()[:-1], "4"]) # --size 4 for the new pack
|
||
self.assertEqual(code, 0, out)
|
||
self.assertEqual((self.log(2)["sources"], self.log(2)["size"]), ([A, C, W, R], 4))
|
||
self.assertEqual((self.log(1)["sources"], self.log(1)["size"]), ([B, D], 2)) # the registered pack keeps its size
|
||
self.assertEqual(self.packs_log()["size"], 2) # the work folder's first size stays its default
|
||
self.assertEqual(self.kp(["run-before", "3", *self.common()])[0], 2) # no source is left for a pack 3
|
||
|
||
def test_the_next_pack_s_size_shapes_the_stop_line_s_next_pack(self) -> None:
|
||
log = self.packs_log()
|
||
doc = json.loads(self.blocks.read_text(encoding="utf-8"))
|
||
self.assertEqual(kpack.next_blocks(doc, log, 1, 2, 10**6, 10**7), self.blocks_of[A] + self.blocks_of[C])
|
||
self.assertEqual(kpack.next_blocks(doc, log, 1, 4, 10**6, 10**7), sum(self.blocks_of[x] for x in (A, C, W, R)))
|
||
|
||
def test_the_run_s_last_pack_has_no_next_pack_when_the_root_holds_more(self) -> None:
|
||
# variants S and R build the ten pages of a root that also holds the release notes: once the run's blocks
|
||
# are done (done = total), the next pack is 0, whatever the root holds after it
|
||
log = self.packs_log()
|
||
doc = json.loads(self.blocks.read_text(encoding="utf-8"))
|
||
self.assertEqual(kpack.next_blocks(doc, log, 1, 2, 500, 500), 0)
|
||
|
||
def test_the_skeleton_is_extended_with_the_new_docs_pages_only(self) -> None:
|
||
part = freeze_root(self.tmp, {"docs-en-pots.md": POTS}, set(), name="part")
|
||
work = self.tmp / "work-x"
|
||
self.assertEqual(self.kp(["skeleton", "--sources", str(part), "--work", str(work)])[0], 0)
|
||
before = json.loads((work / "K-work" / "index.json").read_text(encoding="utf-8"))
|
||
(work / "K-work" / "packs.json").write_text(json.dumps({"schema": kpack.PACKS_SCHEMA, "size": 5, "packs": [
|
||
{"pack": 1, "sources": ["docs-en-pots.md"], "blocks": 2, "accepted": 1}]}), encoding="utf-8")
|
||
files = {"docs-en-pots.md": POTS, "docs-en-seeds.md": SEEDS, "docs-en-trays.md": TRAYS,
|
||
"docs-en-whats-new-2026-w15.md": "# Week 15\n\n## Sowing\n\n- Sowing got a page.\n",
|
||
"release-v2.0.0.md": "## 2.0.0\n\n- Added trays.\n"}
|
||
full = freeze_root(self.tmp, files, {"docs-en-whats-new-2026-w15.md"}, name="full")
|
||
code, out = self.kp(["skeleton", "--sources", str(full), "--work", str(work), "--extend"])
|
||
self.assertEqual(code, 0, out)
|
||
after = json.loads((work / "K-work" / "index.json").read_text(encoding="utf-8"))
|
||
self.assertEqual(after["entries"][:len(before["entries"])], before["entries"]) # never changed, no new alias
|
||
added = after["entries"][len(before["entries"]):]
|
||
self.assertEqual([(e["title"], e["origin"]["file"]) for e in added],
|
||
[("Seeds", "docs-en-seeds.md"), ("Sowing", "docs-en-seeds.md"), ("Trays", "docs-en-trays.md")])
|
||
self.assertEqual(added[1]["aliases"], ["Sowing", "The sowing"]) # a new name two new pages share
|
||
self.assertIn("2 new docs pages", out)
|
||
self.assertIn("3 entries added", out)
|
||
self.assertIn("2 names equal to an existing entry, not added", out) # "The soil" and "Pots"
|
||
self.assertEqual(self.kp(["skeleton", "--sources", str(full), "--work", str(work), "--extend"])[1].count(
|
||
"0 new docs pages"), 1) # the pages are extended once
|
||
self.assertEqual(json.loads((work / "K-work" / "index.json").read_text(encoding="utf-8")), after)
|
||
|
||
def test_the_pages_of_the_built_packs_are_the_same_from_a_root_that_holds_more(self) -> None:
|
||
# A2.4: the auditor and the assembly see only the packs built; a root with sources not yet built changes no page
|
||
part = freeze_root(self.tmp, {x: BREAD[x] for x in (A, B)}, set(), name="ab")
|
||
self.packs_file.write_text(json.dumps({"schema": kpack.PACKS_SCHEMA, "size": 2, "packs": []}))
|
||
self.write(1, self.pack_1())
|
||
from okfwiki import kassemble
|
||
self.assertEqual(self.run_after(1)[0], kpack.EXIT_GUARD)
|
||
(self.work / "K-guard" / "p1" / "answer.json").write_text(json.dumps(
|
||
{"homes": [{"page": "cold-proofing", "home": "proofing"}], "none": [], "main_pages": []}), encoding="utf-8")
|
||
self.assertEqual(self.run_after(1)[0], 0)
|
||
self.assertEqual(self.roots[-1], [A, B])
|
||
pages: list[dict[str, bytes]] = []
|
||
for root, out in ((part, self.tmp / "w-part"), (self.root, self.tmp / "w-full")):
|
||
kassemble.build(self.work, root, out, None, at="2026-10-14T10:00:00Z")
|
||
pages.append({p.name: p.read_bytes() for p in out.iterdir() if p.name != "log.md"})
|
||
self.assertEqual(pages[0], pages[1])
|
||
|
||
# order 28 A2: the rest of a collection in packs that never mix the groups and stay under a cap of blocks
|
||
|
||
def test_with_the_groups_apart_a_new_pack_never_holds_docs_pages_and_release_notes(self) -> None:
|
||
order = kblocks.order(sorted(BREAD))
|
||
self.assertEqual(kpack.layout(order, self.packs_log(), 4), [[B, D], [A, C, W, R]]) # as before, without it
|
||
self.assertEqual(kpack.layout(order, self.packs_log(), 4, apart=True), [[B, D], [A, C, W], [R]])
|
||
self.assertEqual(kpack.layout(order, self.packs_log(), 2, apart=True), [[B, D], [A, C], [W], [R]])
|
||
|
||
def test_a_cap_closes_a_new_pack_before_the_source_that_would_take_it_over(self) -> None:
|
||
order = kblocks.order(sorted(BREAD))
|
||
b = self.blocks_of
|
||
cap = b[A] + b[C] # A and C fill it; W would take it over
|
||
self.assertEqual(kpack.layout(order, self.packs_log(), 5, cap=cap, apart=True, blocks=b),
|
||
[[B, D], [A, C], [W], [R]])
|
||
self.assertEqual(kpack.layout(order, self.packs_log(), 5, cap=cap + b[W], blocks=b), [[B, D], [A, C, W], [R]])
|
||
|
||
def test_a_source_over_the_cap_alone_gets_a_pack_of_its_own_and_a_registered_pack_never_moves(self) -> None:
|
||
order = kblocks.order(sorted(BREAD))
|
||
b = self.blocks_of
|
||
self.assertGreater(b[B] + b[D], b[A]) # the registered pack is over the cap below, and keeps its sources
|
||
cap = b[A] - 1 # A alone is over it
|
||
got = kpack.layout(order, self.packs_log(), 5, cap=cap, apart=True, blocks=b)
|
||
self.assertEqual(got[0], [B, D])
|
||
self.assertEqual(got[1], [A])
|
||
self.assertEqual([x for p in got[2:] for x in p], [C, W, R])
|
||
for p in got[2:]:
|
||
self.assertTrue(len(p) == 1 or sum(b[x] for x in p) <= cap, p)
|
||
|
||
def test_a_cap_needs_the_blocks_of_each_source(self) -> None:
|
||
with self.assertRaises(RunError):
|
||
kpack.layout(kblocks.order(sorted(BREAD)), self.packs_log(), 5, cap=10)
|
||
|
||
def test_the_cap_and_the_groups_apart_are_registered_and_shape_the_later_packs(self) -> None:
|
||
cap = self.blocks_of[A] + self.blocks_of[C]
|
||
code, out = self.kp(["run-before", "2", *self.common()[:-1], "5", "--max-blocks", str(cap), "--groups-apart"])
|
||
self.assertEqual(code, 0, out)
|
||
self.assertEqual((self.log(2)["sources"], self.log(2)["max_blocks"]), ([A, C], cap))
|
||
log = self.packs_log()
|
||
self.assertEqual((log["max_blocks"], log["groups_apart"]), (cap, True)) # the work folder's, as its size
|
||
self.assertNotIn("max_blocks", self.log(1)) # the registered pack is as it was
|
||
code, out = self.kp(["run-before", "3", *self.common()[:-1], "5"]) # the same packs without the options
|
||
self.assertEqual(code, 0, out)
|
||
self.assertEqual(self.log(3)["sources"], [W])
|
||
self.assertEqual(self.kp(["run-before", "4", *self.common()[:-1], "20"])[0], 0)
|
||
self.assertEqual(self.log(4)["sources"], [R])
|
||
|
||
def test_the_cap_and_the_groups_apart_shape_the_stop_line_s_next_pack(self) -> None:
|
||
doc = json.loads(self.blocks.read_text(encoding="utf-8"))
|
||
log, b = self.packs_log(), self.blocks_of
|
||
self.assertEqual(kpack.next_blocks(doc, log, 1, 4, 10**6, 10**7, apart=True), b[A] + b[C] + b[W])
|
||
self.assertEqual(kpack.next_blocks(doc, log, 1, 4, 10**6, 10**7, cap=b[A] + b[C], apart=True), b[A] + b[C])
|
||
|
||
|
||
POTS ="# Pots\n\nPots hold soil.\n\n## Soil\n\nPots need loose soil.\n"
|
||
SEEDS = ("# Seeds\n\nSeeds start in spring.\n\n## The soil\n\nSeeds need fine soil.\n\n## Sowing\n\nSow thinly.\n\n"
|
||
"## Pots\n\nSow in small pots.\n")
|
||
TRAYS = "# Trays\n\nTrays hold seedlings.\n\n## The sowing\n\nSow in rows.\n"
|
||
|
||
|
||
class CharCapTest(ContinueTest):
|
||
"""Motor B17: a new pack may close at a cap of characters (--max-chars), registered like --max-blocks; there is no
|
||
cap by default (the operator 07.11: no artificial limits; the largest pack measured to hold is a measurement, not
|
||
where a compiler run fails). run-before names a source larger than the largest measured to hold, and stops nothing.
|
||
"""
|
||
|
||
def chars_of(self) -> Counter[str]:
|
||
return kpack.chars_per_source(json.loads(self.blocks.read_text(encoding="utf-8")))
|
||
|
||
def test_there_is_no_cap_unless_given_or_recorded(self) -> None:
|
||
self.assertIsNone(kpack.pack_char_cap({}, None))
|
||
self.assertEqual(kpack.pack_char_cap({"max_chars": 900}, None), 900)
|
||
self.assertEqual(kpack.pack_char_cap({"max_chars": 900}, 500), 500)
|
||
with self.assertRaises(RunError):
|
||
kpack.pack_char_cap({}, 0)
|
||
self.assertEqual(kpack.LARGEST_HELD, 387_767) # a 387 767-character PDF of a real folder, one run, 07.11
|
||
|
||
def test_a_char_cap_closes_a_new_pack_before_the_source_that_would_take_it_over(self) -> None:
|
||
order, c = kblocks.order(sorted(BREAD)), self.chars_of()
|
||
self.assertEqual(kpack.layout(order, self.packs_log(), 5, apart=True, char_cap=c[A] + c[C], chars=c),
|
||
[[B, D], [A, C], [W], [R]])
|
||
self.assertEqual(kpack.layout(order, self.packs_log(), 5, apart=True, char_cap=c[A] - 1, chars=c)[1], [A])
|
||
|
||
def test_the_char_cap_is_registered_shapes_the_later_packs_and_the_next_share(self) -> None:
|
||
c = self.chars_of()
|
||
cap = c[A] + c[C]
|
||
code, out = self.kp(["run-before", "2", *self.common()[:-1], "5", "--max-chars", str(cap), "--groups-apart"])
|
||
self.assertEqual(code, 0, out)
|
||
self.assertEqual((self.log(2)["sources"], self.log(2)["max_chars"]), ([A, C], cap))
|
||
self.assertEqual(self.packs_log()["max_chars"], cap)
|
||
doc = json.loads(self.blocks.read_text(encoding="utf-8"))
|
||
self.assertEqual(kpack.next_pack(doc, self.packs_log(), 1, 5, 10**6, 10**7, apart=True, char_cap=cap,
|
||
chars=c), [A, C])
|
||
code, out = self.kp(["run-before", "3", *self.common()[:-1], "5"]) # the same packs without the option
|
||
self.assertEqual((code, self.log(3)["sources"]), (0, [W]), out)
|
||
|
||
def test_a_source_larger_than_any_measured_to_hold_is_named_and_stops_nothing(self) -> None:
|
||
c = self.chars_of()
|
||
with mock.patch.object(kpack, "LARGEST_HELD", c[A] - 1):
|
||
code, out = self.kp(["run-before", "2", *self.common()[:-1], "5"])
|
||
self.assertEqual(code, 0, out)
|
||
self.assertIn(A, self.log(2)["sources"])
|
||
self.assertNotIn("max_chars", self.log(2))
|
||
self.assertIn(f"large: {A} holds {c[A]} characters of blocks, more than the {c[A] - 1} of the largest source "
|
||
"measured to hold in one compiler run", out)
|
||
|
||
def test_norwegian_and_accented_letters_are_spelled_out_not_dropped(self) -> None:
|
||
# motor step 2 (B12): "Årlig kontroll" lost its first letter as "rlig-kontroll"
|
||
index: dict[str, Any] = {"entries": []}
|
||
self.assertEqual(kpack.unique_id(index, "Årlig kontroll"), "aarlig-kontroll")
|
||
self.assertEqual(kpack.unique_id(index, "Ærlig søknad"), "aerlig-soeknad")
|
||
self.assertEqual(kpack.unique_id(index, "Café Müller"), "cafe-muller")
|
||
self.assertEqual(kpack.unique_id(index, "Straße"), "strasse")
|
||
self.assertEqual(kpack.unique_id(index, "Plain ASCII words"), "plain-ascii-words")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
unittest.main()
|