"""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 Golden(unittest.TestCase): """The exact files and summary each exporter produces from the clean fixture. Content is not compared: OKF stamps the export date.""" def export(self, script, *args): with tempfile.TemporaryDirectory() as d: kb = Path(d) / "kb" shutil.copytree(str(FIX), str(kb)) r = run(kb, script, *args) self.assertEqual(r.returncode, 0, r.stdout + r.stderr) files = sorted(p.relative_to(kb / "outputs").as_posix() for p in (kb / "outputs").rglob("*") if p.is_file()) report = [l for l in r.stdout.splitlines() if not l.startswith("output:")] return files, report def expected(self, name): return (ROOT / "tests/expected" / name).read_text(encoding="utf-8").splitlines() def test_okf(self): files, report = self.export(OKF) self.assertEqual(files, self.expected("okf-files.txt")) self.assertEqual(report, self.expected("okf-report.txt")) def test_starlight(self): files, report = self.export(STARLIGHT, "--mode", "full", "--today", "2026-11-11") self.assertEqual(files, self.expected("starlight-files.txt")) self.assertEqual(report, self.expected("starlight-report.txt")) 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")) def test_bare_wikilinks_become_bundle_links(self): # A bare [[X]] used to degrade to plain text, losing the link. Path_ = self.okf.Path class P: def __init__(self, title): self.title = title pages = {Path_("entities/maria-nowak.md"): P("Maria Nowak"), Path_("topics/r&d plan.md"): P("R&D plan")} index = self.okf.build_link_index(pages) c = {"wikilinks": 0} out = self.okf.strip_wikilinks( "See [[entities/maria-nowak]], [[Maria Nowak|Maria]], [[maria-nowak#Role]], " "[[wiki/entities/maria-nowak.md]], [[R&D plan]] and [[nowhere]].", c, index) self.assertEqual(out, "See [entities/maria-nowak](/entities/maria-nowak.md), [Maria](/entities/maria-nowak.md), " "[maria-nowak#Role](/entities/maria-nowak.md#Role), " "[wiki/entities/maria-nowak.md](/entities/maria-nowak.md), " "[R&D plan](/topics/r%26d%20plan.md) and nowhere.") self.assertEqual(c["wikilinks"], 6) self.assertEqual(c["wikilinks_linked"], 5) # a dual link still keeps only its Markdown half self.assertEqual(self.okf.strip_wikilinks( "[[entities/maria-nowak]] / [Maria](/wiki/entities/maria-nowak.md)", {"wikilinks": 0}, index), "[Maria](/wiki/entities/maria-nowak.md)") def publish_filter(path): text = path.read_text(encoding="utf-8") start = text.index("# --- publish filter") return text[start:text.index("# --- end publish filter ---", start)] class PublishFilter(unittest.TestCase): """N7: an export publishes, so it fails closed. One planted leak of each kind must not reach either exporter's output.""" LEAKY = ("---\ntype: concept\ntldr: leak test\n---\n\n# Leaky\n\nVisible intro.\n" "\n" "Inline SECRET-PRIVATE text.\n\n" "```html\n\n```\n\n" "## Crux\n\n> SECRET-CRUX quote\n\n" "## Notes\n\nSECRET-NOTE\n\n" "## After\n\nStill here.\n", "Still here."): self.assertIn(kept, text) def test_okf(self): r = run(self.kb, OKF, "--drop-section", "Crux", "--drop-section", "notes") self.assertIn(r.returncode, (0, 1), r.stdout + r.stderr) self.check(self.kb / "outputs/okf/entities/leaky.md") def test_starlight(self): r = run(self.kb, STARLIGHT, "--mode", "content-only", "--today", "2026-11-23", "--drop-section", "Crux", "--drop-section", "Notes") self.assertEqual(r.returncode, 0, r.stdout + r.stderr) self.check(next((self.kb / "outputs/starlight").rglob("leaky.md"))) def test_unclosed_frontmatter_is_refused(self): (self.kb / "wiki/entities/broken.md").write_text( "---\ntype: concept\nowner_email: SECRET-FM\n\n# Broken\n", encoding="utf-8") for script, args in ((OKF, ()), (STARLIGHT, ("--mode", "content-only", "--today", "2026-11-23"))): r = run(self.kb, script, *args) self.assertEqual(r.returncode, 1, r.stdout + r.stderr) self.assertIn("REFUSED: entities/broken.md", r.stdout) for f in (self.kb / "outputs").rglob("*.md"): self.assertNotIn("SECRET-FM", f.read_text(encoding="utf-8"), f) if __name__ == "__main__": unittest.main()