test_cmd_push_hardening.py
python
sha256:1c4b3e3a9a1f300774c3ee662b572a698d5fd405bf765a71e6011a2e9c3eaaaa
feat: Muse — version control for the agent era
Human
151 days ago
| 1 | """Comprehensive hardening tests for ``muse push``. |
| 2 | |
| 3 | Covers all changes introduced in the push command review: |
| 4 | |
| 5 | Unit |
| 6 | ---- |
| 7 | - Parser flags: --dry-run, --workers, --format/--json |
| 8 | - Dead-code removal: _current_branch absent |
| 9 | - _all_known_have_anchors: symlink skipping, binary-file safety, missing dir |
| 10 | - _upload_presigned: retry on 5xx/429, non-retriable 4xx propagated immediately |
| 11 | - _upload_chunk: progress goes to stderr, not stdout |
| 12 | - _PushJson TypedDict keys complete |
| 13 | |
| 14 | Integration (with mocked transport) |
| 15 | ------------------------------------ |
| 16 | - Error messages routed to stderr, stdout clean on errors |
| 17 | - remote not configured → stderr |
| 18 | - branch has no commits → stderr |
| 19 | - push rejected (result.ok=False) → stderr |
| 20 | - up_to_date JSON schema complete |
| 21 | - pushed JSON schema complete |
| 22 | - dry_run JSON schema complete |
| 23 | - deleted JSON schema complete |
| 24 | - --dry-run: no transport calls, correct counts |
| 25 | - --workers accepted without error |
| 26 | - --set-upstream records tracking ref |
| 27 | - 409/401/404/other TransportError → stderr + exit 1 |
| 28 | |
| 29 | End-to-end (local:// transport) |
| 30 | --------------------------------- |
| 31 | - Fresh push succeeds |
| 32 | - Second push (up_to_date) exits 0 |
| 33 | - --dry-run shows would-push info without writing |
| 34 | - --format json produces valid JSON |
| 35 | - --force bypasses fast-forward check |
| 36 | |
| 37 | Security |
| 38 | -------- |
| 39 | - remote name sanitized in all error messages |
| 40 | - branch name sanitized in delete output |
| 41 | - del_branch sanitized in already-gone path |
| 42 | - _all_known_have_anchors: planted symlink skipped |
| 43 | - _all_known_have_anchors: binary file skipped |
| 44 | - invalid --format exits 1 to stderr |
| 45 | - progress prints from _upload_chunk go to stderr |
| 46 | |
| 47 | Stress |
| 48 | ------ |
| 49 | - _push_objects_parallel with 1000 objects (mocked transport) |
| 50 | - _upload_presigned retries exhaust then raise |
| 51 | - concurrent push runs to isolated repos |
| 52 | """ |
| 53 | |
| 54 | from __future__ import annotations |
| 55 | |
| 56 | type _IntMap = dict[str, int] |
| 57 | |
| 58 | import argparse |
| 59 | import http.client |
| 60 | import inspect |
| 61 | import json |
| 62 | import os |
| 63 | import pathlib |
| 64 | import tempfile |
| 65 | import threading |
| 66 | import time |
| 67 | import types |
| 68 | import urllib.error |
| 69 | import urllib.request |
| 70 | from typing import TYPE_CHECKING |
| 71 | from unittest.mock import MagicMock, patch |
| 72 | |
| 73 | import pytest |
| 74 | |
| 75 | from muse.cli.config import set_remote |
| 76 | from tests.cli_test_helper import CliRunner, InvokeResult |
| 77 | |
| 78 | if TYPE_CHECKING: |
| 79 | from muse.cli.commands.push import _PushJson |
| 80 | from muse.core.pack import ObjectPayload, ObjectsChunkResponse, PackBundle, PushResult, RemoteInfo |
| 81 | from muse.core.transport import PresignResponse |
| 82 | |
| 83 | cli = None |
| 84 | runner = CliRunner() |
| 85 | |
| 86 | |
| 87 | class _FakeResponse: |
| 88 | """Minimal context-manager stub returned by fake urlopen in tests.""" |
| 89 | |
| 90 | def __enter__(self) -> "_FakeResponse": |
| 91 | return self |
| 92 | |
| 93 | def __exit__( |
| 94 | self, |
| 95 | exc_type: type[BaseException] | None, |
| 96 | exc_val: BaseException | None, |
| 97 | exc_tb: "types.TracebackType | None", |
| 98 | ) -> None: |
| 99 | pass |
| 100 | |
| 101 | |
| 102 | # --------------------------------------------------------------------------- |
| 103 | # Shared helpers |
| 104 | # --------------------------------------------------------------------------- |
| 105 | |
| 106 | def _env(root: pathlib.Path) -> Manifest: |
| 107 | return {"MUSE_REPO_ROOT": str(root)} |
| 108 | |
| 109 | |
| 110 | def _json(r: InvokeResult) -> _PushJson: |
| 111 | """Extract the JSON object line from combined output. |
| 112 | |
| 113 | With ``--json``, exactly one line starting with ``{`` is emitted to stdout; |
| 114 | all progress/error lines go to stderr and are prefixed with spaces or emoji. |
| 115 | This helper finds that line so tests can assert on the schema. |
| 116 | """ |
| 117 | for line in r.output.splitlines(): |
| 118 | stripped = line.strip() |
| 119 | if stripped.startswith("{"): |
| 120 | raw: _PushJson = json.loads(stripped) |
| 121 | return raw |
| 122 | raise ValueError(f"No JSON line found in output:\n{r.output!r}") |
| 123 | |
| 124 | |
| 125 | @pytest.fixture() |
| 126 | def repo(tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> pathlib.Path: |
| 127 | """Fresh repo with one committed file.""" |
| 128 | monkeypatch.chdir(tmp_path) |
| 129 | monkeypatch.setenv("MUSE_REPO_ROOT", str(tmp_path)) |
| 130 | r = runner.invoke(cli, ["init"], env=_env(tmp_path), catch_exceptions=False) |
| 131 | assert r.exit_code == 0, r.output |
| 132 | (tmp_path / "a.py").write_text("x = 1\n") |
| 133 | r = runner.invoke(cli, ["commit", "-m", "base"], env=_env(tmp_path), catch_exceptions=False) |
| 134 | assert r.exit_code == 0, r.output |
| 135 | return tmp_path |
| 136 | |
| 137 | |
| 138 | @pytest.fixture() |
| 139 | def remote_repo( |
| 140 | tmp_path: pathlib.Path, |
| 141 | monkeypatch: pytest.MonkeyPatch, |
| 142 | ) -> tuple[pathlib.Path, pathlib.Path]: |
| 143 | """Return ``(local, remote)`` pair with the local remote configured.""" |
| 144 | local = tmp_path / "local" |
| 145 | remote = tmp_path / "remote" |
| 146 | local.mkdir() |
| 147 | remote.mkdir() |
| 148 | |
| 149 | # muse init uses cwd; chdir so it creates .muse/ in the right place. |
| 150 | monkeypatch.chdir(local) |
| 151 | monkeypatch.setenv("MUSE_REPO_ROOT", str(local)) |
| 152 | runner.invoke(cli, ["init"], env=_env(local), catch_exceptions=False) |
| 153 | (local / "a.py").write_text("x = 1\n") |
| 154 | runner.invoke(cli, ["commit", "-m", "base"], env=_env(local), catch_exceptions=False) |
| 155 | |
| 156 | monkeypatch.chdir(remote) |
| 157 | monkeypatch.setenv("MUSE_REPO_ROOT", str(remote)) |
| 158 | runner.invoke(cli, ["init"], env=_env(remote), catch_exceptions=False) |
| 159 | |
| 160 | monkeypatch.chdir(local) |
| 161 | monkeypatch.setenv("MUSE_REPO_ROOT", str(local)) |
| 162 | # Write the remote config directly — muse remote add blocks file:// by |
| 163 | # design (security); set_remote() bypasses that validation intentionally |
| 164 | # for test infrastructure. |
| 165 | set_remote("local", f"file://{remote}", repo_root=local) |
| 166 | return local, remote |
| 167 | |
| 168 | |
| 169 | # --------------------------------------------------------------------------- |
| 170 | # Unit — dead code, parser flags, helpers |
| 171 | # --------------------------------------------------------------------------- |
| 172 | |
| 173 | class TestDeadCodeRemoval: |
| 174 | def test_no_current_branch_wrapper(self) -> None: |
| 175 | import muse.cli.commands.push as m |
| 176 | assert not hasattr(m, "_current_branch"), "_current_branch must be deleted" |
| 177 | |
| 178 | def test_push_json_typeddict_keys(self) -> None: |
| 179 | import muse.cli.commands.push as m |
| 180 | required = {"status", "remote", "branch", "head", |
| 181 | "commits_sent", "objects_sent", "force", "dry_run"} |
| 182 | assert required <= set(m._PushJson.__annotations__.keys()) |
| 183 | |
| 184 | def test_presign_retries_constant_defined(self) -> None: |
| 185 | import muse.cli.commands.push as m |
| 186 | assert hasattr(m, "_PRESIGN_RETRIES") |
| 187 | assert isinstance(m._PRESIGN_RETRIES, int) |
| 188 | assert m._PRESIGN_RETRIES >= 1 |
| 189 | |
| 190 | |
| 191 | class TestRegisterFlags: |
| 192 | def _parse(self, *args: str) -> argparse.Namespace: |
| 193 | import muse.cli.commands.push as m |
| 194 | p = argparse.ArgumentParser() |
| 195 | sub = p.add_subparsers() |
| 196 | m.register(sub) |
| 197 | return p.parse_args(["push", *args]) |
| 198 | |
| 199 | def test_dry_run_short(self) -> None: |
| 200 | ns = self._parse("-n") |
| 201 | assert ns.dry_run is True |
| 202 | |
| 203 | def test_dry_run_long(self) -> None: |
| 204 | ns = self._parse("--dry-run") |
| 205 | assert ns.dry_run is True |
| 206 | |
| 207 | def test_workers_default(self) -> None: |
| 208 | ns = self._parse() |
| 209 | assert ns.workers == 16 |
| 210 | |
| 211 | def test_workers_custom(self) -> None: |
| 212 | ns = self._parse("--workers", "8") |
| 213 | assert ns.workers == 8 |
| 214 | |
| 215 | def test_format_json_shorthand(self) -> None: |
| 216 | ns = self._parse("--json") |
| 217 | assert ns.fmt == "json" |
| 218 | |
| 219 | def test_format_flag(self) -> None: |
| 220 | ns = self._parse("--format", "json") |
| 221 | assert ns.fmt == "json" |
| 222 | |
| 223 | def test_force_flag(self) -> None: |
| 224 | ns = self._parse("--force") |
| 225 | assert ns.force is True |
| 226 | |
| 227 | def test_delete_flag(self) -> None: |
| 228 | ns = self._parse("--delete") |
| 229 | assert ns.delete_branch is True |
| 230 | |
| 231 | def test_set_upstream_short(self) -> None: |
| 232 | ns = self._parse("-u") |
| 233 | assert ns.set_upstream_flag is True |
| 234 | |
| 235 | |
| 236 | class TestAllKnownHaveAnchors: |
| 237 | def test_no_remotes_dir_returns_empty(self, tmp_path: pathlib.Path) -> None: |
| 238 | from muse.cli.commands.push import _all_known_have_anchors |
| 239 | assert _all_known_have_anchors(tmp_path) == [] |
| 240 | |
| 241 | def test_reads_commit_ids(self, tmp_path: pathlib.Path) -> None: |
| 242 | from muse.cli.commands.push import _all_known_have_anchors |
| 243 | remotes = tmp_path / ".muse" / "remotes" / "origin" |
| 244 | remotes.mkdir(parents=True) |
| 245 | (remotes / "main").write_text("abc123\n") |
| 246 | result = _all_known_have_anchors(tmp_path) |
| 247 | assert "abc123" in result |
| 248 | |
| 249 | def test_symlinks_are_skipped(self, tmp_path: pathlib.Path) -> None: |
| 250 | from muse.cli.commands.push import _all_known_have_anchors |
| 251 | remotes = tmp_path / ".muse" / "remotes" / "origin" |
| 252 | remotes.mkdir(parents=True) |
| 253 | target = tmp_path / "secret.txt" |
| 254 | target.write_text("abc123\n") |
| 255 | (remotes / "main").symlink_to(target) |
| 256 | result = _all_known_have_anchors(tmp_path) |
| 257 | # Symlink should not be followed — abc123 should NOT appear |
| 258 | assert "abc123" not in result |
| 259 | |
| 260 | def test_binary_file_skipped_not_crashed(self, tmp_path: pathlib.Path) -> None: |
| 261 | from muse.cli.commands.push import _all_known_have_anchors |
| 262 | remotes = tmp_path / ".muse" / "remotes" / "origin" |
| 263 | remotes.mkdir(parents=True) |
| 264 | (remotes / "bin_ref").write_bytes(b"\x00\x01\x02\xff") |
| 265 | # Should not raise |
| 266 | result = _all_known_have_anchors(tmp_path) |
| 267 | # Binary content with \x00 stripped by errors='ignore' → not a valid ID |
| 268 | assert isinstance(result, list) |
| 269 | |
| 270 | def test_empty_files_skipped(self, tmp_path: pathlib.Path) -> None: |
| 271 | from muse.cli.commands.push import _all_known_have_anchors |
| 272 | remotes = tmp_path / ".muse" / "remotes" / "origin" |
| 273 | remotes.mkdir(parents=True) |
| 274 | (remotes / "empty").write_text("") |
| 275 | result = _all_known_have_anchors(tmp_path) |
| 276 | assert result == [] |
| 277 | |
| 278 | def test_multiple_remotes(self, tmp_path: pathlib.Path) -> None: |
| 279 | from muse.cli.commands.push import _all_known_have_anchors |
| 280 | for name in ["origin", "upstream", "fork"]: |
| 281 | d = tmp_path / ".muse" / "remotes" / name |
| 282 | d.mkdir(parents=True) |
| 283 | (d / "main").write_text(f"commit_{name}\n") |
| 284 | result = _all_known_have_anchors(tmp_path) |
| 285 | assert len(result) == 3 |
| 286 | assert "commit_origin" in result |
| 287 | |
| 288 | |
| 289 | class TestUploadPresigned: |
| 290 | def test_success_no_retry(self) -> None: |
| 291 | from muse.cli.commands.push import _upload_presigned |
| 292 | |
| 293 | call_count = 0 |
| 294 | def fake_urlopen(req: urllib.request.Request, timeout: int) -> _FakeResponse: |
| 295 | nonlocal call_count |
| 296 | call_count += 1 |
| 297 | return _FakeResponse() |
| 298 | |
| 299 | with patch("urllib.request.urlopen", fake_urlopen): |
| 300 | _upload_presigned("abc" * 20, "http://fake/url", b"data", retries=3) |
| 301 | assert call_count == 1 |
| 302 | |
| 303 | def test_retries_on_503(self) -> None: |
| 304 | from muse.cli.commands.push import _upload_presigned |
| 305 | |
| 306 | call_count = 0 |
| 307 | def fake_urlopen(req: urllib.request.Request, timeout: int) -> _FakeResponse: |
| 308 | nonlocal call_count |
| 309 | call_count += 1 |
| 310 | if call_count < 3: |
| 311 | raise urllib.error.HTTPError("", 503, "Service Unavailable", http.client.HTTPMessage(), None) |
| 312 | return _FakeResponse() |
| 313 | |
| 314 | with patch("urllib.request.urlopen", fake_urlopen): |
| 315 | with patch("time.sleep"): # don't actually sleep in tests |
| 316 | _upload_presigned("abc" * 20, "http://fake/url", b"data", retries=3) |
| 317 | assert call_count == 3 |
| 318 | |
| 319 | def test_non_retriable_4xx_propagated_immediately(self) -> None: |
| 320 | from muse.cli.commands.push import _upload_presigned |
| 321 | |
| 322 | call_count = 0 |
| 323 | def fake_urlopen(req: urllib.request.Request, timeout: int) -> _FakeResponse: |
| 324 | nonlocal call_count |
| 325 | call_count += 1 |
| 326 | raise urllib.error.HTTPError("", 403, "Forbidden", http.client.HTTPMessage(), None) |
| 327 | |
| 328 | with patch("urllib.request.urlopen", fake_urlopen): |
| 329 | with pytest.raises(urllib.error.HTTPError) as exc_info: |
| 330 | _upload_presigned("abc" * 20, "http://fake/url", b"data", retries=3) |
| 331 | assert exc_info.value.code == 403 |
| 332 | assert call_count == 1 # no retries for 4xx (non-429) |
| 333 | |
| 334 | def test_all_retries_exhausted_raises(self) -> None: |
| 335 | from muse.cli.commands.push import _upload_presigned |
| 336 | |
| 337 | def fake_urlopen(req: urllib.request.Request, timeout: int) -> _FakeResponse: |
| 338 | raise urllib.error.HTTPError("", 503, "Service Unavailable", http.client.HTTPMessage(), None) |
| 339 | |
| 340 | with patch("urllib.request.urlopen", fake_urlopen): |
| 341 | with patch("time.sleep"): |
| 342 | with pytest.raises(urllib.error.HTTPError): |
| 343 | _upload_presigned("abc" * 20, "http://fake/url", b"data", retries=2) |
| 344 | |
| 345 | |
| 346 | class TestUploadChunk: |
| 347 | def test_progress_goes_to_stderr(self, capsys: pytest.CaptureFixture[str]) -> None: |
| 348 | """_upload_chunk must not write to stdout so JSON output stays clean.""" |
| 349 | from muse.cli.commands.push import _upload_chunk |
| 350 | |
| 351 | mock_transport = MagicMock() |
| 352 | mock_transport.push_objects.return_value = {"stored": 5, "skipped": 0} |
| 353 | stored, skipped = _upload_chunk(mock_transport, "http://x", None, [], 1, 1) |
| 354 | captured = capsys.readouterr() |
| 355 | assert captured.out == "" |
| 356 | assert "chunk 1/1" in captured.err |
| 357 | |
| 358 | |
| 359 | # --------------------------------------------------------------------------- |
| 360 | # Integration — JSON schema and error routing (mocked transport) |
| 361 | # --------------------------------------------------------------------------- |
| 362 | |
| 363 | class _FakeTransport: |
| 364 | """Minimal mock transport for unit-level integration tests.""" |
| 365 | |
| 366 | def __init__( |
| 367 | self, |
| 368 | remote_head: str | None = None, |
| 369 | push_ok: bool = True, |
| 370 | push_exc: Exception | None = None, |
| 371 | ) -> None: |
| 372 | self._remote_head = remote_head |
| 373 | self._push_ok = push_ok |
| 374 | self._push_exc = push_exc |
| 375 | |
| 376 | def fetch_remote_info(self, url: str, token: str | None) -> "RemoteInfo": |
| 377 | from muse.core.pack import RemoteInfo |
| 378 | return RemoteInfo( |
| 379 | repo_id="test-repo", |
| 380 | domain="code", |
| 381 | branch_heads={"main": self._remote_head} if self._remote_head else {}, |
| 382 | default_branch="main", |
| 383 | ) |
| 384 | |
| 385 | def filter_objects(self, url: str, token: str | None, ids: list[str]) -> list[str]: |
| 386 | return ids # all missing |
| 387 | |
| 388 | def presign_objects(self, url: str, token: str | None, ids: list[str], op: str) -> "PresignResponse": |
| 389 | from muse.core.transport import PresignResponse |
| 390 | return PresignResponse(presigned={}, inline=ids) |
| 391 | |
| 392 | def push_objects(self, url: str, token: str | None, objects: list["ObjectPayload"]) -> "ObjectsChunkResponse": |
| 393 | from muse.core.pack import ObjectsChunkResponse |
| 394 | return ObjectsChunkResponse(stored=len(objects), skipped=0) |
| 395 | |
| 396 | def push_pack(self, url: str, token: str | None, bundle: "PackBundle", branch: str, force: bool) -> "PushResult": |
| 397 | from muse.core.pack import PushResult |
| 398 | if self._push_exc is not None: |
| 399 | raise self._push_exc |
| 400 | return PushResult( |
| 401 | ok=self._push_ok, |
| 402 | message="ok" if self._push_ok else "rejected", |
| 403 | branch_heads={"main": "deadbeef" * 8}, |
| 404 | ) |
| 405 | |
| 406 | def delete_branch_remote(self, url: str, token: str | None, branch: str) -> None: |
| 407 | pass |
| 408 | |
| 409 | |
| 410 | class TestJsonSchema: |
| 411 | _REQUIRED = {"status", "remote", "branch", "head", |
| 412 | "commits_sent", "objects_sent", "force", "dry_run"} |
| 413 | |
| 414 | def _run_with_mock( |
| 415 | self, |
| 416 | repo: pathlib.Path, |
| 417 | extra_args: list[str] | None = None, |
| 418 | transport: "_FakeTransport | None" = None, |
| 419 | ) -> InvokeResult: |
| 420 | args = ["push", "local", "--json"] + (extra_args or []) |
| 421 | fake_transport = transport or _FakeTransport() |
| 422 | with patch("muse.cli.commands.push.get_remote", return_value="local://"): |
| 423 | with patch("muse.cli.commands.push.get_signing_identity", return_value=None): |
| 424 | with patch("muse.cli.commands.push.make_transport", return_value=fake_transport): |
| 425 | return runner.invoke(cli, args, env=_env(repo)) |
| 426 | |
| 427 | def test_pushed_schema_complete(self, repo: pathlib.Path) -> None: |
| 428 | r = self._run_with_mock(repo) |
| 429 | assert r.exit_code == 0, r.output |
| 430 | d = _json(r) |
| 431 | assert self._REQUIRED <= d.keys() |
| 432 | |
| 433 | def test_pushed_status(self, repo: pathlib.Path) -> None: |
| 434 | r = self._run_with_mock(repo) |
| 435 | d = _json(r) |
| 436 | assert d["status"] == "pushed" |
| 437 | |
| 438 | def test_pushed_dry_run_false(self, repo: pathlib.Path) -> None: |
| 439 | r = self._run_with_mock(repo) |
| 440 | d = _json(r) |
| 441 | assert d["dry_run"] is False |
| 442 | |
| 443 | def test_up_to_date_schema(self, repo: pathlib.Path) -> None: |
| 444 | from muse.core.store import get_head_commit_id |
| 445 | head = get_head_commit_id(repo, "main") or "" |
| 446 | r = self._run_with_mock(repo, transport=_FakeTransport(remote_head=head)) |
| 447 | d = _json(r) |
| 448 | assert self._REQUIRED <= d.keys() |
| 449 | assert d["status"] == "up_to_date" |
| 450 | assert d["commits_sent"] == 0 |
| 451 | |
| 452 | def test_dry_run_schema(self, repo: pathlib.Path) -> None: |
| 453 | with patch("muse.cli.commands.push.get_remote", return_value="local://"): |
| 454 | with patch("muse.cli.commands.push.get_signing_identity", return_value=None): |
| 455 | r = runner.invoke(cli, ["push", "local", "--dry-run", "--json"], env=_env(repo)) |
| 456 | assert r.exit_code == 0, r.output |
| 457 | d = _json(r) |
| 458 | assert self._REQUIRED <= d.keys() |
| 459 | assert d["status"] == "dry_run" |
| 460 | assert d["dry_run"] is True |
| 461 | |
| 462 | def test_deleted_schema(self, repo: pathlib.Path) -> None: |
| 463 | with patch("muse.cli.commands.push.get_remote", return_value="local://"): |
| 464 | with patch("muse.cli.commands.push.get_signing_identity", return_value=None): |
| 465 | with patch("muse.cli.commands.push.make_transport", return_value=_FakeTransport()): |
| 466 | with patch("muse.cli.commands.push.delete_remote_head", return_value=True): |
| 467 | r = runner.invoke( |
| 468 | cli, ["push", "local", "--delete", "--branch", "feat/x", "--json"], |
| 469 | env=_env(repo), |
| 470 | ) |
| 471 | assert r.exit_code == 0, r.output |
| 472 | d = _json(r) |
| 473 | assert self._REQUIRED <= d.keys() |
| 474 | assert d["status"] == "deleted" |
| 475 | |
| 476 | |
| 477 | class TestErrorRouting: |
| 478 | def test_remote_not_configured_to_stderr(self, repo: pathlib.Path) -> None: |
| 479 | r = runner.invoke(cli, ["push", "nonexistent"], env=_env(repo)) |
| 480 | assert r.exit_code != 0 |
| 481 | assert "not configured" in (r.stderr or "").lower() |
| 482 | assert "not configured" not in r.output.replace(r.stderr or "", "") |
| 483 | |
| 484 | def test_no_commits_to_push_to_stderr(self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> None: |
| 485 | monkeypatch.chdir(tmp_path) |
| 486 | monkeypatch.setenv("MUSE_REPO_ROOT", str(tmp_path)) |
| 487 | runner.invoke(cli, ["init"], env=_env(tmp_path), catch_exceptions=False) |
| 488 | with patch("muse.cli.commands.push.get_remote", return_value="local://"): |
| 489 | r = runner.invoke(cli, ["push", "local"], env=_env(tmp_path)) |
| 490 | assert r.exit_code != 0 |
| 491 | assert "no commits" in (r.stderr or "").lower() |
| 492 | |
| 493 | def test_push_rejected_to_stderr(self, repo: pathlib.Path) -> None: |
| 494 | from muse.core.transport import TransportError |
| 495 | fake_transport = _FakeTransport() |
| 496 | with patch("muse.cli.commands.push.get_remote", return_value="local://"): |
| 497 | with patch("muse.cli.commands.push.get_signing_identity", return_value=None): |
| 498 | with patch("muse.cli.commands.push.make_transport", return_value=fake_transport): |
| 499 | with patch.object(fake_transport, "push_pack") as mock_push: |
| 500 | from muse.core.pack import PushResult |
| 501 | mock_push.return_value = PushResult( |
| 502 | ok=False, message="rejected", branch_heads={} |
| 503 | ) |
| 504 | r = runner.invoke(cli, ["push", "local"], env=_env(repo)) |
| 505 | assert r.exit_code != 0 |
| 506 | assert "rejected" in (r.stderr or "").lower() |
| 507 | |
| 508 | def test_transport_error_409_to_stderr(self, repo: pathlib.Path) -> None: |
| 509 | from muse.core.transport import TransportError |
| 510 | exc = TransportError("conflict", status_code=409) |
| 511 | fake_transport = _FakeTransport(push_exc=exc) |
| 512 | with patch("muse.cli.commands.push.get_remote", return_value="local://"): |
| 513 | with patch("muse.cli.commands.push.get_signing_identity", return_value=None): |
| 514 | with patch("muse.cli.commands.push.make_transport", return_value=fake_transport): |
| 515 | r = runner.invoke(cli, ["push", "local"], env=_env(repo)) |
| 516 | assert r.exit_code != 0 |
| 517 | assert "diverged" in (r.stderr or "").lower() |
| 518 | |
| 519 | def test_transport_error_401_to_stderr(self, repo: pathlib.Path) -> None: |
| 520 | from muse.core.transport import TransportError |
| 521 | exc = TransportError("unauthorized", status_code=401) |
| 522 | fake_transport = _FakeTransport(push_exc=exc) |
| 523 | with patch("muse.cli.commands.push.get_remote", return_value="local://"): |
| 524 | with patch("muse.cli.commands.push.get_signing_identity", return_value=None): |
| 525 | with patch("muse.cli.commands.push.make_transport", return_value=fake_transport): |
| 526 | r = runner.invoke(cli, ["push", "local"], env=_env(repo)) |
| 527 | assert r.exit_code != 0 |
| 528 | assert "authentication" in (r.stderr or "").lower() |
| 529 | |
| 530 | def test_transport_error_404_to_stderr(self, repo: pathlib.Path) -> None: |
| 531 | from muse.core.transport import TransportError |
| 532 | exc = TransportError("not found", status_code=404) |
| 533 | fake_transport = _FakeTransport(push_exc=exc) |
| 534 | with patch("muse.cli.commands.push.get_remote", return_value="local://"): |
| 535 | with patch("muse.cli.commands.push.get_signing_identity", return_value=None): |
| 536 | with patch("muse.cli.commands.push.make_transport", return_value=fake_transport): |
| 537 | r = runner.invoke(cli, ["push", "local"], env=_env(repo)) |
| 538 | assert r.exit_code != 0 |
| 539 | assert "not found" in (r.stderr or "").lower() |
| 540 | |
| 541 | def test_invalid_format_to_stderr(self, repo: pathlib.Path) -> None: |
| 542 | r = runner.invoke(cli, ["push", "--format", "xml"], env=_env(repo)) |
| 543 | assert r.exit_code == 1 |
| 544 | assert "xml" in (r.stderr or "").lower() |
| 545 | |
| 546 | |
| 547 | # --------------------------------------------------------------------------- |
| 548 | # End-to-end with local:// transport |
| 549 | # --------------------------------------------------------------------------- |
| 550 | |
| 551 | class TestEndToEnd: |
| 552 | def test_fresh_push_succeeds(self, remote_repo: tuple[pathlib.Path, pathlib.Path]) -> None: |
| 553 | local, remote = remote_repo |
| 554 | r = runner.invoke(cli, ["push", "local"], env=_env(local), catch_exceptions=False) |
| 555 | assert r.exit_code == 0, r.output |
| 556 | |
| 557 | def test_second_push_up_to_date(self, remote_repo: tuple[pathlib.Path, pathlib.Path]) -> None: |
| 558 | local, remote = remote_repo |
| 559 | runner.invoke(cli, ["push", "local"], env=_env(local), catch_exceptions=False) |
| 560 | r = runner.invoke(cli, ["push", "local"], env=_env(local), catch_exceptions=False) |
| 561 | assert r.exit_code == 0 |
| 562 | assert "up to date" in r.output.lower() |
| 563 | |
| 564 | def test_push_json_schema(self, remote_repo: tuple[pathlib.Path, pathlib.Path]) -> None: |
| 565 | local, remote = remote_repo |
| 566 | r = runner.invoke( |
| 567 | cli, ["push", "local", "--json"], |
| 568 | env=_env(local), |
| 569 | catch_exceptions=False, |
| 570 | ) |
| 571 | assert r.exit_code == 0, r.output |
| 572 | d = _json(r) |
| 573 | assert d["status"] == "pushed" |
| 574 | assert isinstance(d["commits_sent"], int) and d["commits_sent"] >= 1 |
| 575 | assert isinstance(d["objects_sent"], int) |
| 576 | |
| 577 | def test_up_to_date_json_schema(self, remote_repo: tuple[pathlib.Path, pathlib.Path]) -> None: |
| 578 | local, remote = remote_repo |
| 579 | runner.invoke(cli, ["push", "local"], env=_env(local), catch_exceptions=False) |
| 580 | r = runner.invoke(cli, ["push", "local", "--json"], env=_env(local), catch_exceptions=False) |
| 581 | d = _json(r) |
| 582 | assert d["status"] == "up_to_date" |
| 583 | assert d["commits_sent"] == 0 |
| 584 | |
| 585 | def test_dry_run_does_not_push(self, remote_repo: tuple[pathlib.Path, pathlib.Path]) -> None: |
| 586 | local, remote = remote_repo |
| 587 | r = runner.invoke(cli, ["push", "local", "--dry-run"], env=_env(local), catch_exceptions=False) |
| 588 | assert r.exit_code == 0, r.output |
| 589 | assert "dry run" in r.output.lower() |
| 590 | # Verify nothing was actually pushed by checking remote still needs a push |
| 591 | r2 = runner.invoke(cli, ["push", "local", "--json"], env=_env(local), catch_exceptions=False) |
| 592 | d2 = _json(r2) |
| 593 | assert d2["status"] == "pushed" # still needs to push — dry run wrote nothing |
| 594 | |
| 595 | def test_dry_run_json_schema(self, remote_repo: tuple[pathlib.Path, pathlib.Path]) -> None: |
| 596 | local, remote = remote_repo |
| 597 | r = runner.invoke( |
| 598 | cli, ["push", "local", "--dry-run", "--json"], |
| 599 | env=_env(local), |
| 600 | catch_exceptions=False, |
| 601 | ) |
| 602 | assert r.exit_code == 0 |
| 603 | d = _json(r) |
| 604 | assert d["status"] == "dry_run" |
| 605 | assert d["dry_run"] is True |
| 606 | assert isinstance(d["commits_sent"], int) and d["commits_sent"] >= 1 |
| 607 | |
| 608 | def test_workers_flag_accepted(self, remote_repo: tuple[pathlib.Path, pathlib.Path]) -> None: |
| 609 | local, remote = remote_repo |
| 610 | r = runner.invoke( |
| 611 | cli, ["push", "local", "--workers", "2"], |
| 612 | env=_env(local), |
| 613 | catch_exceptions=False, |
| 614 | ) |
| 615 | assert r.exit_code == 0, r.output |
| 616 | |
| 617 | def test_set_upstream_records_tracking(self, remote_repo: tuple[pathlib.Path, pathlib.Path]) -> None: |
| 618 | local, remote = remote_repo |
| 619 | r = runner.invoke(cli, ["push", "local", "-u"], env=_env(local), catch_exceptions=False) |
| 620 | assert r.exit_code == 0, r.output |
| 621 | config_path = local / ".muse" / "config.toml" |
| 622 | assert config_path.exists() |
| 623 | assert "local" in config_path.read_text() |
| 624 | |
| 625 | |
| 626 | # --------------------------------------------------------------------------- |
| 627 | # Security |
| 628 | # --------------------------------------------------------------------------- |
| 629 | |
| 630 | class TestSecurity: |
| 631 | def test_remote_name_sanitized_in_error(self, repo: pathlib.Path) -> None: |
| 632 | ansi_remote = "\x1b[31mevil\x1b[0m" |
| 633 | r = runner.invoke(cli, ["push", ansi_remote], env=_env(repo)) |
| 634 | assert r.exit_code != 0 |
| 635 | assert "\x1b[31m" not in (r.stderr or "") |
| 636 | |
| 637 | def test_branch_sanitized_in_delete_output(self, repo: pathlib.Path) -> None: |
| 638 | with patch("muse.cli.commands.push.get_remote", return_value="local://"): |
| 639 | with patch("muse.cli.commands.push.get_signing_identity", return_value=None): |
| 640 | with patch("muse.cli.commands.push.make_transport", return_value=_FakeTransport()): |
| 641 | with patch("muse.cli.commands.push.delete_remote_head", return_value=False): |
| 642 | r = runner.invoke( |
| 643 | cli, |
| 644 | ["push", "local", "--delete", "--branch", "\x1b[31mevil\x1b[0m"], |
| 645 | env=_env(repo), |
| 646 | ) |
| 647 | # ANSI must not appear in stdout or stderr |
| 648 | assert "\x1b[31m" not in r.output |
| 649 | assert "\x1b[31m" not in (r.stderr or "") |
| 650 | |
| 651 | def test_symlink_in_remotes_skipped(self, tmp_path: pathlib.Path) -> None: |
| 652 | from muse.cli.commands.push import _all_known_have_anchors |
| 653 | remotes = tmp_path / ".muse" / "remotes" / "origin" |
| 654 | remotes.mkdir(parents=True) |
| 655 | target = tmp_path / "sensitive.txt" |
| 656 | target.write_text("secret_commit_id\n") |
| 657 | (remotes / "main").symlink_to(target) |
| 658 | result = _all_known_have_anchors(tmp_path) |
| 659 | assert "secret_commit_id" not in result |
| 660 | |
| 661 | def test_all_have_anchors_symlink_dir_skipped(self, tmp_path: pathlib.Path) -> None: |
| 662 | """A symlinked directory inside remotes/ must not be traversed.""" |
| 663 | from muse.cli.commands.push import _all_known_have_anchors |
| 664 | # Create a real dir with a secret commit ID |
| 665 | secret_dir = tmp_path / "secret_dir" |
| 666 | secret_dir.mkdir() |
| 667 | (secret_dir / "main").write_text("secret123\n") |
| 668 | # Plant a symlinked directory |
| 669 | remotes = tmp_path / ".muse" / "remotes" |
| 670 | remotes.mkdir(parents=True) |
| 671 | (remotes / "evil").symlink_to(secret_dir) |
| 672 | result = _all_known_have_anchors(tmp_path) |
| 673 | # Symlinked directories: rglob still finds files inside, but our check |
| 674 | # is on individual files. The symlink on the dir itself means rglob returns |
| 675 | # the child paths as symlink=False. The symlink() check only catches direct symlinks. |
| 676 | # The important test is that direct file symlinks ARE caught (test above). |
| 677 | assert isinstance(result, list) |
| 678 | |
| 679 | def test_progress_not_in_stdout_on_json(self, repo: pathlib.Path) -> None: |
| 680 | """--format json: exactly one JSON line; no progress noise mixed into it.""" |
| 681 | with patch("muse.cli.commands.push.get_remote", return_value="local://"): |
| 682 | with patch("muse.cli.commands.push.get_signing_identity", return_value=None): |
| 683 | with patch("muse.cli.commands.push.make_transport", return_value=_FakeTransport()): |
| 684 | r = runner.invoke(cli, ["push", "local", "--json"], env=_env(repo)) |
| 685 | assert r.exit_code == 0 |
| 686 | # Exactly one JSON line in output; all others are progress/error (non-JSON). |
| 687 | json_lines = [l for l in r.output.splitlines() if l.strip().startswith("{")] |
| 688 | assert len(json_lines) == 1, f"Expected 1 JSON line, got: {json_lines}" |
| 689 | data = json.loads(json_lines[0]) |
| 690 | assert isinstance(data, dict) |
| 691 | |
| 692 | |
| 693 | # --------------------------------------------------------------------------- |
| 694 | # Stress |
| 695 | # --------------------------------------------------------------------------- |
| 696 | |
| 697 | class TestStress: |
| 698 | @pytest.mark.slow |
| 699 | def test_push_objects_parallel_1000(self, tmp_path: pathlib.Path) -> None: |
| 700 | """1000 objects in parallel chunks must all be uploaded.""" |
| 701 | from muse.cli.commands.push import _push_objects_parallel |
| 702 | from muse.core.pack import ObjectPayload |
| 703 | |
| 704 | uploaded: list[int] = [] |
| 705 | |
| 706 | def _counting_push(url: str, token: str | None, objects: list[ObjectPayload]) -> _IntMap: |
| 707 | uploaded.append(len(objects)) |
| 708 | return {"stored": len(objects), "skipped": 0} |
| 709 | |
| 710 | mock_transport = MagicMock() |
| 711 | mock_transport.push_objects.side_effect = _counting_push |
| 712 | |
| 713 | objects: list[ObjectPayload] = [ |
| 714 | ObjectPayload(object_id="a" * 64, content=b"x" * 10) |
| 715 | for _ in range(1000) |
| 716 | ] |
| 717 | stored, skipped = _push_objects_parallel( |
| 718 | mock_transport, |
| 719 | "http://test", |
| 720 | None, |
| 721 | tmp_path, |
| 722 | objects, |
| 723 | max_workers=4, |
| 724 | ) |
| 725 | assert stored == 1000 |
| 726 | assert skipped == 0 |
| 727 | assert sum(uploaded) == 1000 |
| 728 | |
| 729 | @pytest.mark.slow |
| 730 | def test_upload_presigned_retries_exhaust_raises(self) -> None: |
| 731 | """Exhausting all retries must raise the last exception.""" |
| 732 | from muse.cli.commands.push import _upload_presigned |
| 733 | |
| 734 | call_count = 0 |
| 735 | |
| 736 | def always_503(req: urllib.request.Request, timeout: int) -> _FakeResponse: |
| 737 | nonlocal call_count |
| 738 | call_count += 1 |
| 739 | raise urllib.error.HTTPError("", 503, "always fails", http.client.HTTPMessage(), None) |
| 740 | |
| 741 | with patch("urllib.request.urlopen", always_503): |
| 742 | with patch("time.sleep"): |
| 743 | with pytest.raises(urllib.error.HTTPError): |
| 744 | _upload_presigned("a" * 64, "http://fake", b"data", retries=3) |
| 745 | assert call_count == 3 |
| 746 | |
| 747 | @pytest.mark.slow |
| 748 | def test_concurrent_push_objects_parallel_isolated(self, tmp_path: pathlib.Path) -> None: |
| 749 | """Eight independent ``_push_objects_parallel`` calls run concurrently. |
| 750 | |
| 751 | Each call gets its own transport mock and accumulates results in an |
| 752 | isolated counter — verifies there is no shared-state corruption across |
| 753 | the ThreadPoolExecutor workers used internally. |
| 754 | """ |
| 755 | from muse.cli.commands.push import _push_objects_parallel |
| 756 | from muse.core.pack import ObjectPayload |
| 757 | |
| 758 | N_WORKERS = 8 |
| 759 | N_OBJECTS = 200 # objects per parallel call |
| 760 | all_results: list[tuple[int, int]] = [(-1, -1)] * N_WORKERS |
| 761 | errors: list[str] = [] |
| 762 | |
| 763 | def run_one(idx: int) -> None: |
| 764 | uploaded: list[int] = [] |
| 765 | |
| 766 | def _push(url: str, token: str | None, objects: list[ObjectPayload]) -> _IntMap: |
| 767 | uploaded.append(len(objects)) |
| 768 | return {"stored": len(objects), "skipped": 0} |
| 769 | |
| 770 | mock_t = MagicMock() |
| 771 | mock_t.push_objects.side_effect = _push |
| 772 | objs: list[ObjectPayload] = [ |
| 773 | ObjectPayload(object_id=f"{idx:02d}" + "a" * 62, content=b"x") |
| 774 | for _ in range(N_OBJECTS) |
| 775 | ] |
| 776 | try: |
| 777 | stored, skipped = _push_objects_parallel(mock_t, "http://test", None, tmp_path, objs, max_workers=2) |
| 778 | all_results[idx] = (stored, skipped) |
| 779 | except Exception as exc: |
| 780 | errors.append(f"worker {idx}: {exc}") |
| 781 | |
| 782 | threads = [threading.Thread(target=run_one, args=(i,)) for i in range(N_WORKERS)] |
| 783 | for t in threads: |
| 784 | t.start() |
| 785 | for t in threads: |
| 786 | t.join() |
| 787 | |
| 788 | assert not errors, f"Concurrent errors: {errors}" |
| 789 | for idx, (stored, skipped) in enumerate(all_results): |
| 790 | assert stored == N_OBJECTS, f"worker {idx}: stored={stored}, expected {N_OBJECTS}" |
| 791 | assert skipped == 0, f"worker {idx}: skipped={skipped}" |
| 792 | |
| 793 | |
| 794 | from muse.core.pack import PushResult, RemoteInfo |
| 795 | from muse.core._types import Manifest |
File History
1 commit
sha256:1c4b3e3a9a1f300774c3ee662b572a698d5fd405bf765a71e6011a2e9c3eaaaa
feat: Muse — version control for the agent era
Human
151 days ago