"""muse#54 item 7 -- `muse reflog diff @{N} @{M}` as a first-class point-in-time comparison, resolving both specs from the same branch's reflog automatically instead of requiring manual `muse diff "branch@{N}" "branch@{M}"` construction. """ from __future__ import annotations import datetime import json import pathlib from tests.cli_test_helper import CliRunner from muse.core.types import fake_id, Manifest from muse.core.paths import ref_path, 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 TestReflogDiff: def test_diff_two_head_indices(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") # @{2} is c1 (oldest of the three), @{0} is c3 (newest/current HEAD). result = _run(root, "reflog", "diff", "2", "0", "--json") assert result.exit_code == 0, result.output data = json.loads(result.output) assert data["from_ref"] == "@{2}" assert data["to_ref"] == "@{0}" assert data["has_changes"] is True def test_diff_defaults_second_index_to_zero(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") result = _run(root, "reflog", "diff", "1", "--json") assert result.exit_code == 0, result.output data = json.loads(result.output) assert data["to_ref"] == "@{0}" def test_diff_with_branch_flag(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") result = _run(root, "reflog", "diff", "1", "0", "--branch", "main", "--json") assert result.exit_code == 0, result.output data = json.loads(result.output) assert data["from_ref"] == "main@{1}" assert data["to_ref"] == "main@{0}" def test_diff_identical_indices_has_no_changes(self, tmp_path: pathlib.Path) -> None: root, repo_id = _init_repo(tmp_path) _commit_file(root, "v1\n", "c1") result = _run(root, "reflog", "diff", "0", "0", "--json") assert result.exit_code == 0, result.output data = json.loads(result.output) assert data["has_changes"] is False def test_diff_out_of_range_index_errors(self, tmp_path: pathlib.Path) -> None: root, repo_id = _init_repo(tmp_path) _commit_file(root, "v1\n", "c1") result = _run(root, "reflog", "diff", "99", "0", "--json") assert result.exit_code == 1