"""Unit tests for muse.plugins.registry — resolve_plugin, read_domain, registered_domains.""" import json import pathlib import pytest from muse._version import __version__ from muse.core.paths import muse_dir, repo_json_path from muse.core.errors import MuseCLIError from muse.domain import MuseDomainPlugin from muse.plugins.code.plugin import CodePlugin from muse.plugins.registry import read_domain, registered_domains, resolve_plugin def _make_repo(tmp_path: pathlib.Path, domain: str = "code") -> pathlib.Path: """Scaffold a minimal .muse/repo.json so registry helpers can run.""" dot_muse = muse_dir(tmp_path) dot_muse.mkdir() repo_json_path(tmp_path).write_text( json.dumps({"repo_id": "test-id", "schema_version": __version__, "domain": domain}) ) return tmp_path class TestReadDomain: def test_returns_stored_domain(self, tmp_path: pathlib.Path) -> None: root = _make_repo(tmp_path, domain="code") assert read_domain(root) == "code" def test_defaults_to_code_when_key_missing(self, tmp_path: pathlib.Path) -> None: dot_muse = muse_dir(tmp_path) dot_muse.mkdir() repo_json_path(tmp_path).write_text(json.dumps({"repo_id": "x"})) assert read_domain(tmp_path) == "code" def test_defaults_to_code_when_repo_json_absent(self, tmp_path: pathlib.Path) -> None: muse_dir(tmp_path).mkdir() assert read_domain(tmp_path) == "code" def test_defaults_to_code_when_muse_dir_absent(self, tmp_path: pathlib.Path) -> None: assert read_domain(tmp_path) == "code" class TestResolvePlugin: def test_returns_code_plugin_for_code_domain(self, tmp_path: pathlib.Path) -> None: root = _make_repo(tmp_path, domain="code") plugin = resolve_plugin(root) assert isinstance(plugin, CodePlugin) def test_returned_plugin_satisfies_protocol(self, tmp_path: pathlib.Path) -> None: root = _make_repo(tmp_path, domain="code") plugin = resolve_plugin(root) assert isinstance(plugin, MuseDomainPlugin) def test_raises_for_unknown_domain(self, tmp_path: pathlib.Path) -> None: root = _make_repo(tmp_path, domain="unknown-domain") with pytest.raises(MuseCLIError, match="unknown-domain"): resolve_plugin(root) def test_raises_error_mentions_registered_domains(self, tmp_path: pathlib.Path) -> None: root = _make_repo(tmp_path, domain="bogus") with pytest.raises(MuseCLIError, match="code"): resolve_plugin(root) def test_defaults_to_code_plugin_when_no_domain_key(self, tmp_path: pathlib.Path) -> None: dot_muse = muse_dir(tmp_path) dot_muse.mkdir() repo_json_path(tmp_path).write_text(json.dumps({"repo_id": "x"})) plugin = resolve_plugin(tmp_path) assert isinstance(plugin, CodePlugin) class TestRegisteredDomains: def test_includes_code(self) -> None: assert "code" in registered_domains() def test_midi_suspended(self) -> None: """MIDI domain is temporarily suspended pending its own security audit.""" assert "midi" not in registered_domains() def test_returns_sorted_list(self) -> None: domains = registered_domains() assert domains == sorted(domains) def test_returns_list_of_strings(self) -> None: domains = registered_domains() assert all(isinstance(d, str) for d in domains)