test_reflog_anchor.py
python
sha256:0471c16969dec806b1879428130fb224c414a8ae6ac98a4b567f1b00cfebe6d2
feat: muse#54 item 8 — muse reflog anchor COMMIT
Sonnet 5
patch
19 hours ago
| 1 | """muse#54 item 8 -- `muse reflog anchor COMMIT [--json]`. |
| 2 | |
| 3 | Given a commit ID, search the HEAD log and every branch's reflog for |
| 4 | entries that moved a ref TO that commit, reporting ref + @{N} index for |
| 5 | each match. |
| 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 TestReflogAnchor: |
| 54 | def test_finds_commit_on_head_log(self, tmp_path: pathlib.Path) -> None: |
| 55 | root, repo_id = _init_repo(tmp_path) |
| 56 | c1 = _commit_file(root, "v1\n", "c1") |
| 57 | c2 = _commit_file(root, "v2\n", "c2") |
| 58 | |
| 59 | result = _run(root, "reflog", "anchor", c2, "--json") |
| 60 | assert result.exit_code == 0, result.output |
| 61 | data = json.loads(result.output) |
| 62 | assert data["commit_id"] == c2 |
| 63 | assert data["count"] >= 1 |
| 64 | assert any(m["ref"] == "HEAD" and m["index"] == 0 for m in data["matches"]) |
| 65 | |
| 66 | def test_finds_commit_on_named_branch_log(self, tmp_path: pathlib.Path) -> None: |
| 67 | root, repo_id = _init_repo(tmp_path) |
| 68 | c1 = _commit_file(root, "v1\n", "c1") |
| 69 | |
| 70 | result = _run(root, "reflog", "anchor", c1, "--json") |
| 71 | assert result.exit_code == 0, result.output |
| 72 | data = json.loads(result.output) |
| 73 | assert any(m["ref"] == "refs/heads/main" for m in data["matches"]) |
| 74 | |
| 75 | def test_unmatched_commit_returns_empty(self, tmp_path: pathlib.Path) -> None: |
| 76 | root, repo_id = _init_repo(tmp_path) |
| 77 | _commit_file(root, "v1\n", "c1") |
| 78 | |
| 79 | fake_commit = "sha256:" + "0" * 64 |
| 80 | result = _run(root, "reflog", "anchor", fake_commit, "--json") |
| 81 | assert result.exit_code == 0, result.output |
| 82 | data = json.loads(result.output) |
| 83 | assert data["count"] == 0 |
| 84 | assert data["matches"] == [] |
| 85 | |
| 86 | def test_accepts_bare_hex_without_prefix(self, tmp_path: pathlib.Path) -> None: |
| 87 | root, repo_id = _init_repo(tmp_path) |
| 88 | c1 = _commit_file(root, "v1\n", "c1") |
| 89 | bare_hex = c1.split(":", 1)[1] |
| 90 | |
| 91 | result = _run(root, "reflog", "anchor", bare_hex, "--json") |
| 92 | assert result.exit_code == 0, result.output |
| 93 | data = json.loads(result.output) |
| 94 | assert data["commit_id"] == c1 |
| 95 | assert data["count"] >= 1 |
| 96 | |
| 97 | def test_second_commit_not_anchored_at_first_commits_index(self, tmp_path: pathlib.Path) -> None: |
| 98 | root, repo_id = _init_repo(tmp_path) |
| 99 | c1 = _commit_file(root, "v1\n", "c1") |
| 100 | c2 = _commit_file(root, "v2\n", "c2") |
| 101 | |
| 102 | result = _run(root, "reflog", "anchor", c1, "--json") |
| 103 | assert result.exit_code == 0, result.output |
| 104 | data = json.loads(result.output) |
| 105 | # c1 should be found, but not at the newest (@{0}) HEAD index -- |
| 106 | # that belongs to c2. |
| 107 | head_matches = [m for m in data["matches"] if m["ref"] == "HEAD"] |
| 108 | assert all(m["index"] != 0 for m in head_matches) |
File History
1 commit
sha256:0471c16969dec806b1879428130fb224c414a8ae6ac98a4b567f1b00cfebe6d2
feat: muse#54 item 8 — muse reflog anchor COMMIT
Sonnet 5
patch
19 hours ago