test_idempotency.py
file-level
1
files
1
commits
0
hotspots
0
🧊 dead
0
💥 blast risk
| 1 | """Data-integrity tests for idempotency and atomic writes.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | from pathlib import Path |
| 6 | from unittest.mock import patch |
| 7 | |
| 8 | from cli.digest import FootprintRecord, compute_footprint_digest, sha256_hex |
| 9 | from cli.footprint import resolve_footprint |
| 10 | from cli.kit_root import kit_root |
| 11 | from cli.version_lock import read_version_lock |
| 12 | from tests.support import load_fixture_config, run_cli |
| 13 | |
| 14 | |
| 15 | def test_init_twice_identical_lock(tmp_path: Path) -> None: |
| 16 | run_cli(["init", "--regime", "git-only", "--non-interactive"], cwd=tmp_path) |
| 17 | lock1 = read_version_lock(tmp_path / ".overseer" / "version.lock") |
| 18 | run_cli(["init", "--regime", "git-only", "--non-interactive"], cwd=tmp_path) |
| 19 | lock2 = read_version_lock(tmp_path / ".overseer" / "version.lock") |
| 20 | assert lock1.installed_at == lock2.installed_at |
| 21 | assert lock1.footprint_digest == lock2.footprint_digest |
| 22 | |
| 23 | |
| 24 | def test_footprint_digest_matches_reference(tmp_path: Path) -> None: |
| 25 | run_cli(["init", "--regime", "git-only", "--non-interactive"], cwd=tmp_path) |
| 26 | from adapters.config import load_config |
| 27 | |
| 28 | config = load_config(tmp_path / ".overseer" / "config.yaml") |
| 29 | rendered = resolve_footprint(config, kit=kit_root()) |
| 30 | records = [] |
| 31 | for item in rendered: |
| 32 | on_disk = (tmp_path / item.destination).read_bytes() |
| 33 | records.append(FootprintRecord(item.destination, sha256_hex(on_disk))) |
| 34 | expected = compute_footprint_digest(records) |
| 35 | lock = read_version_lock(tmp_path / ".overseer" / "version.lock") |
| 36 | assert lock.footprint_digest == expected |
| 37 | |
| 38 | |
| 39 | def test_write_failure_leaves_lock_unchanged(tmp_path: Path) -> None: |
| 40 | run_cli(["init", "--regime", "git-only", "--non-interactive"], cwd=tmp_path) |
| 41 | before = read_version_lock(tmp_path / ".overseer" / "version.lock") |
| 42 | calls = {"n": 0} |
| 43 | |
| 44 | def flaky_write(path: Path, data: bytes, *, destination: str) -> None: |
| 45 | calls["n"] += 1 |
| 46 | if calls["n"] == 2: |
| 47 | from cli.atomic import WriteFailure |
| 48 | |
| 49 | raise WriteFailure(path, OSError("simulated failure")) |
| 50 | from cli.footprint_writes import write_footprint_bytes as real |
| 51 | |
| 52 | real(path, data, destination=destination) |
| 53 | |
| 54 | with patch("cli.commands.init.write_footprint_bytes", side_effect=flaky_write): |
| 55 | code = run_cli( |
| 56 | ["init", "--regime", "git-only", "--non-interactive", "--force"], |
| 57 | cwd=tmp_path, |
| 58 | ) |
| 59 | assert code == 5 |
| 60 | after = read_version_lock(tmp_path / ".overseer" / "version.lock") |
| 61 | assert after.footprint_digest == before.footprint_digest |
| 62 | |
| 63 | |
| 64 | def test_dry_run_writes_zero_bytes(tmp_path: Path) -> None: |
| 65 | before = set(tmp_path.rglob("*")) |
| 66 | run_cli( |
| 67 | ["init", "--regime", "git-only", "--non-interactive", "--dry-run"], |
| 68 | cwd=tmp_path, |
| 69 | ) |
| 70 | after = set(tmp_path.rglob("*")) |
| 71 | assert before == after |