test_release_analysis.py
python
sha256:d11a87833d5fad6059b7662844bf5448a8911a17cce7a51811f71ad394f248eb
bump to v0.2.0rc13
Human
patch
6 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.semver import ( |
| 12 | ChangelogEntry, |
| 13 | SemVerTag, |
| 14 | SemanticReleaseReport, |
| 15 | ) |
| 16 | from muse.core.commits import ( |
| 17 | CommitRecord, |
| 18 | write_commit, |
| 19 | ) |
| 20 | from muse.core.snapshots import ( |
| 21 | SnapshotRecord, |
| 22 | write_snapshot, |
| 23 | ) |
| 24 | from muse.core.releases import ( |
| 25 | ReleaseRecord, |
| 26 | write_release, |
| 27 | ) |
| 28 | from muse.plugins.code.release_analysis import _empty_report, compute_release_analysis |
| 29 | from muse.core.types import Manifest, fake_id, now_utc_iso |
| 30 | from muse.core.paths import muse_dir, snapshots_dir |
| 31 | |
| 32 | |
| 33 | # --------------------------------------------------------------------------- |
| 34 | # Fixtures |
| 35 | # --------------------------------------------------------------------------- |
| 36 | |
| 37 | |
| 38 | @pytest.fixture() |
| 39 | def repo(tmp_path: pathlib.Path) -> pathlib.Path: |
| 40 | """Minimal repo layout: .muse/commits/, snapshots/, objects/, releases/.""" |
| 41 | muse = muse_dir(tmp_path) |
| 42 | for sub in ("commits", "snapshots", "objects", "releases", "refs", "refs/heads"): |
| 43 | (muse / sub).mkdir(parents=True, exist_ok=True) |
| 44 | (muse / "HEAD").write_text("ref: refs/heads/main\n") |
| 45 | repo_id = fake_id("repo") |
| 46 | import json |
| 47 | (muse / "repo.json").write_text(json.dumps({"repo_id": repo_id})) |
| 48 | return tmp_path |
| 49 | |
| 50 | |
| 51 | def _make_release(repo_root: pathlib.Path, tag: str = "v1.0.0") -> ReleaseRecord: |
| 52 | import json |
| 53 | muse = muse_dir(repo_root) |
| 54 | repo_id = json.loads((muse / "repo.json").read_text())["repo_id"] |
| 55 | manifest: Manifest = {} |
| 56 | snap_id = compute_snapshot_id(manifest) |
| 57 | write_snapshot(repo_root, SnapshotRecord(snapshot_id=snap_id, manifest=manifest)) |
| 58 | committed_at = datetime.now(timezone.utc) |
| 59 | commit_id = compute_commit_id( |
| 60 | parent_ids=[], |
| 61 | snapshot_id=snap_id, |
| 62 | message="initial", |
| 63 | committed_at_iso=committed_at.isoformat(), |
| 64 | ) |
| 65 | write_commit(repo_root, CommitRecord( |
| 66 | commit_id=commit_id, |
| 67 | branch="main", |
| 68 | snapshot_id=snap_id, |
| 69 | message="initial", |
| 70 | committed_at=committed_at, |
| 71 | sem_ver_bump="minor", |
| 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=now_utc_iso(), |
| 82 | agent_id="", |
| 83 | model_id="", |
| 84 | ) |
| 85 | ] |
| 86 | return ReleaseRecord( |
| 87 | release_id=fake_id(tag + "-release"), |
| 88 | repo_id="test-repo", |
| 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 = snapshots_dir(repo) / 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 |
File History
1 commit
sha256:d11a87833d5fad6059b7662844bf5448a8911a17cce7a51811f71ad394f248eb
bump to v0.2.0rc13
Human
patch
6 days ago