|
| 1 | +""" |
| 2 | +Test suite for MCP CLI package management enhancements. |
| 3 | +
|
| 4 | +This module tests the enhanced package management commands with MCP host |
| 5 | +configuration integration following CrackingShells testing standards. |
| 6 | +""" |
| 7 | + |
| 8 | +import unittest |
| 9 | +import sys |
| 10 | +from pathlib import Path |
| 11 | +from unittest.mock import patch, MagicMock |
| 12 | + |
| 13 | +# Add the parent directory to the path to import wobble |
| 14 | +sys.path.insert(0, str(Path(__file__).parent.parent)) |
| 15 | + |
| 16 | +try: |
| 17 | + from wobble.decorators import regression_test, integration_test |
| 18 | +except ImportError: |
| 19 | + # Fallback decorators if wobble is not available |
| 20 | + def regression_test(func): |
| 21 | + return func |
| 22 | + |
| 23 | + def integration_test(scope="component"): |
| 24 | + def decorator(func): |
| 25 | + return func |
| 26 | + return decorator |
| 27 | + |
| 28 | +from hatch.cli_hatch import parse_host_list, request_confirmation |
| 29 | +from hatch.mcp_host_config import MCPHostType |
| 30 | + |
| 31 | + |
| 32 | +class TestMCPCLIPackageManagement(unittest.TestCase): |
| 33 | + """Test suite for MCP CLI package management enhancements.""" |
| 34 | + |
| 35 | + @regression_test |
| 36 | + def test_parse_host_list_comma_separated(self): |
| 37 | + """Test parsing comma-separated host list.""" |
| 38 | + hosts = parse_host_list("claude-desktop,cursor,vscode") |
| 39 | + expected = [MCPHostType.CLAUDE_DESKTOP, MCPHostType.CURSOR, MCPHostType.VSCODE] |
| 40 | + self.assertEqual(hosts, expected) |
| 41 | + |
| 42 | + @regression_test |
| 43 | + def test_parse_host_list_single_host(self): |
| 44 | + """Test parsing single host.""" |
| 45 | + hosts = parse_host_list("claude-desktop") |
| 46 | + expected = [MCPHostType.CLAUDE_DESKTOP] |
| 47 | + self.assertEqual(hosts, expected) |
| 48 | + |
| 49 | + @regression_test |
| 50 | + def test_parse_host_list_empty(self): |
| 51 | + """Test parsing empty host list.""" |
| 52 | + hosts = parse_host_list("") |
| 53 | + self.assertEqual(hosts, []) |
| 54 | + |
| 55 | + @regression_test |
| 56 | + def test_parse_host_list_none(self): |
| 57 | + """Test parsing None host list.""" |
| 58 | + hosts = parse_host_list(None) |
| 59 | + self.assertEqual(hosts, []) |
| 60 | + |
| 61 | + @regression_test |
| 62 | + def test_parse_host_list_all(self): |
| 63 | + """Test parsing 'all' host list.""" |
| 64 | + with patch('hatch.cli_hatch.MCPHostRegistry.detect_available_hosts') as mock_detect: |
| 65 | + mock_detect.return_value = [MCPHostType.CLAUDE_DESKTOP, MCPHostType.CURSOR] |
| 66 | + hosts = parse_host_list("all") |
| 67 | + expected = [MCPHostType.CLAUDE_DESKTOP, MCPHostType.CURSOR] |
| 68 | + self.assertEqual(hosts, expected) |
| 69 | + mock_detect.assert_called_once() |
| 70 | + |
| 71 | + @regression_test |
| 72 | + def test_parse_host_list_invalid_host(self): |
| 73 | + """Test parsing invalid host raises ValueError.""" |
| 74 | + with self.assertRaises(ValueError) as context: |
| 75 | + parse_host_list("invalid-host") |
| 76 | + |
| 77 | + self.assertIn("Unknown host 'invalid-host'", str(context.exception)) |
| 78 | + self.assertIn("Available:", str(context.exception)) |
| 79 | + |
| 80 | + @regression_test |
| 81 | + def test_parse_host_list_mixed_valid_invalid(self): |
| 82 | + """Test parsing mixed valid and invalid hosts.""" |
| 83 | + with self.assertRaises(ValueError) as context: |
| 84 | + parse_host_list("claude-desktop,invalid-host,cursor") |
| 85 | + |
| 86 | + self.assertIn("Unknown host 'invalid-host'", str(context.exception)) |
| 87 | + |
| 88 | + @regression_test |
| 89 | + def test_parse_host_list_whitespace_handling(self): |
| 90 | + """Test parsing host list with whitespace.""" |
| 91 | + hosts = parse_host_list(" claude-desktop , cursor , vscode ") |
| 92 | + expected = [MCPHostType.CLAUDE_DESKTOP, MCPHostType.CURSOR, MCPHostType.VSCODE] |
| 93 | + self.assertEqual(hosts, expected) |
| 94 | + |
| 95 | + @regression_test |
| 96 | + def test_request_confirmation_auto_approve(self): |
| 97 | + """Test confirmation with auto-approve flag.""" |
| 98 | + result = request_confirmation("Test message?", auto_approve=True) |
| 99 | + self.assertTrue(result) |
| 100 | + |
| 101 | + @regression_test |
| 102 | + def test_request_confirmation_user_yes(self): |
| 103 | + """Test confirmation with user saying yes.""" |
| 104 | + with patch('builtins.input', return_value='y'): |
| 105 | + result = request_confirmation("Test message?", auto_approve=False) |
| 106 | + self.assertTrue(result) |
| 107 | + |
| 108 | + @regression_test |
| 109 | + def test_request_confirmation_user_yes_full(self): |
| 110 | + """Test confirmation with user saying 'yes'.""" |
| 111 | + with patch('builtins.input', return_value='yes'): |
| 112 | + result = request_confirmation("Test message?", auto_approve=False) |
| 113 | + self.assertTrue(result) |
| 114 | + |
| 115 | + @regression_test |
| 116 | + def test_request_confirmation_user_no(self): |
| 117 | + """Test confirmation with user saying no.""" |
| 118 | + with patch('builtins.input', return_value='n'): |
| 119 | + result = request_confirmation("Test message?", auto_approve=False) |
| 120 | + self.assertFalse(result) |
| 121 | + |
| 122 | + @regression_test |
| 123 | + def test_request_confirmation_user_no_full(self): |
| 124 | + """Test confirmation with user saying 'no'.""" |
| 125 | + with patch('builtins.input', return_value='no'): |
| 126 | + result = request_confirmation("Test message?", auto_approve=False) |
| 127 | + self.assertFalse(result) |
| 128 | + |
| 129 | + @regression_test |
| 130 | + def test_request_confirmation_user_empty(self): |
| 131 | + """Test confirmation with user pressing enter (default no).""" |
| 132 | + with patch('builtins.input', return_value=''): |
| 133 | + result = request_confirmation("Test message?", auto_approve=False) |
| 134 | + self.assertFalse(result) |
| 135 | + |
| 136 | + @integration_test(scope="component") |
| 137 | + def test_package_add_argument_parsing(self): |
| 138 | + """Test package add command argument parsing with MCP flags.""" |
| 139 | + from hatch.cli_hatch import main |
| 140 | + import argparse |
| 141 | + |
| 142 | + # Mock argparse to capture parsed arguments |
| 143 | + with patch('argparse.ArgumentParser.parse_args') as mock_parse: |
| 144 | + mock_args = MagicMock() |
| 145 | + mock_args.command = 'package' |
| 146 | + mock_args.pkg_command = 'add' |
| 147 | + mock_args.package_path_or_name = 'test-package' |
| 148 | + mock_args.host = 'claude-desktop,cursor' |
| 149 | + mock_args.no_mcp_config = False |
| 150 | + mock_args.env = None |
| 151 | + mock_args.version = None |
| 152 | + mock_args.force_download = False |
| 153 | + mock_args.refresh_registry = False |
| 154 | + mock_args.auto_approve = False |
| 155 | + mock_parse.return_value = mock_args |
| 156 | + |
| 157 | + # Mock environment manager to avoid actual operations |
| 158 | + with patch('hatch.cli_hatch.HatchEnvironmentManager') as mock_env_manager: |
| 159 | + mock_env_manager.return_value.add_package_to_environment.return_value = True |
| 160 | + mock_env_manager.return_value.get_current_environment.return_value = "default" |
| 161 | + |
| 162 | + # Mock MCP manager |
| 163 | + with patch('hatch.cli_hatch.MCPHostConfigurationManager'): |
| 164 | + with patch('builtins.print') as mock_print: |
| 165 | + result = main() |
| 166 | + |
| 167 | + # Should succeed |
| 168 | + self.assertEqual(result, 0) |
| 169 | + |
| 170 | + # Should print success message |
| 171 | + mock_print.assert_any_call("Successfully added package: test-package") |
| 172 | + |
| 173 | + @integration_test(scope="component") |
| 174 | + def test_package_sync_argument_parsing(self): |
| 175 | + """Test package sync command argument parsing.""" |
| 176 | + from hatch.cli_hatch import main |
| 177 | + import argparse |
| 178 | + |
| 179 | + # Mock argparse to capture parsed arguments |
| 180 | + with patch('argparse.ArgumentParser.parse_args') as mock_parse: |
| 181 | + mock_args = MagicMock() |
| 182 | + mock_args.command = 'package' |
| 183 | + mock_args.pkg_command = 'sync' |
| 184 | + mock_args.package_name = 'test-package' |
| 185 | + mock_args.host = 'claude-desktop,cursor' |
| 186 | + mock_args.env = None |
| 187 | + mock_args.dry_run = False |
| 188 | + mock_args.auto_approve = False |
| 189 | + mock_args.no_backup = False |
| 190 | + mock_parse.return_value = mock_args |
| 191 | + |
| 192 | + # Mock environment manager |
| 193 | + with patch('hatch.cli_hatch.HatchEnvironmentManager') as mock_env_manager: |
| 194 | + mock_env_manager.return_value.get_current_environment.return_value = "default" |
| 195 | + mock_env_manager.return_value.list_packages.return_value = [ |
| 196 | + {'name': 'test-package', 'version': '1.0.0'} |
| 197 | + ] |
| 198 | + |
| 199 | + # Mock MCP manager |
| 200 | + with patch('hatch.cli_hatch.MCPHostConfigurationManager'): |
| 201 | + with patch('builtins.print') as mock_print: |
| 202 | + result = main() |
| 203 | + |
| 204 | + # Should succeed |
| 205 | + self.assertEqual(result, 0) |
| 206 | + |
| 207 | + # Should print sync message |
| 208 | + mock_print.assert_any_call("Synchronizing MCP servers for package 'test-package' to hosts: ['claude-desktop', 'cursor']") |
| 209 | + |
| 210 | + @integration_test(scope="component") |
| 211 | + def test_package_sync_package_not_found(self): |
| 212 | + """Test package sync when package doesn't exist.""" |
| 213 | + from hatch.cli_hatch import main |
| 214 | + import argparse |
| 215 | + |
| 216 | + # Mock argparse to capture parsed arguments |
| 217 | + with patch('argparse.ArgumentParser.parse_args') as mock_parse: |
| 218 | + mock_args = MagicMock() |
| 219 | + mock_args.command = 'package' |
| 220 | + mock_args.pkg_command = 'sync' |
| 221 | + mock_args.package_name = 'nonexistent-package' |
| 222 | + mock_args.host = 'claude-desktop' |
| 223 | + mock_args.env = None |
| 224 | + mock_parse.return_value = mock_args |
| 225 | + |
| 226 | + # Mock environment manager with empty package list |
| 227 | + with patch('hatch.cli_hatch.HatchEnvironmentManager') as mock_env_manager: |
| 228 | + mock_env_manager.return_value.get_current_environment.return_value = "default" |
| 229 | + mock_env_manager.return_value.list_packages.return_value = [] |
| 230 | + |
| 231 | + with patch('builtins.print') as mock_print: |
| 232 | + result = main() |
| 233 | + |
| 234 | + # Should fail |
| 235 | + self.assertEqual(result, 1) |
| 236 | + |
| 237 | + # Should print error message |
| 238 | + mock_print.assert_any_call("Package 'nonexistent-package' not found in environment 'default'") |
| 239 | + |
| 240 | + |
| 241 | +if __name__ == '__main__': |
| 242 | + unittest.main() |
0 commit comments