"""Cutover-layout 하드닝 회귀 모음. vault canonical cutover 이후 발견된 '조용한 검사기 / 심링크 맹점' 계열 결함들의 회귀를 막는다. 각 테스트는 고친 결함이 되살아나면 실패한다. """ from __future__ import annotations import json from pathlib import Path import sys import tempfile import unittest RUNTIME = Path(__file__).resolve().parents[1] / "runtime" sys.path.insert(0, str(RUNTIME)) import proof_manifest # noqa: E402 import source_hygiene # noqa: E402 import semantic_surface_extractor as sse # noqa: E402 import semantic_certificate as sc # noqa: E402 import typed_contract_check # noqa: E402 class ProofManifestSymlinkTest(unittest.TestCase): """#22 — ``_atomic_write_json`` 의 os.replace 가 심링크를 실파일로 갈아치우면 안 된다.""" def test_output_writes_through_symlink_and_keeps_link(self) -> None: with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) (root / "vault").mkdir() canonical = root / "vault" / "manifest.json" canonical.write_text("{}\n", encoding="utf-8") link = root / "manifest.json" link.symlink_to("vault/manifest.json") proof_manifest._atomic_write_json(link, {"schema_version": "x", "status": "PASS"}) self.assertTrue(link.is_symlink(), "심링크가 실파일로 대체되면 안 된다") self.assertIn("PASS", canonical.read_text(encoding="utf-8")) class SourceHygieneContentRootsTest(unittest.TestCase): """#21 — 콘텐츠 스캔 루트를 'vault' 하드코딩이 아니라 레이아웃에서 도출한다.""" def test_fallback_prefers_vault_when_present(self) -> None: with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) (root / "vault").mkdir() roots = source_hygiene._content_roots(root) self.assertEqual(roots, [root / "vault"]) def test_fallback_uses_legacy_roots_when_no_vault(self) -> None: with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) (root / "raw").mkdir() (root / "wiki").mkdir() roots = set(source_hygiene._content_roots(root)) self.assertEqual(roots, {root / "raw", root / "wiki"}) class ZeroDocumentGuardTest(unittest.TestCase): """#10 — 자동 탐색이 0건이면 '검사할 게 없어 PASS' 로 조용히 넘어가면 안 된다.""" def test_surface_extractor_auto_zero_fails(self) -> None: original = sse.eligible_documents sse.eligible_documents = lambda *a, **k: () try: result = sse.check(Path(".")) codes = {item["code"] for item in result["findings"]} self.assertEqual(result["status"], "FAIL") self.assertIn("NO_ELIGIBLE_DOCUMENTS", codes) finally: sse.eligible_documents = original def test_surface_extractor_explicit_empty_is_vacuous_pass(self) -> None: result = sse.check(Path("."), paths=[]) self.assertEqual(result["status"], "PASS") self.assertEqual(result["document_count"], 0) def test_certificate_auto_zero_fails(self) -> None: original = sse.eligible_documents sse.eligible_documents = lambda *a, **k: () try: result = sc.check(Path(".")) codes = {item["code"] for item in result["findings"]} self.assertEqual(result["status"], "FAIL") self.assertIn("NO_REQUIRED_DOCUMENTS", codes) finally: sse.eligible_documents = original class TypedContractRecursiveScanTest(unittest.TestCase): """#23 — document_roots 가 nested 트리를 가리켜도 문서를 놓치지 않는다(rglob).""" def test_nested_documents_are_discovered(self) -> None: with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) nested = root / "content" / "a" / "b" nested.mkdir(parents=True) (nested / "deep.md").write_text("---\ntitle: t\n---\n# deep\n", encoding="utf-8") (root / "content" / "top.md").write_text("---\ntitle: t\n---\n# top\n", encoding="utf-8") config = {"document_roots": ["content"]} docs = typed_contract_check._documents(root, config) slugs = {d.slug for d in docs} self.assertIn("deep", slugs, "nested 문서를 non-recursive glob 이 놓치면 안 된다") self.assertIn("top", slugs) if __name__ == "__main__": unittest.main()