gabriel / muse public
test_release_analysis.py python
183 lines 6.6 KB
e74bbfd6 chore: ignore .hypothesis, .pytest_cache, .mypy_cache, .ruff_cache; add… gabriel 8h ago
1 """Tests for muse.plugins.code.release_analysis.compute_release_analysis."""
2
3 from __future__ import annotations
4
5 import pathlib
6 import uuid
7 from datetime import datetime, timezone
8
9 import pytest
10
11 from muse.core.store import (
12 ChangelogEntry,
13 ReleaseRecord,
14 SemanticReleaseReport,
15 SemVerTag,
16 write_release,
17 )
18 from muse.plugins.code.release_analysis import _empty_report, compute_release_analysis
19
20
21 # ---------------------------------------------------------------------------
22 # Fixtures
23 # ---------------------------------------------------------------------------
24
25
26 @pytest.fixture()
27 def repo(tmp_path: pathlib.Path) -> pathlib.Path:
28 """Minimal repo layout: .muse/commits/, snapshots/, objects/, releases/."""
29 muse = tmp_path / ".muse"
30 for sub in ("commits", "snapshots", "objects", "releases", "refs", "refs/heads"):
31 (muse / sub).mkdir(parents=True, exist_ok=True)
32 (muse / "HEAD").write_text("ref: refs/heads/main\n")
33 repo_id = str(uuid.uuid4())
34 import json
35 (muse / "repo.json").write_text(json.dumps({"repo_id": repo_id}))
36 return tmp_path
37
38
39 def _make_release(repo_root: pathlib.Path, tag: str = "v1.0.0") -> ReleaseRecord:
40 import json
41 muse = repo_root / ".muse"
42 repo_id = json.loads((muse / "repo.json").read_text())["repo_id"]
43 snap_id = "a" * 64
44 # Write a minimal snapshot.
45 snap_dir = muse / "snapshots"
46 snap_dir.mkdir(exist_ok=True)
47 snap_file = snap_dir / f"{snap_id}.json"
48 snap_file.write_text(json.dumps({
49 "snapshot_id": snap_id,
50 "manifest": {},
51 "created_at": datetime.now(timezone.utc).isoformat(),
52 }))
53 # Write a minimal commit.
54 commit_id = "b" * 64
55 commit_dir = muse / "commits"
56 commit_dir.mkdir(exist_ok=True)
57 (commit_dir / f"{commit_id}.json").write_text(json.dumps({
58 "commit_id": commit_id,
59 "repo_id": repo_id,
60 "branch": "main",
61 "snapshot_id": snap_id,
62 "message": "initial",
63 "committed_at": datetime.now(timezone.utc).isoformat(),
64 "parent_commit_id": None,
65 "sem_ver_bump": "minor",
66 "breaking_changes": [],
67 "agent_id": "",
68 "model_id": "",
69 "format_version": 5,
70 "reviewed_by": [],
71 "test_runs": 0,
72 }))
73 semver = SemVerTag(major=1, minor=0, patch=0, pre="", build="")
74 changelog: list[ChangelogEntry] = [
75 ChangelogEntry(
76 commit_id=commit_id,
77 message="initial",
78 sem_ver_bump="minor",
79 breaking_changes=[],
80 author="gabriel",
81 committed_at=datetime.now(timezone.utc).isoformat(),
82 agent_id="",
83 model_id="",
84 )
85 ]
86 return ReleaseRecord(
87 release_id=str(uuid.uuid4()),
88 repo_id=repo_id,
89 tag=tag,
90 semver=semver,
91 channel="stable",
92 commit_id=commit_id,
93 snapshot_id=snap_id,
94 title="Test release",
95 body="",
96 changelog=changelog,
97 )
98
99
100 # ---------------------------------------------------------------------------
101 # Tests
102 # ---------------------------------------------------------------------------
103
104
105 class TestEmptyReport:
106 def test_empty_report_has_all_keys(self) -> None:
107 report = _empty_report()
108 assert report["languages"] == []
109 assert report["total_files"] == 0
110 assert report["total_symbols"] == 0
111 assert report["api_added"] == []
112 assert report["human_commits"] == 0
113
114
115 class TestComputeReleaseAnalysis:
116 def test_returns_semantic_report_shape(self, repo: pathlib.Path) -> None:
117 release = _make_release(repo)
118 report = compute_release_analysis(repo, release)
119 # Must return a dict with the expected keys.
120 assert isinstance(report, dict)
121 required = {
122 "languages", "total_files", "semantic_files", "total_symbols",
123 "symbols_by_kind", "files_changed", "api_added", "api_removed",
124 "api_modified", "file_hotspots", "refactor_events",
125 "breaking_changes", "human_commits", "agent_commits",
126 "unique_agents", "unique_models", "reviewers",
127 }
128 assert required.issubset(report.keys())
129
130 def test_empty_manifest_yields_zero_symbols(self, repo: pathlib.Path) -> None:
131 release = _make_release(repo)
132 report = compute_release_analysis(repo, release)
133 assert report["total_symbols"] == 0
134 assert report["total_files"] == 0
135 assert report["languages"] == []
136
137 def test_human_commit_counted(self, repo: pathlib.Path) -> None:
138 release = _make_release(repo)
139 # changelog has one entry with no agent_id → human commit
140 report = compute_release_analysis(repo, release)
141 assert report["human_commits"] == 1
142 assert report["agent_commits"] == 0
143
144 def test_agent_commit_counted(self, repo: pathlib.Path) -> None:
145 release = _make_release(repo)
146 release.changelog[0]["agent_id"] = "code-bot"
147 release.changelog[0]["model_id"] = "claude-opus-4"
148 report = compute_release_analysis(repo, release)
149 assert report["agent_commits"] == 1
150 assert report["human_commits"] == 0
151 assert report["unique_agents"] == ["code-bot"]
152 assert report["unique_models"] == ["claude-opus-4"]
153
154 def test_missing_snapshot_returns_empty_report(self, repo: pathlib.Path) -> None:
155 release = _make_release(repo)
156 release.snapshot_id = "c" * 64 # nonexistent snapshot
157 report = compute_release_analysis(repo, release)
158 assert report["total_files"] == 0
159 assert report["languages"] == []
160
161 def test_exception_in_analysis_returns_empty_report(self, repo: pathlib.Path) -> None:
162 """Even if analysis explodes, push must not fail."""
163 release = _make_release(repo)
164 # Corrupt the snapshot file so _compute raises.
165 snap_file = repo / ".muse" / "snapshots" / f"{release.snapshot_id}.json"
166 snap_file.write_text("not valid json {{{")
167 report = compute_release_analysis(repo, release)
168 assert isinstance(report, dict)
169
170 def test_semantic_report_roundtrips_through_release_record(
171 self, repo: pathlib.Path
172 ) -> None:
173 """SemanticReleaseReport survives to_dict() / from_dict()."""
174 release = _make_release(repo)
175 report = compute_release_analysis(repo, release)
176 release.semantic_report = report
177
178 from muse.core.store import ReleaseRecord as RR
179 d = release.to_dict()
180 assert "semantic_report" in d
181 restored = RR.from_dict(d)
182 assert restored.semantic_report is not None
183 assert restored.semantic_report["total_files"] == report["total_files"]