okf-wiki/tests/test_folder_build.py
Kjell Tore Guttormsen 0fd86a23a3
okf-wiki 1.1.1
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>
2026-11-14 21:03:20 +01:00

183 lines
11 KiB
Python

"""Motor step 2: a folder of Markdown and text files builds with no code change.
An invented folder (subfolders, capitals, Norwegian letters, Windows line breaks, a .txt file without a heading) goes
through every step that is code: sources folder, kblocks order and blocks, kpack skeleton, one pack accepted from a
hand-written compiler output, kassemble build and the auditor. Every source here is invented; no text from any
documentation.
Run: python3 -m unittest discover -s tests -p 'test_folder_build.py'
"""
from __future__ import annotations
import io
import json
import sys
import tempfile
import unittest
from contextlib import redirect_stderr, redirect_stdout
from pathlib import Path
from typing import Any, Callable
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
from okfwiki import audit, kassemble, kblocks, kpack, sources, view # noqa: E402
FOLDER = {
"Hage/Årlig plan.md": "# Årlig plan\r\n\r\nKompost legges ut hver vår.\r\n\r\n## Såing\r\n\r\nGulrot sås i mai.\r\n",
"notat.txt": "Bladene samles i en binge om høsten.\n\nBingen snus to ganger.\n",
}
NEW = {"topic_shape": "en ting en gartner gjør", "citation": "Se planen for det"}
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()
class FolderBuildTest(unittest.TestCase):
def setUp(self) -> None:
self._tmp = tempfile.TemporaryDirectory()
self.tmp = Path(self._tmp.name)
self.folder = self.tmp / "Mine dokumenter"
for rel, text in FOLDER.items():
p = self.folder / rel
p.parent.mkdir(parents=True, exist_ok=True)
p.write_bytes(text.encode("utf-8"))
self.root = self.tmp / "sources"
self.work = self.tmp / "work"
self.wiki = self.tmp / "wiki"
def tearDown(self) -> None:
self._tmp.cleanup()
def run_ok(self, fn: Callable[[list[str]], int], args: list[str]) -> str:
code, out = quiet(fn, args)
self.assertEqual(code, 0, f"{args[0]}: {out}")
return out
def frag(self, blocks: list[dict[str, Any]], concept: str, file: str, quote: str, text: str,
new: bool) -> dict[str, Any]:
lines = (self.root / "kilde" / file).read_text(encoding="utf-8").split("\n")
line = next(n for n, raw in enumerate(lines, 1) if quote in raw)
block = next(b for b in 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": "documented"}
if new:
f["tests"] = NEW
return f
def test_an_invented_folder_builds_and_passes_the_auditor(self) -> None:
self.run_ok(sources.main, ["folder", str(self.folder), "--collection", "hage", "--out", str(self.root)])
names = sorted(p.name for p in (self.root / "kilde").iterdir())
self.assertEqual(names, ["hage__aarlig-plan.md", "notat.txt"])
self.work.mkdir()
order, blocks_path = self.work / "order.txt", self.work / "blocks.json"
self.run_ok(kblocks.main, ["order", str(self.root), "--out", str(order)])
self.assertEqual(order.read_text(encoding="utf-8").split(), names)
self.run_ok(kblocks.main, ["blocks", str(self.root), "--order", str(order), "--out", str(blocks_path)])
blocks: list[dict[str, Any]] = json.loads(blocks_path.read_text(encoding="utf-8"))["blocks"]
self.assertEqual({b["file"] for b in blocks}, set(names))
self.run_ok(kpack.main, ["skeleton", "--sources", str(self.root), "--work", str(self.work)])
index = json.loads((self.work / "K-work" / "index.json").read_text(encoding="utf-8"))
ids = {e["id"] for e in index["entries"]}
self.assertIn("aarlig-plan", ids) # B12: the Norwegian letters survive in the page id
self.assertIn("saaing", ids)
plan, notat = names
doc = {"fragments": [
self.frag(blocks, "Årlig plan", plan, "Kompost legges ut hver vår.", "Kompost legges ut hver vår.", False),
self.frag(blocks, "Såing", plan, "Gulrot sås i mai.", "Gulrot sås i mai.", False),
self.frag(blocks, "Løvkompost", notat, "samles i en binge om høsten",
"Løvet samles i en binge om høsten.", True),
self.frag(blocks, "Løvkompost", notat, "Bingen snus to ganger.", "Bingen snus to ganger.", False),
], "main_pages": [{"source": plan, "concept": "Årlig plan"}, {"source": notat, "concept": "Løvkompost"}],
"homes": [{"concept": "Såing", "home": "Årlig plan"}]}
out = self.work / "K-fragments" / "pack-1.json"
out.parent.mkdir(parents=True)
out.write_text(json.dumps(doc, ensure_ascii=False), encoding="utf-8")
common = ["--blocks", str(blocks_path), "--sources", str(self.root), "--work", str(self.work)]
self.run_ok(kpack.main, ["pack", "1", *common])
# the trial of 07.11: a .txt without a heading showed the compiler only its safe name, and the new concept was
# titled "honninghoesting"; the task names each source's original as well
part = (self.work / "K-oppgaver" / "pack-1-part-1.md").read_text(encoding="utf-8")
self.assertIn("- hage__aarlig-plan.md (original name: Hage/Årlig plan.md): ", part)
# the same trial, run again with the original name shown: still "honninghoesting", an id, because the contract
# said nothing of what a new concept is called; it becomes the page's title
self.assertIn('"new" with `concept` its title as a reader would write it, in the language and letters of '
"the source (never an id)", " ".join(part.split()))
self.run_ok(kpack.main, ["accept", "1", *common])
self.run_ok(kassemble.main, ["build", "--work", str(self.work), "--sources", str(self.root), "--out",
str(self.wiki)])
pages = sorted(p.name for p in self.wiki.glob("*.md"))
self.assertIn("loevkompost.md", pages)
result = self.run_ok(audit.main, [str(self.wiki), str(self.root)])
report = json.loads(result)
self.assertEqual(report["findings"], [])
self.assertEqual(report["link_rule"]["main_pages"], {"pass": 2, "of": 2})
def test_a_folder_of_pdf_and_office_files_builds_with_the_page_and_paragraph_on_every_footnote(self) -> None:
from test_readers import docx, pdf, xlsx
folder = self.tmp / "Kontor"
folder.mkdir()
pdf(folder / "Rapport.pdf", ["Side en om bier", "Side to om honning"])
docx(folder / "Plan.docx")
xlsx(folder / "Kuber.xlsx")
self.run_ok(sources.main, ["folder", str(folder), "--collection", "kontor", "--out", str(self.root)])
self.work.mkdir()
order, blocks_path = self.work / "order.txt", self.work / "blocks.json"
self.run_ok(kblocks.main, ["order", str(self.root), "--out", str(order)])
self.run_ok(kblocks.main, ["blocks", str(self.root), "--order", str(order), "--out", str(blocks_path)])
blocks: list[dict[str, Any]] = json.loads(blocks_path.read_text(encoding="utf-8"))["blocks"]
self.run_ok(kpack.main, ["skeleton", "--sources", str(self.root), "--work", str(self.work)])
plan, rapport, kuber = "plan.docx.md", "rapport.pdf.md", "kuber.xlsx.md"
doc = {"fragments": [
self.frag(blocks, "Årlig plan", plan, "Kompost vendes hver uke.", "Kompost vendes hver uke.", False),
self.frag(blocks, "Honning", rapport, "Side to om honning", "Side to om honning.", True),
self.frag(blocks, "Bikuber", kuber, "B: 12", "Det står 12 ved bikuber.", True),
], "main_pages": [{"source": plan, "concept": "Årlig plan"}, {"source": rapport, "concept": "Honning"},
{"source": kuber, "concept": "Bikuber"}],
"homes": []}
out = self.work / "K-fragments" / "pack-1.json"
out.parent.mkdir(parents=True)
out.write_text(json.dumps(doc, ensure_ascii=False), encoding="utf-8")
common = ["--blocks", str(blocks_path), "--sources", str(self.root), "--work", str(self.work)]
self.run_ok(kpack.main, ["pack", "1", *common])
self.run_ok(kpack.main, ["accept", "1", *common])
self.run_ok(kassemble.main, ["build", "--work", str(self.work), "--sources", str(self.root), "--out",
str(self.wiki)])
honning = (self.wiki / "honning.md").read_text(encoding="utf-8")
self.assertIn(" x-locator:\n line: 3\n page: 2\n", honning)
self.assertRegex(honning, r"\]: Rapport\.pdf, page 2\n")
self.assertRegex((self.wiki / "aarlig-plan.md").read_text(encoding="utf-8"), r"\]: Plan\.docx, paragraph 3\n")
# a spreadsheet row is one line; the footnote names the cell the quote stands in
bikuber = (self.wiki / "bikuber.md").read_text(encoding="utf-8")
self.assertIn(" x-locator:\n line: 3\n sheet: Plan\n cell: B1\n", bikuber)
self.assertRegex(bikuber, r"\]: Kuber\.xlsx, sheet Plan, cell B1\n")
report = json.loads(self.run_ok(audit.main, [str(self.wiki), str(self.root)]))
self.assertEqual(report["findings"], [])
self.assertEqual(report["link_rule"]["main_pages"], {"pass": 3, "of": 3})
# a cell outside the quoted row is caught
(self.wiki / "bikuber.md").write_text(bikuber.replace("cell: B1", "cell: B3"), encoding="utf-8")
report = json.loads(quiet(audit.main, [str(self.wiki), str(self.root)])[1])
self.assertEqual([f["check"] for f in report["findings"]], ["quote-wrong-place"])
(self.wiki / "bikuber.md").write_text(bikuber, encoding="utf-8")
# a place that does not match the frozen copy's places is caught
page = self.wiki / "honning.md"
page.write_text(honning.replace("page: 2", "page: 1").replace("page 2", "page 1"), encoding="utf-8")
report = json.loads(quiet(audit.main, [str(self.wiki), str(self.root)])[1])
self.assertEqual([f["check"] for f in report["findings"]], ["quote-wrong-place"])
page.write_text(honning, encoding="utf-8")
# the view names the page and opens the original at it
html = self.tmp / "html"
self.run_ok(view.main, ["render", str(self.wiki), str(self.root), "--out", str(html)])
shown = (html / "honning.html").read_text(encoding="utf-8")
self.assertIn("page 2", shown)
self.assertIn('href="original/rapport.pdf#page=2"', shown)
self.assertEqual((html / "original" / "rapport.pdf").read_bytes(), (folder / "Rapport.pdf").read_bytes())
if __name__ == "__main__":
unittest.main()