test_cli_workflow.py
python
sha256:144495ae90498f9d51aeac4bf7f9ee9e9502d14474745a856b96921f7349c0ce
next round of fixing tests
Human
16 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.refs import get_head_commit_id |
| 155 | from muse.core.commits import read_commit |
| 156 | from muse.core.types import load_json_file |
| 157 | repo_id = load_json_file(repo_json_path(repo))["repo_id"] |
| 158 | commit_id = get_head_commit_id(repo, "main") |
| 159 | commit = read_commit(repo, commit_id) |
| 160 | assert commit is not None |
| 161 | assert commit.metadata.get("section") == "chorus" |
| 162 | |
| 163 | |
| 164 | class TestStatus: |
| 165 | def test_clean_after_commit(self, repo: pathlib.Path) -> None: |
| 166 | _write(repo, "beat.mid") |
| 167 | runner.invoke(cli, ["commit", "-m", "First"]) |
| 168 | result = runner.invoke(cli, ["status"]) |
| 169 | assert result.exit_code == 0 |
| 170 | assert "Nothing to commit" in result.output |
| 171 | |
| 172 | def test_shows_new_file(self, repo: pathlib.Path) -> None: |
| 173 | _write(repo, "beat.mid") |
| 174 | result = runner.invoke(cli, ["status"]) |
| 175 | assert result.exit_code == 0 |
| 176 | assert "beat.mid" in result.output |
| 177 | |
| 178 | def test_short_flag(self, repo: pathlib.Path) -> None: |
| 179 | _write(repo, "beat.mid") |
| 180 | runner.invoke(cli, ["code", "add", "beat.mid"]) |
| 181 | result = runner.invoke(cli, ["status", "--short"]) |
| 182 | assert result.exit_code == 0 |
| 183 | assert "A " in result.output |
| 184 | |
| 185 | def test_json_flag(self, repo: pathlib.Path) -> None: |
| 186 | _write(repo, "beat.mid") |
| 187 | result = runner.invoke(cli, ["status", "--json"]) |
| 188 | assert result.exit_code == 0 |
| 189 | import json as _json |
| 190 | d = _json.loads(result.output) |
| 191 | assert d["branch"] == "main" |
| 192 | |
| 193 | |
| 194 | class TestLog: |
| 195 | def test_empty_log(self, repo: pathlib.Path) -> None: |
| 196 | result = runner.invoke(cli, ["log"]) |
| 197 | assert result.exit_code == 0 |
| 198 | assert "no commits" in result.output |
| 199 | |
| 200 | def test_shows_commit(self, repo: pathlib.Path) -> None: |
| 201 | _write(repo, "beat.mid") |
| 202 | runner.invoke(cli, ["commit", "-m", "First take"]) |
| 203 | result = runner.invoke(cli, ["log"]) |
| 204 | assert result.exit_code == 0 |
| 205 | assert "First take" in result.output |
| 206 | |
| 207 | def test_oneline(self, repo: pathlib.Path) -> None: |
| 208 | _write(repo, "beat.mid") |
| 209 | runner.invoke(cli, ["commit", "-m", "First take"]) |
| 210 | result = runner.invoke(cli, ["log", "--oneline"]) |
| 211 | assert result.exit_code == 0 |
| 212 | assert "First take" in result.output |
| 213 | assert "Author:" not in result.output |
| 214 | |
| 215 | def test_multiple_commits_newest_first(self, repo: pathlib.Path) -> None: |
| 216 | _write(repo, "a.mid") |
| 217 | runner.invoke(cli, ["commit", "-m", "First"]) |
| 218 | _write(repo, "b.mid") |
| 219 | runner.invoke(cli, ["commit", "-m", "Second"]) |
| 220 | result = runner.invoke(cli, ["log", "--oneline"]) |
| 221 | lines = [l for l in result.output.strip().splitlines() if l.strip()] |
| 222 | assert "Second" in lines[0] |
| 223 | assert "First" in lines[1] |
| 224 | |
| 225 | def test_max_count_limits_output(self, repo: pathlib.Path) -> None: |
| 226 | """muse log -n 2 returns only the two most recent commits from a longer chain.""" |
| 227 | for i in range(1, 6): |
| 228 | _write(repo, f"track{i}.mid") |
| 229 | runner.invoke(cli, ["commit", "-m", f"Commit {i}"]) |
| 230 | |
| 231 | result = runner.invoke(cli, ["log", "--oneline", "--limit", "2"]) |
| 232 | assert result.exit_code == 0 |
| 233 | lines = [l for l in result.output.strip().splitlines() if l.strip()] |
| 234 | assert len(lines) == 2 |
| 235 | assert "Commit 5" in lines[0] |
| 236 | assert "Commit 4" in lines[1] |
| 237 | |
| 238 | def test_max_count_one_returns_single_commit(self, repo: pathlib.Path) -> None: |
| 239 | """muse log -n 1 returns exactly the HEAD commit.""" |
| 240 | for i in range(1, 4): |
| 241 | _write(repo, f"t{i}.mid") |
| 242 | runner.invoke(cli, ["commit", "-m", f"Take {i}"]) |
| 243 | |
| 244 | result = runner.invoke(cli, ["log", "--oneline", "--limit", "1"]) |
| 245 | assert result.exit_code == 0 |
| 246 | lines = [l for l in result.output.strip().splitlines() if l.strip()] |
| 247 | assert len(lines) == 1 |
| 248 | assert "Take 3" in lines[0] |
| 249 | |
| 250 | def test_max_count_larger_than_history_returns_all(self, repo: pathlib.Path) -> None: |
| 251 | """muse log -n 100 on a 3-commit repo returns all 3 without error.""" |
| 252 | for i in range(1, 4): |
| 253 | _write(repo, f"f{i}.mid") |
| 254 | runner.invoke(cli, ["commit", "-m", f"Track {i}"]) |
| 255 | |
| 256 | result = runner.invoke(cli, ["log", "--oneline", "--limit", "100"]) |
| 257 | assert result.exit_code == 0 |
| 258 | lines = [l for l in result.output.strip().splitlines() if l.strip()] |
| 259 | assert len(lines) == 3 |
| 260 | |
| 261 | |
| 262 | class TestBranch: |
| 263 | def test_list_shows_main(self, repo: pathlib.Path) -> None: |
| 264 | result = runner.invoke(cli, ["branch"]) |
| 265 | assert result.exit_code == 0 |
| 266 | assert "main" in result.output |
| 267 | assert "* " in result.output |
| 268 | |
| 269 | def test_create_branch(self, repo: pathlib.Path) -> None: |
| 270 | result = runner.invoke(cli, ["branch", "feature/chorus"]) |
| 271 | assert result.exit_code == 0 |
| 272 | result = runner.invoke(cli, ["branch"]) |
| 273 | assert "feature/chorus" in result.output |
| 274 | |
| 275 | def test_delete_branch_force(self, repo: pathlib.Path) -> None: |
| 276 | """Force-delete an unmerged branch with -D.""" |
| 277 | runner.invoke(cli, ["branch", "feature/x"]) |
| 278 | result = runner.invoke(cli, ["branch", "-D", "feature/x"]) |
| 279 | assert result.exit_code == 0 |
| 280 | result = runner.invoke(cli, ["branch"]) |
| 281 | assert "feature/x" not in result.output |
| 282 | |
| 283 | def test_delete_branch_safe_blocks_unmerged(self, repo: pathlib.Path) -> None: |
| 284 | """Safe delete (-d) must reject a branch that has not been merged.""" |
| 285 | runner.invoke(cli, ["branch", "feature/unmerged"]) |
| 286 | result = runner.invoke(cli, ["branch", "-d", "feature/unmerged"]) |
| 287 | assert result.exit_code != 0 |
| 288 | assert "not fully merged" in result.stderr |
| 289 | |
| 290 | |
| 291 | class TestCheckout: |
| 292 | def test_create_and_switch(self, repo: pathlib.Path) -> None: |
| 293 | result = runner.invoke(cli, ["checkout", "-b", "feature/chorus"]) |
| 294 | assert result.exit_code == 0 |
| 295 | assert "feature/chorus" in result.output |
| 296 | status = runner.invoke(cli, ["status"]) |
| 297 | assert "feature/chorus" in status.output |
| 298 | |
| 299 | def test_switch_existing_branch(self, repo: pathlib.Path) -> None: |
| 300 | runner.invoke(cli, ["checkout", "-b", "feature/chorus"]) |
| 301 | runner.invoke(cli, ["checkout", "main"]) |
| 302 | result = runner.invoke(cli, ["status"]) |
| 303 | assert "main" in result.output |
| 304 | |
| 305 | def test_already_on_branch(self, repo: pathlib.Path) -> None: |
| 306 | result = runner.invoke(cli, ["checkout", "main"]) |
| 307 | assert result.exit_code == 0 |
| 308 | assert "Already on" in result.output |
| 309 | |
| 310 | |
| 311 | class TestMerge: |
| 312 | def test_fast_forward(self, repo: pathlib.Path) -> None: |
| 313 | _write(repo, "verse.mid") |
| 314 | runner.invoke(cli, ["commit", "-m", "Verse"]) |
| 315 | runner.invoke(cli, ["checkout", "-b", "feature/chorus"]) |
| 316 | _write(repo, "chorus.mid") |
| 317 | runner.invoke(cli, ["commit", "-m", "Add chorus"]) |
| 318 | runner.invoke(cli, ["checkout", "main"]) |
| 319 | result = runner.invoke(cli, ["merge", "feature/chorus"]) |
| 320 | assert result.exit_code == 0 |
| 321 | assert "Fast-forward" in result.output |
| 322 | |
| 323 | def test_clean_three_way_merge(self, repo: pathlib.Path) -> None: |
| 324 | _write(repo, "base.mid") |
| 325 | runner.invoke(cli, ["commit", "-m", "Base"]) |
| 326 | runner.invoke(cli, ["checkout", "-b", "branch-a"]) |
| 327 | _write(repo, "a.mid") |
| 328 | runner.invoke(cli, ["commit", "-m", "Add A"]) |
| 329 | runner.invoke(cli, ["checkout", "main"]) |
| 330 | runner.invoke(cli, ["checkout", "-b", "branch-b"]) |
| 331 | _write(repo, "b.mid") |
| 332 | runner.invoke(cli, ["commit", "-m", "Add B"]) |
| 333 | runner.invoke(cli, ["checkout", "main"]) |
| 334 | result = runner.invoke(cli, ["merge", "branch-a"]) |
| 335 | assert result.exit_code == 0 |
| 336 | |
| 337 | def test_cannot_merge_self(self, repo: pathlib.Path) -> None: |
| 338 | result = runner.invoke(cli, ["merge", "main"]) |
| 339 | assert result.exit_code != 0 |
| 340 | |
| 341 | |
| 342 | class TestDiff: |
| 343 | def test_no_diff_clean(self, repo: pathlib.Path) -> None: |
| 344 | _write(repo, "beat.mid") |
| 345 | runner.invoke(cli, ["commit", "-m", "First"]) |
| 346 | result = runner.invoke(cli, ["diff"]) |
| 347 | assert result.exit_code == 0 |
| 348 | assert "No differences" in result.output |
| 349 | |
| 350 | def test_shows_new_file(self, repo: pathlib.Path) -> None: |
| 351 | _write(repo, "beat.mid") |
| 352 | runner.invoke(cli, ["commit", "-m", "First"]) |
| 353 | _write(repo, "lead.mid") |
| 354 | result = runner.invoke(cli, ["diff"]) |
| 355 | assert result.exit_code == 0 |
| 356 | assert "lead.mid" in result.output |
| 357 | |
| 358 | |
| 359 | class TestTag: |
| 360 | def test_add_and_list(self, repo: pathlib.Path) -> None: |
| 361 | _write(repo, "beat.mid") |
| 362 | runner.invoke(cli, ["commit", "-m", "Tagged take"]) |
| 363 | result = runner.invoke(cli, ["tag", "add", "emotion:joyful"]) |
| 364 | assert result.exit_code == 0 |
| 365 | result = runner.invoke(cli, ["tag", "list"]) |
| 366 | assert "emotion:joyful" in result.output |
| 367 | |
| 368 | |
| 369 | class TestDiffWorkingTreeSymbols: |
| 370 | """Regression: muse diff must show semantic symbols for uncommitted files. |
| 371 | |
| 372 | Before the fix, diff fell back to a plain ``A file.md`` when the blob |
| 373 | wasn't in the object store (only written on commit). After the fix, it |
| 374 | reads directly from disk (hash-verified) and extracts symbols via the |
| 375 | appropriate adapter. |
| 376 | """ |
| 377 | |
| 378 | def test_new_markdown_file_shows_sections(self, repo: pathlib.Path) -> None: |
| 379 | _write(repo, "first.py", "def setup(): pass") |
| 380 | runner.invoke(cli, ["commit", "-m", "init"]) |
| 381 | _write(repo, "README.md", "# Overview\n\n## Installation\n\n## Usage\n") |
| 382 | result = runner.invoke(cli, ["diff"]) |
| 383 | assert result.exit_code == 0 |
| 384 | # Symbol-level output must list the heading sections. |
| 385 | assert "Overview" in result.output |
| 386 | assert "Installation" in result.output |
| 387 | |
| 388 | def test_new_markdown_file_shows_A_prefix(self, repo: pathlib.Path) -> None: |
| 389 | _write(repo, "first.py", "def setup(): pass") |
| 390 | runner.invoke(cli, ["commit", "-m", "init"]) |
| 391 | _write(repo, "README.md", "# Title\n\n## Intro\n") |
| 392 | result = runner.invoke(cli, ["diff"]) |
| 393 | assert result.exit_code == 0 |
| 394 | # The PatchOp for a newly-added file must use 'A' not 'M'. |
| 395 | lines = result.output.splitlines() |
| 396 | readme_line = next((l for l in lines if "README.md" in l), None) |
| 397 | assert readme_line is not None |
| 398 | assert readme_line.startswith("A"), f"Expected 'A README.md', got: {readme_line!r}" |
| 399 | |
| 400 | def test_new_python_file_shows_functions(self, repo: pathlib.Path) -> None: |
| 401 | runner.invoke(cli, ["commit", "-m", "empty"]) |
| 402 | _write(repo, "utils.py", "def add(a, b):\n return a + b\n\ndef sub(a, b):\n return a - b\n") |
| 403 | result = runner.invoke(cli, ["diff"]) |
| 404 | assert result.exit_code == 0 |
| 405 | assert "add" in result.output |
| 406 | assert "sub" in result.output |
| 407 | |
| 408 | def test_modified_file_shows_M_prefix(self, repo: pathlib.Path) -> None: |
| 409 | _write(repo, "utils.py", "def foo(): pass\ndef bar(): pass\n") |
| 410 | runner.invoke(cli, ["commit", "-m", "First"]) |
| 411 | _write(repo, "utils.py", "def foo(): pass\ndef bar(): return 1\n") |
| 412 | result = runner.invoke(cli, ["diff"]) |
| 413 | assert result.exit_code == 0 |
| 414 | lines = result.output.splitlines() |
| 415 | utils_line = next((l for l in lines if "utils.py" in l), None) |
| 416 | assert utils_line is not None |
| 417 | assert utils_line.startswith("M"), f"Expected 'M utils.py', got: {utils_line!r}" |
| 418 | |
| 419 | |
| 420 | class TestShelf: |
| 421 | def test_shelf_save_and_pop(self, repo: pathlib.Path) -> None: |
| 422 | _write(repo, "beat.mid") |
| 423 | runner.invoke(cli, ["commit", "-m", "First"]) |
| 424 | _write(repo, "lead.mid") |
| 425 | result = runner.invoke(cli, ["shelf", "save"]) |
| 426 | assert result.exit_code == 0 |
| 427 | assert not (repo / "lead.mid").exists() |
| 428 | result = runner.invoke(cli, ["shelf", "pop"]) |
| 429 | assert result.exit_code == 0 |
| 430 | assert (repo / "lead.mid").exists() |
File History
1 commit
sha256:144495ae90498f9d51aeac4bf7f9ee9e9502d14474745a856b96921f7349c0ce
next round of fixing tests
Human
16 days ago