gabriel / muse public
test_cli_clone.py python
314 lines 11.0 KB
Raw
sha256:81ae324db5ad375fbfe4834c6fcb378312cafad3cc92dec5d3e5c427306621a2 fix: remove commit_exists filter from have anchors — server… Sonnet 4.6 patch 21 days ago
1 """Tests for muse clone CLI command."""
2
3 from __future__ import annotations
4
5 import base64
6 import json
7 import pathlib
8 import unittest.mock
9
10 import pytest
11 from tests.cli_test_helper import CliRunner
12
13 cli = None # argparse migration — CliRunner ignores this arg
14 from muse.cli.config import get_remote, get_upstream
15 from muse.core.mpack import BlobPayload, MPack, RemoteInfo
16 from muse.core.ids import hash_commit, hash_snapshot
17 from muse.core.commits import read_commit
18 from muse.core.snapshots import read_snapshot
19 from muse.core.transport import TransportError
20 from muse.core.types import Manifest, blob_id, long_id
21 from muse.core.paths import head_path, muse_dir, repo_json_path
22
23 runner = CliRunner()
24
25
26 # ---------------------------------------------------------------------------
27 # Helpers
28 # ---------------------------------------------------------------------------
29
30
31
32 def _make_remote_info(
33 domain: str = "midi",
34 default_branch: str = "main",
35 branch_heads: Manifest | None = None,
36 repo_id: str = "remote-repo-id",
37 ) -> RemoteInfo:
38 return RemoteInfo(
39 repo_id=repo_id,
40 domain=domain,
41 default_branch=default_branch,
42 branch_heads={"main": long_id("c" * 64)} if branch_heads is None else branch_heads,
43 )
44
45
46 def _make_bundle(branch: str = "main", message: str = "initial") -> MPack:
47 """Build a MPack whose commit/snapshot IDs are content-addressed.
48
49 Uses ``hash_commit`` and ``hash_snapshot`` so that
50 ``read_commit``/``read_snapshot`` pass ``_verify_commit_id``/
51 ``_verify_snapshot_id`` on the receiving end.
52 """
53 content = b"cloned content"
54 oid = blob_id(content)
55 committed_at = "2026-01-01T00:00:00+00:00"
56 snap_id = hash_snapshot({"hello.txt": oid})
57 cid = hash_commit(
58 parent_ids=[],
59 snapshot_id=snap_id,
60 message=message,
61 committed_at_iso=committed_at,
62 author="alice",
63 )
64 return MPack(
65 commits=[
66 {
67 "commit_id": cid,
68 "repo_id": "remote-repo-id",
69 "branch": branch,
70 "snapshot_id": snap_id,
71 "message": message,
72 "committed_at": committed_at,
73 "parent_commit_id": None,
74 "parent2_commit_id": None,
75 "author": "alice",
76 "metadata": {},
77 "structured_delta": None,
78 "sem_ver_bump": "none",
79 "breaking_changes": [],
80 "agent_id": "",
81 "model_id": "",
82 "toolchain_id": "",
83 "prompt_hash": "",
84 "signature": "",
85 "signer_key_id": "",
86 "reviewed_by": [],
87 "test_runs": 0,
88 }
89 ],
90 snapshots=[
91 {
92 "snapshot_id": snap_id,
93 "delta_upsert": {"hello.txt": oid},
94 "delta_remove": [],
95 "parent_snapshot_id": None,
96 "created_at": committed_at,
97 }
98 ],
99 blobs=[
100 BlobPayload(object_id=oid, content=content)
101 ],
102 branch_heads={branch: cid},
103 )
104
105
106 def _mock_transport(info: RemoteInfo, mpack: MPack) -> unittest.mock.MagicMock:
107 mock = unittest.mock.MagicMock()
108 mock.fetch_remote_info.return_value = info
109 mock.fetch_pack.return_value = mpack
110 _fetch_result = {
111 "repo_id": info["repo_id"],
112 "domain": info["domain"],
113 "default_branch": info["default_branch"],
114 "branch_heads": info["branch_heads"],
115 "commits": mpack["commits"],
116 "snapshots": mpack["snapshots"],
117 "blobs_received": len(mpack["blobs"]),
118 }
119
120 mock.fetch_mpack.return_value = _fetch_result
121 return mock
122
123
124 # ---------------------------------------------------------------------------
125 # Tests
126 # ---------------------------------------------------------------------------
127
128
129 class TestClone:
130 def test_clone_creates_muse_dir(
131 self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
132 ) -> None:
133 monkeypatch.chdir(tmp_path)
134 info = _make_remote_info()
135 mpack = _make_bundle()
136 mock = _mock_transport(info, mpack)
137
138 with unittest.mock.patch("muse.cli.commands.clone.make_transport", return_value=mock):
139 result = runner.invoke(
140 cli, ["clone", "https://hub.example.com/repos/r1", "my-repo"]
141 )
142
143 assert result.exit_code == 0, result.output
144 assert (tmp_path / "my-repo" / ".muse").is_dir()
145
146 def test_clone_sets_origin_remote(
147 self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
148 ) -> None:
149 monkeypatch.chdir(tmp_path)
150 info = _make_remote_info()
151 mpack = _make_bundle()
152 mock = _mock_transport(info, mpack)
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 origin = get_remote("origin", tmp_path / "my-repo")
160 assert origin == "https://hub.example.com/repos/r1"
161
162 def test_clone_sets_upstream_tracking(
163 self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
164 ) -> None:
165 monkeypatch.chdir(tmp_path)
166 info = _make_remote_info()
167 mpack = _make_bundle()
168 mock = _mock_transport(info, mpack)
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", "my-repo"]
173 )
174
175 upstream = get_upstream("main", tmp_path / "my-repo")
176 assert upstream == "origin"
177
178 def test_clone_writes_commits_and_snapshots(
179 self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
180 ) -> None:
181 monkeypatch.chdir(tmp_path)
182 mpack = _make_bundle()
183 cid = mpack["commits"][0]["commit_id"]
184 info = _make_remote_info(branch_heads={"main": cid})
185 mock = _mock_transport(info, mpack)
186
187 with unittest.mock.patch("muse.cli.commands.clone.make_transport", return_value=mock):
188 runner.invoke(
189 cli, ["clone", "https://hub.example.com/repos/r1", "dest"]
190 )
191
192 dest = tmp_path / "dest"
193 cid = mpack["commits"][0]["commit_id"]
194 snap_id = mpack["snapshots"][0]["snapshot_id"]
195 assert isinstance(cid, str) and isinstance(snap_id, str)
196 assert read_commit(dest, cid) is not None
197 assert read_snapshot(dest, snap_id) is not None
198
199 def test_clone_propagates_domain(
200 self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
201 ) -> None:
202 monkeypatch.chdir(tmp_path)
203 info = _make_remote_info(domain="code")
204 mpack = _make_bundle()
205 mock = _mock_transport(info, mpack)
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((repo_json_path(dest)).read_text())
214 assert repo_meta["domain"] == "code"
215
216 def test_clone_uses_remote_repo_id(
217 self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
218 ) -> None:
219 monkeypatch.chdir(tmp_path)
220 info = _make_remote_info(repo_id="the-real-repo-id")
221 mpack = _make_bundle()
222 mock = _mock_transport(info, mpack)
223
224 with unittest.mock.patch("muse.cli.commands.clone.make_transport", return_value=mock):
225 runner.invoke(
226 cli, ["clone", "https://hub.example.com/repos/r1", "dest"]
227 )
228
229 dest = tmp_path / "dest"
230 repo_meta = json.loads((repo_json_path(dest)).read_text())
231 assert repo_meta["repo_id"] == "the-real-repo-id"
232
233 def test_clone_infers_directory_name_from_url(
234 self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
235 ) -> None:
236 monkeypatch.chdir(tmp_path)
237 info = _make_remote_info()
238 mpack = _make_bundle()
239 mock = _mock_transport(info, mpack)
240
241 with unittest.mock.patch("muse.cli.commands.clone.make_transport", return_value=mock):
242 runner.invoke(cli, ["clone", "https://hub.example.com/repos/my-project"])
243
244 assert (tmp_path / "my-project" / ".muse").is_dir()
245
246 def test_clone_transport_error_fails_cleanly(
247 self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
248 ) -> None:
249 monkeypatch.chdir(tmp_path)
250 mock = unittest.mock.MagicMock()
251 mock.fetch_remote_info.side_effect = TransportError("connection refused", 0)
252
253 with unittest.mock.patch("muse.cli.commands.clone.make_transport", return_value=mock):
254 result = runner.invoke(
255 cli, ["clone", "https://hub.example.com/repos/r1", "dest"]
256 )
257
258 assert result.exit_code != 0
259 assert "Cannot reach remote" in result.stderr
260
261 def test_clone_existing_repo_fails(
262 self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
263 ) -> None:
264 monkeypatch.chdir(tmp_path)
265 # Pre-create a .muse directory.
266 muse_dir(tmp_path / "dest").mkdir(parents=True)
267
268 mock = unittest.mock.MagicMock()
269 with unittest.mock.patch("muse.cli.commands.clone.make_transport", return_value=mock):
270 result = runner.invoke(
271 cli, ["clone", "https://hub.example.com/repos/r1", "dest"]
272 )
273
274 assert result.exit_code != 0
275 assert "already a Muse repository" in result.stderr
276
277 def test_clone_empty_repo_fails(
278 self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
279 ) -> None:
280 monkeypatch.chdir(tmp_path)
281 info = _make_remote_info(branch_heads={}) # no branches
282 mock = unittest.mock.MagicMock()
283 mock.fetch_remote_info.return_value = info
284
285 with unittest.mock.patch("muse.cli.commands.clone.make_transport", return_value=mock):
286 result = runner.invoke(
287 cli, ["clone", "https://hub.example.com/repos/r1", "dest"]
288 )
289
290 assert result.exit_code != 0
291 assert "empty" in result.stderr.lower() or "no branches" in result.stderr.lower()
292
293 def test_clone_branch_flag_selects_branch(
294 self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
295 ) -> None:
296 monkeypatch.chdir(tmp_path)
297 info = _make_remote_info(
298 default_branch="main",
299 branch_heads={"main": long_id("c" * 64), "dev": long_id("d" * 64)},
300 )
301 mpack = _make_bundle(branch="dev", message="initial on dev")
302 mock = _mock_transport(info, mpack)
303
304 with unittest.mock.patch("muse.cli.commands.clone.make_transport", return_value=mock):
305 # Options must precede positional args in add_typer groups.
306 result = runner.invoke(
307 cli,
308 ["clone", "--branch", "dev", "https://hub.example.com/repos/r1", "dest"],
309 )
310
311 assert result.exit_code == 0, result.output
312 dest = tmp_path / "dest"
313 head_ref = (head_path(dest)).read_text().strip()
314 assert "dev" in head_ref
File History 6 commits
sha256:81ae324db5ad375fbfe4834c6fcb378312cafad3cc92dec5d3e5c427306621a2 fix: remove commit_exists filter from have anchors — server… Sonnet 4.6 patch 21 days ago
sha256:36c3cb3e76619d4c30a6d9bf81b5ec4ff148e30dcfed913e3114ca7b43b81c7e fix: rename objects→blobs in push client and all stale test… Sonnet 4.6 patch 22 days ago
sha256:0313c134f0ef4518a9c3a0ec359ffdc42546dc720010730374edfe0857caf7ef rename: delta_add → delta_upsert across wire format, source… Sonnet 4.6 minor 23 days ago
sha256:fb19dc03703eb3fc11d016ea19f619eebfab7bde2acf247346dc0f032e65ff19 fix(push): step 0 log shows full /refs URL instead of misle… Sonnet 4.6 patch 23 days ago
sha256:c06a9b9b9fee26c68ea725b44d54b2c0a171301ce9de746d5b656617b4463a9a fix: repair four test failures from post-migration audit Sonnet 4.6 patch 28 days ago
sha256:1900655993c83c4107067375548a7be823e471d2515830842f1a12cba4bd3cdf fix: unified object store migration — idempotent writes, JS… Sonnet 4.6 minor 29 days ago