okf-wiki/tests/test_contract_marks_view.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

102 lines
5.1 KiB
Python

"""Contract K4 across the merge of wave 2: the real `okfwiki.sources marks` output fed to the real `view render --marks`.
Order 8 wrote marks against K4 and order 7 wrote the view against a file in K4's form; neither saw the other's code.
This test runs both commands as a user does, on two synthetic K1 roots (one changed, one gone, one unchanged source)
and a collection of three pages, one per kind. The denominator comes from this file: 2 marked pages, 1 unmarked.
Run: python3 -m unittest discover -s tests -p 'test_contract_marks_view.py'
"""
from __future__ import annotations
import hashlib
import json
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
from typing import Any
ROOT = Path(__file__).resolve().parents[1]
OLD = {"docs-en-bread.md": "# Bread\n\nFlour, water and salt.\n",
"docs-en-compost.md": "# Compost\n\nTurn the heap every week.\n",
"docs-en-spade.md": "# Spade\n\nDig before the frost.\n"}
NEW = {"docs-en-bread.md": OLD["docs-en-bread.md"],
"docs-en-compost.md": "# Compost\n\nTurn the heap every second week.\n"}
def write_root(root: Path, files: dict[str, str], frozen_at: str) -> None:
(root / "kilde").mkdir(parents=True)
entries = []
for name, text in sorted(files.items()):
data = text.encode("utf-8")
(root / "kilde" / name).write_bytes(data)
entries.append({"bytes": len(data), "kind": "docs", "lines": len(text.splitlines()), "main_topic": True,
"path": f"kilde/{name}", "sha256": hashlib.sha256(data).hexdigest(),
"url": "https://example.org/docs/en/" + name})
doc = {"collection": "garden", "frozen_at": frozen_at, "schema": "okf-wiki/source-manifest/1",
"sources": entries, "subset_of": None}
(root / "source-manifest.json").write_text(json.dumps(doc, indent=1, sort_keys=True) + "\n", encoding="utf-8")
def page(title: str, source: str, quote: str) -> str:
return (f"---\ntype: Concept\ntitle: {title}\ndescription: About {title.lower()}.\nsources:\n"
f" - id: s-1\n resource: /kilde/{source}\n title: Notes\n x-locator: {{ line: 3 }}\n"
f" x-quote: '{quote}'\n"
f"generated: {{ by: claude-code/claude-opus-5-5, at: 2026-10-08T09:00:00Z }}\n---\n\n# {title}\n\n"
f"{quote}[^s-1]\n\n[^s-1]: Notes, line 3\n")
PAGES = {"bread.md": page("Bread", "docs-en-bread.md", "Flour, water and salt."),
"compost.md": page("Compost", "docs-en-compost.md", "Turn the heap every week."),
"spade.md": page("Spade", "docs-en-spade.md", "Dig before the frost."),
"index.md": "---\ntype: Index\n---\n\n# Garden\n\n- [Bread](bread.md)\n- [Compost](compost.md)\n"}
class MarksIntoViewContract(unittest.TestCase):
def setUp(self) -> None:
self._tmp = tempfile.TemporaryDirectory()
self.tmp = Path(self._tmp.name)
write_root(self.tmp / "old", OLD, "2026-10-08T06:30:00Z")
write_root(self.tmp / "new", NEW, "2026-10-09T06:30:00Z")
(self.tmp / "wiki").mkdir()
for name, text in PAGES.items():
(self.tmp / "wiki" / name).write_text(text, encoding="utf-8")
def tearDown(self) -> None:
self._tmp.cleanup()
def run_cli(self, module: str, *args: str) -> subprocess.CompletedProcess[bytes]:
return subprocess.run([sys.executable, "-m", module, *args], cwd=ROOT, capture_output=True, timeout=120)
def render(self) -> Path:
diff = self.run_cli("okfwiki.sources", "diff", str(self.tmp / "old"), str(self.tmp / "new"))
self.assertEqual(1, diff.returncode, diff.stderr.decode())
(self.tmp / "diff.json").write_bytes(diff.stdout)
marks = self.run_cli("okfwiki.sources", "marks", str(self.tmp / "wiki"), "--diff", str(self.tmp / "diff.json"))
self.assertEqual(1, marks.returncode, marks.stderr.decode())
(self.tmp / "marks.json").write_bytes(marks.stdout)
out = self.tmp / "out"
view = self.run_cli("okfwiki.view", "render", str(self.tmp / "wiki"), str(self.tmp / "old"), "--out", str(out),
"--marks", str(self.tmp / "marks.json"))
self.assertEqual(0, view.returncode, view.stderr.decode())
return out
def test_marks_names_the_two_pages_on_changed_or_gone_sources(self) -> None:
self.render()
marks: dict[str, Any] = json.loads((self.tmp / "marks.json").read_text(encoding="utf-8"))
self.assertEqual(["compost.md", "spade.md"], [p["page"] for p in marks["pages"]])
def test_the_view_marks_exactly_the_pages_marks_names(self) -> None:
out = self.render()
html = {n: (out / f"{n}.html").read_text(encoding="utf-8") for n in ("bread", "compost", "spade")}
self.assertNotIn('class="source-mark"', html["bread"])
self.assertRegex(html["compost"], r"/kilde/docs-en-compost\.md</code>[^<]*changed")
self.assertRegex(html["spade"], r"/kilde/docs-en-spade\.md</code>[^<]*gone")
index = (out / "index.html").read_text(encoding="utf-8")
self.assertIn("Pages built on changed or gone sources (2)", index)
if __name__ == "__main__":
unittest.main()