gabriel / muse public
test_release_analysis.py python
163 lines 5.9 KB
Raw
sha256:1900655993c83c4107067375548a7be823e471d2515830842f1a12cba4bd3cdf fix: unified object store migration — idempotent writes, JS… Sonnet 4.6 minor ⚠ breaking 29 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.ids import hash_commit as compute_commit_id, hash_snapshot as 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 from muse.core.paths import muse_dir, snapshots_dir
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 = muse_dir(tmp_path)
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 = fake_id("repo")
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 = muse_dir(repo_root)
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 branch="main",
62 snapshot_id=snap_id,
63 message="initial",
64 committed_at=committed_at,
65 sem_ver_bump="minor",
66 ))
67 semver = SemVerTag(major=1, minor=0, patch=0, pre="", build="")
68 changelog: list[ChangelogEntry] = [
69 ChangelogEntry(
70 commit_id=commit_id,
71 message="initial",
72 sem_ver_bump="minor",
73 breaking_changes=[],
74 author="gabriel",
75 committed_at=now_utc_iso(),
76 agent_id="",
77 model_id="",
78 )
79 ]
80 return ReleaseRecord(
81 release_id=fake_id(tag + "-release"),
82 repo_id="test-repo",
83 tag=tag,
84 semver=semver,
85 channel="stable",
86 commit_id=commit_id,
87 snapshot_id=snap_id,
88 title="Test release",
89 body="",
90 changelog=changelog,
91 )
92
93
94 # ---------------------------------------------------------------------------
95 # Tests
96 # ---------------------------------------------------------------------------
97
98
99 class TestEmptyReport:
100 def test_empty_report_has_all_keys(self) -> None:
101 report = _empty_report()
102 assert report["languages"] == []
103 assert report["total_files"] == 0
104 assert report["total_symbols"] == 0
105 assert report["api_added"] == []
106 assert report["human_commits"] == 0
107
108
109 class TestComputeReleaseAnalysis:
110 def test_returns_semantic_report_shape(self, repo: pathlib.Path) -> None:
111 release = _make_release(repo)
112 report = compute_release_analysis(repo, release)
113 # Must return a dict with the expected keys.
114 assert isinstance(report, dict)
115 required = {
116 "languages", "total_files", "semantic_files", "total_symbols",
117 "symbols_by_kind", "files_changed", "api_added", "api_removed",
118 "api_modified", "file_hotspots", "refactor_events",
119 "breaking_changes", "human_commits", "agent_commits",
120 "unique_agents", "unique_models", "reviewers",
121 }
122 assert required.issubset(report.keys())
123
124 def test_empty_manifest_yields_zero_symbols(self, repo: pathlib.Path) -> None:
125 release = _make_release(repo)
126 report = compute_release_analysis(repo, release)
127 assert report["total_symbols"] == 0
128 assert report["total_files"] == 0
129 assert report["languages"] == []
130
131 def test_human_commit_counted(self, repo: pathlib.Path) -> None:
132 release = _make_release(repo)
133 # changelog has one entry with no agent_id → human commit
134 report = compute_release_analysis(repo, release)
135 assert report["human_commits"] == 1
136 assert report["agent_commits"] == 0
137
138 def test_agent_commit_counted(self, repo: pathlib.Path) -> None:
139 release = _make_release(repo)
140 release.changelog[0]["agent_id"] = "code-bot"
141 release.changelog[0]["model_id"] = "claude-opus-4"
142 report = compute_release_analysis(repo, release)
143 assert report["agent_commits"] == 1
144 assert report["human_commits"] == 0
145 assert report["unique_agents"] == ["code-bot"]
146 assert report["unique_models"] == ["claude-opus-4"]
147
148 def test_missing_snapshot_returns_empty_report(self, repo: pathlib.Path) -> None:
149 release = _make_release(repo)
150 release.snapshot_id = "c" * 64 # nonexistent snapshot
151 report = compute_release_analysis(repo, release)
152 assert report["total_files"] == 0
153 assert report["languages"] == []
154
155 def test_exception_in_analysis_returns_empty_report(self, repo: pathlib.Path) -> None:
156 """Even if analysis explodes, push must not fail."""
157 release = _make_release(repo)
158 # Corrupt the snapshot file so _compute raises.
159 snap_file = snapshots_dir(repo) / f"{release.snapshot_id}.json"
160 snap_file.write_text("not valid json {{{")
161 report = compute_release_analysis(repo, release)
162 assert isinstance(report, dict)
163
File History 1 commit
sha256:1900655993c83c4107067375548a7be823e471d2515830842f1a12cba4bd3cdf fix: unified object store migration — idempotent writes, JS… Sonnet 4.6 minor 29 days ago