"""Comprehensive tests for ``muse cherry-pick``. Covers: - E2E: cherry-pick a specific commit onto current branch - Integration: commit is replayed, creates new commit - Security: sanitized output for conflict paths - Stress: cherry-pick many commits """ from __future__ import annotations import datetime import json import pathlib import pytest from tests.cli_test_helper import CliRunner from muse.core.types import long_id, fake_id, blob_id from muse.core.paths import heads_dir, ref_path, muse_dir cli = None # argparse migration — CliRunner ignores this arg runner = CliRunner() # --------------------------------------------------------------------------- # Shared helpers # --------------------------------------------------------------------------- def _env(root: pathlib.Path) -> Manifest: return {"MUSE_REPO_ROOT": str(root)} def _init_repo(tmp_path: pathlib.Path) -> tuple[pathlib.Path, str]: dot_muse = muse_dir(tmp_path) dot_muse.mkdir() repo_id = fake_id("repo") (dot_muse / "repo.json").write_text(json.dumps({ "repo_id": repo_id, "domain": "code", "default_branch": "main", "created_at": "2025-01-01T00:00:00+00:00", }), encoding="utf-8") (dot_muse / "HEAD").write_text("ref: refs/heads/main", encoding="utf-8") (dot_muse / "refs" / "heads").mkdir(parents=True) (dot_muse / "snapshots").mkdir() (dot_muse / "commits").mkdir() (dot_muse / "objects").mkdir() return tmp_path, repo_id def _make_commit(root: pathlib.Path, repo_id: str, branch: str = "main", message: str = "test", manifest: Manifest | None = None) -> str: from muse.core.commits import ( CommitRecord, write_commit, ) from muse.core.snapshots import ( SnapshotRecord, write_snapshot, ) from muse.core.ids import hash_commit, hash_snapshot ref_file = ref_path(root, branch) parent_id = ref_file.read_text().strip() if ref_file.exists() else None m = manifest or {} snap_id = hash_snapshot(m) committed_at = datetime.datetime.now(datetime.timezone.utc) commit_id = hash_commit( parent_ids=[parent_id] if parent_id else [], snapshot_id=snap_id, message=message, committed_at_iso=committed_at.isoformat(), ) write_snapshot(root, SnapshotRecord(snapshot_id=snap_id, manifest=m)) write_commit(root, CommitRecord( commit_id=commit_id, branch=branch, snapshot_id=snap_id, message=message, committed_at=committed_at, parent_commit_id=parent_id, )) ref_file.parent.mkdir(parents=True, exist_ok=True) ref_file.write_text(commit_id, encoding="utf-8") return commit_id def _write_object(root: pathlib.Path, content: bytes) -> str: from muse.core.object_store import write_object oid = blob_id(content) write_object(root, oid, content) return oid # --------------------------------------------------------------------------- # Parser flag tests # --------------------------------------------------------------------------- class TestRegisterFlags: def _parse(self, *args: str) -> "argparse.Namespace": import argparse from muse.cli.commands.cherry_pick import register p = argparse.ArgumentParser() sub = p.add_subparsers() register(sub) return p.parse_args(["cherry-pick", *args]) def test_default_json_out_is_false(self) -> None: ns = self._parse("abc123") assert ns.json_out is False def test_json_flag_sets_json_out(self) -> None: ns = self._parse("--json", "abc123") assert ns.json_out is True def test_j_shorthand_sets_json_out(self) -> None: ns = self._parse("-j", "abc123") assert ns.json_out is True # --------------------------------------------------------------------------- # Tests # --------------------------------------------------------------------------- class TestCherryPickCLI: def test_cherry_pick_commit_from_another_branch(self, tmp_path: pathlib.Path) -> None: root, repo_id = _init_repo(tmp_path) base = _make_commit(root, repo_id, branch="main", message="base") (heads_dir(root) / "feature").write_text(base) obj = _write_object(root, b"feature content") feature_commit = _make_commit(root, repo_id, branch="feature", message="feature work", manifest={"new.mid": obj}) result = runner.invoke( cli, ["cherry-pick", feature_commit], env=_env(root), catch_exceptions=False ) assert result.exit_code == 0 def test_cherry_pick_invalid_commit_fails(self, tmp_path: pathlib.Path) -> None: root, repo_id = _init_repo(tmp_path) _make_commit(root, repo_id) result = runner.invoke(cli, ["cherry-pick", "deadbeef" * 8], env=_env(root)) assert result.exit_code != 0 def test_cherry_pick_creates_new_commit(self, tmp_path: pathlib.Path) -> None: root, repo_id = _init_repo(tmp_path) base = _make_commit(root, repo_id, branch="main", message="base") (heads_dir(root) / "feature").write_text(base) obj = _write_object(root, b"cherry content") feature_commit = _make_commit(root, repo_id, branch="feature", message="cherry", manifest={"c.mid": obj}) original_head = (heads_dir(root) / "main").read_text().strip() runner.invoke(cli, ["cherry-pick", feature_commit], env=_env(root), catch_exceptions=False) new_head = (heads_dir(root) / "main").read_text().strip() assert new_head != original_head def test_cherry_pick_format_json(self, tmp_path: pathlib.Path) -> None: root, repo_id = _init_repo(tmp_path) base = _make_commit(root, repo_id, branch="main", message="base") (heads_dir(root) / "feature").write_text(base) obj = _write_object(root, b"json pick") feature_commit = _make_commit(root, repo_id, branch="feature", message="json", manifest={"j.mid": obj}) result = runner.invoke( cli, ["cherry-pick", "--json", feature_commit], env=_env(root), catch_exceptions=False ) assert result.exit_code == 0 data = json.loads(result.output) assert isinstance(data, dict) def test_cherry_pick_output_sanitized(self, tmp_path: pathlib.Path) -> None: root, repo_id = _init_repo(tmp_path) base = _make_commit(root, repo_id, branch="main", message="base") (heads_dir(root) / "feature").write_text(base) obj = _write_object(root, b"safe content") feature_commit = _make_commit(root, repo_id, branch="feature", message="safe", manifest={"s.mid": obj}) result = runner.invoke(cli, ["cherry-pick", feature_commit], env=_env(root), catch_exceptions=False) assert "\x1b" not in result.output class TestCherryPickStress: def test_cherry_pick_sequence(self, tmp_path: pathlib.Path) -> None: root, repo_id = _init_repo(tmp_path) base = _make_commit(root, repo_id, branch="main", message="base") (heads_dir(root) / "feature").write_text(base) commits = [] for i in range(5): obj = _write_object(root, f"content {i}".encode()) c = _make_commit(root, repo_id, branch="feature", message=f"commit {i}", manifest={f"f{i}.mid": obj}) commits.append(c) for commit_id in commits: result = runner.invoke( cli, ["cherry-pick", commit_id], env=_env(root), catch_exceptions=False ) assert result.exit_code == 0