test_governance_idempotency.py python
76 lines 2.4 KB
Raw
sha256:4671b7f787ddbe63ced31c895b688c77ab495653b65a730b423329f26b3c1439 feat: K1-P1 complete — agent provenance, build-verification… Sonnet 4.6 patch 62 days ago
1 """Data-integrity tests for governance-sync idempotency (§8 data-integrity tier)."""
2
3 from __future__ import annotations
4
5 import json
6 from pathlib import Path
7 from unittest.mock import patch
8
9 from cli.kit_root import kit_root
10 from tests.support import FIXTURES, ok, make_runner, run_cli, write_config
11
12
13 def _seed(tmp_path: Path) -> None:
14 write_config(tmp_path, "config-git-only.yaml")
15 docs = tmp_path / "docs"
16 docs.mkdir(parents=True, exist_ok=True)
17 (docs / "OVERSEER-HANDOVER.md").write_text(
18 (FIXTURES / "governance-handover-drift.md").read_text(encoding="utf-8"),
19 encoding="utf-8",
20 )
21 (docs / "ROADMAP.md").write_text(
22 (FIXTURES / "governance-roadmap-drift.md").read_text(encoding="utf-8"),
23 encoding="utf-8",
24 )
25
26
27 def _runner(tmp_path: Path):
28 return make_runner(
29 {
30 "git rev-parse --abbrev-ref HEAD": ok("main"),
31 "git status --porcelain": ok(""),
32 "git rev-parse origin/main": ok("cafebabe"),
33 "gh pr list": ok("[]"),
34 "git remote get-url origin": ok("[email protected]:owner/repo.git"),
35 }
36 )
37
38
39 def test_governance_sync_second_run_aligned(tmp_path: Path) -> None:
40 _seed(tmp_path)
41 runner = _runner(tmp_path)
42 code1 = run_cli(["governance-sync"], cwd=tmp_path, runner=runner, kit=kit_root())
43 assert code1 == 0
44 handover = (FIXTURES / "governance-handover-drift.md").read_text(encoding="utf-8").replace(
45 "deadbeef",
46 "cafebabe",
47 )
48 (tmp_path / "docs" / "OVERSEER-HANDOVER.md").write_text(handover, encoding="utf-8")
49 code2 = run_cli(["governance-sync"], cwd=tmp_path, runner=runner, kit=kit_root())
50 assert code2 == 0
51
52
53 def test_mid_apply_failure_leaves_no_commit(tmp_path: Path) -> None:
54 _seed(tmp_path)
55 runner = _runner(tmp_path)
56 calls = {"n": 0}
57
58 def flaky_write(path: Path, text: str) -> None:
59 calls["n"] += 1
60 if calls["n"] == 2:
61 from cli.atomic import WriteFailure
62
63 raise WriteFailure(path, OSError("simulated"))
64 from cli.atomic import atomic_write_text as real
65
66 real(path, text)
67
68 with patch("tools.governance_hygiene.engine.atomic_write_text", side_effect=flaky_write):
69 code = run_cli(
70 ["governance-sync", "--write"],
71 cwd=tmp_path,
72 runner=runner,
73 kit=kit_root(),
74 )
75 assert code == 5
76 assert not (tmp_path / ".overseer" / "last_governance_sync").exists()
File History 1 commit
sha256:4671b7f787ddbe63ced31c895b688c77ab495653b65a730b423329f26b3c1439 feat: K1-P1 complete — agent provenance, build-verification… Sonnet 4.6 patch 62 days ago