test_mv_supercharge.py
python
sha256:88ac91129873e6a496e9189515aa690eb893ae25d69c8f72af141a2be5068eb3
docs: docstring sprint contract→find-symbol — idiomatic run…
Sonnet 4.6
patch
142 days ago
| 1 | """Supercharge tests for ``muse mv``. |
| 2 | |
| 3 | Coverage tiers |
| 4 | -------------- |
| 5 | - JSON envelope: exit_code and duration_ms present on success and dry_run |
| 6 | - Error payload: errors go to stdout as JSON in --json mode, no dual stderr prose |
| 7 | - Data integrity: sha256: OID prefix preserved; source/dest echoed verbatim |
| 8 | - TypedDicts: _MvResultJson and _MvErrorJson with required annotations |
| 9 | - Docstring: module docstring covers exit_code and duration_ms |
| 10 | - No-prose pollution: JSON stdout is valid on all non-error paths |
| 11 | - Stress: 50-file move-all with --json, correct stage state |
| 12 | """ |
| 13 | from __future__ import annotations |
| 14 | |
| 15 | import datetime |
| 16 | import hashlib |
| 17 | import json |
| 18 | import pathlib |
| 19 | from typing import get_type_hints |
| 20 | |
| 21 | from muse.core.object_store import write_object |
| 22 | from muse.core.snapshot import compute_commit_id, compute_snapshot_id |
| 23 | from muse.core.store import CommitRecord, SnapshotRecord, write_commit, write_snapshot |
| 24 | from muse.core._types import Manifest, long_id |
| 25 | from muse.plugins.code.stage import make_entry, read_stage, write_stage |
| 26 | from tests.cli_test_helper import CliRunner |
| 27 | |
| 28 | runner = CliRunner() |
| 29 | |
| 30 | _REPO_ID = "mv-supercharge" |
| 31 | _DT = datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc) |
| 32 | |
| 33 | |
| 34 | # --------------------------------------------------------------------------- |
| 35 | # Helpers |
| 36 | # --------------------------------------------------------------------------- |
| 37 | |
| 38 | |
| 39 | def _sha(data: bytes) -> str: |
| 40 | return long_id(hashlib.sha256(data).hexdigest()) |
| 41 | |
| 42 | |
| 43 | def _init_repo(tmp_path: pathlib.Path) -> pathlib.Path: |
| 44 | muse = tmp_path / ".muse" |
| 45 | for d in ("commits", "snapshots", "objects", "refs/heads", "code"): |
| 46 | (muse / d).mkdir(parents=True, exist_ok=True) |
| 47 | (muse / "HEAD").write_text("ref: refs/heads/main", encoding="utf-8") |
| 48 | (muse / "repo.json").write_text( |
| 49 | json.dumps({"repo_id": _REPO_ID, "domain": "code"}), encoding="utf-8" |
| 50 | ) |
| 51 | return tmp_path |
| 52 | |
| 53 | |
| 54 | def _env(root: pathlib.Path) -> dict[str, str]: |
| 55 | return {"MUSE_REPO_ROOT": str(root)} |
| 56 | |
| 57 | |
| 58 | def _commit(root: pathlib.Path, files: dict[str, bytes]) -> str: |
| 59 | manifest: Manifest = {} |
| 60 | for rel, content in files.items(): |
| 61 | oid = _sha(content) |
| 62 | write_object(root, oid, content) |
| 63 | manifest[rel] = oid |
| 64 | abs_p = root / rel |
| 65 | abs_p.parent.mkdir(parents=True, exist_ok=True) |
| 66 | abs_p.write_bytes(content) |
| 67 | snap_id = compute_snapshot_id(manifest) |
| 68 | write_snapshot(root, SnapshotRecord(snapshot_id=snap_id, manifest=manifest, created_at=_DT)) |
| 69 | cid = compute_commit_id([], snap_id, "test", _DT.isoformat()) |
| 70 | write_commit(root, CommitRecord( |
| 71 | commit_id=cid, repo_id=_REPO_ID, branch="main", |
| 72 | snapshot_id=snap_id, message="test", committed_at=_DT, |
| 73 | )) |
| 74 | (root / ".muse" / "refs" / "heads" / "main").write_text(cid, encoding="utf-8") |
| 75 | return cid |
| 76 | |
| 77 | |
| 78 | def _mv(root: pathlib.Path, *args: str): |
| 79 | from muse.cli.app import main as cli |
| 80 | return runner.invoke(cli, ["mv", *args], env=_env(root)) |
| 81 | |
| 82 | |
| 83 | # --------------------------------------------------------------------------- |
| 84 | # JSON envelope — exit_code |
| 85 | # --------------------------------------------------------------------------- |
| 86 | |
| 87 | |
| 88 | class TestJsonEnvelopeExitCode: |
| 89 | def test_success_has_exit_code_zero(self, tmp_path: pathlib.Path) -> None: |
| 90 | root = _init_repo(tmp_path) |
| 91 | _commit(root, {"a.py": b"# a\n"}) |
| 92 | r = _mv(root, "--json", "a.py", "b.py") |
| 93 | assert r.exit_code == 0 |
| 94 | d = json.loads(r.output) |
| 95 | assert "exit_code" in d, "exit_code missing from success envelope" |
| 96 | assert d["exit_code"] == 0 |
| 97 | |
| 98 | def test_dry_run_has_exit_code_zero(self, tmp_path: pathlib.Path) -> None: |
| 99 | root = _init_repo(tmp_path) |
| 100 | _commit(root, {"a.py": b"# a\n"}) |
| 101 | r = _mv(root, "--json", "--dry-run", "a.py", "b.py") |
| 102 | assert r.exit_code == 0 |
| 103 | d = json.loads(r.output) |
| 104 | assert "exit_code" in d, "exit_code missing from dry_run envelope" |
| 105 | assert d["exit_code"] == 0 |
| 106 | |
| 107 | def test_error_payload_has_exit_code_nonzero(self, tmp_path: pathlib.Path) -> None: |
| 108 | root = _init_repo(tmp_path) |
| 109 | _commit(root, {"anchor.py": b"# a\n"}) |
| 110 | r = _mv(root, "--json", "ghost.py", "dest.py") |
| 111 | d = json.loads(r.output) |
| 112 | assert "exit_code" in d |
| 113 | assert d["exit_code"] != 0 |
| 114 | |
| 115 | |
| 116 | # --------------------------------------------------------------------------- |
| 117 | # JSON envelope — duration_ms |
| 118 | # --------------------------------------------------------------------------- |
| 119 | |
| 120 | |
| 121 | class TestJsonEnvelopeDurationMs: |
| 122 | def test_success_has_duration_ms(self, tmp_path: pathlib.Path) -> None: |
| 123 | root = _init_repo(tmp_path) |
| 124 | _commit(root, {"a.py": b"# a\n"}) |
| 125 | r = _mv(root, "--json", "a.py", "b.py") |
| 126 | d = json.loads(r.output) |
| 127 | assert "duration_ms" in d, "duration_ms missing from success envelope" |
| 128 | assert isinstance(d["duration_ms"], float) |
| 129 | assert d["duration_ms"] >= 0.0 |
| 130 | |
| 131 | def test_dry_run_has_duration_ms(self, tmp_path: pathlib.Path) -> None: |
| 132 | root = _init_repo(tmp_path) |
| 133 | _commit(root, {"a.py": b"# a\n"}) |
| 134 | r = _mv(root, "--json", "--dry-run", "a.py", "b.py") |
| 135 | d = json.loads(r.output) |
| 136 | assert "duration_ms" in d, "duration_ms missing from dry_run envelope" |
| 137 | assert isinstance(d["duration_ms"], float) |
| 138 | |
| 139 | def test_force_move_has_duration_ms(self, tmp_path: pathlib.Path) -> None: |
| 140 | root = _init_repo(tmp_path) |
| 141 | _commit(root, {"src.py": b"# s\n", "dst.py": b"# d\n"}) |
| 142 | r = _mv(root, "--json", "--force", "src.py", "dst.py") |
| 143 | d = json.loads(r.output) |
| 144 | assert "duration_ms" in d |
| 145 | |
| 146 | |
| 147 | # --------------------------------------------------------------------------- |
| 148 | # Error payload — errors route to stdout as JSON in --json mode |
| 149 | # --------------------------------------------------------------------------- |
| 150 | |
| 151 | |
| 152 | class TestErrorPayload: |
| 153 | def test_untracked_source_error_goes_to_stdout(self, tmp_path: pathlib.Path) -> None: |
| 154 | root = _init_repo(tmp_path) |
| 155 | _commit(root, {"anchor.py": b"# a\n"}) |
| 156 | (root / "ghost.py").write_text("# untracked\n") |
| 157 | r = _mv(root, "--json", "ghost.py", "dest.py") |
| 158 | assert r.exit_code != 0 |
| 159 | assert not r.stderr.strip(), f"unexpected stderr: {r.stderr!r}" |
| 160 | d = json.loads(r.output) |
| 161 | assert d["status"] == "error" |
| 162 | |
| 163 | def test_missing_disk_source_error_goes_to_stdout(self, tmp_path: pathlib.Path) -> None: |
| 164 | root = _init_repo(tmp_path) |
| 165 | _commit(root, {"a.py": b"# a\n"}) |
| 166 | (root / "a.py").unlink() |
| 167 | r = _mv(root, "--json", "a.py", "b.py") |
| 168 | assert r.exit_code != 0 |
| 169 | assert not r.stderr.strip(), f"unexpected stderr: {r.stderr!r}" |
| 170 | d = json.loads(r.output) |
| 171 | assert d["status"] == "error" |
| 172 | |
| 173 | def test_dest_already_tracked_error_goes_to_stdout(self, tmp_path: pathlib.Path) -> None: |
| 174 | root = _init_repo(tmp_path) |
| 175 | _commit(root, {"src.py": b"# s\n", "dst.py": b"# d\n"}) |
| 176 | r = _mv(root, "--json", "src.py", "dst.py") |
| 177 | assert r.exit_code != 0 |
| 178 | assert not r.stderr.strip(), f"unexpected stderr: {r.stderr!r}" |
| 179 | d = json.loads(r.output) |
| 180 | assert d["status"] == "error" |
| 181 | |
| 182 | def test_dest_exists_on_disk_error_goes_to_stdout(self, tmp_path: pathlib.Path) -> None: |
| 183 | root = _init_repo(tmp_path) |
| 184 | _commit(root, {"src.py": b"# s\n"}) |
| 185 | (root / "dst.py").write_text("# existing\n") |
| 186 | r = _mv(root, "--json", "src.py", "dst.py") |
| 187 | assert r.exit_code != 0 |
| 188 | assert not r.stderr.strip(), f"unexpected stderr: {r.stderr!r}" |
| 189 | d = json.loads(r.output) |
| 190 | assert d["status"] == "error" |
| 191 | |
| 192 | def test_path_traversal_error_goes_to_stderr_in_text_mode(self, tmp_path: pathlib.Path) -> None: |
| 193 | """Path traversal in text mode is caught before --json is parsed — stderr is OK.""" |
| 194 | root = _init_repo(tmp_path) |
| 195 | _commit(root, {"anchor.py": b"# a\n"}) |
| 196 | r = _mv(root, "../../../etc/passwd", "dest.py") |
| 197 | assert r.exit_code != 0 |
| 198 | |
| 199 | def test_error_payload_has_status_error(self, tmp_path: pathlib.Path) -> None: |
| 200 | root = _init_repo(tmp_path) |
| 201 | _commit(root, {"anchor.py": b"# a\n"}) |
| 202 | r = _mv(root, "--json", "ghost.py", "dest.py") |
| 203 | d = json.loads(r.output) |
| 204 | assert d["status"] == "error" |
| 205 | |
| 206 | def test_error_payload_has_error_field(self, tmp_path: pathlib.Path) -> None: |
| 207 | root = _init_repo(tmp_path) |
| 208 | _commit(root, {"anchor.py": b"# a\n"}) |
| 209 | r = _mv(root, "--json", "ghost.py", "dest.py") |
| 210 | d = json.loads(r.output) |
| 211 | assert "error" in d |
| 212 | assert d["error"] |
| 213 | |
| 214 | def test_no_emoji_on_stderr_in_json_mode(self, tmp_path: pathlib.Path) -> None: |
| 215 | root = _init_repo(tmp_path) |
| 216 | _commit(root, {"anchor.py": b"# a\n"}) |
| 217 | r = _mv(root, "--json", "ghost.py", "dest.py") |
| 218 | assert "❌" not in r.stderr |
| 219 | |
| 220 | |
| 221 | # --------------------------------------------------------------------------- |
| 222 | # Data integrity |
| 223 | # --------------------------------------------------------------------------- |
| 224 | |
| 225 | |
| 226 | class TestDataIntegrity: |
| 227 | def test_object_id_is_sha256_prefixed(self, tmp_path: pathlib.Path) -> None: |
| 228 | """object_id in JSON response must carry sha256: prefix.""" |
| 229 | root = _init_repo(tmp_path) |
| 230 | content = b"# content\n" |
| 231 | _commit(root, {"a.py": content}) |
| 232 | r = _mv(root, "--json", "a.py", "b.py") |
| 233 | d = json.loads(r.output) |
| 234 | assert d["object_id"].startswith("sha256:"), ( |
| 235 | f"object_id missing sha256: prefix: {d['object_id']!r}" |
| 236 | ) |
| 237 | |
| 238 | def test_source_dest_echoed_verbatim(self, tmp_path: pathlib.Path) -> None: |
| 239 | root = _init_repo(tmp_path) |
| 240 | _commit(root, {"src/module.py": b"# m\n"}) |
| 241 | r = _mv(root, "--json", "src/module.py", "lib/module.py") |
| 242 | d = json.loads(r.output) |
| 243 | assert d["source"] == "src/module.py" |
| 244 | assert d["dest"] == "lib/module.py" |
| 245 | |
| 246 | def test_object_id_matches_staged_entry(self, tmp_path: pathlib.Path) -> None: |
| 247 | """object_id in JSON must match what was written to the stage.""" |
| 248 | root = _init_repo(tmp_path) |
| 249 | content = b"# exact\n" |
| 250 | _commit(root, {"x.py": content}) |
| 251 | r = _mv(root, "--json", "x.py", "y.py") |
| 252 | d = json.loads(r.output) |
| 253 | stage = read_stage(root) |
| 254 | assert stage["y.py"]["object_id"] == d["object_id"] |
| 255 | |
| 256 | def test_staged_only_move_object_id_preserved(self, tmp_path: pathlib.Path) -> None: |
| 257 | """Staged-only file move must echo the correct object_id.""" |
| 258 | root = _init_repo(tmp_path) |
| 259 | _commit(root, {"anchor.py": b"# anchor\n"}) |
| 260 | content = b"# staged only\n" |
| 261 | oid = _sha(content) |
| 262 | write_object(root, oid, content) |
| 263 | (root / "new.py").write_bytes(content) |
| 264 | stage = read_stage(root) |
| 265 | stage["new.py"] = make_entry(oid, "A") |
| 266 | write_stage(root, stage) |
| 267 | r = _mv(root, "--json", "new.py", "renamed.py") |
| 268 | d = json.loads(r.output) |
| 269 | assert d["object_id"] == oid |
| 270 | |
| 271 | |
| 272 | # --------------------------------------------------------------------------- |
| 273 | # No-prose pollution |
| 274 | # --------------------------------------------------------------------------- |
| 275 | |
| 276 | |
| 277 | class TestNoProsePollution: |
| 278 | def test_success_stdout_is_valid_json(self, tmp_path: pathlib.Path) -> None: |
| 279 | root = _init_repo(tmp_path) |
| 280 | _commit(root, {"a.py": b"# a\n"}) |
| 281 | r = _mv(root, "--json", "a.py", "b.py") |
| 282 | json.loads(r.output) # must not raise |
| 283 | |
| 284 | def test_dry_run_stdout_is_valid_json(self, tmp_path: pathlib.Path) -> None: |
| 285 | root = _init_repo(tmp_path) |
| 286 | _commit(root, {"a.py": b"# a\n"}) |
| 287 | r = _mv(root, "--json", "--dry-run", "a.py", "b.py") |
| 288 | json.loads(r.output) # must not raise |
| 289 | |
| 290 | def test_no_emoji_in_success_json(self, tmp_path: pathlib.Path) -> None: |
| 291 | root = _init_repo(tmp_path) |
| 292 | _commit(root, {"a.py": b"# a\n"}) |
| 293 | r = _mv(root, "--json", "a.py", "b.py") |
| 294 | assert "✅" not in r.output |
| 295 | assert "❌" not in r.output |
| 296 | |
| 297 | |
| 298 | # --------------------------------------------------------------------------- |
| 299 | # TypedDicts |
| 300 | # --------------------------------------------------------------------------- |
| 301 | |
| 302 | |
| 303 | class TestTypedDicts: |
| 304 | def test_mv_result_json_typeddict_exists(self) -> None: |
| 305 | from muse.cli.commands.mv import _MvResultJson |
| 306 | assert _MvResultJson is not None |
| 307 | |
| 308 | def test_mv_error_json_typeddict_exists(self) -> None: |
| 309 | from muse.cli.commands.mv import _MvErrorJson |
| 310 | assert _MvErrorJson is not None |
| 311 | |
| 312 | def test_mv_result_json_has_exit_code_annotation(self) -> None: |
| 313 | from muse.cli.commands.mv import _MvResultJson |
| 314 | hints = get_type_hints(_MvResultJson) |
| 315 | assert "exit_code" in hints |
| 316 | |
| 317 | def test_mv_result_json_has_duration_ms_annotation(self) -> None: |
| 318 | from muse.cli.commands.mv import _MvResultJson |
| 319 | hints = get_type_hints(_MvResultJson) |
| 320 | assert "duration_ms" in hints |
| 321 | |
| 322 | def test_mv_error_json_has_required_fields(self) -> None: |
| 323 | from muse.cli.commands.mv import _MvErrorJson |
| 324 | hints = get_type_hints(_MvErrorJson) |
| 325 | for field in ("status", "error", "exit_code"): |
| 326 | assert field in hints, f"Missing annotation: {field!r}" |
| 327 | |
| 328 | |
| 329 | # --------------------------------------------------------------------------- |
| 330 | # Docstring coverage |
| 331 | # --------------------------------------------------------------------------- |
| 332 | |
| 333 | |
| 334 | class TestDocstring: |
| 335 | def _doc(self) -> str: |
| 336 | import muse.cli.commands.mv as mod |
| 337 | return mod.__doc__ or "" |
| 338 | |
| 339 | def test_docstring_documents_exit_code(self) -> None: |
| 340 | assert "exit_code" in self._doc() |
| 341 | |
| 342 | def test_docstring_documents_duration_ms(self) -> None: |
| 343 | assert "duration_ms" in self._doc() |
| 344 | |
| 345 | |
| 346 | # --------------------------------------------------------------------------- |
| 347 | # Stress |
| 348 | # --------------------------------------------------------------------------- |
| 349 | |
| 350 | |
| 351 | class TestStress: |
| 352 | def test_50_files_move_all_json(self, tmp_path: pathlib.Path) -> None: |
| 353 | """Move all 50 tracked files with --json; all envelopes must be valid.""" |
| 354 | root = _init_repo(tmp_path) |
| 355 | n = 50 |
| 356 | files = {f"f{i:03d}.py": f"# {i}\n".encode() for i in range(n)} |
| 357 | _commit(root, files) |
| 358 | |
| 359 | for i in range(n): |
| 360 | r = _mv(root, "--json", f"f{i:03d}.py", f"moved/f{i:03d}.py") |
| 361 | assert r.exit_code == 0, f"move {i} failed: {r.output}" |
| 362 | d = json.loads(r.output) |
| 363 | assert d["exit_code"] == 0 |
| 364 | assert "duration_ms" in d |
| 365 | assert d["source"] == f"f{i:03d}.py" |
| 366 | assert d["dest"] == f"moved/f{i:03d}.py" |
| 367 | |
| 368 | stage = read_stage(root) |
| 369 | for i in range(n): |
| 370 | assert stage[f"f{i:03d}.py"]["mode"] == "D" |
| 371 | assert stage[f"moved/f{i:03d}.py"]["mode"] == "A" |
File History
1 commit
sha256:88ac91129873e6a496e9189515aa690eb893ae25d69c8f72af141a2be5068eb3
docs: docstring sprint contract→find-symbol — idiomatic run…
Sonnet 4.6
patch
142 days ago