test_reflog_stat.py
python
sha256:a5f8a020b53ddab68f231cf27489fe865525f85ee924133034262d39b332076e
fix(clone,pull): never claim success when a snapshot hash m…
Sonnet 5
patch
9 minutes ago
| 1 | """muse#54 item 6 -- `muse reflog stat [--branch <name>] [--json]`. |
| 2 | |
| 3 | Summary statistics over a branch's (or HEAD's) reflog: total entries, a |
| 4 | breakdown by operation category, the most active author, a reset count, and |
| 5 | first/last entry timestamps. |
| 6 | """ |
| 7 | from __future__ import annotations |
| 8 | |
| 9 | import json |
| 10 | import pathlib |
| 11 | |
| 12 | from tests.cli_test_helper import CliRunner |
| 13 | from muse.core.types import fake_id, Manifest |
| 14 | from muse.core.paths import muse_dir |
| 15 | |
| 16 | cli = None |
| 17 | runner = CliRunner() |
| 18 | |
| 19 | |
| 20 | def _env(root: pathlib.Path) -> Manifest: |
| 21 | return {"MUSE_REPO_ROOT": str(root)} |
| 22 | |
| 23 | |
| 24 | def _run(root: pathlib.Path, *args: str): |
| 25 | return runner.invoke(cli, list(args), env=_env(root)) |
| 26 | |
| 27 | |
| 28 | def _init_repo(tmp_path: pathlib.Path) -> tuple[pathlib.Path, str]: |
| 29 | dot_muse = muse_dir(tmp_path) |
| 30 | dot_muse.mkdir() |
| 31 | repo_id = fake_id("repo") |
| 32 | (dot_muse / "repo.json").write_text(json.dumps({ |
| 33 | "repo_id": repo_id, "domain": "code", "default_branch": "main", |
| 34 | "created_at": "2025-01-01T00:00:00+00:00", |
| 35 | }), encoding="utf-8") |
| 36 | (dot_muse / "HEAD").write_text("ref: refs/heads/main\n", encoding="utf-8") |
| 37 | (dot_muse / "refs" / "heads").mkdir(parents=True) |
| 38 | (dot_muse / "snapshots").mkdir() |
| 39 | (dot_muse / "commits").mkdir() |
| 40 | (dot_muse / "objects").mkdir() |
| 41 | return tmp_path, repo_id |
| 42 | |
| 43 | |
| 44 | def _commit_file(root: pathlib.Path, content: str, message: str) -> str: |
| 45 | (root / "f.txt").write_text(content) |
| 46 | add = _run(root, "code", "add", "f.txt") |
| 47 | assert add.exit_code == 0, add.output |
| 48 | commit = _run(root, "commit", "-m", message, "--json") |
| 49 | assert commit.exit_code == 0, commit.output |
| 50 | return json.loads(commit.output)["commit_id"] |
| 51 | |
| 52 | |
| 53 | class TestReflogStat: |
| 54 | def test_empty_reflog_reports_zero(self, tmp_path: pathlib.Path) -> None: |
| 55 | root, repo_id = _init_repo(tmp_path) |
| 56 | result = _run(root, "reflog", "stat", "--json") |
| 57 | assert result.exit_code == 0, result.output |
| 58 | data = json.loads(result.output) |
| 59 | assert data["total_entries"] == 0 |
| 60 | assert data["operation_breakdown"] == {} |
| 61 | assert data["most_active_author"] is None |
| 62 | assert data["reset_count"] == 0 |
| 63 | assert data["first_timestamp"] is None |
| 64 | assert data["last_timestamp"] is None |
| 65 | |
| 66 | def test_counts_commits_and_most_active_author(self, tmp_path: pathlib.Path) -> None: |
| 67 | root, repo_id = _init_repo(tmp_path) |
| 68 | _commit_file(root, "v1\n", "c1") |
| 69 | _commit_file(root, "v2\n", "c2") |
| 70 | _commit_file(root, "v3\n", "c3") |
| 71 | |
| 72 | result = _run(root, "reflog", "stat", "--json") |
| 73 | assert result.exit_code == 0, result.output |
| 74 | data = json.loads(result.output) |
| 75 | assert data["total_entries"] == 3 |
| 76 | assert data["operation_breakdown"]["commit"] == 3 |
| 77 | assert data["most_active_author"] is not None |
| 78 | assert data["reset_count"] == 0 |
| 79 | assert data["first_timestamp"] is not None |
| 80 | assert data["last_timestamp"] is not None |
| 81 | assert data["first_timestamp"] <= data["last_timestamp"] |
| 82 | |
| 83 | def test_reset_count_tracks_reset_operations(self, tmp_path: pathlib.Path) -> None: |
| 84 | root, repo_id = _init_repo(tmp_path) |
| 85 | _commit_file(root, "v1\n", "c1") |
| 86 | _commit_file(root, "v2\n", "c2") |
| 87 | |
| 88 | reset_result = _run(root, "reset", "HEAD~1", "--hard", "--json") |
| 89 | assert reset_result.exit_code == 0, reset_result.output |
| 90 | |
| 91 | result = _run(root, "reflog", "stat", "--json") |
| 92 | assert result.exit_code == 0, result.output |
| 93 | data = json.loads(result.output) |
| 94 | assert data["reset_count"] == 1 |
| 95 | assert data["operation_breakdown"]["reset"] == 1 |
| 96 | |
| 97 | def test_branch_flag_targets_named_branch(self, tmp_path: pathlib.Path) -> None: |
| 98 | root, repo_id = _init_repo(tmp_path) |
| 99 | _commit_file(root, "v1\n", "c1") |
| 100 | |
| 101 | result = _run(root, "reflog", "stat", "--branch", "main", "--json") |
| 102 | assert result.exit_code == 0, result.output |
| 103 | data = json.loads(result.output) |
| 104 | assert data["ref"] == "refs/heads/main" |
| 105 | assert data["total_entries"] == 1 |
| 106 | |
| 107 | def test_invalid_branch_name_errors(self, tmp_path: pathlib.Path) -> None: |
| 108 | root, repo_id = _init_repo(tmp_path) |
| 109 | _commit_file(root, "v1\n", "c1") |
| 110 | |
| 111 | result = _run(root, "reflog", "stat", "--branch", "../escape", "--json") |
| 112 | assert result.exit_code == 1 |
File History
1 commit
sha256:a5f8a020b53ddab68f231cf27489fe865525f85ee924133034262d39b332076e
fix(clone,pull): never claim success when a snapshot hash m…
Sonnet 5
patch
9 minutes ago