gabriel / muse public
test_cli_workflow.py python
429 lines 17.6 KB
Raw
sha256:51ce277f663e01a43eaffbe77509b1de7ac2d4251b55d23306304bcdeb92c90d feat(pack): delta-encode snapshots in MPackBundle wire format Sonnet 4.6 minor ⚠ breaking 121 days ago
1 """End-to-end CLI workflow tests — init, commit, log, status, branch, merge."""
2
3 import pathlib
4
5 import pytest
6 from tests.cli_test_helper import CliRunner
7 from muse.core.paths import head_path, muse_dir, repo_json_path
8
9 cli = None # argparse migration — CliRunner ignores this arg
10
11 runner = CliRunner()
12
13
14 @pytest.fixture
15 def repo(tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> pathlib.Path:
16 """Initialise a fresh Muse repo in tmp_path and set it as cwd."""
17 monkeypatch.chdir(tmp_path)
18 monkeypatch.setenv("MUSE_REPO_ROOT", str(tmp_path))
19 result = runner.invoke(cli, ["init"])
20 assert result.exit_code == 0, result.output
21 return tmp_path
22
23
24 def _write(repo: pathlib.Path, filename: str, content: str = "data") -> None:
25 (repo / filename).write_text(content)
26
27
28 class TestInit:
29 def test_creates_muse_dir(self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> None:
30 monkeypatch.chdir(tmp_path)
31 result = runner.invoke(cli, ["init"])
32 assert result.exit_code == 0
33 assert muse_dir(tmp_path).is_dir()
34 assert (head_path(tmp_path)).exists()
35 assert (repo_json_path(tmp_path)).exists()
36 assert (tmp_path).is_dir()
37
38 def test_reinit_requires_force(self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> None:
39 monkeypatch.chdir(tmp_path)
40 runner.invoke(cli, ["init"])
41 result = runner.invoke(cli, ["init"])
42 assert result.exit_code != 0
43 assert "force" in result.stderr.lower()
44
45 def test_bare_repo(self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> None:
46 monkeypatch.chdir(tmp_path)
47 result = runner.invoke(cli, ["init", "--bare"])
48 assert result.exit_code == 0
49 # Bare repos have the internal store but no template files are copied.
50 assert muse_dir(tmp_path).exists()
51
52 def test_creates_museignore(self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> None:
53 monkeypatch.chdir(tmp_path)
54 result = runner.invoke(cli, ["init"])
55 assert result.exit_code == 0
56 ignore_file = tmp_path / ".museignore"
57 assert ignore_file.exists(), ".museignore should be created by muse init"
58
59 def test_museignore_is_valid_toml(self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> None:
60 import tomllib
61
62 monkeypatch.chdir(tmp_path)
63 runner.invoke(cli, ["init"])
64 ignore_file = tmp_path / ".museignore"
65 with ignore_file.open("rb") as fh:
66 config = tomllib.load(fh)
67 assert isinstance(config, dict), ".museignore must be valid TOML"
68
69 def test_museignore_has_global_section(self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> None:
70 import tomllib
71
72 monkeypatch.chdir(tmp_path)
73 runner.invoke(cli, ["init"])
74 with (tmp_path / ".museignore").open("rb") as fh:
75 config = tomllib.load(fh)
76 assert "global" in config, ".museignore should have a [global] section"
77 assert isinstance(config["global"].get("patterns"), list)
78
79 def test_museignore_has_domain_section_for_midi(
80 self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
81 ) -> None:
82 import tomllib
83
84 monkeypatch.chdir(tmp_path)
85 runner.invoke(cli, ["init", "--domain", "midi"])
86 with (tmp_path / ".museignore").open("rb") as fh:
87 config = tomllib.load(fh)
88 domain_map = config.get("domain", {})
89 assert "midi" in domain_map, "[domain.midi] section should be present for --domain midi"
90
91 def test_museignore_has_domain_section_for_code(
92 self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
93 ) -> None:
94 import tomllib
95
96 monkeypatch.chdir(tmp_path)
97 runner.invoke(cli, ["init", "--domain", "code"])
98 with (tmp_path / ".museignore").open("rb") as fh:
99 config = tomllib.load(fh)
100 domain_map = config.get("domain", {})
101 assert "code" in domain_map, "[domain.code] section should be present for --domain code"
102
103 def test_museignore_not_overwritten_on_reinit(
104 self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
105 ) -> None:
106 monkeypatch.chdir(tmp_path)
107 runner.invoke(cli, ["init"])
108 custom = '[global]\npatterns = ["custom.txt"]\n'
109 (tmp_path / ".museignore").write_text(custom)
110 runner.invoke(cli, ["init", "--force"])
111 assert (tmp_path / ".museignore").read_text() == custom
112
113 def test_museignore_parseable_by_load_ignore_config(
114 self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
115 ) -> None:
116 from muse.core.ignore import load_ignore_config, resolve_patterns
117
118 monkeypatch.chdir(tmp_path)
119 runner.invoke(cli, ["init", "--domain", "midi"])
120 config = load_ignore_config(tmp_path)
121 patterns = resolve_patterns(config, "midi")
122 assert isinstance(patterns, list)
123 assert len(patterns) > 0, "midi init should produce non-empty pattern list"
124
125
126 class TestCommit:
127 def test_commit_with_message(self, repo: pathlib.Path) -> None:
128 _write(repo, "beat.mid")
129 result = runner.invoke(cli, ["commit", "-m", "Initial commit"])
130 assert result.exit_code == 0
131 assert "Initial commit" in result.output
132
133 def test_nothing_to_commit(self, repo: pathlib.Path) -> None:
134 _write(repo, "beat.mid")
135 runner.invoke(cli, ["commit", "-m", "First"])
136 result = runner.invoke(cli, ["commit", "-m", "Second"])
137 assert result.exit_code == 0
138 assert "Nothing to commit" in result.output
139
140 def test_allow_empty(self, repo: pathlib.Path) -> None:
141 result = runner.invoke(cli, ["commit", "-m", "Empty", "--allow-empty"])
142 assert result.exit_code == 0
143
144 def test_message_required(self, repo: pathlib.Path) -> None:
145 _write(repo, "beat.mid")
146 result = runner.invoke(cli, ["commit"])
147 assert result.exit_code != 0
148
149 def test_section_metadata(self, repo: pathlib.Path) -> None:
150 _write(repo, "beat.mid")
151 result = runner.invoke(cli, ["commit", "-m", "Chorus take", "--section", "chorus"])
152 assert result.exit_code == 0
153
154 from muse.core.store import get_head_commit_id, read_commit
155 from muse.core.types import load_json_file
156 repo_id = load_json_file(repo_json_path(repo))["repo_id"]
157 commit_id = get_head_commit_id(repo, "main")
158 commit = read_commit(repo, commit_id)
159 assert commit is not None
160 assert commit.metadata.get("section") == "chorus"
161
162
163 class TestStatus:
164 def test_clean_after_commit(self, repo: pathlib.Path) -> None:
165 _write(repo, "beat.mid")
166 runner.invoke(cli, ["commit", "-m", "First"])
167 result = runner.invoke(cli, ["status"])
168 assert result.exit_code == 0
169 assert "Nothing to commit" in result.output
170
171 def test_shows_new_file(self, repo: pathlib.Path) -> None:
172 _write(repo, "beat.mid")
173 result = runner.invoke(cli, ["status"])
174 assert result.exit_code == 0
175 assert "beat.mid" in result.output
176
177 def test_short_flag(self, repo: pathlib.Path) -> None:
178 _write(repo, "beat.mid")
179 runner.invoke(cli, ["code", "add", "beat.mid"])
180 result = runner.invoke(cli, ["status", "--short"])
181 assert result.exit_code == 0
182 assert "A " in result.output
183
184 def test_json_flag(self, repo: pathlib.Path) -> None:
185 _write(repo, "beat.mid")
186 result = runner.invoke(cli, ["status", "--json"])
187 assert result.exit_code == 0
188 import json as _json
189 d = _json.loads(result.output)
190 assert d["branch"] == "main"
191
192
193 class TestLog:
194 def test_empty_log(self, repo: pathlib.Path) -> None:
195 result = runner.invoke(cli, ["log"])
196 assert result.exit_code == 0
197 assert "no commits" in result.output
198
199 def test_shows_commit(self, repo: pathlib.Path) -> None:
200 _write(repo, "beat.mid")
201 runner.invoke(cli, ["commit", "-m", "First take"])
202 result = runner.invoke(cli, ["log"])
203 assert result.exit_code == 0
204 assert "First take" in result.output
205
206 def test_oneline(self, repo: pathlib.Path) -> None:
207 _write(repo, "beat.mid")
208 runner.invoke(cli, ["commit", "-m", "First take"])
209 result = runner.invoke(cli, ["log", "--oneline"])
210 assert result.exit_code == 0
211 assert "First take" in result.output
212 assert "Author:" not in result.output
213
214 def test_multiple_commits_newest_first(self, repo: pathlib.Path) -> None:
215 _write(repo, "a.mid")
216 runner.invoke(cli, ["commit", "-m", "First"])
217 _write(repo, "b.mid")
218 runner.invoke(cli, ["commit", "-m", "Second"])
219 result = runner.invoke(cli, ["log", "--oneline"])
220 lines = [l for l in result.output.strip().splitlines() if l.strip()]
221 assert "Second" in lines[0]
222 assert "First" in lines[1]
223
224 def test_max_count_limits_output(self, repo: pathlib.Path) -> None:
225 """muse log -n 2 returns only the two most recent commits from a longer chain."""
226 for i in range(1, 6):
227 _write(repo, f"track{i}.mid")
228 runner.invoke(cli, ["commit", "-m", f"Commit {i}"])
229
230 result = runner.invoke(cli, ["log", "--oneline", "--limit", "2"])
231 assert result.exit_code == 0
232 lines = [l for l in result.output.strip().splitlines() if l.strip()]
233 assert len(lines) == 2
234 assert "Commit 5" in lines[0]
235 assert "Commit 4" in lines[1]
236
237 def test_max_count_one_returns_single_commit(self, repo: pathlib.Path) -> None:
238 """muse log -n 1 returns exactly the HEAD commit."""
239 for i in range(1, 4):
240 _write(repo, f"t{i}.mid")
241 runner.invoke(cli, ["commit", "-m", f"Take {i}"])
242
243 result = runner.invoke(cli, ["log", "--oneline", "--limit", "1"])
244 assert result.exit_code == 0
245 lines = [l for l in result.output.strip().splitlines() if l.strip()]
246 assert len(lines) == 1
247 assert "Take 3" in lines[0]
248
249 def test_max_count_larger_than_history_returns_all(self, repo: pathlib.Path) -> None:
250 """muse log -n 100 on a 3-commit repo returns all 3 without error."""
251 for i in range(1, 4):
252 _write(repo, f"f{i}.mid")
253 runner.invoke(cli, ["commit", "-m", f"Track {i}"])
254
255 result = runner.invoke(cli, ["log", "--oneline", "--limit", "100"])
256 assert result.exit_code == 0
257 lines = [l for l in result.output.strip().splitlines() if l.strip()]
258 assert len(lines) == 3
259
260
261 class TestBranch:
262 def test_list_shows_main(self, repo: pathlib.Path) -> None:
263 result = runner.invoke(cli, ["branch"])
264 assert result.exit_code == 0
265 assert "main" in result.output
266 assert "* " in result.output
267
268 def test_create_branch(self, repo: pathlib.Path) -> None:
269 result = runner.invoke(cli, ["branch", "feature/chorus"])
270 assert result.exit_code == 0
271 result = runner.invoke(cli, ["branch"])
272 assert "feature/chorus" in result.output
273
274 def test_delete_branch_force(self, repo: pathlib.Path) -> None:
275 """Force-delete an unmerged branch with -D."""
276 runner.invoke(cli, ["branch", "feature/x"])
277 result = runner.invoke(cli, ["branch", "-D", "feature/x"])
278 assert result.exit_code == 0
279 result = runner.invoke(cli, ["branch"])
280 assert "feature/x" not in result.output
281
282 def test_delete_branch_safe_blocks_unmerged(self, repo: pathlib.Path) -> None:
283 """Safe delete (-d) must reject a branch that has not been merged."""
284 runner.invoke(cli, ["branch", "feature/unmerged"])
285 result = runner.invoke(cli, ["branch", "-d", "feature/unmerged"])
286 assert result.exit_code != 0
287 assert "not fully merged" in result.stderr
288
289
290 class TestCheckout:
291 def test_create_and_switch(self, repo: pathlib.Path) -> None:
292 result = runner.invoke(cli, ["checkout", "-b", "feature/chorus"])
293 assert result.exit_code == 0
294 assert "feature/chorus" in result.output
295 status = runner.invoke(cli, ["status"])
296 assert "feature/chorus" in status.output
297
298 def test_switch_existing_branch(self, repo: pathlib.Path) -> None:
299 runner.invoke(cli, ["checkout", "-b", "feature/chorus"])
300 runner.invoke(cli, ["checkout", "main"])
301 result = runner.invoke(cli, ["status"])
302 assert "main" in result.output
303
304 def test_already_on_branch(self, repo: pathlib.Path) -> None:
305 result = runner.invoke(cli, ["checkout", "main"])
306 assert result.exit_code == 0
307 assert "Already on" in result.output
308
309
310 class TestMerge:
311 def test_fast_forward(self, repo: pathlib.Path) -> None:
312 _write(repo, "verse.mid")
313 runner.invoke(cli, ["commit", "-m", "Verse"])
314 runner.invoke(cli, ["checkout", "-b", "feature/chorus"])
315 _write(repo, "chorus.mid")
316 runner.invoke(cli, ["commit", "-m", "Add chorus"])
317 runner.invoke(cli, ["checkout", "main"])
318 result = runner.invoke(cli, ["merge", "feature/chorus"])
319 assert result.exit_code == 0
320 assert "Fast-forward" in result.output
321
322 def test_clean_three_way_merge(self, repo: pathlib.Path) -> None:
323 _write(repo, "base.mid")
324 runner.invoke(cli, ["commit", "-m", "Base"])
325 runner.invoke(cli, ["checkout", "-b", "branch-a"])
326 _write(repo, "a.mid")
327 runner.invoke(cli, ["commit", "-m", "Add A"])
328 runner.invoke(cli, ["checkout", "main"])
329 runner.invoke(cli, ["checkout", "-b", "branch-b"])
330 _write(repo, "b.mid")
331 runner.invoke(cli, ["commit", "-m", "Add B"])
332 runner.invoke(cli, ["checkout", "main"])
333 result = runner.invoke(cli, ["merge", "branch-a"])
334 assert result.exit_code == 0
335
336 def test_cannot_merge_self(self, repo: pathlib.Path) -> None:
337 result = runner.invoke(cli, ["merge", "main"])
338 assert result.exit_code != 0
339
340
341 class TestDiff:
342 def test_no_diff_clean(self, repo: pathlib.Path) -> None:
343 _write(repo, "beat.mid")
344 runner.invoke(cli, ["commit", "-m", "First"])
345 result = runner.invoke(cli, ["diff"])
346 assert result.exit_code == 0
347 assert "No differences" in result.output
348
349 def test_shows_new_file(self, repo: pathlib.Path) -> None:
350 _write(repo, "beat.mid")
351 runner.invoke(cli, ["commit", "-m", "First"])
352 _write(repo, "lead.mid")
353 result = runner.invoke(cli, ["diff"])
354 assert result.exit_code == 0
355 assert "lead.mid" in result.output
356
357
358 class TestTag:
359 def test_add_and_list(self, repo: pathlib.Path) -> None:
360 _write(repo, "beat.mid")
361 runner.invoke(cli, ["commit", "-m", "Tagged take"])
362 result = runner.invoke(cli, ["tag", "add", "emotion:joyful"])
363 assert result.exit_code == 0
364 result = runner.invoke(cli, ["tag", "list"])
365 assert "emotion:joyful" in result.output
366
367
368 class TestDiffWorkingTreeSymbols:
369 """Regression: muse diff must show semantic symbols for uncommitted files.
370
371 Before the fix, diff fell back to a plain ``A file.md`` when the blob
372 wasn't in the object store (only written on commit). After the fix, it
373 reads directly from disk (hash-verified) and extracts symbols via the
374 appropriate adapter.
375 """
376
377 def test_new_markdown_file_shows_sections(self, repo: pathlib.Path) -> None:
378 _write(repo, "first.py", "def setup(): pass")
379 runner.invoke(cli, ["commit", "-m", "init"])
380 _write(repo, "README.md", "# Overview\n\n## Installation\n\n## Usage\n")
381 result = runner.invoke(cli, ["diff"])
382 assert result.exit_code == 0
383 # Symbol-level output must list the heading sections.
384 assert "Overview" in result.output
385 assert "Installation" in result.output
386
387 def test_new_markdown_file_shows_A_prefix(self, repo: pathlib.Path) -> None:
388 _write(repo, "first.py", "def setup(): pass")
389 runner.invoke(cli, ["commit", "-m", "init"])
390 _write(repo, "README.md", "# Title\n\n## Intro\n")
391 result = runner.invoke(cli, ["diff"])
392 assert result.exit_code == 0
393 # The PatchOp for a newly-added file must use 'A' not 'M'.
394 lines = result.output.splitlines()
395 readme_line = next((l for l in lines if "README.md" in l), None)
396 assert readme_line is not None
397 assert readme_line.startswith("A"), f"Expected 'A README.md', got: {readme_line!r}"
398
399 def test_new_python_file_shows_functions(self, repo: pathlib.Path) -> None:
400 runner.invoke(cli, ["commit", "-m", "empty"])
401 _write(repo, "utils.py", "def add(a, b):\n return a + b\n\ndef sub(a, b):\n return a - b\n")
402 result = runner.invoke(cli, ["diff"])
403 assert result.exit_code == 0
404 assert "add" in result.output
405 assert "sub" in result.output
406
407 def test_modified_file_shows_M_prefix(self, repo: pathlib.Path) -> None:
408 _write(repo, "utils.py", "def foo(): pass\ndef bar(): pass\n")
409 runner.invoke(cli, ["commit", "-m", "First"])
410 _write(repo, "utils.py", "def foo(): pass\ndef bar(): return 1\n")
411 result = runner.invoke(cli, ["diff"])
412 assert result.exit_code == 0
413 lines = result.output.splitlines()
414 utils_line = next((l for l in lines if "utils.py" in l), None)
415 assert utils_line is not None
416 assert utils_line.startswith("M"), f"Expected 'M utils.py', got: {utils_line!r}"
417
418
419 class TestShelf:
420 def test_shelf_save_and_pop(self, repo: pathlib.Path) -> None:
421 _write(repo, "beat.mid")
422 runner.invoke(cli, ["commit", "-m", "First"])
423 _write(repo, "lead.mid")
424 result = runner.invoke(cli, ["shelf", "save"])
425 assert result.exit_code == 0
426 assert not (repo / "lead.mid").exists()
427 result = runner.invoke(cli, ["shelf", "pop"])
428 assert result.exit_code == 0
429 assert (repo / "lead.mid").exists()
File History 1 commit
sha256:51ce277f663e01a43eaffbe77509b1de7ac2d4251b55d23306304bcdeb92c90d feat(pack): delta-encode snapshots in MPackBundle wire format Sonnet 4.6 minor 121 days ago