from __future__ import annotations import subprocess from pathlib import Path from repo_manager.cache import build_closed_provenance_export, cache_status from repo_manager.index_store import save_index from repo_manager.observe import observe_repository def _git(repo: Path, *args: str) -> None: subprocess.run(["git", *args], cwd=repo, check=True, capture_output=True) def _repo(tmp_path: Path) -> Path: repo = tmp_path / "example" (repo / "workplans").mkdir(parents=True) _git(repo, "init") _git(repo, "config", "user.email", "test@example.com") _git(repo, "config", "user.name", "Test") (repo / "workplans" / "EX-WP-0001.md").write_text( "---\nid: EX-WP-0001\ntitle: Example\nstatus: active\n---\n", encoding="utf-8", ) _git(repo, "add", ".") _git(repo, "commit", "-m", "seed") return repo def test_cache_status_reports_age_and_detects_source_drift(tmp_path: Path) -> None: repo = _repo(tmp_path) missing = cache_status(repo) assert missing["cache_present"] is False assert missing["stale_reasons"] == ["cache_missing"] _snapshot, index = observe_repository(repo) save_index(index) fresh = cache_status(repo) assert fresh["advisory"] is True assert fresh["stale"] is False assert fresh["age_seconds"] >= 0 path = repo / "workplans" / "EX-WP-0001.md" path.write_text(path.read_text(encoding="utf-8") + "\nChanged.\n", encoding="utf-8") stale = cache_status(repo) assert stale["stale"] is True assert stale["stale_reasons"] == ["authoritative_source_changed"] def test_closed_export_excludes_live_and_bound_rows() -> None: workplans = [ {"id": "1", "repo_id": "r1", "slug": "closed", "status": "finished"}, {"id": "2", "repo_id": "r1", "slug": "live", "status": "active"}, { "id": "3", "repo_id": "r1", "slug": "bound", "status": "archived", "backing_filename": "bound.md", }, ] result = build_closed_provenance_export( workplans, [{"id": "r1", "slug": "example"}], source="http://state-hub.test", ) assert result["count"] == 1 assert result["rows"][0]["slug"] == "closed" assert result["rows"][0]["repo_slug"] == "example" assert len(result["rows_sha256"]) == 64