"""Comprehensive tests for the remaining 8 commands: domain-info, show-ref, verify-object, symbolic-ref, for-each-ref, name-rev, check-ref-format, verify-pack. (check-ignore and check-attr are covered via attribute/ignore tests elsewhere.) Coverage tiers -------------- - Integration: core functionality, JSON/text formats, key flags - Security: errors to stderr, no traceback on bad input - Stress: 100+ object verify, 200 ref iterations """ from __future__ import annotations import datetime import json import pathlib from muse.core.errors import ExitCode from muse.core.object_store import write_object from muse.core.ids import hash_commit, hash_snapshot from muse.core.commits import ( CommitRecord, write_commit, ) from muse.core.snapshots import ( SnapshotRecord, write_snapshot, ) from muse.core.types import Manifest, blob_id from muse.core.paths import head_path, muse_dir, ref_path from tests.cli_test_helper import CliRunner, InvokeResult runner = CliRunner() # --------------------------------------------------------------------------- # Shared helpers # --------------------------------------------------------------------------- def _make_repo(tmp_path: pathlib.Path, domain: str = "code") -> pathlib.Path: repo = tmp_path / "repo" dot_muse = muse_dir(repo) for sub in ("objects", "commits", "snapshots", "refs/heads"): (dot_muse / sub).mkdir(parents=True) (dot_muse / "HEAD").write_text("ref: refs/heads/main") (dot_muse / "repo.json").write_text(json.dumps({"repo_id": "test-repo", "domain": domain})) return repo _TS = datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc) def _snap(repo: pathlib.Path, manifest: Manifest | None = None) -> str: sid = hash_snapshot(manifest or {}) write_snapshot(repo, SnapshotRecord( snapshot_id=sid, manifest=manifest or {}, created_at=_TS, )) return sid def _commit( repo: pathlib.Path, snap_id: str, *, branch: str = "main", parent: str | None = None, message: str = "test", ) -> str: parents = [parent] if parent else [] cid = hash_commit( parent_ids=parents, snapshot_id=snap_id, message=message, committed_at_iso=_TS.isoformat(), ) write_commit(repo, CommitRecord( commit_id=cid, branch=branch, snapshot_id=snap_id, message=message, committed_at=_TS, parent_commit_id=parent, )) return cid def _set_head(repo: pathlib.Path, branch: str, commit_id: str) -> None: ref = ref_path(repo, branch) ref.parent.mkdir(parents=True, exist_ok=True) ref.write_text(commit_id) (head_path(repo)).write_text(f"ref: refs/heads/{branch}") def _invoke(cmd: str, repo: pathlib.Path, *args: str, input: bytes | None = None) -> InvokeResult: from muse.cli.app import main as cli return runner.invoke( cli, [cmd, *args], env={"MUSE_REPO_ROOT": str(repo)}, input=input, ) def _fake_oid(n: int) -> str: return format(n, "064x") def _write_obj(repo: pathlib.Path, data: bytes) -> str: """Write bytes to the object store; returns the prefixed SHA-256 object ID.""" oid = blob_id(data) write_object(repo, oid, data) return oid # =========================================================================== # domain-info # =========================================================================== class TestDomainInfo: def test_all_domains_returns_list(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) result = _invoke("domain-info", repo, "--all-domains", "--json") assert result.exit_code == 0 data = json.loads(result.output) assert "registered_domains" in data assert isinstance(data["registered_domains"], list) def test_all_domains_text_format(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) result = _invoke("domain-info", repo, "--all-domains") assert result.exit_code == 0 assert len(result.output.strip()) > 0 def test_json_shorthand(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) result = _invoke("domain-info", repo, "--all-domains", "--json") assert result.exit_code == 0 assert "registered_domains" in json.loads(result.output) def test_no_traceback_on_bad_domain(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path, domain="nonexistent-domain") result = _invoke("domain-info", repo) assert "Traceback" not in result.output # =========================================================================== # show-ref # =========================================================================== class TestShowRef: def test_shows_branches(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) sid = _snap(repo) cid = _commit(repo, sid) _set_head(repo, "main", cid) result = _invoke("show-ref", repo, "--json") assert result.exit_code == 0 data = json.loads(result.output) assert data["count"] == 1 assert any(r["ref"] == "refs/heads/main" for r in data["refs"]) def test_head_only_flag(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) sid = _snap(repo) cid = _commit(repo, sid) _set_head(repo, "main", cid) result = _invoke("show-ref", repo, "--head", "--json") assert result.exit_code == 0 data = json.loads(result.output) assert data["head"] is not None assert data["head"]["commit_id"] == cid def test_head_text_format(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) sid = _snap(repo) cid = _commit(repo, sid) _set_head(repo, "main", cid) result = _invoke("show-ref", repo) assert result.exit_code == 0 assert cid in result.output assert "main" in result.output def test_verify_existing_ref(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) sid = _snap(repo) cid = _commit(repo, sid) _set_head(repo, "main", cid) result = _invoke("show-ref", repo, "--verify", "refs/heads/main") assert result.exit_code == 0 def test_verify_nonexistent_ref_exits_1(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) result = _invoke("show-ref", repo, "--verify", "ghost") assert result.exit_code == ExitCode.USER_ERROR def test_empty_repo_no_refs(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) result = _invoke("show-ref", repo, "--json") assert result.exit_code == 0 data = json.loads(result.output) assert data["count"] == 0 def test_200_sequential_calls(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) sid = _snap(repo) cid = _commit(repo, sid) _set_head(repo, "main", cid) for i in range(200): result = _invoke("show-ref", repo) assert result.exit_code == 0, f"failed at {i}" # =========================================================================== # verify-object # =========================================================================== class TestVerifyObject: def test_valid_object_ok(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) oid = _write_obj(repo, b"hello world") result = _invoke("verify-object", repo, "--json", oid) assert result.exit_code == 0 data = json.loads(result.output) assert data["all_ok"] is True assert data["results"][0]["ok"] is True def test_missing_object_fails(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) result = _invoke("verify-object", repo, "--json", f"dead{'beef' * 15}") assert result.exit_code == ExitCode.USER_ERROR data = json.loads(result.output) assert data["all_ok"] is False assert not data["results"][0]["ok"] def test_invalid_object_id_fails(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) result = _invoke("verify-object", repo, "--json", "not-hex") assert result.exit_code == ExitCode.USER_ERROR data = json.loads(result.output) assert data["all_ok"] is False def test_text_format(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) oid = _write_obj(repo, b"text test") result = _invoke("verify-object", repo, oid) assert result.exit_code == 0 assert "OK" in result.output def test_quiet_mode_exit_code(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) oid = _write_obj(repo, b"quiet") result = _invoke("verify-object", repo, "--quiet", oid) assert result.exit_code == 0 def test_multiple_objects(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) oid1 = _write_obj(repo, b"obj1") oid2 = _write_obj(repo, b"obj2") result = _invoke("verify-object", repo, "--json", oid1, oid2) assert result.exit_code == 0 data = json.loads(result.output) assert data["checked"] == 2 assert data["failed"] == 0 def test_100_objects(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) oids = [_write_obj(repo, f"obj-{i}".encode()) for i in range(100)] result = _invoke("verify-object", repo, "--json", *oids) assert result.exit_code == 0 data = json.loads(result.output) assert data["checked"] == 100 assert data["failed"] == 0 def test_no_traceback_on_bad_id(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) result = _invoke("verify-object", repo, "bad") assert "Traceback" not in result.output # =========================================================================== # symbolic-ref # =========================================================================== class TestSymbolicRef: def test_reads_head_branch(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) sid = _snap(repo) cid = _commit(repo, sid) _set_head(repo, "main", cid) result = _invoke("symbolic-ref", repo, "--json", "HEAD") assert result.exit_code == 0 data = json.loads(result.output) assert data["branch"] == "main" assert "refs/heads/main" in data["symbolic_target"] def test_short_flag(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) sid = _snap(repo) cid = _commit(repo, sid) _set_head(repo, "main", cid) result = _invoke("symbolic-ref", repo, "--short", "HEAD") assert result.exit_code == 0 assert result.output.strip() == "main" def test_set_changes_head(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) sid = _snap(repo) cid = _commit(repo, sid, branch="dev") _set_head(repo, "dev", cid) result = _invoke("symbolic-ref", repo, "--set", "dev", "HEAD") assert result.exit_code == 0 assert (head_path(repo)).read_text().strip() == "ref: refs/heads/dev" def test_unsupported_ref_errors(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) result = _invoke("symbolic-ref", repo, "refs/heads/main") assert result.exit_code == ExitCode.USER_ERROR def test_no_traceback_on_bad_ref(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) result = _invoke("symbolic-ref", repo, "bad-ref") assert "Traceback" not in result.output # =========================================================================== # for-each-ref # =========================================================================== class TestForEachRef: def test_lists_all_refs(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) sid = _snap(repo) cid1 = _commit(repo, sid, branch="main", message="init-main") cid2 = _commit(repo, sid, branch="dev", message="init-dev") _set_head(repo, "main", cid1) _set_head(repo, "dev", cid2) result = _invoke("for-each-ref", repo, "--json") assert result.exit_code == 0 data = json.loads(result.output) branch_names = {r["branch"] for r in data["refs"]} assert "main" in branch_names assert "dev" in branch_names def test_text_format(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) sid = _snap(repo) cid = _commit(repo, sid) _set_head(repo, "main", cid) result = _invoke("for-each-ref", repo) assert result.exit_code == 0 assert "main" in result.output def test_count_limit(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) sid = _snap(repo) for i in range(5): cid = _commit(repo, sid, branch=f"branch-{i}", message=f"branch-{i}") _set_head(repo, f"branch-{i}", cid) result = _invoke("for-each-ref", repo, "--count", "3", "--json") assert result.exit_code == 0 data = json.loads(result.output) assert len(data["refs"]) == 3 def test_empty_repo(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) result = _invoke("for-each-ref", repo, "--json") assert result.exit_code == 0 data = json.loads(result.output) assert data["refs"] == [] # =========================================================================== # name-rev # =========================================================================== class TestNameRev: def test_tip_commit_names_to_branch(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) sid = _snap(repo) cid = _commit(repo, sid, branch="main") _set_head(repo, "main", cid) result = _invoke("name-rev", repo, "--json", cid) assert result.exit_code == 0 data = json.loads(result.output) assert len(data["results"]) == 1 assert data["results"][0]["commit_id"] == cid assert "main" in data["results"][0]["name"] def test_parent_commit_named_with_tilde(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) sid = _snap(repo) c1 = _commit(repo, sid, branch="main", message="c1-msg") c2 = _commit(repo, sid, branch="main", parent=c1, message="c2-msg") _set_head(repo, "main", c2) result = _invoke("name-rev", repo, "--json", c1) assert result.exit_code == 0 data = json.loads(result.output) name = data["results"][0]["name"] assert "~" in name or "main" in name def test_no_commit_ids_errors(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) result = _invoke("name-rev", repo) assert result.exit_code == ExitCode.USER_ERROR def test_no_traceback_on_empty_input(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) result = _invoke("name-rev", repo) assert "Traceback" not in result.output # =========================================================================== # check-ref-format # =========================================================================== class TestCheckRefFormat: def test_valid_branch_name(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) result = _invoke("check-ref-format", repo, "--json", "main") assert result.exit_code == 0 data = json.loads(result.output) assert data["all_valid"] is True def test_valid_feature_branch(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) result = _invoke("check-ref-format", repo, "--json", "feat/add-melody") assert result.exit_code == 0 data = json.loads(result.output) assert data["all_valid"] is True def test_invalid_null_byte_rejected(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) result = _invoke("check-ref-format", repo, "--json", "bad\x00branch") assert result.exit_code == ExitCode.USER_ERROR data = json.loads(result.output) assert data["all_valid"] is False def test_multiple_names_mixed_validity(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) result = _invoke("check-ref-format", repo, "--json", "main", "bad\x00branch") assert result.exit_code == ExitCode.USER_ERROR data = json.loads(result.output) assert data["all_valid"] is False valid = {r["name"]: r["valid"] for r in data["results"]} assert valid["main"] is True assert valid["bad\x00branch"] is False def test_text_format(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) result = _invoke("check-ref-format", repo, "main") assert result.exit_code == 0 assert "ok" in result.output.lower() def test_quiet_mode(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) result = _invoke("check-ref-format", repo, "--quiet", "main") assert result.exit_code == 0 assert result.output.strip() == "" def test_no_args_errors(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) result = _invoke("check-ref-format", repo) assert result.exit_code == ExitCode.USER_ERROR def test_no_traceback_on_bad_name(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) result = _invoke("check-ref-format", repo, "bad\x00branch") assert "Traceback" not in result.output # =========================================================================== # verify-pack # =========================================================================== class TestVerifyPack: def _make_pack_bytes(self, repo: pathlib.Path, cid: str, sid: str) -> bytes: from muse.cli.app import main as cli result = runner.invoke( cli, ["pack-objects", cid], env={"MUSE_REPO_ROOT": str(repo)}, ) assert result.exit_code == 0 return result.stdout_bytes def test_valid_pack_from_stdin(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) sid = _snap(repo) cid = _commit(repo, sid, message="test-pack") pack_bytes = self._make_pack_bytes(repo, cid, sid) result = _invoke("verify-pack", repo, "--json", input=pack_bytes) assert result.exit_code == 0 data = json.loads(result.output) assert data["all_ok"] is True assert data["commits_checked"] >= 1 def test_empty_stdin_errors(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) result = _invoke("verify-pack", repo, input=b"") assert result.exit_code == ExitCode.USER_ERROR def test_corrupted_msgpack_errors(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) result = _invoke("verify-pack", repo, input=b"\xff\xfe corrupted") assert result.exit_code == ExitCode.USER_ERROR def test_no_traceback_on_bad_input(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) result = _invoke("verify-pack", repo, input=b"not msgpack at all") assert "Traceback" not in result.output