"""Supercharge tests for ``muse worktree`` — agent-usability, coverage gaps. Coverage matrix --------------- - duration_ms: every JSON-outputting subcommand includes it - exit_code: every JSON-outputting subcommand includes it - list envelope: {worktrees, exit_code, duration_ms} — not a bare array - TypedDicts: verify new fields exist in class annotations - Docstrings: every handler docstring mentions exit_code and duration_ms - Performance: duration_ms is a non-negative float within bounds """ from __future__ import annotations import argparse import json import pathlib import pytest from tests.cli_test_helper import CliRunner, InvokeResult from muse.core.types import NULL_COMMIT_ID from muse.core.paths import muse_dir, ref_path from muse.cli.commands.worktree import ( _WorktreeAddJson, _WorktreeListEntryJson, _WorktreeRemoveJson, _WorktreePruneJson, run_worktree_add, run_worktree_list, run_worktree_remove, run_worktree_prune, run_worktree_repair, run_worktree_status, ) runner = CliRunner() # --------------------------------------------------------------------------- # Helpers # --------------------------------------------------------------------------- def _make_repo(tmp_path: pathlib.Path, branch: str = "main") -> pathlib.Path: repo = tmp_path / "myproject" dot_muse = muse_dir(repo) for d in ("objects", "commits", "snapshots", "refs/heads"): (dot_muse / d).mkdir(parents=True, exist_ok=True) (dot_muse / "repo.json").write_text(json.dumps({"repo_id": "test-repo"})) (dot_muse / "HEAD").write_text(f"ref: refs/heads/{branch}\n") (dot_muse / "refs" / "heads" / branch).write_text(NULL_COMMIT_ID) return repo def _add_branch(repo: pathlib.Path, branch: str) -> None: branch_ref = ref_path(repo, branch) branch_ref.parent.mkdir(parents=True, exist_ok=True) branch_ref.write_text(NULL_COMMIT_ID) def _invoke(repo: pathlib.Path, args: list[str]) -> InvokeResult: return runner.invoke(None, args, env={"MUSE_REPO_ROOT": str(repo)}) def _add_worktree(repo: pathlib.Path, name: str = "wt1", branch: str = "main") -> None: _invoke(repo, ["worktree", "add", name, branch]) # --------------------------------------------------------------------------- # duration_ms — every JSON subcommand must include it # --------------------------------------------------------------------------- class TestDurationMs: def test_add_json_has_duration_ms(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) r = _invoke(repo, ["worktree", "add", "wt1", "main", "--json"]) assert r.exit_code == 0 assert "duration_ms" in json.loads(r.output) def test_list_json_has_duration_ms(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) r = _invoke(repo, ["worktree", "list", "--json"]) assert r.exit_code == 0 assert "duration_ms" in json.loads(r.output) def test_status_json_has_duration_ms(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) r = _invoke(repo, ["worktree", "status", "main", "--json"]) assert r.exit_code == 0 assert "duration_ms" in json.loads(r.output) def test_remove_json_has_duration_ms(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) _add_worktree(repo) r = _invoke(repo, ["worktree", "remove", "wt1", "--json"]) assert r.exit_code == 0 assert "duration_ms" in json.loads(r.output) def test_prune_json_has_duration_ms(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) r = _invoke(repo, ["worktree", "prune", "--json"]) assert r.exit_code == 0 assert "duration_ms" in json.loads(r.output) def test_repair_json_has_duration_ms(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) r = _invoke(repo, ["worktree", "repair", "--json"]) assert r.exit_code == 0 assert "duration_ms" in json.loads(r.output) def test_duration_ms_is_numeric(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) r = _invoke(repo, ["worktree", "list", "--json"]) data = json.loads(r.output) assert isinstance(data["duration_ms"], (int, float)) def test_duration_ms_is_non_negative(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) r = _invoke(repo, ["worktree", "list", "--json"]) data = json.loads(r.output) assert data["duration_ms"] >= 0 # --------------------------------------------------------------------------- # exit_code — every JSON subcommand must include it # --------------------------------------------------------------------------- class TestExitCode: def test_add_json_has_exit_code(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) r = _invoke(repo, ["worktree", "add", "wt1", "main", "--json"]) assert r.exit_code == 0 assert "exit_code" in json.loads(r.output) def test_list_json_has_exit_code(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) r = _invoke(repo, ["worktree", "list", "--json"]) assert r.exit_code == 0 assert "exit_code" in json.loads(r.output) def test_status_json_has_exit_code(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) r = _invoke(repo, ["worktree", "status", "main", "--json"]) assert r.exit_code == 0 assert "exit_code" in json.loads(r.output) def test_remove_json_has_exit_code(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) _add_worktree(repo) r = _invoke(repo, ["worktree", "remove", "wt1", "--json"]) assert r.exit_code == 0 assert "exit_code" in json.loads(r.output) def test_prune_json_has_exit_code(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) r = _invoke(repo, ["worktree", "prune", "--json"]) assert r.exit_code == 0 assert "exit_code" in json.loads(r.output) def test_repair_json_has_exit_code(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) r = _invoke(repo, ["worktree", "repair", "--json"]) assert r.exit_code == 0 assert "exit_code" in json.loads(r.output) def test_add_exit_code_zero_on_success(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) r = _invoke(repo, ["worktree", "add", "wt1", "main", "--json"]) assert json.loads(r.output)["exit_code"] == 0 def test_list_exit_code_zero(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) r = _invoke(repo, ["worktree", "list", "--json"]) assert json.loads(r.output)["exit_code"] == 0 def test_prune_exit_code_zero_nothing_to_prune(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) r = _invoke(repo, ["worktree", "prune", "--json"]) assert json.loads(r.output)["exit_code"] == 0 def test_exit_code_is_int(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) r = _invoke(repo, ["worktree", "list", "--json"]) assert isinstance(json.loads(r.output)["exit_code"], int) def test_exit_code_mirrors_process_exit(self, tmp_path: pathlib.Path) -> None: """exit_code in JSON must equal the process exit code on success.""" repo = _make_repo(tmp_path) r = _invoke(repo, ["worktree", "add", "wt1", "main", "--json"]) data = json.loads(r.output) assert data["exit_code"] == r.exit_code # --------------------------------------------------------------------------- # list envelope — {worktrees, exit_code, duration_ms}, not a bare array # --------------------------------------------------------------------------- class TestListEnvelope: def test_list_json_is_dict_not_array(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) r = _invoke(repo, ["worktree", "list", "--json"]) data = json.loads(r.output) assert isinstance(data, dict), "list --json must return an envelope dict, not a bare array" def test_list_json_has_worktrees_key(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) r = _invoke(repo, ["worktree", "list", "--json"]) data = json.loads(r.output) assert "worktrees" in data def test_list_json_worktrees_is_array(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) r = _invoke(repo, ["worktree", "list", "--json"]) data = json.loads(r.output) assert isinstance(data["worktrees"], list) def test_list_json_includes_main_in_worktrees(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) r = _invoke(repo, ["worktree", "list", "--json"]) data = json.loads(r.output) assert any(w["is_main"] for w in data["worktrees"]) def test_list_json_entry_fields_intact(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) r = _invoke(repo, ["worktree", "list", "--json"]) entry = json.loads(r.output)["worktrees"][0] for key in ("name", "branch", "path", "head_commit", "is_main"): assert key in entry, f"worktree entry missing key: {key}" def test_list_json_count_after_add(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) _add_worktree(repo, "wt1") r = _invoke(repo, ["worktree", "list", "--json"]) data = json.loads(r.output) # main + wt1 assert len(data["worktrees"]) == 2 def test_list_j_alias_envelope(self, tmp_path: pathlib.Path) -> None: """-j and --json produce the same envelope structure.""" repo = _make_repo(tmp_path) r1 = _invoke(repo, ["worktree", "list", "--json"]) r2 = _invoke(repo, ["worktree", "list", "-j"]) d1 = json.loads(r1.output); d1.pop("duration_ms", None); d1.pop("timestamp", None) d2 = json.loads(r2.output); d2.pop("duration_ms", None); d2.pop("timestamp", None) assert d1 == d2 # --------------------------------------------------------------------------- # TypedDicts — verify annotations carry the new fields # --------------------------------------------------------------------------- class TestTypedDicts: def test_worktree_add_json_has_duration_ms_annotation(self) -> None: assert "duration_ms" in _WorktreeAddJson.__annotations__ def test_worktree_add_json_has_exit_code_annotation(self) -> None: assert "exit_code" in _WorktreeAddJson.__annotations__ def test_worktree_list_entry_json_unchanged(self) -> None: """List entry TypedDict keeps its 5 data fields.""" for field in ("name", "branch", "path", "head_commit", "is_main"): assert field in _WorktreeListEntryJson.__annotations__ def test_worktree_remove_json_has_duration_ms_annotation(self) -> None: assert "duration_ms" in _WorktreeRemoveJson.__annotations__ def test_worktree_remove_json_has_exit_code_annotation(self) -> None: assert "exit_code" in _WorktreeRemoveJson.__annotations__ def test_worktree_prune_json_has_duration_ms_annotation(self) -> None: assert "duration_ms" in _WorktreePruneJson.__annotations__ def test_worktree_prune_json_has_exit_code_annotation(self) -> None: assert "exit_code" in _WorktreePruneJson.__annotations__ def test_worktree_list_json_typeddict_exists(self) -> None: """_WorktreeListJson envelope TypedDict must exist.""" from muse.cli.commands.worktree import _WorktreeListJson assert "worktrees" in _WorktreeListJson.__annotations__ assert "exit_code" in _WorktreeListJson.__annotations__ assert "duration_ms" in _WorktreeListJson.__annotations__ def test_worktree_status_json_typeddict_exists(self) -> None: """_WorktreeStatusJson TypedDict must exist with exit_code/duration_ms.""" from muse.cli.commands.worktree import _WorktreeStatusJson assert "exit_code" in _WorktreeStatusJson.__annotations__ assert "duration_ms" in _WorktreeStatusJson.__annotations__ def test_worktree_repair_json_typeddict_exists(self) -> None: """_WorktreeRepairJson TypedDict must exist with exit_code/duration_ms.""" from muse.cli.commands.worktree import _WorktreeRepairJson assert "repaired" in _WorktreeRepairJson.__annotations__ assert "exit_code" in _WorktreeRepairJson.__annotations__ assert "duration_ms" in _WorktreeRepairJson.__annotations__ # --------------------------------------------------------------------------- # Docstrings — handlers must document exit_code and duration_ms # --------------------------------------------------------------------------- class TestDocstrings: def test_add_docstring_mentions_exit_code(self) -> None: assert "exit_code" in (run_worktree_add.__doc__ or "") def test_add_docstring_mentions_duration_ms(self) -> None: assert "duration_ms" in (run_worktree_add.__doc__ or "") def test_list_docstring_mentions_exit_code(self) -> None: assert "exit_code" in (run_worktree_list.__doc__ or "") def test_list_docstring_mentions_duration_ms(self) -> None: assert "duration_ms" in (run_worktree_list.__doc__ or "") def test_status_docstring_mentions_exit_code(self) -> None: assert "exit_code" in (run_worktree_status.__doc__ or "") def test_status_docstring_mentions_duration_ms(self) -> None: assert "duration_ms" in (run_worktree_status.__doc__ or "") def test_remove_docstring_mentions_exit_code(self) -> None: assert "exit_code" in (run_worktree_remove.__doc__ or "") def test_remove_docstring_mentions_duration_ms(self) -> None: assert "duration_ms" in (run_worktree_remove.__doc__ or "") def test_prune_docstring_mentions_exit_code(self) -> None: assert "exit_code" in (run_worktree_prune.__doc__ or "") def test_prune_docstring_mentions_duration_ms(self) -> None: assert "duration_ms" in (run_worktree_prune.__doc__ or "") def test_repair_docstring_mentions_exit_code(self) -> None: assert "exit_code" in (run_worktree_repair.__doc__ or "") def test_repair_docstring_mentions_duration_ms(self) -> None: assert "duration_ms" in (run_worktree_repair.__doc__ or "") # --------------------------------------------------------------------------- # Performance — duration_ms stays within reason # --------------------------------------------------------------------------- class TestPerformance: def test_list_duration_ms_under_1000(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) r = _invoke(repo, ["worktree", "list", "--json"]) assert r.exit_code == 0 assert json.loads(r.output)["duration_ms"] < 1000 def test_add_duration_ms_under_1000(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) r = _invoke(repo, ["worktree", "add", "wt1", "main", "--json"]) assert r.exit_code == 0 assert json.loads(r.output)["duration_ms"] < 1000 def test_prune_duration_ms_under_1000(self, tmp_path: pathlib.Path) -> None: repo = _make_repo(tmp_path) r = _invoke(repo, ["worktree", "prune", "--json"]) assert r.exit_code == 0 assert json.loads(r.output)["duration_ms"] < 1000 # --------------------------------------------------------------------------- # Flag registration # --------------------------------------------------------------------------- class TestRegisterFlags: def _parse(self, *args: str) -> "argparse.Namespace": import argparse from muse.cli.commands.worktree import register p = argparse.ArgumentParser() sub = p.add_subparsers() register(sub) return p.parse_args(["worktree", *args]) def test_default_json_out_is_false_list(self) -> None: ns = self._parse("list") assert ns.json_out is False def test_json_flag_sets_json_out_list(self) -> None: ns = self._parse("list", "--json") assert ns.json_out is True def test_j_shorthand_sets_json_out_list(self) -> None: ns = self._parse("list", "-j") assert ns.json_out is True