gabriel / muse public
test_cli_clone.py python
297 lines 10.5 KB
Raw
sha256:660fcac1df3ab28f61862e961890bd2ca8b754fa0242079d93ca1e25037ec8a6 chore(tests): add docstring to tests/__init__.py so rc14 tr… Human 26 days ago
1 """Tests for muse clone CLI command."""
2
3 from __future__ import annotations
4
5 import base64
6 import hashlib
7 import json
8 import pathlib
9 import unittest.mock
10
11 import pytest
12 from tests.cli_test_helper import CliRunner
13
14 cli = None # argparse migration — CliRunner ignores this arg
15 from muse.cli.config import get_remote, get_upstream
16 from muse.core.pack import ObjectPayload, PackBundle, RemoteInfo
17 from muse.core.snapshot import compute_commit_id, compute_snapshot_id
18 from muse.core.store import read_commit, read_snapshot
19 from muse.core.transport import TransportError
20 from muse.core._types import Manifest
21
22 runner = CliRunner()
23
24
25 # ---------------------------------------------------------------------------
26 # Helpers
27 # ---------------------------------------------------------------------------
28
29
30 def _sha(content: bytes) -> str:
31 return hashlib.sha256(content).hexdigest()
32
33
34 def _make_remote_info(
35 domain: str = "midi",
36 default_branch: str = "main",
37 branch_heads: Manifest | None = None,
38 repo_id: str = "remote-repo-id",
39 ) -> RemoteInfo:
40 return RemoteInfo(
41 repo_id=repo_id,
42 domain=domain,
43 default_branch=default_branch,
44 branch_heads={"main": "c" * 64} if branch_heads is None else branch_heads,
45 )
46
47
48 def _make_bundle(branch: str = "main", message: str = "initial") -> PackBundle:
49 """Build a PackBundle whose commit/snapshot IDs are content-addressed.
50
51 Uses ``compute_commit_id`` and ``compute_snapshot_id`` so that
52 ``read_commit``/``read_snapshot`` pass ``_verify_commit_id``/
53 ``_verify_snapshot_id`` on the receiving end.
54 """
55 content = b"cloned content"
56 oid = _sha(content)
57 committed_at = "2026-01-01T00:00:00+00:00"
58 snap_id = compute_snapshot_id({"hello.txt": oid})
59 cid = compute_commit_id([], snap_id, message, committed_at)
60 return PackBundle(
61 commits=[
62 {
63 "commit_id": cid,
64 "repo_id": "remote-repo-id",
65 "branch": branch,
66 "snapshot_id": snap_id,
67 "message": message,
68 "committed_at": committed_at,
69 "parent_commit_id": None,
70 "parent2_commit_id": None,
71 "author": "alice",
72 "metadata": {},
73 "structured_delta": None,
74 "sem_ver_bump": "none",
75 "breaking_changes": [],
76 "agent_id": "",
77 "model_id": "",
78 "toolchain_id": "",
79 "prompt_hash": "",
80 "signature": "",
81 "signer_key_id": "",
82 "format_version": 5,
83 "reviewed_by": [],
84 "test_runs": 0,
85 }
86 ],
87 snapshots=[
88 {
89 "snapshot_id": snap_id,
90 "manifest": {"hello.txt": oid},
91 "created_at": committed_at,
92 }
93 ],
94 objects=[
95 ObjectPayload(object_id=oid, content=content)
96 ],
97 branch_heads={branch: cid},
98 )
99
100
101 def _mock_transport(info: RemoteInfo, bundle: PackBundle) -> unittest.mock.MagicMock:
102 mock = unittest.mock.MagicMock()
103 mock.fetch_remote_info.return_value = info
104 mock.fetch_pack.return_value = bundle
105 return mock
106
107
108 # ---------------------------------------------------------------------------
109 # Tests
110 # ---------------------------------------------------------------------------
111
112
113 class TestClone:
114 def test_clone_creates_muse_dir(
115 self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
116 ) -> None:
117 monkeypatch.chdir(tmp_path)
118 info = _make_remote_info()
119 bundle = _make_bundle()
120 mock = _mock_transport(info, bundle)
121
122 with unittest.mock.patch("muse.cli.commands.clone.make_transport", return_value=mock):
123 result = runner.invoke(
124 cli, ["clone", "https://hub.example.com/repos/r1", "my-repo"]
125 )
126
127 assert result.exit_code == 0, result.output
128 assert (tmp_path / "my-repo" / ".muse").is_dir()
129
130 def test_clone_sets_origin_remote(
131 self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
132 ) -> None:
133 monkeypatch.chdir(tmp_path)
134 info = _make_remote_info()
135 bundle = _make_bundle()
136 mock = _mock_transport(info, bundle)
137
138 with unittest.mock.patch("muse.cli.commands.clone.make_transport", return_value=mock):
139 runner.invoke(
140 cli, ["clone", "https://hub.example.com/repos/r1", "my-repo"]
141 )
142
143 origin = get_remote("origin", tmp_path / "my-repo")
144 assert origin == "https://hub.example.com/repos/r1"
145
146 def test_clone_sets_upstream_tracking(
147 self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
148 ) -> None:
149 monkeypatch.chdir(tmp_path)
150 info = _make_remote_info()
151 bundle = _make_bundle()
152 mock = _mock_transport(info, bundle)
153
154 with unittest.mock.patch("muse.cli.commands.clone.make_transport", return_value=mock):
155 runner.invoke(
156 cli, ["clone", "https://hub.example.com/repos/r1", "my-repo"]
157 )
158
159 upstream = get_upstream("main", tmp_path / "my-repo")
160 assert upstream == "origin"
161
162 def test_clone_writes_commits_and_snapshots(
163 self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
164 ) -> None:
165 monkeypatch.chdir(tmp_path)
166 info = _make_remote_info()
167 bundle = _make_bundle()
168 mock = _mock_transport(info, bundle)
169
170 with unittest.mock.patch("muse.cli.commands.clone.make_transport", return_value=mock):
171 runner.invoke(
172 cli, ["clone", "https://hub.example.com/repos/r1", "dest"]
173 )
174
175 dest = tmp_path / "dest"
176 cid = bundle["commits"][0]["commit_id"]
177 snap_id = bundle["snapshots"][0]["snapshot_id"]
178 assert isinstance(cid, str) and isinstance(snap_id, str)
179 assert read_commit(dest, cid) is not None
180 assert read_snapshot(dest, snap_id) is not None
181
182 def test_clone_propagates_domain(
183 self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
184 ) -> None:
185 monkeypatch.chdir(tmp_path)
186 info = _make_remote_info(domain="code")
187 bundle = _make_bundle()
188 mock = _mock_transport(info, bundle)
189
190 with unittest.mock.patch("muse.cli.commands.clone.make_transport", return_value=mock):
191 runner.invoke(
192 cli, ["clone", "https://hub.example.com/repos/r1", "dest"]
193 )
194
195 dest = tmp_path / "dest"
196 repo_meta = json.loads((dest / ".muse" / "repo.json").read_text())
197 assert repo_meta["domain"] == "code"
198
199 def test_clone_uses_remote_repo_id(
200 self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
201 ) -> None:
202 monkeypatch.chdir(tmp_path)
203 info = _make_remote_info(repo_id="the-real-repo-id")
204 bundle = _make_bundle()
205 mock = _mock_transport(info, bundle)
206
207 with unittest.mock.patch("muse.cli.commands.clone.make_transport", return_value=mock):
208 runner.invoke(
209 cli, ["clone", "https://hub.example.com/repos/r1", "dest"]
210 )
211
212 dest = tmp_path / "dest"
213 repo_meta = json.loads((dest / ".muse" / "repo.json").read_text())
214 assert repo_meta["repo_id"] == "the-real-repo-id"
215
216 def test_clone_infers_directory_name_from_url(
217 self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
218 ) -> None:
219 monkeypatch.chdir(tmp_path)
220 info = _make_remote_info()
221 bundle = _make_bundle()
222 mock = _mock_transport(info, bundle)
223
224 with unittest.mock.patch("muse.cli.commands.clone.make_transport", return_value=mock):
225 runner.invoke(cli, ["clone", "https://hub.example.com/repos/my-project"])
226
227 assert (tmp_path / "my-project" / ".muse").is_dir()
228
229 def test_clone_transport_error_fails_cleanly(
230 self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
231 ) -> None:
232 monkeypatch.chdir(tmp_path)
233 mock = unittest.mock.MagicMock()
234 mock.fetch_remote_info.side_effect = TransportError("connection refused", 0)
235
236 with unittest.mock.patch("muse.cli.commands.clone.make_transport", return_value=mock):
237 result = runner.invoke(
238 cli, ["clone", "https://hub.example.com/repos/r1", "dest"]
239 )
240
241 assert result.exit_code != 0
242 assert "Cannot reach remote" in result.output
243
244 def test_clone_existing_repo_fails(
245 self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
246 ) -> None:
247 monkeypatch.chdir(tmp_path)
248 # Pre-create a .muse directory.
249 (tmp_path / "dest" / ".muse").mkdir(parents=True)
250
251 mock = unittest.mock.MagicMock()
252 with unittest.mock.patch("muse.cli.commands.clone.make_transport", return_value=mock):
253 result = runner.invoke(
254 cli, ["clone", "https://hub.example.com/repos/r1", "dest"]
255 )
256
257 assert result.exit_code != 0
258 assert "already a Muse repository" in result.output
259
260 def test_clone_empty_repo_fails(
261 self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
262 ) -> None:
263 monkeypatch.chdir(tmp_path)
264 info = _make_remote_info(branch_heads={}) # no branches
265 mock = unittest.mock.MagicMock()
266 mock.fetch_remote_info.return_value = info
267
268 with unittest.mock.patch("muse.cli.commands.clone.make_transport", return_value=mock):
269 result = runner.invoke(
270 cli, ["clone", "https://hub.example.com/repos/r1", "dest"]
271 )
272
273 assert result.exit_code != 0
274 assert "empty" in result.output.lower() or "no branches" in result.output.lower()
275
276 def test_clone_branch_flag_selects_branch(
277 self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
278 ) -> None:
279 monkeypatch.chdir(tmp_path)
280 info = _make_remote_info(
281 default_branch="main",
282 branch_heads={"main": "c" * 64, "dev": "d" * 64},
283 )
284 bundle = _make_bundle(branch="dev", message="initial on dev")
285 mock = _mock_transport(info, bundle)
286
287 with unittest.mock.patch("muse.cli.commands.clone.make_transport", return_value=mock):
288 # Options must precede positional args in add_typer groups.
289 result = runner.invoke(
290 cli,
291 ["clone", "--branch", "dev", "https://hub.example.com/repos/r1", "dest"],
292 )
293
294 assert result.exit_code == 0, result.output
295 dest = tmp_path / "dest"
296 head_ref = (dest / ".muse" / "HEAD").read_text().strip()
297 assert "dev" in head_ref
File History 5 commits
sha256:660fcac1df3ab28f61862e961890bd2ca8b754fa0242079d93ca1e25037ec8a6 chore(tests): add docstring to tests/__init__.py so rc14 tr… Human 26 days ago
sha256:d8316ffae901be06347e16ab55be11868eb519dd16ade3e8aa16a99e662f7e62 baseline: rc14 re-baseline after rc3 store corruption recovery Human patch 26 days ago
sha256:50d413a635f57761c3fce1f70251b957b6423821525bf330747ef4007339222e Merge branch 'dev' into main Human 29 days ago
sha256:fb67fed5a4d3e40de84bdd163de94ef1386570bef1dd1a020a732c8a038962ce Merge branch 'dev' into main Human 48 days ago
sha256:1c4b3e3a9a1f300774c3ee662b572a698d5fd405bf765a71e6011a2e9c3eaaaa feat: Muse — version control for the agent era Human 100 days ago