|
| 1 | +import os |
| 2 | +from pathlib import Path |
| 3 | +from unittest.mock import MagicMock, mock_open, patch |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from m3.core.mcp_config_generator.mcp_config_generators.claude_mcp_config import ( |
| 8 | + ClaudeConfigGenerator, |
| 9 | +) |
| 10 | +from m3.core.mcp_config_generator.mcp_config_generators.fast_mcp_config import ( |
| 11 | + FastMCPConfigGenerator, |
| 12 | +) |
| 13 | +from m3.core.utils.exceptions import M3ValidationError |
| 14 | +from m3.m3 import M3 |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture |
| 18 | +def mock_m3() -> M3: |
| 19 | + """Fixture for mock M3 instance.""" |
| 20 | + from m3.core.config import M3Config |
| 21 | + |
| 22 | + config = M3Config(env_vars={"TEST_ENV": "value"}) |
| 23 | + m3 = M3(config=config) |
| 24 | + return m3 |
| 25 | + |
| 26 | + |
| 27 | +class TestClaudeConfigGenerator: |
| 28 | + """Tests for ClaudeConfigGenerator.""" |
| 29 | + |
| 30 | + @patch( |
| 31 | + "shutil.which", |
| 32 | + return_value="/usr/bin/python", |
| 33 | + ) |
| 34 | + @patch("os.path.isdir", return_value=True) |
| 35 | + @patch( |
| 36 | + "m3.core.mcp_config_generator.mcp_config_generators.claude_mcp_config.ClaudeConfigGenerator._get_claude_config_path" |
| 37 | + ) |
| 38 | + def test_generate_with_defaults( |
| 39 | + self, |
| 40 | + mock_get_path: MagicMock, |
| 41 | + mock_isdir: MagicMock, |
| 42 | + mock_which: MagicMock, |
| 43 | + mock_m3: M3, |
| 44 | + tmp_path: Path, |
| 45 | + ) -> None: |
| 46 | + """Test generating config with defaults.""" |
| 47 | + mock_get_path.return_value = None |
| 48 | + config = ClaudeConfigGenerator.generate(mock_m3) |
| 49 | + assert isinstance(config, dict) |
| 50 | + assert "mcpServers" in config |
| 51 | + assert "m3" in config["mcpServers"] |
| 52 | + server = config["mcpServers"]["m3"] |
| 53 | + assert server["command"].endswith("python") |
| 54 | + assert server["args"] == ["-m", "m3.core.server"] |
| 55 | + assert os.path.isdir(server["cwd"]) |
| 56 | + assert "TEST_ENV" in server["env"] |
| 57 | + |
| 58 | + @patch("shutil.which", return_value=None) |
| 59 | + def test_invalid_command_raises_error( |
| 60 | + self, mock_which: MagicMock, mock_m3: M3 |
| 61 | + ) -> None: |
| 62 | + """Test invalid command raises error.""" |
| 63 | + with pytest.raises(M3ValidationError, match="Invalid command"): |
| 64 | + ClaudeConfigGenerator.generate(mock_m3, command="/invalid/python") |
| 65 | + |
| 66 | + @patch("os.path.isdir", return_value=False) |
| 67 | + def test_invalid_cwd_raises_error(self, mock_isdir: MagicMock, mock_m3: M3) -> None: |
| 68 | + """Test invalid cwd raises error.""" |
| 69 | + with pytest.raises(M3ValidationError, match="Invalid cwd"): |
| 70 | + ClaudeConfigGenerator.generate(mock_m3, cwd="/invalid/dir") |
| 71 | + |
| 72 | + @patch("builtins.open", new_callable=mock_open) |
| 73 | + @patch("json.load") |
| 74 | + @patch("json.dump") |
| 75 | + @patch( |
| 76 | + "m3.core.mcp_config_generator.mcp_config_generators.claude_mcp_config.ClaudeConfigGenerator._get_claude_config_path" |
| 77 | + ) |
| 78 | + @patch("pathlib.Path.exists") |
| 79 | + def test_merge_with_existing_config( |
| 80 | + self, |
| 81 | + mock_exists: MagicMock, |
| 82 | + mock_get_path: MagicMock, |
| 83 | + mock_dump: MagicMock, |
| 84 | + mock_load: MagicMock, |
| 85 | + mock_open_file: MagicMock, |
| 86 | + mock_m3: M3, |
| 87 | + tmp_path: Path, |
| 88 | + ) -> None: |
| 89 | + """Test merging with existing Claude config.""" |
| 90 | + mock_path = tmp_path / "claude_config.json" |
| 91 | + mock_get_path.return_value = mock_path |
| 92 | + mock_exists.return_value = True |
| 93 | + mock_load.return_value = {"mcpServers": {"existing": {}}} |
| 94 | + _config = ClaudeConfigGenerator.generate(mock_m3) |
| 95 | + mock_dump.assert_called_once() |
| 96 | + dumped_config = mock_dump.call_args[0][0] |
| 97 | + assert "existing" in dumped_config["mcpServers"] |
| 98 | + assert "m3" in dumped_config["mcpServers"] |
| 99 | + |
| 100 | + @patch("builtins.open", new_callable=mock_open) |
| 101 | + @patch("json.dump") |
| 102 | + def test_save_to_custom_path( |
| 103 | + self, |
| 104 | + mock_dump: MagicMock, |
| 105 | + mock_open_file: MagicMock, |
| 106 | + mock_m3: M3, |
| 107 | + tmp_path: Path, |
| 108 | + ) -> None: |
| 109 | + """Test saving to custom path.""" |
| 110 | + save_path = tmp_path / "custom.json" |
| 111 | + config = ClaudeConfigGenerator.generate(mock_m3, save_path=str(save_path)) |
| 112 | + mock_dump.assert_called_once_with(config, mock_open_file(), indent=2) |
| 113 | + |
| 114 | + |
| 115 | +class TestFastMCPConfigGenerator: |
| 116 | + """Tests for FastMCPConfigGenerator.""" |
| 117 | + |
| 118 | + @patch("shutil.which", return_value="/usr/bin/python") |
| 119 | + @patch("os.path.isdir", return_value=True) |
| 120 | + def test_generate_with_defaults( |
| 121 | + self, mock_isdir: MagicMock, mock_which: MagicMock, mock_m3: M3 |
| 122 | + ) -> None: |
| 123 | + """Test generating config with defaults.""" |
| 124 | + config = FastMCPConfigGenerator.generate(mock_m3) |
| 125 | + assert isinstance(config, dict) |
| 126 | + assert "mcpServers" in config |
| 127 | + assert "m3" in config["mcpServers"] |
| 128 | + server = config["mcpServers"]["m3"] |
| 129 | + assert server["command"].endswith("python") |
| 130 | + assert server["args"] == ["-m", "m3.core.server"] |
| 131 | + assert os.path.isdir(server["cwd"]) |
| 132 | + assert "TEST_ENV" in server["env"] |
| 133 | + |
| 134 | + @patch("shutil.which", return_value=None) |
| 135 | + def test_invalid_command_raises_error( |
| 136 | + self, mock_which: MagicMock, mock_m3: M3 |
| 137 | + ) -> None: |
| 138 | + """Test invalid command raises error.""" |
| 139 | + with pytest.raises(M3ValidationError, match="Invalid command"): |
| 140 | + FastMCPConfigGenerator.generate(mock_m3, command="/invalid/python") |
| 141 | + |
| 142 | + @patch("os.path.isdir", return_value=False) |
| 143 | + def test_invalid_cwd_raises_error(self, mock_isdir: MagicMock, mock_m3: M3) -> None: |
| 144 | + """Test invalid cwd raises error.""" |
| 145 | + with pytest.raises(M3ValidationError, match="Invalid cwd"): |
| 146 | + FastMCPConfigGenerator.generate(mock_m3, cwd="/invalid/dir") |
| 147 | + |
| 148 | + @patch("builtins.open", new_callable=mock_open) |
| 149 | + @patch("json.dump") |
| 150 | + def test_save_to_custom_path( |
| 151 | + self, |
| 152 | + mock_dump: MagicMock, |
| 153 | + mock_open_file: MagicMock, |
| 154 | + mock_m3: M3, |
| 155 | + tmp_path: Path, |
| 156 | + ) -> None: |
| 157 | + """Test saving to custom path.""" |
| 158 | + save_path = tmp_path / "custom.json" |
| 159 | + config = FastMCPConfigGenerator.generate(mock_m3, save_path=str(save_path)) |
| 160 | + mock_dump.assert_called_once_with(config, mock_open_file(), indent=2) |
0 commit comments