gabriel / muse public
test_restore_supercharge.py python
488 lines 18.3 KB
Raw
sha256:88ac91129873e6a496e9189515aa690eb893ae25d69c8f72af141a2be5068eb3 docs: docstring sprint contract→find-symbol — idiomatic run… Sonnet 4.6 patch 138 days ago
1 """Supercharge tests for ``muse restore`` — performance, data integrity,
2 object-store corruption, concurrency, and source+staged combos.
3
4 Coverage tiers added here:
5 - Performance: duration_ms present, non-negative, and reasonable
6 - Data integrity: complete JSON schema, correct types, exit_code field
7 - Error mapping: object store corruption → exit code 3 (INTERNAL_ERROR)
8 - Concurrent: two threads restore independent files without racing
9 - Source+staged: --source --staged restores stage entry from source commit
10 - Text summary: text output includes "Restored N" summary line
11 - Docstring gap: _resolve_source_manifest returns {} for bad ref (not raises)
12 """
13
14 from __future__ import annotations
15
16 import json
17 import pathlib
18 import threading
19 import time
20 import datetime
21 import hashlib
22
23 import pytest
24
25 from tests.cli_test_helper import CliRunner
26
27 from muse.core.object_store import write_object
28 from muse.core.snapshot import compute_commit_id, compute_snapshot_id
29 from muse.core.store import CommitRecord, SnapshotRecord, write_commit, write_snapshot
30 from muse.core._types import Manifest, long_id
31 from muse.plugins.code.stage import StagedFileMap, make_entry, read_stage, write_stage
32
33 runner = CliRunner()
34
35 _REPO_ID = "restore-supercharge-test"
36 _counter = 1000 # offset to avoid collisions with test_cmd_restore.py
37
38
39 def _sha(data: bytes) -> str:
40 return long_id(hashlib.sha256(data).hexdigest())
41
42
43 def _init_repo(path: pathlib.Path) -> pathlib.Path:
44 muse = 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 path
52
53
54 def _env(repo: pathlib.Path) -> dict[str, str]:
55 return {"MUSE_REPO_ROOT": str(repo)}
56
57
58 def _commit_files(root: pathlib.Path, files: dict[str, bytes], branch: str = "main") -> str:
59 global _counter
60 _counter += 1
61 manifest: Manifest = {}
62 for rel_path, content in files.items():
63 obj_id = _sha(content)
64 write_object(root, obj_id, content)
65 manifest[rel_path] = obj_id
66 abs_path = root / rel_path
67 abs_path.parent.mkdir(parents=True, exist_ok=True)
68 abs_path.write_bytes(content)
69 snap_id = compute_snapshot_id(manifest)
70 write_snapshot(root, SnapshotRecord(snapshot_id=snap_id, manifest=manifest))
71 committed_at = datetime.datetime.now(datetime.timezone.utc)
72 commit_id = compute_commit_id(
73 [], snap_id, f"commit {_counter}", committed_at.isoformat()
74 )
75 write_commit(
76 root,
77 CommitRecord(
78 commit_id=commit_id,
79 repo_id=_REPO_ID,
80 branch=branch,
81 snapshot_id=snap_id,
82 message=f"commit {_counter}",
83 committed_at=committed_at,
84 ),
85 )
86 (root / ".muse" / "refs" / "heads" / branch).write_text(commit_id, encoding="utf-8")
87 return commit_id
88
89
90 def _invoke(repo: pathlib.Path, *args: str):
91 from muse.cli.app import main as cli
92 return runner.invoke(cli, ["restore", *args], env=_env(repo))
93
94
95 # ---------------------------------------------------------------------------
96 # Performance tier
97 # ---------------------------------------------------------------------------
98
99
100 def test_restore_json_has_duration_ms(tmp_path: pathlib.Path) -> None:
101 """JSON output must include 'duration_ms' as a non-negative float."""
102 root = _init_repo(tmp_path)
103 _commit_files(root, {"a.py": b"# orig\n"})
104 (root / "a.py").write_bytes(b"# dirty\n")
105
106 result = _invoke(root, "--json", "a.py")
107 assert result.exit_code == 0
108 data = json.loads(result.stdout)
109 assert "duration_ms" in data, "JSON must include 'duration_ms'"
110 assert isinstance(data["duration_ms"], (int, float)), "duration_ms must be numeric"
111 assert data["duration_ms"] >= 0, "duration_ms must be non-negative"
112
113
114 def test_restore_duration_ms_is_reasonable(tmp_path: pathlib.Path) -> None:
115 """duration_ms for a single-file restore should be well under 5 seconds."""
116 root = _init_repo(tmp_path)
117 _commit_files(root, {"a.py": b"# orig\n"})
118 (root / "a.py").write_bytes(b"# dirty\n")
119
120 result = _invoke(root, "--json", "a.py")
121 assert result.exit_code == 0
122 data = json.loads(result.stdout)
123 assert data["duration_ms"] < 5_000, f"duration_ms={data['duration_ms']} is suspiciously large"
124
125
126 def test_restore_dry_run_json_has_duration_ms(tmp_path: pathlib.Path) -> None:
127 """duration_ms must be present even in dry-run mode."""
128 root = _init_repo(tmp_path)
129 _commit_files(root, {"a.py": b"# orig\n"})
130 (root / "a.py").write_bytes(b"# dirty\n")
131
132 result = _invoke(root, "--dry-run", "--json", "a.py")
133 assert result.exit_code == 0
134 data = json.loads(result.stdout)
135 assert "duration_ms" in data
136
137
138 # ---------------------------------------------------------------------------
139 # Data integrity tier
140 # ---------------------------------------------------------------------------
141
142
143 def test_restore_json_schema_complete_on_success(tmp_path: pathlib.Path) -> None:
144 """All required JSON fields are present with correct types on success."""
145 root = _init_repo(tmp_path)
146 _commit_files(root, {"s.py": b"# orig\n"})
147 (root / "s.py").write_bytes(b"# dirty\n")
148
149 result = _invoke(root, "--json", "s.py")
150 assert result.exit_code == 0
151 data = json.loads(result.stdout)
152
153 assert isinstance(data["restored"], list)
154 assert isinstance(data["not_found"], list)
155 assert isinstance(data["dry_run"], bool)
156 assert isinstance(data["staged"], bool)
157 assert isinstance(data["worktree"], bool)
158 assert isinstance(data["duration_ms"], (int, float))
159 assert isinstance(data["exit_code"], int)
160
161
162 def test_restore_json_exit_code_zero_on_success(tmp_path: pathlib.Path) -> None:
163 """exit_code in JSON is 0 when all files are restored successfully."""
164 root = _init_repo(tmp_path)
165 _commit_files(root, {"ok.py": b"# orig\n"})
166 (root / "ok.py").write_bytes(b"# dirty\n")
167
168 result = _invoke(root, "--json", "ok.py")
169 assert result.exit_code == 0
170 data = json.loads(result.stdout)
171 assert data["exit_code"] == 0
172
173
174 def test_restore_json_exit_code_one_when_file_not_found(tmp_path: pathlib.Path) -> None:
175 """exit_code in JSON is 1 (USER_ERROR) when a file is not in source."""
176 root = _init_repo(tmp_path)
177 _commit_files(root, {"anchor.py": b"# anchor\n"})
178
179 result = _invoke(root, "--json", "ghost.py")
180 assert result.exit_code != 0
181 data = json.loads(result.stdout)
182 assert data["exit_code"] == 1
183
184
185 def test_restore_json_restored_list_correct(tmp_path: pathlib.Path) -> None:
186 """restored list contains exactly the successfully restored paths."""
187 root = _init_repo(tmp_path)
188 _commit_files(root, {"x.py": b"# x\n", "y.py": b"# y\n"})
189 (root / "x.py").write_bytes(b"# dirty x\n")
190 (root / "y.py").write_bytes(b"# dirty y\n")
191
192 result = _invoke(root, "--json", "x.py", "y.py")
193 data = json.loads(result.stdout)
194 assert sorted(data["restored"]) == ["x.py", "y.py"]
195 assert data["not_found"] == []
196
197
198 def test_restore_json_not_found_list_correct(tmp_path: pathlib.Path) -> None:
199 """not_found list contains paths that were absent from the source manifest."""
200 root = _init_repo(tmp_path)
201 _commit_files(root, {"real.py": b"# real\n"})
202 (root / "real.py").write_bytes(b"# dirty\n")
203
204 result = _invoke(root, "--json", "real.py", "ghost.py")
205 data = json.loads(result.stdout)
206 assert "real.py" in data["restored"]
207 assert "ghost.py" in data["not_found"]
208
209
210 def test_restore_json_staged_and_worktree_flags_reflect_args(tmp_path: pathlib.Path) -> None:
211 """staged/worktree fields in JSON reflect the CLI flags used."""
212 root = _init_repo(tmp_path)
213 _commit_files(root, {"f.py": b"# orig\n"})
214 obj_id = _sha(b"# mod\n")
215 write_object(root, obj_id, b"# mod\n")
216 stage: StagedFileMap = {"f.py": make_entry(obj_id, "M")}
217 write_stage(root, stage)
218
219 result = _invoke(root, "--staged", "--worktree", "--json", "f.py")
220 data = json.loads(result.stdout)
221 assert data["staged"] is True
222 assert data["worktree"] is True
223
224
225 # ---------------------------------------------------------------------------
226 # Error mapping — object store corruption → exit code 3
227 # ---------------------------------------------------------------------------
228
229
230 def test_restore_missing_object_exits_3(tmp_path: pathlib.Path) -> None:
231 """When an object_id is in the manifest but missing from the store, exit code must be 3."""
232 root = _init_repo(tmp_path)
233 content = b"# original\n"
234 obj_id = _sha(content)
235
236 # Build a manifest pointing at an object that is NOT in the store.
237 # We write the commit but deliberately don't call write_object.
238 manifest: Manifest = {"corrupt.py": obj_id}
239 snap_id = compute_snapshot_id(manifest)
240 write_snapshot(root, SnapshotRecord(snapshot_id=snap_id, manifest=manifest))
241 committed_at = datetime.datetime.now(datetime.timezone.utc)
242 global _counter
243 _counter += 1
244 commit_id = compute_commit_id([], snap_id, f"corrupt commit {_counter}", committed_at.isoformat())
245 write_commit(
246 root,
247 CommitRecord(
248 commit_id=commit_id,
249 repo_id=_REPO_ID,
250 branch="main",
251 snapshot_id=snap_id,
252 message=f"corrupt commit {_counter}",
253 committed_at=committed_at,
254 ),
255 )
256 (root / ".muse" / "refs" / "heads" / "main").write_text(commit_id, encoding="utf-8")
257 # Create the file on disk so path resolution doesn't fail
258 (root / "corrupt.py").write_bytes(b"# dirty\n")
259
260 result = _invoke(root, "corrupt.py")
261 assert result.exit_code == 3, (
262 f"Expected exit code 3 (INTERNAL_ERROR) for missing object, got {result.exit_code}"
263 )
264
265
266 def test_restore_missing_object_json_exit_code_3(tmp_path: pathlib.Path) -> None:
267 """JSON exit_code is 3 when the object is missing from the store."""
268 root = _init_repo(tmp_path)
269 content = b"# original\n"
270 obj_id = _sha(content)
271
272 manifest: Manifest = {"corrupt2.py": obj_id}
273 snap_id = compute_snapshot_id(manifest)
274 write_snapshot(root, SnapshotRecord(snapshot_id=snap_id, manifest=manifest))
275 committed_at = datetime.datetime.now(datetime.timezone.utc)
276 global _counter
277 _counter += 1
278 commit_id = compute_commit_id([], snap_id, f"corrupt2 {_counter}", committed_at.isoformat())
279 write_commit(
280 root,
281 CommitRecord(
282 commit_id=commit_id,
283 repo_id=_REPO_ID,
284 branch="main",
285 snapshot_id=snap_id,
286 message=f"corrupt2 {_counter}",
287 committed_at=committed_at,
288 ),
289 )
290 (root / ".muse" / "refs" / "heads" / "main").write_text(commit_id, encoding="utf-8")
291 (root / "corrupt2.py").write_bytes(b"# dirty\n")
292
293 result = _invoke(root, "--json", "corrupt2.py")
294 assert result.exit_code == 3
295 data = json.loads(result.stdout)
296 assert data["exit_code"] == 3
297
298
299 # ---------------------------------------------------------------------------
300 # Concurrent restore
301 # ---------------------------------------------------------------------------
302
303
304 def test_restore_concurrent_independent_files(tmp_path: pathlib.Path) -> None:
305 """Two threads restore independent files without racing or corrupting each other."""
306 root = _init_repo(tmp_path)
307 original_a = b"# thread-a original\n"
308 original_b = b"# thread-b original\n"
309 _commit_files(root, {"ta.py": original_a, "tb.py": original_b})
310 (root / "ta.py").write_bytes(b"# dirty a\n")
311 (root / "tb.py").write_bytes(b"# dirty b\n")
312
313 errors: list[Exception] = []
314
315 def restore_a():
316 try:
317 result = _invoke(root, "ta.py")
318 assert result.exit_code == 0, f"thread-a exit {result.exit_code}"
319 except Exception as exc:
320 errors.append(exc)
321
322 def restore_b():
323 try:
324 result = _invoke(root, "tb.py")
325 assert result.exit_code == 0, f"thread-b exit {result.exit_code}"
326 except Exception as exc:
327 errors.append(exc)
328
329 t1 = threading.Thread(target=restore_a)
330 t2 = threading.Thread(target=restore_b)
331 t1.start()
332 t2.start()
333 t1.join(timeout=10)
334 t2.join(timeout=10)
335
336 assert not errors, f"Concurrent restore errors: {errors}"
337 assert (root / "ta.py").read_bytes() == original_a
338 assert (root / "tb.py").read_bytes() == original_b
339
340
341 # ---------------------------------------------------------------------------
342 # --source --staged combo
343 # ---------------------------------------------------------------------------
344
345
346 def test_restore_source_and_staged_clears_stage_from_source(tmp_path: pathlib.Path) -> None:
347 """--source <ref> --staged clears the stage entry so it matches source."""
348 root = _init_repo(tmp_path)
349 v1_content = b"# v1\n"
350 v1_commit = _commit_files(root, {"versioned.py": v1_content})
351
352 # Update to v2
353 v2_content = b"# v2\n"
354 _commit_files(root, {"versioned.py": v2_content})
355
356 # Stage a modification on top of v2
357 mod_content = b"# staged mod\n"
358 obj_id = _sha(mod_content)
359 write_object(root, obj_id, mod_content)
360 stage: StagedFileMap = {"versioned.py": make_entry(obj_id, "M")}
361 write_stage(root, stage)
362
363 # --source v1_commit --staged should clear the stage entry
364 result = _invoke(root, "--source", v1_commit, "--staged", "versioned.py")
365 assert result.exit_code == 0
366 stage_after = read_stage(root)
367 assert "versioned.py" not in stage_after
368
369
370 def test_restore_source_staged_worktree_restores_from_source(tmp_path: pathlib.Path) -> None:
371 """--source <ref> --staged --worktree restores disk from source, clears stage."""
372 root = _init_repo(tmp_path)
373 v1_content = b"# v1 original\n"
374 v1_commit = _commit_files(root, {"combo.py": v1_content})
375 _commit_files(root, {"combo.py": b"# v2\n"})
376
377 mod_content = b"# staged mod\n"
378 obj_id = _sha(mod_content)
379 write_object(root, obj_id, mod_content)
380 stage: StagedFileMap = {"combo.py": make_entry(obj_id, "M")}
381 write_stage(root, stage)
382 (root / "combo.py").write_bytes(b"# dirty disk\n")
383
384 result = _invoke(root, "--source", v1_commit, "--staged", "--worktree", "combo.py")
385 assert result.exit_code == 0
386 assert (root / "combo.py").read_bytes() == v1_content
387 stage_after = read_stage(root)
388 assert "combo.py" not in stage_after
389
390
391 # ---------------------------------------------------------------------------
392 # Text summary output
393 # ---------------------------------------------------------------------------
394
395
396 def test_restore_text_output_summary_line(tmp_path: pathlib.Path) -> None:
397 """Text output includes a summary line like 'Restored 2 file(s)'."""
398 root = _init_repo(tmp_path)
399 _commit_files(root, {"p.py": b"# p\n", "q.py": b"# q\n"})
400 (root / "p.py").write_bytes(b"# dirty p\n")
401 (root / "q.py").write_bytes(b"# dirty q\n")
402
403 result = _invoke(root, "p.py", "q.py")
404 assert result.exit_code == 0
405 output = result.stdout + (result.stderr or "")
406 assert "2" in output, f"Expected count in output: {output!r}"
407
408
409 def test_restore_text_output_errors_noted(tmp_path: pathlib.Path) -> None:
410 """Text output notes how many errors occurred when some paths fail."""
411 root = _init_repo(tmp_path)
412 _commit_files(root, {"real.py": b"# real\n"})
413 (root / "real.py").write_bytes(b"# dirty\n")
414
415 result = _invoke(root, "real.py", "ghost.py")
416 assert result.exit_code != 0
417 output = (result.stdout or "") + (result.stderr or "")
418 # Should mention the failure somehow
419 assert "ghost" in output or "error" in output.lower() or "not" in output.lower()
420
421
422 # ---------------------------------------------------------------------------
423 # _resolve_source_manifest — docstring gap: bad ref returns {}, never raises
424 # ---------------------------------------------------------------------------
425
426
427 def test_resolve_source_manifest_bad_ref_returns_empty(tmp_path: pathlib.Path) -> None:
428 """_resolve_source_manifest returns {} for a non-existent ref — never raises."""
429 from muse.cli.commands.restore import _resolve_source_manifest
430 root = _init_repo(tmp_path)
431 _commit_files(root, {"a.py": b"# a\n"})
432 result = _resolve_source_manifest(root, source_ref="nonexistent-branch-xyz")
433 assert result == {}
434
435
436 def test_resolve_source_manifest_valid_ref(tmp_path: pathlib.Path) -> None:
437 """_resolve_source_manifest resolves a valid branch name to its manifest."""
438 from muse.cli.commands.restore import _resolve_source_manifest
439 root = _init_repo(tmp_path)
440 content = b"# branch content\n"
441 _commit_files(root, {"b.py": content}, branch="main")
442 manifest = _resolve_source_manifest(root, source_ref="main")
443 assert "b.py" in manifest
444 assert manifest["b.py"] == _sha(content)
445
446
447 # ---------------------------------------------------------------------------
448 # Edge: restore staged-only with --source doesn't require file on disk
449 # ---------------------------------------------------------------------------
450
451
452 def test_restore_staged_only_source_does_not_require_disk_file(tmp_path: pathlib.Path) -> None:
453 """--staged with --source works even when the disk file doesn't exist."""
454 root = _init_repo(tmp_path)
455 v1_commit = _commit_files(root, {"staged_only.py": b"# v1\n"})
456 # Stage a modification
457 obj_id = _sha(b"# mod\n")
458 write_object(root, obj_id, b"# mod\n")
459 stage: StagedFileMap = {"staged_only.py": make_entry(obj_id, "M")}
460 write_stage(root, stage)
461 # Delete disk file
462 (root / "staged_only.py").unlink()
463
464 result = _invoke(root, "--source", v1_commit, "--staged", "staged_only.py")
465 assert result.exit_code == 0
466 stage_after = read_stage(root)
467 assert "staged_only.py" not in stage_after
468
469
470 # ---------------------------------------------------------------------------
471 # Performance: duration_ms for 50-file restore is under 10 seconds
472 # ---------------------------------------------------------------------------
473
474
475 def test_restore_50_files_duration_ms_reasonable(tmp_path: pathlib.Path) -> None:
476 """50-file restore reports duration_ms and completes under 10 seconds."""
477 root = _init_repo(tmp_path)
478 files = {f"perf_{i}.py": f"# orig {i}\n".encode() for i in range(50)}
479 _commit_files(root, files)
480 for name in files:
481 (root / name).write_bytes(b"# dirty\n")
482
483 result = _invoke(root, "--json", *files.keys())
484 assert result.exit_code == 0
485 data = json.loads(result.stdout)
486 assert "duration_ms" in data
487 assert data["duration_ms"] < 10_000
488 assert len(data["restored"]) == 50
File History 1 commit
sha256:88ac91129873e6a496e9189515aa690eb893ae25d69c8f72af141a2be5068eb3 docs: docstring sprint contract→find-symbol — idiomatic run… Sonnet 4.6 patch 138 days ago