test_reflog_diff.py
python
sha256:ce542c4d0f6dacc9520c02d0e3378bf96bc377d118a7402f2bbc8af1749b08e0
feat(code-test): audit cache internals, add cache_hit/--cac…
Sonnet 5
minor
⚠ breaking
5 hours ago
| 1 | """muse#54 item 7 -- `muse reflog diff @{N} @{M}` as a first-class point-in-time |
| 2 | comparison, resolving both specs from the same branch's reflog automatically |
| 3 | instead of requiring manual `muse diff "branch@{N}" "branch@{M}"` construction. |
| 4 | """ |
| 5 | from __future__ import annotations |
| 6 | |
| 7 | import datetime |
| 8 | import json |
| 9 | import pathlib |
| 10 | |
| 11 | from tests.cli_test_helper import CliRunner |
| 12 | from muse.core.types import fake_id, Manifest |
| 13 | from muse.core.paths import ref_path, muse_dir |
| 14 | |
| 15 | cli = None |
| 16 | runner = CliRunner() |
| 17 | |
| 18 | |
| 19 | def _env(root: pathlib.Path) -> Manifest: |
| 20 | return {"MUSE_REPO_ROOT": str(root)} |
| 21 | |
| 22 | |
| 23 | def _run(root: pathlib.Path, *args: str): |
| 24 | return runner.invoke(cli, list(args), env=_env(root)) |
| 25 | |
| 26 | |
| 27 | def _init_repo(tmp_path: pathlib.Path) -> tuple[pathlib.Path, str]: |
| 28 | dot_muse = muse_dir(tmp_path) |
| 29 | dot_muse.mkdir() |
| 30 | repo_id = fake_id("repo") |
| 31 | (dot_muse / "repo.json").write_text(json.dumps({ |
| 32 | "repo_id": repo_id, "domain": "code", "default_branch": "main", |
| 33 | "created_at": "2025-01-01T00:00:00+00:00", |
| 34 | }), encoding="utf-8") |
| 35 | (dot_muse / "HEAD").write_text("ref: refs/heads/main\n", encoding="utf-8") |
| 36 | (dot_muse / "refs" / "heads").mkdir(parents=True) |
| 37 | (dot_muse / "snapshots").mkdir() |
| 38 | (dot_muse / "commits").mkdir() |
| 39 | (dot_muse / "objects").mkdir() |
| 40 | return tmp_path, repo_id |
| 41 | |
| 42 | |
| 43 | def _commit_file(root: pathlib.Path, content: str, message: str) -> str: |
| 44 | (root / "f.txt").write_text(content) |
| 45 | add = _run(root, "code", "add", "f.txt") |
| 46 | assert add.exit_code == 0, add.output |
| 47 | commit = _run(root, "commit", "-m", message, "--json") |
| 48 | assert commit.exit_code == 0, commit.output |
| 49 | return json.loads(commit.output)["commit_id"] |
| 50 | |
| 51 | |
| 52 | class TestReflogDiff: |
| 53 | def test_diff_two_head_indices(self, tmp_path: pathlib.Path) -> None: |
| 54 | root, repo_id = _init_repo(tmp_path) |
| 55 | _commit_file(root, "v1\n", "c1") |
| 56 | _commit_file(root, "v2\n", "c2") |
| 57 | _commit_file(root, "v3\n", "c3") |
| 58 | |
| 59 | # @{2} is c1 (oldest of the three), @{0} is c3 (newest/current HEAD). |
| 60 | result = _run(root, "reflog", "diff", "2", "0", "--json") |
| 61 | assert result.exit_code == 0, result.output |
| 62 | data = json.loads(result.output) |
| 63 | assert data["from_ref"] == "@{2}" |
| 64 | assert data["to_ref"] == "@{0}" |
| 65 | assert data["has_changes"] is True |
| 66 | |
| 67 | def test_diff_defaults_second_index_to_zero(self, tmp_path: pathlib.Path) -> None: |
| 68 | root, repo_id = _init_repo(tmp_path) |
| 69 | _commit_file(root, "v1\n", "c1") |
| 70 | _commit_file(root, "v2\n", "c2") |
| 71 | |
| 72 | result = _run(root, "reflog", "diff", "1", "--json") |
| 73 | assert result.exit_code == 0, result.output |
| 74 | data = json.loads(result.output) |
| 75 | assert data["to_ref"] == "@{0}" |
| 76 | |
| 77 | def test_diff_with_branch_flag(self, tmp_path: pathlib.Path) -> None: |
| 78 | root, repo_id = _init_repo(tmp_path) |
| 79 | _commit_file(root, "v1\n", "c1") |
| 80 | _commit_file(root, "v2\n", "c2") |
| 81 | |
| 82 | result = _run(root, "reflog", "diff", "1", "0", "--branch", "main", "--json") |
| 83 | assert result.exit_code == 0, result.output |
| 84 | data = json.loads(result.output) |
| 85 | assert data["from_ref"] == "main@{1}" |
| 86 | assert data["to_ref"] == "main@{0}" |
| 87 | |
| 88 | def test_diff_identical_indices_has_no_changes(self, tmp_path: pathlib.Path) -> None: |
| 89 | root, repo_id = _init_repo(tmp_path) |
| 90 | _commit_file(root, "v1\n", "c1") |
| 91 | |
| 92 | result = _run(root, "reflog", "diff", "0", "0", "--json") |
| 93 | assert result.exit_code == 0, result.output |
| 94 | data = json.loads(result.output) |
| 95 | assert data["has_changes"] is False |
| 96 | |
| 97 | def test_diff_out_of_range_index_errors(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", "diff", "99", "0", "--json") |
| 102 | assert result.exit_code == 1 |
File History
1 commit
sha256:ce542c4d0f6dacc9520c02d0e3378bf96bc377d118a7402f2bbc8af1749b08e0
feat(code-test): audit cache internals, add cache_hit/--cac…
Sonnet 5
minor
⚠
5 hours ago