"""Comprehensive tests for ``muse plumbing commit-graph``. Coverage tiers -------------- - Unit: _CommitNode schema, _DEFAULT_MAX - Integration: linear chain, --tip, --max, --count, --first-parent, --stop-at, --ancestry-path, text format, json shorthand - Security: errors to stderr, no traceback on bad tip - Stress: 50-commit chain traversal """ from __future__ import annotations import datetime import json import pathlib from muse.core.errors import ExitCode from muse.core.snapshot import compute_commit_id, compute_snapshot_id from muse.core.store import CommitRecord, SnapshotRecord, write_commit, write_snapshot from tests.cli_test_helper import CliRunner, InvokeResult runner = CliRunner() _DT = datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc) # --------------------------------------------------------------------------- # Helpers # --------------------------------------------------------------------------- def _make_repo(tmp_path: pathlib.Path) -> pathlib.Path: repo = tmp_path / "repo" muse = repo / ".muse" for sub in ("objects", "commits", "snapshots", "refs/heads"): (muse / sub).mkdir(parents=True) (muse / "HEAD").write_text("ref: refs/heads/main") (muse / "repo.json").write_text(json.dumps({"repo_id": "test-repo", "domain": "code"})) return repo def _snap(repo: pathlib.Path) -> str: """Write a snapshot with an empty manifest; return its content-addressed ID.""" sid = compute_snapshot_id({}) write_snapshot(repo, SnapshotRecord( snapshot_id=sid, manifest={}, created_at=_DT, )) return sid def _commit( repo: pathlib.Path, snap_id: str, *, parent: str | None = None, parent2: str | None = None, message: str = "test", ) -> str: """Write a commit with a real content-addressed ID; return the commit ID.""" parent_ids = [p for p in [parent, parent2] if p is not None] commit_id = compute_commit_id(parent_ids, snap_id, message, _DT.isoformat()) write_commit(repo, CommitRecord( commit_id=commit_id, repo_id="test-repo", branch="main", snapshot_id=snap_id, message=message, committed_at=_DT, parent_commit_id=parent, parent2_commit_id=parent2, )) return commit_id def _set_head(repo: pathlib.Path, branch: str, commit_id: str) -> None: ref = repo / ".muse" / "refs" / "heads" / branch ref.parent.mkdir(parents=True, exist_ok=True) ref.write_text(commit_id) (repo / ".muse" / "HEAD").write_text(f"ref: refs/heads/{branch}") def _cg(repo: pathlib.Path, *args: str) -> InvokeResult: from muse.cli.app import main as cli return runner.invoke( cli, ["commit-graph", *args], env={"MUSE_REPO_ROOT": str(repo)}, ) # --------------------------------------------------------------------------- # Unit # --------------------------------------------------------------------------- class TestUnit: def test_commit_node_fields(self) -> None: from muse.cli.commands.plumbing.commit_graph import _CommitNode fields = set(_CommitNode.__annotations__.keys()) assert "commit_id" in fields assert "parent_commit_id" in fields assert "parent2_commit_id" in fields assert "message" in fields assert "snapshot_id" in fields assert "branch" in fields assert "committed_at" in fields assert "author" in fields def test_default_max(self) -> None: from muse.cli.commands.plumbing.commit_graph import _DEFAULT_MAX assert _DEFAULT_MAX >= 1000 def test_ancestors_of_single_commit(self, tmp_path: pathlib.Path) -> None: from muse.cli.commands.plumbing.commit_graph import _ancestors_of repo = _make_repo(tmp_path) snap_id = _snap(repo) cid = _commit(repo, snap_id) result = _ancestors_of(repo, cid) assert cid in result def test_ancestors_of_linear_chain(self, tmp_path: pathlib.Path) -> None: from muse.cli.commands.plumbing.commit_graph import _ancestors_of repo = _make_repo(tmp_path) snap_id = _snap(repo) c1 = _commit(repo, snap_id, message="c1") c2 = _commit(repo, snap_id, parent=c1, message="c2") c3 = _commit(repo, snap_id, parent=c2, message="c3") result = _ancestors_of(repo, c3) assert c1 in result assert c2 in result assert c3 in result def test_ancestors_of_merge_commit_follows_both_parents(self, tmp_path: pathlib.Path) -> None: from muse.cli.commands.plumbing.commit_graph import _ancestors_of repo = _make_repo(tmp_path) snap_id = _snap(repo) base = _commit(repo, snap_id, message="base") left = _commit(repo, snap_id, parent=base, message="left") right = _commit(repo, snap_id, parent=base, message="right") merge = _commit(repo, snap_id, parent=left, parent2=right, message="merge") result = _ancestors_of(repo, merge) assert base in result assert left in result assert right in result assert merge in result def test_ancestors_of_missing_commit_returns_partial(self, tmp_path: pathlib.Path) -> None: from muse.cli.commands.plumbing.commit_graph import _ancestors_of repo = _make_repo(tmp_path) missing = "ab" + "0" * 62 # Missing commit: returns set containing just the start ID that was visited result = _ancestors_of(repo, missing) # BFS: visits the missing cid, can't find record, stops assert missing in result # --------------------------------------------------------------------------- # Integration — JSON format # --------------------------------------------------------------------------- class TestJsonFormat: def test_linear_two_commits(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) snap_id = _snap(repo) c1 = _commit(repo, snap_id, message="c1") c2 = _commit(repo, snap_id, parent=c1, message="c2") _set_head(repo, "main", c2) result = _cg(repo) assert result.exit_code == 0 data = json.loads(result.output) assert data["count"] == 2 ids = {c["commit_id"] for c in data["commits"]} assert {c1, c2} == ids def test_tip_is_present_in_output(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) snap_id = _snap(repo) cid = _commit(repo, snap_id) _set_head(repo, "main", cid) data = json.loads(_cg(repo).output) assert data["tip"] == cid def test_explicit_tip(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) snap_id = _snap(repo) cid = _commit(repo, snap_id) result = _cg(repo, "--tip", cid) assert result.exit_code == 0 data = json.loads(result.output) assert data["tip"] == cid def test_json_shorthand(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) snap_id = _snap(repo) cid = _commit(repo, snap_id) _set_head(repo, "main", cid) result = _cg(repo, "--json") assert result.exit_code == 0 assert "commits" in json.loads(result.output) def test_truncated_flag_when_limited(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) snap_id = _snap(repo) c1 = _commit(repo, snap_id, message="c1") c2 = _commit(repo, snap_id, parent=c1, message="c2") _set_head(repo, "main", c2) data = json.loads(_cg(repo, "--max", "1").output) assert data["truncated"] is True # --------------------------------------------------------------------------- # Integration — --count # --------------------------------------------------------------------------- class TestCountOnly: def test_count_returns_integer(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) snap_id = _snap(repo) cid = _commit(repo, snap_id) _set_head(repo, "main", cid) data = json.loads(_cg(repo, "--count").output) assert data["count"] == 1 assert "commits" not in data def test_count_reflects_chain_length(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) snap_id = _snap(repo) c1 = _commit(repo, snap_id, message="c1") c2 = _commit(repo, snap_id, parent=c1, message="c2") c3 = _commit(repo, snap_id, parent=c2, message="c3") _set_head(repo, "main", c3) data = json.loads(_cg(repo, "--count").output) assert data["count"] == 3 # --------------------------------------------------------------------------- # Integration — --first-parent # --------------------------------------------------------------------------- class TestFirstParent: def test_first_parent_skips_merge_parent(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) snap_id = _snap(repo) p1 = _commit(repo, snap_id, message="p1") p2 = _commit(repo, snap_id, message="p2") merge = _commit(repo, snap_id, parent=p1, parent2=p2, message="merge") _set_head(repo, "main", merge) data = json.loads(_cg(repo, "--first-parent").output) ids = {c["commit_id"] for c in data["commits"]} assert p2 not in ids assert p1 in ids assert merge in ids # --------------------------------------------------------------------------- # Integration — --stop-at # --------------------------------------------------------------------------- class TestStopAt: def test_stop_at_excludes_old_commits(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) snap_id = _snap(repo) c1 = _commit(repo, snap_id, message="c1") c2 = _commit(repo, snap_id, parent=c1, message="c2") c3 = _commit(repo, snap_id, parent=c2, message="c3") _set_head(repo, "main", c3) data = json.loads(_cg(repo, "--stop-at", c2).output) ids = {c["commit_id"] for c in data["commits"]} assert c2 not in ids assert c1 not in ids assert c3 in ids # --------------------------------------------------------------------------- # Integration — text format # --------------------------------------------------------------------------- class TestTextFormat: def test_text_one_id_per_line(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) snap_id = _snap(repo) cid = _commit(repo, snap_id) _set_head(repo, "main", cid) result = _cg(repo, "--format", "text") assert result.exit_code == 0 assert cid in result.output # --------------------------------------------------------------------------- # Error cases # --------------------------------------------------------------------------- class TestErrors: def test_no_commits_errors(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) result = _cg(repo) assert result.exit_code == ExitCode.USER_ERROR def test_tip_not_found_errors(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) result = _cg(repo, "--tip", "dead" + "beef" * 15) assert result.exit_code == ExitCode.USER_ERROR def test_ancestry_path_without_stop_at_errors(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) snap_id = _snap(repo) cid = _commit(repo, snap_id) _set_head(repo, "main", cid) result = _cg(repo, "--ancestry-path") assert result.exit_code == ExitCode.USER_ERROR def test_no_traceback_on_bad_tip(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) result = _cg(repo, "--tip", "bad") assert "Traceback" not in result.output # --------------------------------------------------------------------------- # Stress # --------------------------------------------------------------------------- class TestSecurity: def test_format_error_to_stderr(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) r = _cg(repo, "--format", "xml") assert r.exit_code != 0 assert r.stdout_bytes == b"" assert "error" in r.stderr.lower() def test_no_traceback_on_bad_format(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) r = _cg(repo, "--format", "bad") assert "Traceback" not in r.output assert "Traceback" not in r.stderr def test_ansi_in_tip_rejected_gracefully(self, tmp_path: pathlib.Path) -> None: """An ANSI-injected tip ID must not crash; it's not a valid commit.""" repo = _make_repo(tmp_path) r = _cg(repo, "--tip", "\x1b[31mbad\x1b[0m") assert "Traceback" not in r.output assert "Traceback" not in r.stderr def test_json_shorthand_flag(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) snap_id = _snap(repo) cid = _commit(repo, snap_id) _set_head(repo, "main", cid) r = _cg(repo, "--json") assert r.exit_code == 0 d = json.loads(r.output) assert "commits" in d class TestStress: def test_50_commit_linear_chain(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) snap_id = _snap(repo) prev: str | None = None for i in range(50): prev = _commit(repo, snap_id, parent=prev, message=f"commit {i}") assert prev is not None _set_head(repo, "main", prev) data = json.loads(_cg(repo, "--count").output) assert data["count"] == 50 def test_branching_dag_100_commits(self, tmp_path: pathlib.Path) -> None: """10-branch DAG — --ancestry-path + --first-parent should complete.""" repo = _make_repo(tmp_path) snap_id = _snap(repo) base = _commit(repo, snap_id, message="base") tips: list[str] = [] for i in range(10): tip = _commit(repo, snap_id, parent=base, message=f"branch {i}") tips.append(tip) merge = _commit(repo, snap_id, parent=tips[-1], parent2=tips[-2], message="merge") _set_head(repo, "main", merge) r = _cg(repo, "--json") assert r.exit_code == 0 d = json.loads(r.output) commit_ids = {c["commit_id"] for c in d["commits"]} assert merge in commit_ids assert base in commit_ids def test_200_sequential_calls(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) snap_id = _snap(repo) cid = _commit(repo, snap_id) _set_head(repo, "main", cid) for i in range(200): r = _cg(repo) assert r.exit_code == 0, f"failed at {i}"