gabriel / muse public
test_release_analysis.py python
164 lines 5.8 KB
Raw
sha256:b636f72dcba9e190afb980bece906fa5b717fbde014b76ef023df8cb96e01eb9 docs: expand cache plan with all seven testing tiers and do… Sonnet 4.6 134 days ago
1 """Tests for muse.plugins.code.release_analysis.compute_release_analysis."""
2
3 from __future__ import annotations
4
5 import pathlib
6 from datetime import datetime, timezone
7
8 import pytest
9
10 from muse.core.snapshot import compute_commit_id, compute_snapshot_id
11 from muse.core.store import (
12 ChangelogEntry,
13 CommitRecord,
14 ReleaseRecord,
15 SemanticReleaseReport,
16 SemVerTag,
17 SnapshotRecord,
18 write_commit,
19 write_release,
20 write_snapshot,
21 )
22 from muse.plugins.code.release_analysis import _empty_report, compute_release_analysis
23 from muse.core._types import Manifest, fake_id, now_utc_iso
24
25
26 # ---------------------------------------------------------------------------
27 # Fixtures
28 # ---------------------------------------------------------------------------
29
30
31 @pytest.fixture()
32 def repo(tmp_path: pathlib.Path) -> pathlib.Path:
33 """Minimal repo layout: .muse/commits/, snapshots/, objects/, releases/."""
34 muse = tmp_path / ".muse"
35 for sub in ("commits", "snapshots", "objects", "releases", "refs", "refs/heads"):
36 (muse / sub).mkdir(parents=True, exist_ok=True)
37 (muse / "HEAD").write_text("ref: refs/heads/main\n")
38 repo_id = fake_id("repo")
39 import json
40 (muse / "repo.json").write_text(json.dumps({"repo_id": repo_id}))
41 return tmp_path
42
43
44 def _make_release(repo_root: pathlib.Path, tag: str = "v1.0.0") -> ReleaseRecord:
45 import json
46 muse = repo_root / ".muse"
47 repo_id = json.loads((muse / "repo.json").read_text())["repo_id"]
48 manifest: Manifest = {}
49 snap_id = compute_snapshot_id(manifest)
50 write_snapshot(repo_root, SnapshotRecord(snapshot_id=snap_id, manifest=manifest))
51 committed_at = datetime.now(timezone.utc)
52 commit_id = compute_commit_id(
53 repo_id=repo_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 created_on_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=now_utc_iso(),
77 agent_id="",
78 model_id="",
79 )
80 ]
81 return ReleaseRecord(
82 release_id=fake_id(tag + "-release"),
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 3 commits
sha256:b636f72dcba9e190afb980bece906fa5b717fbde014b76ef023df8cb96e01eb9 docs: expand cache plan with all seven testing tiers and do… Sonnet 4.6 134 days ago
sha256:7f9e2ef5286aedad9c1e6011b4c46ca27f39dbdad6e3409357e36b26e46b3b7c docs: docstring sprint for-each-ref→hotspots — idiomatic ru… Sonnet 4.6 patch 140 days ago
sha256:a09b1b4f6838754495547f200aa0ce88e2f56ffc5b20b900f6f0cff2c3cdede9 fix(cursorignore): remove git-ism (.git/worktrees) Human minor 143 days ago