|
| 1 | +""" |
| 2 | +Test suite for MCP host configuration manager. |
| 3 | +
|
| 4 | +This module tests the core configuration manager with consolidated models |
| 5 | +and integration with backup system. |
| 6 | +""" |
| 7 | + |
| 8 | +import unittest |
| 9 | +import sys |
| 10 | +from pathlib import Path |
| 11 | +import tempfile |
| 12 | +import json |
| 13 | +import os |
| 14 | + |
| 15 | +# Add the parent directory to the path to import wobble |
| 16 | +sys.path.insert(0, str(Path(__file__).parent.parent)) |
| 17 | + |
| 18 | +try: |
| 19 | + from wobble.decorators import regression_test, integration_test |
| 20 | +except ImportError: |
| 21 | + # Fallback decorators if wobble is not available |
| 22 | + def regression_test(func): |
| 23 | + return func |
| 24 | + |
| 25 | + def integration_test(scope="component"): |
| 26 | + def decorator(func): |
| 27 | + return func |
| 28 | + return decorator |
| 29 | + |
| 30 | +from test_data_utils import MCPHostConfigTestDataLoader |
| 31 | +from hatch.mcp_host_config.host_management import MCPHostConfigurationManager, MCPHostRegistry, register_host_strategy |
| 32 | +from hatch.mcp_host_config.models import MCPHostType, MCPServerConfig, HostConfiguration, ConfigurationResult, SyncResult |
| 33 | +from hatch.mcp_host_config.strategies import MCPHostStrategy |
| 34 | + |
| 35 | + |
| 36 | +class TestMCPHostConfigurationManager(unittest.TestCase): |
| 37 | + """Test suite for MCP host configuration manager.""" |
| 38 | + |
| 39 | + def setUp(self): |
| 40 | + """Set up test environment.""" |
| 41 | + self.test_data_loader = MCPHostConfigTestDataLoader() |
| 42 | + self.temp_dir = tempfile.mkdtemp() |
| 43 | + self.temp_config_path = Path(self.temp_dir) / "test_config.json" |
| 44 | + |
| 45 | + # Clear registry before each test |
| 46 | + MCPHostRegistry._strategies.clear() |
| 47 | + MCPHostRegistry._instances.clear() |
| 48 | + |
| 49 | + # Store temp_config_path for strategy access |
| 50 | + temp_config_path = self.temp_config_path |
| 51 | + |
| 52 | + # Register test strategy |
| 53 | + @register_host_strategy(MCPHostType.CLAUDE_DESKTOP) |
| 54 | + class TestStrategy(MCPHostStrategy): |
| 55 | + def get_config_path(self): |
| 56 | + return temp_config_path |
| 57 | + |
| 58 | + def is_host_available(self): |
| 59 | + return True |
| 60 | + |
| 61 | + def read_configuration(self): |
| 62 | + if temp_config_path.exists(): |
| 63 | + with open(temp_config_path, 'r') as f: |
| 64 | + data = json.load(f) |
| 65 | + |
| 66 | + servers = {} |
| 67 | + if "mcpServers" in data: |
| 68 | + for name, config in data["mcpServers"].items(): |
| 69 | + servers[name] = MCPServerConfig(**config) |
| 70 | + |
| 71 | + return HostConfiguration(servers=servers) |
| 72 | + else: |
| 73 | + return HostConfiguration(servers={}) |
| 74 | + |
| 75 | + def write_configuration(self, config, no_backup=False): |
| 76 | + try: |
| 77 | + # Convert MCPServerConfig objects to dict |
| 78 | + servers_dict = {} |
| 79 | + for name, server_config in config.servers.items(): |
| 80 | + servers_dict[name] = server_config.model_dump(exclude_none=True) |
| 81 | + |
| 82 | + # Create configuration data |
| 83 | + config_data = {"mcpServers": servers_dict} |
| 84 | + |
| 85 | + # Write to file |
| 86 | + with open(temp_config_path, 'w') as f: |
| 87 | + json.dump(config_data, f, indent=2) |
| 88 | + |
| 89 | + return True |
| 90 | + except Exception: |
| 91 | + return False |
| 92 | + |
| 93 | + def validate_server_config(self, server_config): |
| 94 | + return True |
| 95 | + |
| 96 | + self.manager = MCPHostConfigurationManager() |
| 97 | + self.temp_config_path = self.temp_config_path |
| 98 | + |
| 99 | + def tearDown(self): |
| 100 | + """Clean up test environment.""" |
| 101 | + # Clean up temp files |
| 102 | + if self.temp_config_path.exists(): |
| 103 | + self.temp_config_path.unlink() |
| 104 | + os.rmdir(self.temp_dir) |
| 105 | + |
| 106 | + # Clear registry after each test |
| 107 | + MCPHostRegistry._strategies.clear() |
| 108 | + MCPHostRegistry._instances.clear() |
| 109 | + |
| 110 | + @regression_test |
| 111 | + def test_configure_server_success(self): |
| 112 | + """Test successful server configuration.""" |
| 113 | + server_config_data = self.test_data_loader.load_mcp_server_config("local") |
| 114 | + server_config = MCPServerConfig(**server_config_data) |
| 115 | + # Add name attribute for the manager to use |
| 116 | + server_config.name = "test_server" |
| 117 | + |
| 118 | + result = self.manager.configure_server( |
| 119 | + server_config=server_config, |
| 120 | + hostname="claude-desktop" |
| 121 | + ) |
| 122 | + |
| 123 | + self.assertIsInstance(result, ConfigurationResult) |
| 124 | + if not result.success: |
| 125 | + print(f"Configuration failed: {result.error_message}") |
| 126 | + self.assertTrue(result.success) |
| 127 | + self.assertIsNone(result.error_message) |
| 128 | + self.assertEqual(result.hostname, "claude-desktop") |
| 129 | + self.assertEqual(result.server_name, "test_server") |
| 130 | + |
| 131 | + # Verify configuration was written |
| 132 | + self.assertTrue(self.temp_config_path.exists()) |
| 133 | + |
| 134 | + # Verify configuration content |
| 135 | + with open(self.temp_config_path, 'r') as f: |
| 136 | + config_data = json.load(f) |
| 137 | + |
| 138 | + self.assertIn("mcpServers", config_data) |
| 139 | + self.assertIn("test_server", config_data["mcpServers"]) |
| 140 | + self.assertEqual(config_data["mcpServers"]["test_server"]["command"], "python") |
| 141 | + |
| 142 | + @regression_test |
| 143 | + def test_configure_server_unknown_host_type(self): |
| 144 | + """Test configuration with unknown host type.""" |
| 145 | + server_config_data = self.test_data_loader.load_mcp_server_config("local") |
| 146 | + server_config = MCPServerConfig(**server_config_data) |
| 147 | + server_config.name = "test_server" |
| 148 | + |
| 149 | + # Clear registry to simulate unknown host type |
| 150 | + MCPHostRegistry._strategies.clear() |
| 151 | + |
| 152 | + result = self.manager.configure_server( |
| 153 | + server_config=server_config, |
| 154 | + hostname="claude-desktop" |
| 155 | + ) |
| 156 | + |
| 157 | + self.assertIsInstance(result, ConfigurationResult) |
| 158 | + self.assertFalse(result.success) |
| 159 | + self.assertIsNotNone(result.error_message) |
| 160 | + self.assertIn("Unknown host type", result.error_message) |
| 161 | + |
| 162 | + @regression_test |
| 163 | + def test_configure_server_validation_failure(self): |
| 164 | + """Test configuration with validation failure.""" |
| 165 | + # Create server config that will fail validation at the strategy level |
| 166 | + server_config_data = self.test_data_loader.load_mcp_server_config("local") |
| 167 | + server_config = MCPServerConfig(**server_config_data) |
| 168 | + server_config.name = "test_server" |
| 169 | + |
| 170 | + # Override the test strategy to always fail validation |
| 171 | + @register_host_strategy(MCPHostType.CLAUDE_DESKTOP) |
| 172 | + class FailingValidationStrategy(MCPHostStrategy): |
| 173 | + def get_config_path(self): |
| 174 | + return self.temp_config_path |
| 175 | + |
| 176 | + def is_host_available(self): |
| 177 | + return True |
| 178 | + |
| 179 | + def read_configuration(self): |
| 180 | + return HostConfiguration(servers={}) |
| 181 | + |
| 182 | + def write_configuration(self, config, no_backup=False): |
| 183 | + return True |
| 184 | + |
| 185 | + def validate_server_config(self, server_config): |
| 186 | + return False # Always fail validation |
| 187 | + |
| 188 | + result = self.manager.configure_server( |
| 189 | + server_config=server_config, |
| 190 | + hostname="claude-desktop" |
| 191 | + ) |
| 192 | + |
| 193 | + self.assertIsInstance(result, ConfigurationResult) |
| 194 | + self.assertFalse(result.success) |
| 195 | + self.assertIsNotNone(result.error_message) |
| 196 | + self.assertIn("Server configuration invalid", result.error_message) |
| 197 | + |
| 198 | + @regression_test |
| 199 | + def test_remove_server_success(self): |
| 200 | + """Test successful server removal.""" |
| 201 | + # First configure a server |
| 202 | + server_config_data = self.test_data_loader.load_mcp_server_config("local") |
| 203 | + server_config = MCPServerConfig(**server_config_data) |
| 204 | + server_config.name = "test_server" |
| 205 | + |
| 206 | + self.manager.configure_server( |
| 207 | + server_config=server_config, |
| 208 | + hostname="claude-desktop" |
| 209 | + ) |
| 210 | + |
| 211 | + # Verify server exists |
| 212 | + with open(self.temp_config_path, 'r') as f: |
| 213 | + config_data = json.load(f) |
| 214 | + self.assertIn("test_server", config_data["mcpServers"]) |
| 215 | + |
| 216 | + # Remove server |
| 217 | + result = self.manager.remove_server( |
| 218 | + server_name="test_server", |
| 219 | + hostname="claude-desktop" |
| 220 | + ) |
| 221 | + |
| 222 | + self.assertIsInstance(result, ConfigurationResult) |
| 223 | + self.assertTrue(result.success) |
| 224 | + self.assertIsNone(result.error_message) |
| 225 | + |
| 226 | + # Verify server was removed |
| 227 | + with open(self.temp_config_path, 'r') as f: |
| 228 | + config_data = json.load(f) |
| 229 | + self.assertNotIn("test_server", config_data["mcpServers"]) |
| 230 | + |
| 231 | + @regression_test |
| 232 | + def test_remove_server_not_found(self): |
| 233 | + """Test removing non-existent server.""" |
| 234 | + result = self.manager.remove_server( |
| 235 | + server_name="nonexistent_server", |
| 236 | + hostname="claude-desktop" |
| 237 | + ) |
| 238 | + |
| 239 | + self.assertIsInstance(result, ConfigurationResult) |
| 240 | + self.assertFalse(result.success) |
| 241 | + self.assertIsNotNone(result.error_message) |
| 242 | + self.assertIn("Server 'nonexistent_server' not found", result.error_message) |
| 243 | + |
| 244 | + @regression_test |
| 245 | + def test_sync_environment_to_hosts_success(self): |
| 246 | + """Test successful environment synchronization.""" |
| 247 | + from hatch.mcp_host_config.models import EnvironmentData, EnvironmentPackageEntry, PackageHostConfiguration |
| 248 | + from datetime import datetime |
| 249 | + |
| 250 | + # Create test environment data |
| 251 | + server_config_data = self.test_data_loader.load_mcp_server_config("local") |
| 252 | + server_config = MCPServerConfig(**server_config_data) |
| 253 | + |
| 254 | + host_config = PackageHostConfiguration( |
| 255 | + config_path="~/test/config.json", |
| 256 | + configured_at=datetime.fromisoformat("2025-09-21T10:00:00.000000"), |
| 257 | + last_synced=datetime.fromisoformat("2025-09-21T10:00:00.000000"), |
| 258 | + server_config=server_config |
| 259 | + ) |
| 260 | + |
| 261 | + package = EnvironmentPackageEntry( |
| 262 | + name="test-package", |
| 263 | + version="1.0.0", |
| 264 | + type="hatch", |
| 265 | + source="github:user/test-package", |
| 266 | + installed_at=datetime.fromisoformat("2025-09-21T10:00:00.000000"), |
| 267 | + configured_hosts={"claude-desktop": host_config} |
| 268 | + ) |
| 269 | + |
| 270 | + env_data = EnvironmentData( |
| 271 | + name="test_env", |
| 272 | + description="Test environment", |
| 273 | + created_at=datetime.fromisoformat("2025-09-21T10:00:00.000000"), |
| 274 | + packages=[package] |
| 275 | + ) |
| 276 | + |
| 277 | + # Sync environment to hosts |
| 278 | + result = self.manager.sync_environment_to_hosts( |
| 279 | + env_data=env_data, |
| 280 | + target_hosts=["claude-desktop"] |
| 281 | + ) |
| 282 | + |
| 283 | + self.assertIsInstance(result, SyncResult) |
| 284 | + self.assertTrue(result.success) |
| 285 | + self.assertEqual(result.servers_synced, 1) |
| 286 | + self.assertEqual(result.hosts_updated, 1) |
| 287 | + self.assertEqual(len(result.results), 1) |
| 288 | + |
| 289 | + # Verify configuration was written |
| 290 | + self.assertTrue(self.temp_config_path.exists()) |
| 291 | + |
| 292 | + # Verify configuration content |
| 293 | + with open(self.temp_config_path, 'r') as f: |
| 294 | + config_data = json.load(f) |
| 295 | + |
| 296 | + self.assertIn("mcpServers", config_data) |
| 297 | + self.assertIn("test-package", config_data["mcpServers"]) |
| 298 | + self.assertEqual(config_data["mcpServers"]["test-package"]["command"], "python") |
| 299 | + |
| 300 | + @regression_test |
| 301 | + def test_sync_environment_to_hosts_no_servers(self): |
| 302 | + """Test environment synchronization with no servers.""" |
| 303 | + from hatch.mcp_host_config.models import EnvironmentData |
| 304 | + from datetime import datetime |
| 305 | + |
| 306 | + # Create empty environment data |
| 307 | + env_data = EnvironmentData( |
| 308 | + name="empty_env", |
| 309 | + description="Empty environment", |
| 310 | + created_at=datetime.fromisoformat("2025-09-21T10:00:00.000000"), |
| 311 | + packages=[] |
| 312 | + ) |
| 313 | + |
| 314 | + # Sync environment to hosts |
| 315 | + result = self.manager.sync_environment_to_hosts( |
| 316 | + env_data=env_data, |
| 317 | + target_hosts=["claude-desktop"] |
| 318 | + ) |
| 319 | + |
| 320 | + self.assertIsInstance(result, SyncResult) |
| 321 | + self.assertTrue(result.success) # Success even with no servers |
| 322 | + self.assertEqual(result.servers_synced, 0) |
| 323 | + self.assertEqual(result.hosts_updated, 1) |
| 324 | + self.assertEqual(len(result.results), 1) |
| 325 | + |
| 326 | + # Verify result message |
| 327 | + self.assertEqual(result.results[0].error_message, "No servers to sync") |
| 328 | + |
| 329 | + |
| 330 | +if __name__ == '__main__': |
| 331 | + unittest.main() |
0 commit comments