ckb/tests/unit/test_exporters.py
Michał Kopeć 45d2830aed Template 2.0.5: routines, redaction, OKF quoting, Unicode search, low-severity fixes
Fixes F20-F24 from the 2.0.0 script review: zero-indent routine lists,
escaped --do values, --remove continuation lines; <private> redaction
across content blocks; OKF scalar quoting and code-fence titles; Unicode
tokens in retrieve and consolidate; bad workload dates, frontmatter at
EOF, `wiki/` refs without a slash, Starlight route collisions, labelled
wikilinks, string source_files, real splash sections, <private> dropped
from both exports.

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

90 lines
4.2 KiB
Python

"""The exporters delete and rewrite their output folder; they must never
reach outside it, whatever the page text or the --out flag says."""
import shutil
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
FIX = ROOT / "tests/fixtures/minimal-kb"
OKF = ROOT / ".agents/skills/ckb-export-okf/scripts/export_okf.py"
STARLIGHT = ROOT / ".agents/skills/ckb-export-starlight/scripts/export_starlight.py"
def run(cwd, script, *args):
return subprocess.run([sys.executable, str(script), *args], cwd=str(cwd),
capture_output=True, text=True, encoding="utf-8", errors="replace")
class ExportContainment(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.outer = Path(self.tmp.name)
self.kb = self.outer / "kb"
shutil.copytree(str(FIX), str(self.kb))
def tearDown(self):
self.tmp.cleanup()
def test_okf_refuses_out_outside_outputs(self):
for bad in (".", "wiki", "outputs", "..", str(self.outer)):
r = run(self.kb, OKF, "--out", bad)
self.assertEqual(r.returncode, 2, f"--out {bad}: {r.stdout}{r.stderr}")
self.assertTrue((self.kb / "wiki" / "index.md").is_file(), f"--out {bad} deleted the wiki")
self.assertTrue((self.kb / "ckb.yaml").is_file(), f"--out {bad} deleted the KB")
def test_okf_default_and_nested_out_work(self):
self.assertIn(run(self.kb, OKF).returncode, (0, 1))
self.assertTrue((self.kb / "outputs/okf").is_dir())
self.assertIn(run(self.kb, OKF, "--out", "outputs/okf-test").returncode, (0, 1))
self.assertTrue((self.kb / "outputs/okf-test").is_dir())
def test_okf_check_writes_nothing(self):
before = sorted(p.relative_to(self.kb) for p in self.kb.rglob("*"))
self.assertIn(run(self.kb, OKF, "--check").returncode, (0, 1))
self.assertEqual(sorted(p.relative_to(self.kb) for p in self.kb.rglob("*")), before)
def test_starlight_image_traversal_is_not_copied(self):
(self.outer / "secret.txt").write_text("do not export", encoding="utf-8")
assets = self.kb / "wiki" / "assets"
assets.mkdir(parents=True, exist_ok=True)
(assets / "ok.png").write_bytes(b"\x89PNG")
page = self.kb / "wiki" / "entities" / "pic.md"
page.parent.mkdir(parents=True, exist_ok=True)
page.write_text("---\ntype: entity\ntldr: pictures\n---\n\n# Pic\n\n"
"![ok](../assets/ok.png)\n\n![x](../assets/../../../secret.txt)\n"
"![y](../assets/../../../../escaped.txt)\n", encoding="utf-8")
r = run(self.kb, STARLIGHT, "--mode", "content-only", "--today", "2026-11-11")
self.assertEqual(r.returncode, 0, r.stdout + r.stderr)
out = self.kb / "outputs" / "starlight"
self.assertTrue((out / "public/assets/ok.png").is_file(), "a normal asset should still be copied")
leaked = [p for p in self.outer.rglob("secret.txt") if p != self.outer / "secret.txt"]
self.assertEqual(leaked, [], f"traversal copied the file to {leaked}")
self.assertIn("not copied", r.stderr)
class OkfScalars(unittest.TestCase):
@classmethod
def setUpClass(cls):
import importlib.util
spec = importlib.util.spec_from_file_location("export_okf", str(OKF))
cls.okf = importlib.util.module_from_spec(spec)
spec.loader.exec_module(cls.okf)
def test_values_a_yaml_reader_would_misread_are_quoted(self):
for v in ("See issue #42 for details", "- list?", "? key", "a: b", "end:", "#tag", " pad", "tab\there"):
out = self.okf.yaml_scalar(v)
self.assertTrue(out.startswith('"'), f"{v!r} -> {out!r}")
for v in ("Plain title", "C#", "issue#42", "-5", "0.8", "2026-11-11"):
self.assertEqual(self.okf.yaml_scalar(v), v)
def test_first_h1_ignores_code_fences(self):
body = "Intro\n\n```bash\n# install deps\nmake\n```\n\n# Real title\n"
self.assertEqual(self.okf.first_h1(body), "Real title")
self.assertIsNone(self.okf.first_h1("```\n# only in code\n```\n"))
if __name__ == "__main__":
unittest.main()