gabriel / muse public
test_code_stage.py python
1,099 lines 42.3 KB
Raw
sha256:2a1cf861048b753a21d6ca853a83cdfc2a46f15dcbb561ee79ebb9dc40c03af6 switch same-commit fix, agent-config user-global config, an… Human patch 15 days ago
1 """Tests for ``muse code add`` / ``muse code reset`` and stage-aware commit/status.
2
3 Coverage matrix:
4
5 Unit tests (pure functions):
6 - _split_into_hunks: empty diff, single hunk, multi-hunk, trailing newlines
7 - _apply_hunks_to_bytes: accept all, accept none, accept partial, new-file
8 - _infer_mode: all three modes (A / M / D)
9 - _colorize_hunk: color escape codes present for +/- lines
10
11 Integration tests (CLI round-trips):
12 - muse code add <file> — stages modified file as mode M
13 - muse code add <new-file> — stages new file as mode A
14 - muse code add . — stages everything
15 - muse code add -A — stages all including new files
16 - muse code add -u — stages tracked files only (excludes untracked)
17 - muse code add -u — stages deleted files as mode D
18 - muse code add <dir> — expands directory recursively
19 - muse code add --dry-run — shows intent without writing
20 - muse code add -v — verbose per-file output
21 - muse code add (re-stage) — updates object_id when file changes again
22 - nonexistent path — exits non-zero
23 - wrong domain — exits non-zero
24
25 Stage-aware commit:
26 - Only staged files appear in the committed snapshot
27 - Unstaged changes do NOT appear in the committed snapshot
28 - Stage is cleared after a successful commit
29 - Staged deletion removes file from next commit
30
31 muse status — three-bucket view:
32 - "Changes staged for commit" section present
33 - "Changes not staged" section present
34 - Untracked files listed
35 - --format json includes staged/unstaged/untracked keys
36 - --json format
37
38 muse code reset:
39 - reset <file> — unstages that file only
40 - reset HEAD <file> — Git-syntax alias works
41 - reset (no args) — clears everything
42 - reset when nothing staged — exits cleanly
43
44 Resilience:
45 - Corrupt stage.json degrades gracefully (read_stage returns {})
46 - Staging a file outside the repo root is rejected
47
48 Stress:
49 - Staging 100 files in one shot
50 """
51
52 from __future__ import annotations
53
54 import json
55 import os
56 import pathlib
57 from collections.abc import Mapping
58
59 import pytest
60
61 from muse.core.types import fake_id
62 from muse.plugins.code.stage import read_stage, stage_path, StagedEntry, StagedFileMap
63 from muse.core.paths import code_dir, muse_dir
64 from tests.cli_test_helper import CliRunner
65
66 cli = None # argparse migration — CliRunner ignores this arg
67 runner = CliRunner()
68
69
70 def _read_stage_raw(root: pathlib.Path) -> StagedFileMap:
71 """Read the current stage index using the production API."""
72 return read_stage(root)
73
74
75 # ---------------------------------------------------------------------------
76 # Unit tests — pure functions
77 # ---------------------------------------------------------------------------
78
79
80 class TestSplitIntoHunks:
81 """Unit tests for _split_into_hunks (no I/O)."""
82
83 def _run(self, diff_text: str) -> list[list[str]]:
84 from muse.cli.commands.code_stage import _split_into_hunks
85 lines = [f"{l}\n" for l in diff_text.splitlines()]
86 return _split_into_hunks(lines)
87
88 def test_empty_diff_returns_no_hunks(self) -> None:
89 assert self._run("") == []
90
91 def test_single_hunk(self) -> None:
92 diff = (
93 "--- a/foo.py\n"
94 "+++ b/foo.py\n"
95 "@@ -1,2 +1,3 @@\n"
96 " def f():\n"
97 "- pass\n"
98 "+ return 1\n"
99 )
100 hunks = self._run(diff)
101 assert len(hunks) == 1
102 assert any("@@" in l for l in hunks[0])
103
104 def test_multi_hunk_has_header_on_each(self) -> None:
105 diff = (
106 "--- a/foo.py\n"
107 "+++ b/foo.py\n"
108 "@@ -1,2 +1,3 @@\n"
109 " line1\n"
110 "-old\n"
111 "+new\n"
112 "@@ -10,2 +11,3 @@\n"
113 " line10\n"
114 "-old10\n"
115 "+new10\n"
116 )
117 hunks = self._run(diff)
118 assert len(hunks) == 2
119 # Each hunk starts with the file header (--- / +++), then @@
120 for h in hunks:
121 assert any(l.startswith("---") for l in h)
122 assert any(l.startswith("+++") for l in h)
123 assert any(l.startswith("@@") for l in h)
124
125 def test_no_header_lines_before_first_hunk_is_still_valid(self) -> None:
126 diff = (
127 "@@ -1,1 +1,1 @@\n"
128 "-old\n"
129 "+new\n"
130 )
131 hunks = self._run(diff)
132 assert len(hunks) == 1
133
134
135 class TestApplyHunksToBytes:
136 """Unit tests for _apply_hunks_to_bytes."""
137
138 def _run(self, before: str, diff_text: str, accept_all: bool = True) -> str:
139 from muse.cli.commands.code_stage import _split_into_hunks, _apply_hunks_to_bytes
140
141 before_lines = before.splitlines(keepends=True)
142 after_lines = diff_text.splitlines(keepends=True)
143
144 import difflib
145 diff = list(difflib.unified_diff(
146 before_lines, after_lines, fromfile="a/f", tofile="b/f", lineterm=""
147 ))
148 diff_nl = [f"{l}\n" for l in diff]
149 hunks = _split_into_hunks(diff_nl)
150
151 accepted = hunks if accept_all else []
152 result = _apply_hunks_to_bytes(before.encode(), accepted)
153 return result.decode()
154
155 def test_accept_all_hunks_produces_after_content(self) -> None:
156 before = "def f():\n pass\n"
157 after = "def f():\n return 1\n"
158 result = self._run(before, after, accept_all=True)
159 assert "return 1" in result
160
161 def test_accept_no_hunks_preserves_original(self) -> None:
162 before = "def f():\n pass\n"
163 after = "def f():\n return 1\n"
164 result = self._run(before, after, accept_all=False)
165 assert result == before
166
167 def test_new_file_from_empty(self) -> None:
168 """Staging a new file from empty before-bytes produces after-content."""
169 before = ""
170 after = "x = 1\ny = 2\n"
171 result = self._run(before, after, accept_all=True)
172 assert "x = 1" in result
173
174 def test_binary_safe_with_replacement(self) -> None:
175 from muse.cli.commands.code_stage import _apply_hunks_to_bytes
176 result = _apply_hunks_to_bytes(b"\xff\xfe", [])
177 assert isinstance(result, bytes)
178
179
180 class TestInferMode:
181 """Unit tests for _infer_mode."""
182
183 def _run(self, rel: str, head: Manifest, exists: bool) -> str:
184 from muse.cli.commands.code_stage import _infer_mode
185 return _infer_mode(rel, head, exists)
186
187 def test_existing_tracked_is_M(self) -> None:
188 assert self._run("src/a.py", {"src/a.py": "abc"}, True) == "M"
189
190 def test_new_untracked_is_A(self) -> None:
191 assert self._run("src/new.py", {}, True) == "A"
192
193 def test_missing_from_disk_is_D(self) -> None:
194 assert self._run("src/gone.py", {"src/gone.py": "abc"}, False) == "D"
195
196 def test_missing_and_not_tracked_is_D(self) -> None:
197 # Shouldn't normally occur, but must not crash.
198 assert self._run("ghost.py", {}, False) == "D"
199
200
201 class TestColorizeHunk:
202 """Unit tests for _colorize_hunk."""
203
204 def test_added_lines_get_green(self) -> None:
205 from muse.cli.commands.code_stage import _colorize_hunk
206 result = _colorize_hunk(["+new line\n"])
207 assert "\x1b[32m" in result # green
208
209 def test_removed_lines_get_red(self) -> None:
210 from muse.cli.commands.code_stage import _colorize_hunk
211 result = _colorize_hunk(["-old line\n"])
212 assert "\x1b[31m" in result # red
213
214 def test_file_header_not_colored(self) -> None:
215 from muse.cli.commands.code_stage import _colorize_hunk
216 result = _colorize_hunk(["--- a/foo.py\n", "+++ b/foo.py\n"])
217 # file header lines should not get red/green
218 assert "\x1b[31m" not in result
219 assert "\x1b[32m" not in result
220
221 def test_at_at_header_gets_cyan(self) -> None:
222 from muse.cli.commands.code_stage import _colorize_hunk
223 result = _colorize_hunk(["@@ -1,2 +1,3 @@\n"])
224 assert "\x1b[36m" in result # cyan
225
226
227 # ---------------------------------------------------------------------------
228 # Fixtures
229 # ---------------------------------------------------------------------------
230
231
232 def _env(root: pathlib.Path) -> Manifest:
233 return {"MUSE_REPO_ROOT": str(root)}
234
235
236 @pytest.fixture()
237 def code_repo(tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> pathlib.Path:
238 """Initialise a fresh code-domain Muse repo with one initial commit."""
239 monkeypatch.chdir(tmp_path)
240
241 result = runner.invoke(cli, ["init", "--domain", "code"], env=_env(tmp_path))
242 assert result.exit_code == 0, result.output
243
244 (tmp_path / "auth.py").write_text("def authenticate():\n pass\n")
245 (tmp_path / "models.py").write_text("class User:\n pass\n")
246
247 r = runner.invoke(cli, ["commit", "-m", "initial"], env=_env(tmp_path))
248 assert r.exit_code == 0, r.output
249
250 return tmp_path
251
252
253 # ---------------------------------------------------------------------------
254 # muse code add — integration tests
255 # ---------------------------------------------------------------------------
256
257
258 class TestCodeAdd:
259 def test_stage_modified_file_is_mode_M(self, code_repo: pathlib.Path) -> None:
260 (code_repo / "auth.py").write_text("def authenticate():\n return True\n")
261 result = runner.invoke(cli, ["code", "add", "auth.py"], env=_env(code_repo))
262 assert result.exit_code == 0, result.output
263 assert "modified" in result.output
264
265 stage = _read_stage_raw(code_repo)
266 assert stage["auth.py"]["mode"] == "M"
267
268 def test_stage_new_file_is_mode_A(self, code_repo: pathlib.Path) -> None:
269 (code_repo / "new_module.py").write_text("x = 1\n")
270 runner.invoke(cli, ["code", "add", "new_module.py"], env=_env(code_repo))
271 stage = _read_stage_raw(code_repo)
272 assert stage["new_module.py"]["mode"] == "A"
273
274 def test_stage_dot_stages_everything(self, code_repo: pathlib.Path) -> None:
275 (code_repo / "auth.py").write_text("# changed\n")
276 runner.invoke(cli, ["code", "add", "."], env=_env(code_repo))
277 stage = _read_stage_raw(code_repo)
278 assert "auth.py" in stage
279
280 def test_stage_A_includes_new_files(self, code_repo: pathlib.Path) -> None:
281 (code_repo / "auth.py").write_text("# changed\n")
282 (code_repo / "new.py").write_text("x = 1\n")
283 runner.invoke(cli, ["code", "add", "-A"], env=_env(code_repo))
284 stage = _read_stage_raw(code_repo)
285 assert "auth.py" in stage
286 assert "new.py" in stage
287
288 def test_stage_u_excludes_new_untracked_files(
289 self, code_repo: pathlib.Path
290 ) -> None:
291 """-u stages only tracked files; new/untracked files are NOT staged."""
292 (code_repo / "auth.py").write_text("# tracked change\n")
293 (code_repo / "brand_new.py").write_text("x = 1\n")
294
295 runner.invoke(cli, ["code", "add", "-u"], env=_env(code_repo))
296
297 assert stage_path(code_repo).exists()
298 stage = _read_stage_raw(code_repo)
299 assert "auth.py" in stage
300 assert "brand_new.py" not in stage
301
302 def test_stage_u_includes_deleted_files(self, code_repo: pathlib.Path) -> None:
303 (code_repo / "models.py").unlink()
304 runner.invoke(cli, ["code", "add", "-u"], env=_env(code_repo))
305 stage = _read_stage_raw(code_repo)
306 assert "models.py" in stage
307 assert stage["models.py"]["mode"] == "D"
308
309 def test_stage_directory_expands_recursively(
310 self, code_repo: pathlib.Path
311 ) -> None:
312 src = code_repo / "src"
313 src.mkdir()
314 (src / "a.py").write_text("x = 1\n")
315 (src / "b.py").write_text("y = 2\n")
316
317 runner.invoke(cli, ["code", "add", "src"], env=_env(code_repo))
318 stage = _read_stage_raw(code_repo)
319 assert "src/a.py" in stage
320 assert "src/b.py" in stage
321
322 def test_dry_run_does_not_write_stage(self, code_repo: pathlib.Path) -> None:
323 (code_repo / "auth.py").write_text("# dry\n")
324 runner.invoke(
325 cli, ["code", "add", "--dry-run", "auth.py"], env=_env(code_repo)
326 )
327 assert not stage_path(code_repo).exists()
328
329 def test_dry_run_output_shows_files(self, code_repo: pathlib.Path) -> None:
330 (code_repo / "auth.py").write_text("# dry\n")
331 result = runner.invoke(
332 cli, ["code", "add", "--dry-run", "auth.py"], env=_env(code_repo)
333 )
334 assert "auth.py" in result.output
335
336 def test_verbose_shows_per_file_output(self, code_repo: pathlib.Path) -> None:
337 (code_repo / "auth.py").write_text("# verbose\n")
338 result = runner.invoke(
339 cli, ["code", "add", "-v", "auth.py"], env=_env(code_repo)
340 )
341 assert result.exit_code == 0
342 assert "auth.py" in result.output
343
344 def test_restage_updates_object_id(self, code_repo: pathlib.Path) -> None:
345 """Staging a file twice with different content updates the object_id."""
346 (code_repo / "auth.py").write_text("# version 1\n")
347 runner.invoke(cli, ["code", "add", "auth.py"], env=_env(code_repo))
348 oid_v1 = _read_stage_raw(code_repo)["auth.py"]["object_id"]
349
350 (code_repo / "auth.py").write_text("# version 2\n")
351 runner.invoke(cli, ["code", "add", "auth.py"], env=_env(code_repo))
352 oid_v2 = _read_stage_raw(code_repo)["auth.py"]["object_id"]
353
354 assert oid_v1 != oid_v2
355
356 def test_staging_unchanged_file_is_idempotent(
357 self, code_repo: pathlib.Path
358 ) -> None:
359 """Staging a file that has not changed since last staging is a no-op."""
360 (code_repo / "auth.py").write_text("# same\n")
361 runner.invoke(cli, ["code", "add", "auth.py"], env=_env(code_repo))
362 result = runner.invoke(cli, ["code", "add", "auth.py"], env=_env(code_repo))
363 assert result.exit_code == 0
364 assert "already up to date" in result.output
365
366 def test_nonexistent_path_exits_error(self, code_repo: pathlib.Path) -> None:
367 result = runner.invoke(
368 cli, ["code", "add", "does_not_exist.py"], env=_env(code_repo)
369 )
370 assert result.exit_code != 0
371
372 def test_wrong_domain_exits_error(
373 self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
374 ) -> None:
375 monkeypatch.chdir(tmp_path)
376 runner.invoke(cli, ["init", "--domain", "midi"], env=_env(tmp_path))
377 result = runner.invoke(cli, ["code", "add", "file.py"], env=_env(tmp_path))
378 assert result.exit_code != 0
379
380
381 # ---------------------------------------------------------------------------
382 # Stage-aware commit
383 # ---------------------------------------------------------------------------
384
385
386 class TestStageAwareCommit:
387 def test_only_staged_file_is_committed(self, code_repo: pathlib.Path) -> None:
388 (code_repo / "auth.py").write_text("def authenticate():\n return True\n")
389 (code_repo / "models.py").write_text("class User:\n name = 'anon'\n")
390
391 runner.invoke(cli, ["code", "add", "auth.py"], env=_env(code_repo))
392
393 r = runner.invoke(
394 cli, ["commit", "-m", "auth only", "--json"],
395 env=_env(code_repo),
396 )
397 assert r.exit_code == 0, r.output
398 data = json.loads(r.output.strip())
399
400 from muse.core.commits import read_commit
401 from muse.core.snapshots import read_snapshot
402 from muse.core.object_store import read_object
403
404 commit = read_commit(code_repo, data["commit_id"])
405 assert commit is not None
406 snap = read_snapshot(code_repo, commit.snapshot_id)
407 assert snap is not None
408
409 auth_bytes = read_object(code_repo, snap.manifest["auth.py"])
410 assert auth_bytes is not None
411 assert b"return True" in auth_bytes
412
413 models_bytes = read_object(code_repo, snap.manifest["models.py"])
414 assert models_bytes is not None
415 # models.py was NOT staged — should have old content (pass, not name='anon')
416 assert b"name = 'anon'" not in models_bytes
417 assert b"pass" in models_bytes
418
419 def test_stage_cleared_after_commit(self, code_repo: pathlib.Path) -> None:
420 (code_repo / "auth.py").write_text("# cleared after commit\n")
421 runner.invoke(cli, ["code", "add", "auth.py"], env=_env(code_repo))
422
423 assert stage_path(code_repo).exists()
424
425 runner.invoke(cli, ["commit", "-m", "clear stage test"], env=_env(code_repo))
426 assert not stage_path(code_repo).exists()
427
428 def test_staged_deletion_removes_file_from_commit(
429 self, code_repo: pathlib.Path
430 ) -> None:
431 (code_repo / "models.py").unlink()
432 runner.invoke(cli, ["code", "add", "-u"], env=_env(code_repo))
433
434 r = runner.invoke(
435 cli, ["commit", "-m", "delete models", "--json"],
436 env=_env(code_repo),
437 )
438 assert r.exit_code == 0, r.output
439 data = json.loads(r.output.strip())
440
441 from muse.core.commits import read_commit
442 from muse.core.snapshots import read_snapshot
443 commit = read_commit(code_repo, data["commit_id"])
444 assert commit is not None
445 snap = read_snapshot(code_repo, commit.snapshot_id)
446 assert snap is not None
447 assert "models.py" not in snap.manifest
448
449 def test_full_snapshot_when_no_stage(self, code_repo: pathlib.Path) -> None:
450 """Without a stage, commit captures the full working tree."""
451 (code_repo / "extra.py").write_text("z = 99\n")
452
453 r = runner.invoke(
454 cli, ["commit", "-m", "full snapshot", "--json"],
455 env=_env(code_repo),
456 )
457 assert r.exit_code == 0, r.output
458 data = json.loads(r.output.strip())
459
460 from muse.core.commits import read_commit
461 from muse.core.snapshots import read_snapshot
462 commit = read_commit(code_repo, data["commit_id"])
463 assert commit is not None
464 snap = read_snapshot(code_repo, commit.snapshot_id)
465 assert snap is not None
466 assert "extra.py" in snap.manifest
467
468
469 # ---------------------------------------------------------------------------
470 # muse status — staged view
471 # ---------------------------------------------------------------------------
472
473
474 class TestStageStatus:
475 def test_shows_staged_section_when_stage_active(
476 self, code_repo: pathlib.Path
477 ) -> None:
478 (code_repo / "auth.py").write_text("# staged change\n")
479 runner.invoke(cli, ["code", "add", "auth.py"], env=_env(code_repo))
480
481 result = runner.invoke(cli, ["status"], env=_env(code_repo))
482 assert result.exit_code == 0, result.output
483 assert "staged for commit" in result.output
484 assert "auth.py" in result.output
485
486 def test_shows_unstaged_section_for_unmodified_tracked_with_changes(
487 self, code_repo: pathlib.Path
488 ) -> None:
489 (code_repo / "auth.py").write_text("# staged\n")
490 (code_repo / "models.py").write_text("# NOT staged\n")
491 runner.invoke(cli, ["code", "add", "auth.py"], env=_env(code_repo))
492
493 result = runner.invoke(cli, ["status"], env=_env(code_repo))
494 assert "not staged" in result.output
495 assert "models.py" in result.output
496
497 def test_shows_untracked_section(self, code_repo: pathlib.Path) -> None:
498 (code_repo / "auth.py").write_text("# staged\n")
499 (code_repo / "brand_new.py").write_text("x = 1\n")
500 runner.invoke(cli, ["code", "add", "auth.py"], env=_env(code_repo))
501
502 result = runner.invoke(cli, ["status"], env=_env(code_repo))
503 assert "Untracked" in result.output
504 assert "brand_new.py" in result.output
505
506 def test_json_format_has_all_buckets(self, code_repo: pathlib.Path) -> None:
507 (code_repo / "auth.py").write_text("# json stage\n")
508 (code_repo / "new_file.py").write_text("x = 1\n")
509 runner.invoke(cli, ["code", "add", "auth.py"], env=_env(code_repo))
510
511 result = runner.invoke(
512 cli, ["status", "--json"], env=_env(code_repo)
513 )
514 assert result.exit_code == 0, result.output
515 data = json.loads(result.output.strip())
516 assert "staged" in data
517 assert "unstaged" in data
518 assert "untracked" in data
519 assert "auth.py" in data["staged"]["modified"]
520 assert "new_file.py" in data["untracked"]
521
522 def test_json_format_with_stage(self, code_repo: pathlib.Path) -> None:
523 (code_repo / "auth.py").write_text("# staged\n")
524 runner.invoke(cli, ["code", "add", "auth.py"], env=_env(code_repo))
525
526 result = runner.invoke(cli, ["status", "--json"], env=_env(code_repo))
527 assert result.exit_code == 0
528 assert "auth.py" in result.output
529
530 def test_short_format_with_stage(self, code_repo: pathlib.Path) -> None:
531 (code_repo / "auth.py").write_text("# short\n")
532 runner.invoke(cli, ["code", "add", "auth.py"], env=_env(code_repo))
533
534 result = runner.invoke(cli, ["status", "--short"], env=_env(code_repo))
535 assert result.exit_code == 0
536 assert "auth.py" in result.output
537
538 def test_clean_tree_after_commit_clears_stage(
539 self, code_repo: pathlib.Path
540 ) -> None:
541 """After staging and committing, status should show clean tree."""
542 (code_repo / "auth.py").write_text("# committed\n")
543 runner.invoke(cli, ["code", "add", "auth.py"], env=_env(code_repo))
544 runner.invoke(cli, ["commit", "-m", "staged commit"], env=_env(code_repo))
545
546 result = runner.invoke(cli, ["status"], env=_env(code_repo))
547 assert result.exit_code == 0
548 # No stage file → falls back to normal drift-based status.
549 assert "staged for commit" not in result.output
550
551
552 # ---------------------------------------------------------------------------
553 # muse code reset
554 # ---------------------------------------------------------------------------
555
556
557 class TestCodeReset:
558 def test_reset_specific_file(self, code_repo: pathlib.Path) -> None:
559 (code_repo / "auth.py").write_text("# staged\n")
560 (code_repo / "models.py").write_text("# also staged\n")
561 runner.invoke(cli, ["code", "add", "-A"], env=_env(code_repo))
562
563 result = runner.invoke(
564 cli, ["code", "reset", "auth.py"], env=_env(code_repo)
565 )
566 assert result.exit_code == 0
567 stage = _read_stage_raw(code_repo)
568 assert "auth.py" not in stage
569 assert "models.py" in stage
570
571 def test_reset_HEAD_syntax(self, code_repo: pathlib.Path) -> None:
572 (code_repo / "auth.py").write_text("# head\n")
573 runner.invoke(cli, ["code", "add", "auth.py"], env=_env(code_repo))
574 result = runner.invoke(
575 cli, ["code", "reset", "HEAD", "auth.py"], env=_env(code_repo)
576 )
577 assert result.exit_code == 0
578 assert not stage_path(code_repo).exists()
579
580 def test_reset_no_args_clears_all(self, code_repo: pathlib.Path) -> None:
581 (code_repo / "auth.py").write_text("# a\n")
582 (code_repo / "models.py").write_text("# b\n")
583 runner.invoke(cli, ["code", "add", "-A"], env=_env(code_repo))
584 result = runner.invoke(cli, ["code", "reset"], env=_env(code_repo))
585 assert result.exit_code == 0
586 assert not stage_path(code_repo).exists()
587
588 def test_reset_when_nothing_staged(self, code_repo: pathlib.Path) -> None:
589 result = runner.invoke(cli, ["code", "reset"], env=_env(code_repo))
590 assert result.exit_code == 0
591 assert "Nothing staged" in result.output
592
593 def test_reset_nonexistent_file_does_not_crash(
594 self, code_repo: pathlib.Path
595 ) -> None:
596 (code_repo / "auth.py").write_text("# staged\n")
597 runner.invoke(cli, ["code", "add", "auth.py"], env=_env(code_repo))
598 result = runner.invoke(
599 cli, ["code", "reset", "not_in_stage.py"], env=_env(code_repo)
600 )
601 assert result.exit_code == 0
602 assert "not staged" in result.output
603
604
605 # ---------------------------------------------------------------------------
606 # Resilience
607 # ---------------------------------------------------------------------------
608
609
610 class TestResilience:
611 def test_corrupt_stage_json_returns_empty(
612 self, code_repo: pathlib.Path
613 ) -> None:
614 """Corrupt stage.json must degrade gracefully — returns {} on read."""
615 stage_dir = code_dir(code_repo)
616 stage_dir.mkdir(parents=True, exist_ok=True)
617 (stage_dir / "stage.json").write_bytes(b"\xde\xad\xbe\xef garbage")
618
619 entries = read_stage(code_repo)
620 assert entries == {}
621
622 def test_truncated_stage_json_returns_empty(
623 self, code_repo: pathlib.Path
624 ) -> None:
625 stage_dir = code_dir(code_repo)
626 stage_dir.mkdir(parents=True, exist_ok=True)
627 (stage_dir / "stage.json").write_bytes(b"\x00\x01\x02")
628
629 entries = read_stage(code_repo)
630 assert entries == {}
631
632 def test_stage_json_is_readable_directly(
633 self, code_repo: pathlib.Path
634 ) -> None:
635 """stage.json is the canonical format — existing files are read directly."""
636 import json as _json
637 stage_dir = code_dir(code_repo)
638 stage_dir.mkdir(parents=True, exist_ok=True)
639 stage_dir.joinpath("stage.json").write_text(_json.dumps({
640 "version": 3,
641 "entries": {
642 "auth.py": {"object_id": f"{'abc123' * 10}ab12", "mode": "M", "staged_at": "2026-01-01T00:00:00+00:00"},
643 },
644 }))
645
646 entries = read_stage(code_repo)
647 assert "auth.py" in entries
648 assert entries["auth.py"]["mode"] == "M"
649 # stage.json is the canonical path — it stays on disk.
650 assert stage_path(code_repo).exists()
651
652 def test_missing_stage_returns_empty(self, code_repo: pathlib.Path) -> None:
653 entries = read_stage(code_repo)
654 assert entries == {}
655
656 def test_write_empty_entries_removes_file(
657 self, code_repo: pathlib.Path
658 ) -> None:
659 from muse.plugins.code.stage import write_stage, StagedFileMap
660
661 path = stage_path(code_repo)
662 path.parent.mkdir(parents=True, exist_ok=True)
663 # Create a non-empty JSON stage file first.
664 import json as _json
665 path.write_bytes(_json.dumps({"version": 3, "entries": {"f.py": {"object_id": "a" * 64, "mode": "M", "staged_at": "x"}}}).encode())
666
667 write_stage(code_repo, {})
668 assert not path.exists()
669
670 def test_clear_stage_idempotent(self, code_repo: pathlib.Path) -> None:
671 from muse.plugins.code.stage import clear_stage, StagedFileMap
672
673 clear_stage(code_repo) # no stage to clear — must not raise
674 clear_stage(code_repo) # idempotent
675
676
677 # ---------------------------------------------------------------------------
678 # Stress test
679 # ---------------------------------------------------------------------------
680
681
682 class TestStageStress:
683 def test_stage_100_files(
684 self, code_repo: pathlib.Path
685 ) -> None:
686 """Staging 100 files must complete without error and write all entries."""
687 for i in range(100):
688 (code_repo / f"module_{i:03d}.py").write_text(f"X_{i} = {i}\n")
689
690 result = runner.invoke(cli, ["code", "add", "-A"], env=_env(code_repo))
691 assert result.exit_code == 0, result.output
692
693 stage = _read_stage_raw(code_repo)
694 # 100 new files + 2 original tracked files (auth.py, models.py)
695 assert len(stage) >= 100
696
697 def test_commit_100_staged_files(
698 self, code_repo: pathlib.Path
699 ) -> None:
700 """Committing 100 staged files produces a correct manifest."""
701 for i in range(100):
702 (code_repo / f"mod_{i:03d}.py").write_text(f"V = {i}\n")
703
704 runner.invoke(cli, ["code", "add", "-A"], env=_env(code_repo))
705 r = runner.invoke(
706 cli, ["commit", "-m", "100 files", "--json"],
707 env=_env(code_repo),
708 )
709 assert r.exit_code == 0, r.output
710 data = json.loads(r.output.strip())
711
712 from muse.core.commits import read_commit
713 from muse.core.snapshots import read_snapshot
714 commit = read_commit(code_repo, data["commit_id"])
715 assert commit is not None
716 snap = read_snapshot(code_repo, commit.snapshot_id)
717 assert snap is not None
718 assert len(snap.manifest) >= 100
719
720
721 def test_add_all_stages_deletions(
722 code_repo: pathlib.Path,
723 ) -> None:
724 """``muse code add -A`` must stage tracked files that have been deleted.
725
726 Regression test: before the fix, ``-A`` used ``_walk_tree`` which only
727 returns files present on disk. Deleted tracked files were therefore
728 silently omitted and the deletion was never recorded in the stage.
729 """
730 # code_repo already has auth.py and models.py committed.
731 os.remove(code_repo / "auth.py")
732
733 r = runner.invoke(cli, ["code", "add", "-A"], env=_env(code_repo))
734 assert r.exit_code == 0, r.output
735
736 from muse.plugins.code.stage import read_stage, StagedFileMap
737 stage = read_stage(code_repo)
738 assert "auth.py" in stage, "deleted tracked file must appear in stage"
739 assert stage["auth.py"]["mode"] == "D", "deleted file must have mode D"
740
741
742 def test_add_dot_stages_museattributes(
743 code_repo: pathlib.Path,
744 ) -> None:
745 """`muse code add .` must stage `.museattributes` when it exists.
746
747 Regression test: before the fix, ``_walk_tree`` skipped all files whose
748 name started with ``.``, so ``.museattributes`` and ``.museignore`` could
749 never be staged with ``muse code add .`` — they required an explicit path.
750 """
751 (code_repo / ".museattributes").write_text("[*.py]\nmerge = python\n")
752
753 r = runner.invoke(cli, ["code", "add", "."], env=_env(code_repo))
754 assert r.exit_code == 0, r.output
755
756 from muse.plugins.code.stage import read_stage
757 stage = read_stage(code_repo)
758 assert ".museattributes" in stage, ".museattributes must be staged by `muse code add .`"
759
760
761 def test_add_dot_stages_museignore(
762 code_repo: pathlib.Path,
763 ) -> None:
764 """`muse code add .` must stage `.museignore` itself when it exists.
765
766 The file that controls ignore patterns should be version-controlled just
767 like ``.gitignore`` is — ``muse code add .`` must include it.
768 Note: the test uses empty patterns so the file doesn't suppress itself.
769 """
770 (code_repo / ".museignore").write_text('[global]\npatterns = []\n')
771
772 r = runner.invoke(cli, ["code", "add", "."], env=_env(code_repo))
773 assert r.exit_code == 0, r.output
774
775 from muse.plugins.code.stage import read_stage
776 stage = read_stage(code_repo)
777 assert ".museignore" in stage, ".museignore itself must be staged by `muse code add .`"
778
779
780 def test_add_dot_does_not_stage_museignore_files(
781 code_repo: pathlib.Path,
782 ) -> None:
783 """``muse code add .`` must not stage files matched by ``.museignore``.
784
785 Regression test: before the fix, ``_walk_tree`` never consulted
786 ``.museignore``, so any file on disk — including ones the user explicitly
787 excluded — could be silently staged and committed.
788 """
789 (code_repo / ".museignore").write_text('[global]\npatterns = ["*.log"]\n')
790 (code_repo / "debug.log").write_text("ignored content\n")
791 (code_repo / "app.py").write_text("# new code\n")
792
793 r = runner.invoke(cli, ["code", "add", "."], env=_env(code_repo))
794 assert r.exit_code == 0, r.output
795
796 from muse.plugins.code.stage import read_stage, StagedFileMap
797 stage = read_stage(code_repo)
798 assert "debug.log" not in stage, ".museignore'd file must NOT be staged"
799 assert "app.py" in stage, "non-ignored new file must be staged"
800
801
802 def test_add_dot_does_not_stage_unchanged_files(
803 code_repo: pathlib.Path,
804 ) -> None:
805 """``muse code add .`` must only stage files whose content differs from HEAD.
806
807 Regression test for the bug where ``muse code add .`` staged every file in
808 the working tree regardless of whether it had changed, because the
809 "skip-if-already-staged" guard was only consulted (and only correct) after a
810 second ``add`` run. On a fresh stage the check was vacuously false for all
811 files, so even unchanged files were staged.
812 """
813 # Make an initial commit so HEAD has a manifest.
814 (code_repo / "alpha.py").write_text("x = 1\n")
815 (code_repo / "beta.py").write_text("y = 2\n")
816 runner.invoke(cli, ["commit", "-m", "initial"], env=_env(code_repo))
817
818 # Modify only one file; leave the other untouched.
819 (code_repo / "alpha.py").write_text("x = 99\n")
820
821 # Stage everything.
822 r = runner.invoke(cli, ["code", "add", "."], env=_env(code_repo))
823 assert r.exit_code == 0, r.output
824
825 # Only the changed file must be staged — NOT the unchanged beta.py.
826 from muse.plugins.code.stage import read_stage, StagedFileMap
827 stage = read_stage(code_repo)
828 assert "alpha.py" in stage, "modified file must be staged"
829 assert "beta.py" not in stage, "unchanged file must NOT appear in stage"
830
831
832 def test_add_dot_stages_deletions(
833 code_repo: pathlib.Path,
834 ) -> None:
835 """``muse code add .`` must stage tracked files that have been deleted from disk.
836
837 Regression test: before the fix, ``muse code add .`` (no flags) only walked
838 the working tree, so deleted files were silently omitted. Users had to know
839 to pass ``-A`` or explicitly name each deleted file — a significant ergonomic
840 gap vs ``git add .`` which has staged deletions since Git 2.0.
841 """
842 # code_repo already has auth.py and models.py committed.
843 os.remove(code_repo / "auth.py")
844
845 r = runner.invoke(cli, ["code", "add", "."], env=_env(code_repo))
846 assert r.exit_code == 0, r.output
847
848 from muse.plugins.code.stage import read_stage, StagedFileMap
849 stage = read_stage(code_repo)
850 assert "auth.py" in stage, "deleted tracked file must appear in stage with `muse code add .`"
851 assert stage["auth.py"]["mode"] == "D", "deleted file must have mode D"
852
853
854 def test_add_explicit_path_stages_deletion(
855 code_repo: pathlib.Path,
856 ) -> None:
857 """``muse code add <path>`` must stage a deletion when the file is gone from disk.
858
859 Mirrors ``git add <path>`` which stages the deletion regardless of whether
860 the file still exists on disk. Before the fix, naming a non-existent path
861 emitted ``❌ Path not found`` and silently skipped the deletion.
862 """
863 # code_repo already has auth.py committed — delete it from disk.
864 os.remove(code_repo / "auth.py")
865
866 r = runner.invoke(cli, ["code", "add", "auth.py"], env=_env(code_repo))
867 assert r.exit_code == 0, r.output
868
869 from muse.plugins.code.stage import read_stage
870 stage = read_stage(code_repo)
871 assert "auth.py" in stage, "deleted tracked file must appear in stage when named explicitly"
872 assert stage["auth.py"]["mode"] == "D", "deleted file must have mode D"
873
874
875 # ---------------------------------------------------------------------------
876 # Regression tests — _head_manifest branch resolution (Bug A)
877 #
878 # Written BEFORE the fix to document expected behaviour. Both tests verify
879 # that _head_manifest resolves the branch through the store abstraction
880 # (get_head_commit_id), not by reading the ref file directly.
881 # ---------------------------------------------------------------------------
882
883
884 class TestHeadManifestResolution:
885 """_head_manifest must use the store abstraction, not the raw ref file."""
886
887 def test_empty_branch_returns_empty_dict(
888 self, tmp_path: pathlib.Path
889 ) -> None:
890 """With no commits on the branch, _head_manifest returns {}."""
891 from muse.cli.commands.code_stage import _head_manifest
892
893 dot_muse = muse_dir(tmp_path)
894 dot_muse.mkdir()
895 (dot_muse / "repo.json").write_text('{"repo_id":"test"}')
896 (dot_muse / "HEAD").write_text("ref: refs/heads/main")
897 (dot_muse / "refs" / "heads").mkdir(parents=True)
898 (dot_muse / "commits").mkdir()
899 (dot_muse / "snapshots").mkdir()
900 # No ref file written — branch has no commits.
901
902 result = _head_manifest(tmp_path)
903 assert result == {}
904
905 def test_branch_with_commit_returns_manifest(
906 self, tmp_path: pathlib.Path
907 ) -> None:
908 """With a real commit on the branch, _head_manifest returns its manifest."""
909 import datetime
910 from muse.cli.commands.code_stage import _head_manifest
911 from muse.core.commits import (
912 CommitRecord,
913 write_commit,
914 )
915 from muse.core.snapshots import SnapshotRecord
916 from muse.core.snapshots import write_snapshot
917 from muse.core.ids import hash_commit as compute_commit_id, hash_snapshot as compute_snapshot_id
918
919 dot_muse = muse_dir(tmp_path)
920 dot_muse.mkdir()
921 (dot_muse / "repo.json").write_text('{"repo_id":"test"}')
922 (dot_muse / "HEAD").write_text("ref: refs/heads/main")
923 (dot_muse / "refs" / "heads").mkdir(parents=True)
924 (dot_muse / "commits").mkdir()
925 (dot_muse / "snapshots").mkdir()
926
927 _hello_id = fake_id("hello.py-content")
928 manifest = {"hello.py": _hello_id}
929 snap_id = compute_snapshot_id(manifest)
930 snap = SnapshotRecord(snapshot_id=snap_id, manifest=manifest)
931 write_snapshot(tmp_path, snap)
932
933 committed_at = datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc)
934 commit_id = compute_commit_id(
935 parent_ids=[],
936 snapshot_id=snap_id,
937 message="init",
938 committed_at_iso=committed_at.isoformat(),
939 author="tester",
940 )
941 commit = CommitRecord(
942 commit_id=commit_id,
943 branch="main",
944 snapshot_id=snap_id,
945 message="init",
946 committed_at=committed_at,
947 author="tester",
948 )
949 write_commit(tmp_path, commit)
950 (dot_muse / "refs" / "heads" / "main").write_text(commit_id)
951
952 result = _head_manifest(tmp_path)
953 assert result == {"hello.py": _hello_id}
954
955
956 # ---------------------------------------------------------------------------
957 # TestRegisterFlags
958 # ---------------------------------------------------------------------------
959 # Regression tests — staging idempotency (Bug B)
960 #
961 # muse code add . on an already-staged repo must be a no-op.
962 # Before the fix, deletions and empty-dir sentinels were re-staged on every
963 # invocation, producing wrong counts and "Staged N" when nothing had changed.
964 # ---------------------------------------------------------------------------
965
966
967 def _env(tmp: pathlib.Path) -> Mapping[str, str]:
968 return {"MUSE_REPO_ROOT": str(tmp)}
969
970
971 class TestStageIdempotency:
972 """Running muse code add . twice must be a no-op on the second call."""
973
974 def _run(self, root: pathlib.Path, *args: str) -> str:
975 r = runner.invoke(cli, list(args), env=_env(root))
976 assert r.exit_code == 0, f"{list(args)} failed:\n{r.output}"
977 return r.output.strip()
978
979 def _setup_repo(self, tmp: pathlib.Path) -> pathlib.Path:
980 self._run(tmp, "init", "--domain", "code")
981 (tmp / "keep.py").write_text("x = 1\n")
982 self._run(tmp, "code", "add", ".")
983 self._run(tmp, "commit", "-m", "initial")
984 return tmp
985
986 def test_second_add_after_deletion_staged_is_noop(
987 self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
988 ) -> None:
989 """Staging a deletion then running muse code add . again must say 'Nothing to stage'."""
990 monkeypatch.chdir(tmp_path)
991 root = self._setup_repo(tmp_path)
992 (root / "keep.py").unlink()
993 out1 = self._run(root, "code", "add", ".")
994 assert "deleted" in out1
995
996 out2 = self._run(root, "code", "add", ".")
997 assert "Nothing" in out2, f"second add should be no-op, got: {out2!r}"
998
999 def test_second_add_after_modification_staged_is_noop(
1000 self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
1001 ) -> None:
1002 """Staging a modification then running muse code add . again must say 'Nothing to stage'."""
1003 monkeypatch.chdir(tmp_path)
1004 root = self._setup_repo(tmp_path)
1005 (root / "keep.py").write_text("x = 2\n")
1006 out1 = self._run(root, "code", "add", ".")
1007 assert "modified" in out1
1008
1009 out2 = self._run(root, "code", "add", ".")
1010 assert "Nothing" in out2, f"second add should be no-op, got: {out2!r}"
1011
1012 def test_committed_empty_dir_not_staged_on_add(
1013 self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
1014 ) -> None:
1015 """muse code add . must not sentinel-stage an already-committed empty dir."""
1016 monkeypatch.chdir(tmp_path)
1017 root = self._setup_repo(tmp_path)
1018 (root / "emptydir").mkdir()
1019 self._run(root, "code", "add", ".")
1020 self._run(root, "commit", "-m", "add emptydir")
1021
1022 # Clean state — now run code add . again
1023 out = self._run(root, "code", "add", ".")
1024 assert "Nothing" in out, (
1025 f"committed empty dir must not be re-staged: {out!r}"
1026 )
1027
1028 def test_total_matches_sum_of_categories(
1029 self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
1030 ) -> None:
1031 """Staged N file(s): A added, M modified, D deleted — N must equal A+M+D."""
1032 monkeypatch.chdir(tmp_path)
1033 root = self._setup_repo(tmp_path)
1034 (root / "new.py").write_text("y = 1\n")
1035 (root / "keep.py").write_text("x = 99\n")
1036 (root / "gone.py").write_text("z = 0\n")
1037 self._run(root, "code", "add", ".")
1038 self._run(root, "commit", "-m", "add gone.py")
1039 (root / "gone.py").unlink()
1040
1041 out = self._run(root, "code", "add", ".")
1042 # Format: "Staged {parts}." where parts are "N added files", "N modified",
1043 # "N deleted", "N directories" — all numbers must be positive.
1044 import re
1045 assert out.startswith("Staged ") and out.rstrip().endswith("."), (
1046 f"unexpected output format: {out!r}"
1047 )
1048 nums = [int(x) for x in re.findall(r"\d+", out)]
1049 assert nums and all(n > 0 for n in nums), (
1050 f"all counts must be positive in: {out!r}"
1051 )
1052
1053
1054 # ---------------------------------------------------------------------------
1055
1056
1057 import argparse as _argparse
1058
1059
1060 class TestRegisterFlags:
1061 """register_add() and register_reset() wire --json / -j correctly."""
1062
1063 def _parse_add(self, *args: str) -> _argparse.Namespace:
1064 from muse.cli.commands.code_stage import register_add
1065 p = _argparse.ArgumentParser()
1066 sub = p.add_subparsers()
1067 register_add(sub)
1068 return p.parse_args(["add", *args])
1069
1070 def _parse_reset(self, *args: str) -> _argparse.Namespace:
1071 from muse.cli.commands.code_stage import register_reset
1072 p = _argparse.ArgumentParser()
1073 sub = p.add_subparsers()
1074 register_reset(sub)
1075 return p.parse_args(["reset", *args])
1076
1077 def test_add_default_json_out_is_false(self) -> None:
1078 ns = self._parse_add("foo.py")
1079 assert ns.json_out is False
1080
1081 def test_add_json_flag_sets_json_out(self) -> None:
1082 ns = self._parse_add("--json", "foo.py")
1083 assert ns.json_out is True
1084
1085 def test_add_j_shorthand_sets_json_out(self) -> None:
1086 ns = self._parse_add("-j", "foo.py")
1087 assert ns.json_out is True
1088
1089 def test_reset_default_json_out_is_false(self) -> None:
1090 ns = self._parse_reset()
1091 assert ns.json_out is False
1092
1093 def test_reset_json_flag_sets_json_out(self) -> None:
1094 ns = self._parse_reset("--json")
1095 assert ns.json_out is True
1096
1097 def test_reset_j_shorthand_sets_json_out(self) -> None:
1098 ns = self._parse_reset("-j")
1099 assert ns.json_out is True
File History 2 commits
sha256:2a1cf861048b753a21d6ca853a83cdfc2a46f15dcbb561ee79ebb9dc40c03af6 switch same-commit fix, agent-config user-global config, an… Human patch 15 days ago
sha256:a154bc65916614c833d5a40a10d81ba3eae0d0495b0afddd34dc34f18d5e91b8 fix: test suite alignment and typing audit — zero violations Sonnet 4.6 minor 23 days ago