test_cmd_merge_base.py
python
sha256:81ae324db5ad375fbfe4834c6fcb378312cafad3cc92dec5d3e5c427306621a2
fix: remove commit_exists filter from have anchors — server…
Sonnet 4.6
patch
21 days ago
| 1 | """Tests for ``muse merge-base``. |
| 2 | |
| 3 | Verifies commit-ID resolution, branch-name resolution, HEAD resolution, |
| 4 | text-format output, and error handling for unresolvable refs. |
| 5 | """ |
| 6 | |
| 7 | from __future__ import annotations |
| 8 | |
| 9 | import datetime |
| 10 | import json |
| 11 | import pathlib |
| 12 | |
| 13 | import pytest |
| 14 | import argparse |
| 15 | from tests.cli_test_helper import CliRunner |
| 16 | |
| 17 | cli = None # argparse migration — CliRunner ignores this arg |
| 18 | from muse.core.errors import ExitCode |
| 19 | from muse.core.ids import hash_commit, hash_snapshot |
| 20 | from muse.core.commits import ( |
| 21 | CommitRecord, |
| 22 | write_commit, |
| 23 | ) |
| 24 | from muse.core.snapshots import ( |
| 25 | SnapshotRecord, |
| 26 | write_snapshot, |
| 27 | ) |
| 28 | from muse.core.types import Manifest, fake_id |
| 29 | from muse.core.paths import head_path, muse_dir, ref_path |
| 30 | |
| 31 | runner = CliRunner() |
| 32 | |
| 33 | |
| 34 | # --------------------------------------------------------------------------- |
| 35 | # Helpers |
| 36 | # --------------------------------------------------------------------------- |
| 37 | |
| 38 | |
| 39 | |
| 40 | def _init_repo(path: pathlib.Path, domain: str = "midi") -> pathlib.Path: |
| 41 | dot_muse = muse_dir(path) |
| 42 | (dot_muse / "commits").mkdir(parents=True) |
| 43 | (dot_muse / "snapshots").mkdir(parents=True) |
| 44 | (dot_muse / "objects").mkdir(parents=True) |
| 45 | (dot_muse / "refs" / "heads").mkdir(parents=True) |
| 46 | (dot_muse / "HEAD").write_text("ref: refs/heads/main", encoding="utf-8") |
| 47 | (dot_muse / "repo.json").write_text( |
| 48 | json.dumps({"repo_id": "test-repo", "domain": domain}), encoding="utf-8" |
| 49 | ) |
| 50 | return path |
| 51 | |
| 52 | |
| 53 | def _env(repo: pathlib.Path) -> Manifest: |
| 54 | return {"MUSE_REPO_ROOT": str(repo)} |
| 55 | |
| 56 | |
| 57 | def _snap(repo: pathlib.Path) -> str: |
| 58 | sid = hash_snapshot({}) |
| 59 | write_snapshot( |
| 60 | repo, |
| 61 | SnapshotRecord( |
| 62 | snapshot_id=sid, |
| 63 | manifest={}, |
| 64 | created_at=datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc), |
| 65 | ), |
| 66 | ) |
| 67 | return sid |
| 68 | |
| 69 | |
| 70 | def _commit( |
| 71 | repo: pathlib.Path, |
| 72 | tag: str, |
| 73 | snap_id: str, |
| 74 | branch: str = "main", |
| 75 | parent: str | None = None, |
| 76 | ) -> str: |
| 77 | committed_at = datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc) |
| 78 | parent_ids: list[str] = [parent] if parent else [] |
| 79 | cid = hash_commit( parent_ids=parent_ids, |
| 80 | snapshot_id=snap_id, |
| 81 | message=tag, |
| 82 | committed_at_iso=committed_at.isoformat(), |
| 83 | author="tester", |
| 84 | ) |
| 85 | write_commit( |
| 86 | repo, |
| 87 | CommitRecord( |
| 88 | commit_id=cid, |
| 89 | branch=branch, |
| 90 | snapshot_id=snap_id, |
| 91 | message=tag, |
| 92 | committed_at=committed_at, |
| 93 | author="tester", |
| 94 | parent_commit_id=parent, |
| 95 | ), |
| 96 | ) |
| 97 | return cid |
| 98 | |
| 99 | |
| 100 | def _set_branch(repo: pathlib.Path, branch: str, commit_id: str) -> None: |
| 101 | ref = ref_path(repo, branch) |
| 102 | ref.parent.mkdir(parents=True, exist_ok=True) |
| 103 | ref.write_text(commit_id, encoding="utf-8") |
| 104 | |
| 105 | |
| 106 | def _linear_dag(repo: pathlib.Path) -> tuple[str, str, str]: |
| 107 | """Build A → B (main) and A → C (feat). Returns (A, B, C).""" |
| 108 | sid = _snap(repo) |
| 109 | cid_a = _commit(repo, "base", sid) |
| 110 | cid_b = _commit(repo, "main-tip", sid, branch="main", parent=cid_a) |
| 111 | cid_c = _commit(repo, "feat-tip", sid, branch="feat", parent=cid_a) |
| 112 | _set_branch(repo, "main", cid_b) |
| 113 | _set_branch(repo, "feat", cid_c) |
| 114 | (head_path(repo)).write_text("ref: refs/heads/main", encoding="utf-8") |
| 115 | return cid_a, cid_b, cid_c |
| 116 | |
| 117 | |
| 118 | # --------------------------------------------------------------------------- |
| 119 | # Tests |
| 120 | # --------------------------------------------------------------------------- |
| 121 | |
| 122 | |
| 123 | class TestMergeBase: |
| 124 | def test_finds_common_ancestor_by_commit_id(self, tmp_path: pathlib.Path) -> None: |
| 125 | repo = _init_repo(tmp_path) |
| 126 | cid_a, cid_b, cid_c = _linear_dag(repo) |
| 127 | result = runner.invoke(cli, ["merge-base", "--json", cid_b, cid_c], env=_env(repo)) |
| 128 | assert result.exit_code == 0, result.output |
| 129 | data = json.loads(result.stdout) |
| 130 | assert data["merge_base"] == cid_a |
| 131 | assert data["commit_a"] == cid_b |
| 132 | assert data["commit_b"] == cid_c |
| 133 | |
| 134 | def test_branch_names_resolve_to_correct_base(self, tmp_path: pathlib.Path) -> None: |
| 135 | repo = _init_repo(tmp_path) |
| 136 | cid_a, _b, _c = _linear_dag(repo) |
| 137 | result = runner.invoke(cli, ["merge-base", "--json", "main", "feat"], env=_env(repo)) |
| 138 | assert result.exit_code == 0, result.output |
| 139 | assert json.loads(result.stdout)["merge_base"] == cid_a |
| 140 | |
| 141 | def test_head_resolves_to_current_branch(self, tmp_path: pathlib.Path) -> None: |
| 142 | repo = _init_repo(tmp_path) |
| 143 | cid_a, _b, _c = _linear_dag(repo) |
| 144 | result = runner.invoke(cli, ["merge-base", "--json", "HEAD", "feat"], env=_env(repo)) |
| 145 | assert result.exit_code == 0, result.output |
| 146 | assert json.loads(result.stdout)["merge_base"] == cid_a |
| 147 | |
| 148 | def test_same_commit_returns_itself(self, tmp_path: pathlib.Path) -> None: |
| 149 | repo = _init_repo(tmp_path) |
| 150 | sid = _snap(repo) |
| 151 | cid = _commit(repo, "solo", sid) |
| 152 | _set_branch(repo, "main", cid) |
| 153 | result = runner.invoke(cli, ["merge-base", "--json", cid, cid], env=_env(repo)) |
| 154 | assert result.exit_code == 0, result.output |
| 155 | assert json.loads(result.stdout)["merge_base"] == cid |
| 156 | |
| 157 | def test_text_format_emits_bare_commit_id(self, tmp_path: pathlib.Path) -> None: |
| 158 | repo = _init_repo(tmp_path) |
| 159 | cid_a, cid_b, cid_c = _linear_dag(repo) |
| 160 | # Default (no --json) emits plain text: just the commit ID |
| 161 | result = runner.invoke( |
| 162 | cli, ["merge-base", cid_b, cid_c], env=_env(repo) |
| 163 | ) |
| 164 | assert result.exit_code == 0, result.output |
| 165 | assert cid_a in result.stdout |
| 166 | |
| 167 | def test_unresolvable_ref_a_exits_user_error(self, tmp_path: pathlib.Path) -> None: |
| 168 | repo = _init_repo(tmp_path) |
| 169 | result = runner.invoke( |
| 170 | cli, ["merge-base", "--json", "no-such-branch", "also-missing"], env=_env(repo) |
| 171 | ) |
| 172 | assert result.exit_code == ExitCode.USER_ERROR |
| 173 | assert "error" in json.loads(result.stdout) |
| 174 | |
| 175 | def test_unknown_flag_rejected(self, tmp_path: pathlib.Path) -> None: |
| 176 | repo = _init_repo(tmp_path) |
| 177 | sid = _snap(repo) |
| 178 | cid = _commit(repo, "c", sid) |
| 179 | _set_branch(repo, "main", cid) |
| 180 | result = runner.invoke( |
| 181 | cli, ["merge-base", "--format", "yaml", cid, cid], env=_env(repo) |
| 182 | ) |
| 183 | # --format flag no longer exists; argparse rejects it |
| 184 | assert result.exit_code != 0 |
| 185 | |
| 186 | |
| 187 | class TestRegisterFlags: |
| 188 | def _parse(self, *args: str) -> "argparse.Namespace": |
| 189 | import argparse |
| 190 | from muse.cli.commands.merge_base import register |
| 191 | p = argparse.ArgumentParser() |
| 192 | subs = p.add_subparsers() |
| 193 | register(subs) |
| 194 | return p.parse_args(["merge-base", fake_id("a"), fake_id("b"), *args]) |
| 195 | |
| 196 | def test_json_short_flag(self) -> None: |
| 197 | args = self._parse("-j") |
| 198 | assert args.json_out is True |
| 199 | |
| 200 | def test_json_long_flag(self) -> None: |
| 201 | args = self._parse("--json") |
| 202 | assert args.json_out is True |
| 203 | |
| 204 | def test_default_no_json(self) -> None: |
| 205 | args = self._parse() |
| 206 | assert args.json_out is False |
File History
4 commits
sha256:81ae324db5ad375fbfe4834c6fcb378312cafad3cc92dec5d3e5c427306621a2
fix: remove commit_exists filter from have anchors — server…
Sonnet 4.6
patch
21 days ago
sha256:36c3cb3e76619d4c30a6d9bf81b5ec4ff148e30dcfed913e3114ca7b43b81c7e
fix: rename objects→blobs in push client and all stale test…
Sonnet 4.6
patch
22 days ago
sha256:c06a9b9b9fee26c68ea725b44d54b2c0a171301ce9de746d5b656617b4463a9a
fix: repair four test failures from post-migration audit
Sonnet 4.6
patch
28 days ago
sha256:1900655993c83c4107067375548a7be823e471d2515830842f1a12cba4bd3cdf
fix: unified object store migration — idempotent writes, JS…
Sonnet 4.6
minor
⚠
29 days ago