gabriel / muse public
test_release_analysis.py python
164 lines 5.8 KB
Raw
sha256:660fcac1df3ab28f61862e961890bd2ca8b754fa0242079d93ca1e25037ec8a6 chore(tests): add docstring to tests/__init__.py so rc14 tr… Human 26 days 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.snapshot import compute_commit_id, compute_snapshot_id
12 from muse.core.store import (
13 ChangelogEntry,
14 CommitRecord,
15 ReleaseRecord,
16 SemanticReleaseReport,
17 SemVerTag,
18 SnapshotRecord,
19 write_commit,
20 write_release,
21 write_snapshot,
22 )
23 from muse.plugins.code.release_analysis import _empty_report, compute_release_analysis
24 from muse.core._types import Manifest
25
26
27 # ---------------------------------------------------------------------------
28 # Fixtures
29 # ---------------------------------------------------------------------------
30
31
32 @pytest.fixture()
33 def repo(tmp_path: pathlib.Path) -> pathlib.Path:
34 """Minimal repo layout: .muse/commits/, snapshots/, objects/, releases/."""
35 muse = tmp_path / ".muse"
36 for sub in ("commits", "snapshots", "objects", "releases", "refs", "refs/heads"):
37 (muse / sub).mkdir(parents=True, exist_ok=True)
38 (muse / "HEAD").write_text("ref: refs/heads/main\n")
39 repo_id = str(uuid.uuid4())
40 import json
41 (muse / "repo.json").write_text(json.dumps({"repo_id": repo_id}))
42 return tmp_path
43
44
45 def _make_release(repo_root: pathlib.Path, tag: str = "v1.0.0") -> ReleaseRecord:
46 import json
47 muse = repo_root / ".muse"
48 repo_id = json.loads((muse / "repo.json").read_text())["repo_id"]
49 manifest: Manifest = {}
50 snap_id = compute_snapshot_id(manifest)
51 write_snapshot(repo_root, SnapshotRecord(snapshot_id=snap_id, manifest=manifest))
52 committed_at = datetime.now(timezone.utc)
53 commit_id = compute_commit_id(
54 parent_ids=[],
55 snapshot_id=snap_id,
56 message="initial",
57 committed_at_iso=committed_at.isoformat(),
58 )
59 write_commit(repo_root, CommitRecord(
60 commit_id=commit_id,
61 repo_id=repo_id,
62 branch="main",
63 snapshot_id=snap_id,
64 message="initial",
65 committed_at=committed_at,
66 sem_ver_bump="minor",
67 ))
68 semver = SemVerTag(major=1, minor=0, patch=0, pre="", build="")
69 changelog: list[ChangelogEntry] = [
70 ChangelogEntry(
71 commit_id=commit_id,
72 message="initial",
73 sem_ver_bump="minor",
74 breaking_changes=[],
75 author="gabriel",
76 committed_at=datetime.now(timezone.utc).isoformat(),
77 agent_id="",
78 model_id="",
79 )
80 ]
81 return ReleaseRecord(
82 release_id=str(uuid.uuid4()),
83 repo_id=repo_id,
84 tag=tag,
85 semver=semver,
86 channel="stable",
87 commit_id=commit_id,
88 snapshot_id=snap_id,
89 title="Test release",
90 body="",
91 changelog=changelog,
92 )
93
94
95 # ---------------------------------------------------------------------------
96 # Tests
97 # ---------------------------------------------------------------------------
98
99
100 class TestEmptyReport:
101 def test_empty_report_has_all_keys(self) -> None:
102 report = _empty_report()
103 assert report["languages"] == []
104 assert report["total_files"] == 0
105 assert report["total_symbols"] == 0
106 assert report["api_added"] == []
107 assert report["human_commits"] == 0
108
109
110 class TestComputeReleaseAnalysis:
111 def test_returns_semantic_report_shape(self, repo: pathlib.Path) -> None:
112 release = _make_release(repo)
113 report = compute_release_analysis(repo, release)
114 # Must return a dict with the expected keys.
115 assert isinstance(report, dict)
116 required = {
117 "languages", "total_files", "semantic_files", "total_symbols",
118 "symbols_by_kind", "files_changed", "api_added", "api_removed",
119 "api_modified", "file_hotspots", "refactor_events",
120 "breaking_changes", "human_commits", "agent_commits",
121 "unique_agents", "unique_models", "reviewers",
122 }
123 assert required.issubset(report.keys())
124
125 def test_empty_manifest_yields_zero_symbols(self, repo: pathlib.Path) -> None:
126 release = _make_release(repo)
127 report = compute_release_analysis(repo, release)
128 assert report["total_symbols"] == 0
129 assert report["total_files"] == 0
130 assert report["languages"] == []
131
132 def test_human_commit_counted(self, repo: pathlib.Path) -> None:
133 release = _make_release(repo)
134 # changelog has one entry with no agent_id → human commit
135 report = compute_release_analysis(repo, release)
136 assert report["human_commits"] == 1
137 assert report["agent_commits"] == 0
138
139 def test_agent_commit_counted(self, repo: pathlib.Path) -> None:
140 release = _make_release(repo)
141 release.changelog[0]["agent_id"] = "code-bot"
142 release.changelog[0]["model_id"] = "claude-opus-4"
143 report = compute_release_analysis(repo, release)
144 assert report["agent_commits"] == 1
145 assert report["human_commits"] == 0
146 assert report["unique_agents"] == ["code-bot"]
147 assert report["unique_models"] == ["claude-opus-4"]
148
149 def test_missing_snapshot_returns_empty_report(self, repo: pathlib.Path) -> None:
150 release = _make_release(repo)
151 release.snapshot_id = "c" * 64 # nonexistent snapshot
152 report = compute_release_analysis(repo, release)
153 assert report["total_files"] == 0
154 assert report["languages"] == []
155
156 def test_exception_in_analysis_returns_empty_report(self, repo: pathlib.Path) -> None:
157 """Even if analysis explodes, push must not fail."""
158 release = _make_release(repo)
159 # Corrupt the snapshot file so _compute raises.
160 snap_file = repo / ".muse" / "snapshots" / f"{release.snapshot_id}.json"
161 snap_file.write_text("not valid json {{{")
162 report = compute_release_analysis(repo, release)
163 assert isinstance(report, dict)
164
File History 5 commits
sha256:660fcac1df3ab28f61862e961890bd2ca8b754fa0242079d93ca1e25037ec8a6 chore(tests): add docstring to tests/__init__.py so rc14 tr… Human 26 days ago
sha256:d8316ffae901be06347e16ab55be11868eb519dd16ade3e8aa16a99e662f7e62 baseline: rc14 re-baseline after rc3 store corruption recovery Human patch 26 days ago
sha256:50d413a635f57761c3fce1f70251b957b6423821525bf330747ef4007339222e Merge branch 'dev' into main Human 29 days ago
sha256:fb67fed5a4d3e40de84bdd163de94ef1386570bef1dd1a020a732c8a038962ce Merge branch 'dev' into main Human 48 days ago
sha256:1c4b3e3a9a1f300774c3ee662b572a698d5fd405bf765a71e6011a2e9c3eaaaa feat: Muse — version control for the agent era Human 101 days ago