"""muse#54 item 9 -- `muse worktree add` / `muse worktree remove` must append reflog entries, so the full ref/worktree lifecycle is visible via `muse reflog`, not silently invisible. """ from __future__ import annotations import datetime import json import pathlib from tests.cli_test_helper import CliRunner from muse.core.types import fake_id, Manifest from muse.core.paths import heads_dir, muse_dir cli = None runner = CliRunner() 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, message: str = "test") -> str: from muse.core.commits import CommitRecord, write_commit from muse.core.snapshots import SnapshotRecord, write_snapshot from muse.core.ids import hash_commit as compute_commit_id, hash_snapshot as compute_snapshot_id ref_file = heads_dir(root) / "main" parent_id = ref_file.read_text().strip() if ref_file.exists() else None manifest: Manifest = {} snap_id = compute_snapshot_id(manifest) committed_at = datetime.datetime.now(datetime.timezone.utc) commit_id = compute_commit_id( 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=manifest)) write_commit(root, CommitRecord( commit_id=commit_id, branch="main", 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 class TestWorktreeAddReflog: def test_add_on_existing_branch_appends_entry(self, tmp_path: pathlib.Path) -> None: root, _ = _init_repo(tmp_path) commit_id = _make_commit(root) result = runner.invoke( cli, ["worktree", "add", "wt1", "main", "--json"], env=_env(root) ) assert result.exit_code == 0, result.output from muse.core.reflog import read_reflog entries = read_reflog(root, branch="main") assert any("worktree: created 'wt1'" in e.operation for e in entries) def test_add_with_new_branch_logs_branch_creation(self, tmp_path: pathlib.Path) -> None: root, _ = _init_repo(tmp_path) commit_id = _make_commit(root) result = runner.invoke( cli, ["worktree", "add", "wt2", "main", "-b", "feat/new", "--json"], env=_env(root), ) assert result.exit_code == 0, result.output from muse.core.reflog import read_reflog entries = read_reflog(root, branch="feat/new") # Two entries land on feat/new's own log: the branch-creation entry # (written first, so it's oldest / last in this newest-first list) # and the worktree-created audit entry (written second, newest). assert len(entries) == 2 assert "worktree: created 'wt2'" in entries[0].operation assert "branch: created from main via worktree add" in entries[-1].operation assert entries[-1].new_id == commit_id class TestWorktreeRemoveReflog: def test_remove_appends_entry_to_branch_log(self, tmp_path: pathlib.Path) -> None: root, _ = _init_repo(tmp_path) _make_commit(root) add_result = runner.invoke( cli, ["worktree", "add", "wt3", "main", "--json"], env=_env(root) ) assert add_result.exit_code == 0, add_result.output remove_result = runner.invoke( cli, ["worktree", "remove", "wt3", "--json"], env=_env(root) ) assert remove_result.exit_code == 0, remove_result.output from muse.core.reflog import read_reflog entries = read_reflog(root, branch="main") assert any("worktree: removed 'wt3'" in e.operation for e in entries) def test_remove_nonexistent_worktree_does_not_crash( self, tmp_path: pathlib.Path ) -> None: root, _ = _init_repo(tmp_path) _make_commit(root) result = runner.invoke( cli, ["worktree", "remove", "ghost", "--json"], env=_env(root) ) assert result.exit_code == 1 # existing "not found" behavior unaffected