test_open.py
python
| 1 | """Tests for ``muse open`` command. |
| 2 | |
| 3 | Tests: |
| 4 | - ``test_open_calls_system_open`` — happy path: subprocess.run(['open', path]) called. |
| 5 | - ``test_open_file_not_found_exits_1`` — exit code 1 when file does not exist. |
| 6 | - ``test_open_requires_macos`` — exit code 1 on non-macOS platforms. |
| 7 | """ |
| 8 | from __future__ import annotations |
| 9 | |
| 10 | import json |
| 11 | import pathlib |
| 12 | import uuid |
| 13 | from unittest.mock import MagicMock, patch |
| 14 | |
| 15 | import pytest |
| 16 | from typer.testing import CliRunner |
| 17 | |
| 18 | from maestro.muse_cli.app import cli |
| 19 | |
| 20 | runner = CliRunner() |
| 21 | |
| 22 | |
| 23 | def _init_muse_repo(root: pathlib.Path, repo_id: str | None = None) -> str: |
| 24 | rid = repo_id or str(uuid.uuid4()) |
| 25 | muse = root / ".muse" |
| 26 | (muse / "refs" / "heads").mkdir(parents=True) |
| 27 | (muse / "repo.json").write_text( |
| 28 | json.dumps({"repo_id": rid, "schema_version": "1"}) |
| 29 | ) |
| 30 | (muse / "HEAD").write_text("refs/heads/main") |
| 31 | (muse / "refs" / "heads" / "main").write_text("") |
| 32 | return rid |
| 33 | |
| 34 | |
| 35 | def test_open_calls_system_open(tmp_path: pathlib.Path) -> None: |
| 36 | """``muse open <path>`` calls subprocess.run(['open', <path>]) and exits 0.""" |
| 37 | _init_muse_repo(tmp_path) |
| 38 | artifact = tmp_path / "muse-work" / "jazz_4b_run1.mid" |
| 39 | artifact.parent.mkdir(parents=True) |
| 40 | artifact.write_bytes(b"MIDI") |
| 41 | |
| 42 | with ( |
| 43 | patch("maestro.muse_cli.commands.open_cmd.platform.system", return_value="Darwin"), |
| 44 | patch("maestro.muse_cli.commands.open_cmd.subprocess.run") as mock_run, |
| 45 | patch.dict("os.environ", {"MUSE_REPO_ROOT": str(tmp_path)}), |
| 46 | ): |
| 47 | mock_run.return_value = MagicMock(returncode=0) |
| 48 | result = runner.invoke(cli, ["open", str(artifact)]) |
| 49 | |
| 50 | assert result.exit_code == 0, result.output |
| 51 | mock_run.assert_called_once() |
| 52 | call_args = mock_run.call_args[0][0] |
| 53 | assert call_args[0] == "open" |
| 54 | assert str(artifact.resolve()) in call_args[1] |
| 55 | |
| 56 | |
| 57 | def test_open_file_not_found_exits_1(tmp_path: pathlib.Path) -> None: |
| 58 | """``muse open <missing>`` exits 1 with a clear error message.""" |
| 59 | _init_muse_repo(tmp_path) |
| 60 | |
| 61 | with ( |
| 62 | patch("maestro.muse_cli.commands.open_cmd.platform.system", return_value="Darwin"), |
| 63 | patch.dict("os.environ", {"MUSE_REPO_ROOT": str(tmp_path)}), |
| 64 | ): |
| 65 | result = runner.invoke(cli, ["open", "no_such_file.mid"]) |
| 66 | |
| 67 | assert result.exit_code == 1 |
| 68 | assert "not found" in result.output.lower() or "❌" in result.output |
| 69 | |
| 70 | |
| 71 | def test_open_requires_macos(tmp_path: pathlib.Path) -> None: |
| 72 | """``muse open`` exits 1 with a clear message on non-macOS platforms.""" |
| 73 | _init_muse_repo(tmp_path) |
| 74 | artifact = tmp_path / "muse-work" / "beat.mid" |
| 75 | artifact.parent.mkdir(parents=True) |
| 76 | artifact.write_bytes(b"MIDI") |
| 77 | |
| 78 | with ( |
| 79 | patch("maestro.muse_cli.commands.open_cmd.platform.system", return_value="Linux"), |
| 80 | patch.dict("os.environ", {"MUSE_REPO_ROOT": str(tmp_path)}), |
| 81 | ): |
| 82 | result = runner.invoke(cli, ["open", str(artifact)]) |
| 83 | |
| 84 | assert result.exit_code == 1 |
| 85 | assert "macOS" in result.output |