gabriel / muse public
test_cmd_worktree.py python
138 lines 5.1 KB
Raw
sha256:51ce277f663e01a43eaffbe77509b1de7ac2d4251b55d23306304bcdeb92c90d feat(pack): delta-encode snapshots in MPackBundle wire format Sonnet 4.6 minor ⚠ breaking 120 days ago
1 """Comprehensive tests for ``muse worktree``.
2
3 Covers:
4 - E2E: add, list, remove worktrees
5 - Integration: worktree directory created and removed
6 - Security: sanitized path output
7 """
8
9 from __future__ import annotations
10
11 import datetime
12 import json
13 import pathlib
14
15 import pytest
16 from tests.cli_test_helper import CliRunner
17 from muse.core.types import fake_id
18 from muse.core.paths import heads_dir, muse_dir
19
20 cli = None # argparse migration — CliRunner ignores this arg
21
22 runner = CliRunner()
23
24
25 # ---------------------------------------------------------------------------
26 # Shared helpers
27 # ---------------------------------------------------------------------------
28
29 def _env(root: pathlib.Path) -> Manifest:
30 return {"MUSE_REPO_ROOT": str(root)}
31
32
33 def _init_repo(tmp_path: pathlib.Path) -> tuple[pathlib.Path, str]:
34 dot_muse = muse_dir(tmp_path)
35 dot_muse.mkdir()
36 repo_id = fake_id("repo")
37 (dot_muse / "repo.json").write_text(json.dumps({
38 "repo_id": repo_id,
39 "domain": "midi",
40 "default_branch": "main",
41 "created_at": "2025-01-01T00:00:00+00:00",
42 }), encoding="utf-8")
43 (dot_muse / "HEAD").write_text("ref: refs/heads/main", encoding="utf-8")
44 (dot_muse / "refs" / "heads").mkdir(parents=True)
45 (dot_muse / "snapshots").mkdir()
46 (dot_muse / "commits").mkdir()
47 (dot_muse / "objects").mkdir()
48 return tmp_path, repo_id
49
50
51 def _make_commit(root: pathlib.Path, repo_id: str, message: str = "test") -> str:
52 from muse.core.store import CommitRecord, SnapshotRecord, write_commit, write_snapshot
53 from muse.core.snapshot import compute_snapshot_id, compute_commit_id
54
55 ref_file = heads_dir(root) / "main"
56 parent_id = ref_file.read_text().strip() if ref_file.exists() else None
57 manifest: Manifest = {}
58 snap_id = compute_snapshot_id(manifest)
59 committed_at = datetime.datetime.now(datetime.timezone.utc)
60 commit_id = compute_commit_id( parent_ids=[parent_id] if parent_id else [],
61 snapshot_id=snap_id, message=message,
62 committed_at_iso=committed_at.isoformat(),
63 )
64 write_snapshot(root, SnapshotRecord(snapshot_id=snap_id, manifest=manifest))
65 write_commit(root, CommitRecord(
66 commit_id=commit_id, repo_id=repo_id, branch="main",
67 snapshot_id=snap_id, message=message, committed_at=committed_at,
68 parent_commit_id=parent_id,
69 ))
70 ref_file.parent.mkdir(parents=True, exist_ok=True)
71 ref_file.write_text(commit_id, encoding="utf-8")
72 return commit_id
73
74
75 # ---------------------------------------------------------------------------
76 # Tests
77 # ---------------------------------------------------------------------------
78
79 class TestWorktreeCLI:
80 def test_worktree_list_empty(self, tmp_path: pathlib.Path) -> None:
81 root, _ = _init_repo(tmp_path)
82 result = runner.invoke(cli, ["worktree", "list"], env=_env(root), catch_exceptions=False)
83 assert result.exit_code == 0
84
85 def test_worktree_add_creates_directory(self, tmp_path: pathlib.Path) -> None:
86 root, repo_id = _init_repo(tmp_path)
87 _make_commit(root, repo_id)
88 result = runner.invoke(
89 cli, ["worktree", "add", "my-wt", "main"],
90 env=_env(root), catch_exceptions=False
91 )
92 assert result.exit_code == 0
93
94 def test_worktree_list_after_add(self, tmp_path: pathlib.Path) -> None:
95 root, repo_id = _init_repo(tmp_path)
96 _make_commit(root, repo_id)
97 runner.invoke(
98 cli, ["worktree", "add", "my-wt2", "main"],
99 env=_env(root), catch_exceptions=False
100 )
101 result = runner.invoke(cli, ["worktree", "list"], env=_env(root), catch_exceptions=False)
102 assert result.exit_code == 0
103
104 def test_worktree_remove(self, tmp_path: pathlib.Path) -> None:
105 root, repo_id = _init_repo(tmp_path)
106 _make_commit(root, repo_id)
107 runner.invoke(
108 cli, ["worktree", "add", "rm-wt", "main"],
109 env=_env(root), catch_exceptions=False
110 )
111 result = runner.invoke(
112 cli, ["worktree", "remove", "rm-wt"],
113 env=_env(root), catch_exceptions=False
114 )
115 assert result.exit_code == 0
116
117 def test_worktree_list_format_json(self, tmp_path: pathlib.Path) -> None:
118 root, _ = _init_repo(tmp_path)
119 result = runner.invoke(
120 cli, ["worktree", "list", "--json"],
121 env=_env(root), catch_exceptions=False
122 )
123 assert result.exit_code == 0
124 data = json.loads(result.output)
125 envelope = data if isinstance(data, dict) else {}
126 worktrees = envelope.get("worktrees", data)
127 assert isinstance(worktrees, list)
128
129 def test_worktree_output_sanitized(self, tmp_path: pathlib.Path) -> None:
130 root, repo_id = _init_repo(tmp_path)
131 _make_commit(root, repo_id)
132 result = runner.invoke(cli, ["worktree", "list"], env=_env(root), catch_exceptions=False)
133 assert "\x1b" not in result.output
134
135 def test_worktree_remove_nonexistent_fails(self, tmp_path: pathlib.Path) -> None:
136 root, _ = _init_repo(tmp_path)
137 result = runner.invoke(cli, ["worktree", "remove", "nonexistent"], env=_env(root))
138 assert result.exit_code != 0
File History 1 commit
sha256:51ce277f663e01a43eaffbe77509b1de7ac2d4251b55d23306304bcdeb92c90d feat(pack): delta-encode snapshots in MPackBundle wire format Sonnet 4.6 minor 120 days ago