"""Garde-fous du bundle docs d'aide (issue #79). Le générateur scripts/bundle_docs.py publie les sources Markdown de docs/aide/ dans Nexus (docs/obsigate// + manifest.json) ; le Hub et l'agent Spark consomment ce manifeste. Ces tests figent le contrat : structure du manifeste, empreintes sha256 réelles, titres extraits (front-matter puis H1), déterminisme octet pour octet et auto-vérification du bundle écrit. """ from __future__ import annotations import hashlib import importlib.util import json from pathlib import Path REPO_ROOT = Path(__file__).resolve().parent.parent SCRIPT = REPO_ROOT / "scripts" / "bundle_docs.py" AIDE = REPO_ROOT / "docs" / "aide" def _charger(): spec = importlib.util.spec_from_file_location("bundle_docs", SCRIPT) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module def _sources(tmp_path: Path) -> Path: content = tmp_path / "aide" content.mkdir() (content / "prise-en-main.md").write_text( "# ObsiGate — prise en main\n\nCorps.\n", encoding="utf-8" ) (content / "guide.md").write_text( "---\ntitle: Guide complet\n---\n# Titre ignoré\n\nCorps.\n", encoding="utf-8", ) sous = content / "avance" sous.mkdir() (sous / "sans-titre.md").write_text("Pas de titre ici.\n", encoding="utf-8") return content class TestBundleDocs: def test_manifeste_structure_et_empreintes(self, tmp_path): mod = _charger() content = _sources(tmp_path) out = tmp_path / "bundle" manifest = mod.construire(content, "9.9.9", out) assert manifest["produit"] == "obsigate" assert manifest["version"] == "9.9.9" chemins = [p["chemin"] for p in manifest["pages"]] assert chemins == sorted(chemins) # tri stable par chemin assert chemins == ["avance/sans-titre.md", "guide.md", "prise-en-main.md"] for page in manifest["pages"]: assert page["slug"] == page["chemin"][: -len(".md")] data = (out / page["chemin"]).read_bytes() assert hashlib.sha256(data).hexdigest() == page["sha256"] # Copie octet-identique à la source. assert data == (content / page["chemin"]).read_bytes() relu = json.loads((out / "manifest.json").read_text(encoding="utf-8")) assert relu == manifest def test_titres_front_matter_puis_h1_puis_slug(self, tmp_path): mod = _charger() manifest = mod.construire(_sources(tmp_path), "9.9.9", tmp_path / "b") titres = {p["slug"]: p["titre"] for p in manifest["pages"]} assert titres["guide"] == "Guide complet" # front-matter prioritaire assert titres["prise-en-main"] == "ObsiGate — prise en main" # H1 assert titres["avance/sans-titre"] == "avance/sans-titre" # repli slug def test_determinisme(self, tmp_path): mod = _charger() content = _sources(tmp_path) m1 = mod.construire(content, "9.9.9", tmp_path / "b1") m2 = mod.construire(content, "9.9.9", tmp_path / "b2") assert m1 == m2 assert (tmp_path / "b1" / "manifest.json").read_bytes() == ( tmp_path / "b2" / "manifest.json" ).read_bytes() def test_auto_verification_detecte_une_alteration(self, tmp_path): mod = _charger() content = _sources(tmp_path) out = tmp_path / "bundle" manifest = mod.construire(content, "9.9.9", out) mod.verifier(out, manifest) # bundle sain : ne lève pas (out / "guide.md").write_text("altéré\n", encoding="utf-8") try: mod.verifier(out, manifest) except SystemExit: pass else: raise AssertionError("verifier() aurait dû rejeter le bundle altéré") def test_sources_reelles_du_depot(self, tmp_path): """Les vraies sources docs/aide/ produisent un bundle valide, dont la page historique rapatriée du Hub.""" mod = _charger() manifest = mod.construire(AIDE, "0.0.0", tmp_path / "bundle") slugs = [p["slug"] for p in manifest["pages"]] assert "prise-en-main" in slugs mod.verifier(tmp_path / "bundle", manifest)