"""muse#54 item 6 -- `muse reflog stat [--branch ] [--json]`. Summary statistics over a branch's (or HEAD's) reflog: total entries, a breakdown by operation category, the most active author, a reset count, and first/last entry timestamps. """ from __future__ import annotations import json import pathlib from tests.cli_test_helper import CliRunner from muse.core.types import fake_id, Manifest from muse.core.paths import muse_dir cli = None runner = CliRunner() def _env(root: pathlib.Path) -> Manifest: return {"MUSE_REPO_ROOT": str(root)} def _run(root: pathlib.Path, *args: str): return runner.invoke(cli, list(args), env=_env(root)) def _init_repo(tmp_path: pathlib.Path) -> tuple[pathlib.Path, str]: dot_muse = muse_dir(tmp_path) dot_muse.mkdir() repo_id = fake_id("repo") (dot_muse / "repo.json").write_text(json.dumps({ "repo_id": repo_id, "domain": "code", "default_branch": "main", "created_at": "2025-01-01T00:00:00+00:00", }), encoding="utf-8") (dot_muse / "HEAD").write_text("ref: refs/heads/main\n", encoding="utf-8") (dot_muse / "refs" / "heads").mkdir(parents=True) (dot_muse / "snapshots").mkdir() (dot_muse / "commits").mkdir() (dot_muse / "objects").mkdir() return tmp_path, repo_id def _commit_file(root: pathlib.Path, content: str, message: str) -> str: (root / "f.txt").write_text(content) add = _run(root, "code", "add", "f.txt") assert add.exit_code == 0, add.output commit = _run(root, "commit", "-m", message, "--json") assert commit.exit_code == 0, commit.output return json.loads(commit.output)["commit_id"] class TestReflogStat: def test_empty_reflog_reports_zero(self, tmp_path: pathlib.Path) -> None: root, repo_id = _init_repo(tmp_path) result = _run(root, "reflog", "stat", "--json") assert result.exit_code == 0, result.output data = json.loads(result.output) assert data["total_entries"] == 0 assert data["operation_breakdown"] == {} assert data["most_active_author"] is None assert data["reset_count"] == 0 assert data["first_timestamp"] is None assert data["last_timestamp"] is None def test_counts_commits_and_most_active_author(self, tmp_path: pathlib.Path) -> None: root, repo_id = _init_repo(tmp_path) _commit_file(root, "v1\n", "c1") _commit_file(root, "v2\n", "c2") _commit_file(root, "v3\n", "c3") result = _run(root, "reflog", "stat", "--json") assert result.exit_code == 0, result.output data = json.loads(result.output) assert data["total_entries"] == 3 assert data["operation_breakdown"]["commit"] == 3 assert data["most_active_author"] is not None assert data["reset_count"] == 0 assert data["first_timestamp"] is not None assert data["last_timestamp"] is not None assert data["first_timestamp"] <= data["last_timestamp"] def test_reset_count_tracks_reset_operations(self, tmp_path: pathlib.Path) -> None: root, repo_id = _init_repo(tmp_path) _commit_file(root, "v1\n", "c1") _commit_file(root, "v2\n", "c2") reset_result = _run(root, "reset", "HEAD~1", "--hard", "--json") assert reset_result.exit_code == 0, reset_result.output result = _run(root, "reflog", "stat", "--json") assert result.exit_code == 0, result.output data = json.loads(result.output) assert data["reset_count"] == 1 assert data["operation_breakdown"]["reset"] == 1 def test_branch_flag_targets_named_branch(self, tmp_path: pathlib.Path) -> None: root, repo_id = _init_repo(tmp_path) _commit_file(root, "v1\n", "c1") result = _run(root, "reflog", "stat", "--branch", "main", "--json") assert result.exit_code == 0, result.output data = json.loads(result.output) assert data["ref"] == "refs/heads/main" assert data["total_entries"] == 1 def test_invalid_branch_name_errors(self, tmp_path: pathlib.Path) -> None: root, repo_id = _init_repo(tmp_path) _commit_file(root, "v1\n", "c1") result = _run(root, "reflog", "stat", "--branch", "../escape", "--json") assert result.exit_code == 1