|
| 1 | +import json |
| 2 | +from unittest.mock import MagicMock, patch |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from uipath_mcp._cli._runtime._exception import UiPathMcpRuntimeError |
| 7 | +from uipath_mcp._cli._runtime._factory import UiPathMcpRuntimeFactory |
| 8 | + |
| 9 | +# Patch UiPath() constructor which requires auth env vars |
| 10 | +_UIPATH_PATCH = patch("uipath_mcp._cli._runtime._runtime.UiPath") |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def mcp_json_single(tmp_path): |
| 15 | + """Create a temporary mcp.json with a single server.""" |
| 16 | + config = { |
| 17 | + "servers": { |
| 18 | + "math-server": { |
| 19 | + "transport": "stdio", |
| 20 | + "command": "python", |
| 21 | + "args": ["server.py"], |
| 22 | + } |
| 23 | + } |
| 24 | + } |
| 25 | + config_path = tmp_path / "mcp.json" |
| 26 | + config_path.write_text(json.dumps(config)) |
| 27 | + return str(config_path) |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture |
| 31 | +def mcp_json_multiple(tmp_path): |
| 32 | + """Create a temporary mcp.json with multiple servers.""" |
| 33 | + config = { |
| 34 | + "servers": { |
| 35 | + "math-server": { |
| 36 | + "transport": "stdio", |
| 37 | + "command": "python", |
| 38 | + "args": ["math_server.py"], |
| 39 | + }, |
| 40 | + "date-server": { |
| 41 | + "transport": "stdio", |
| 42 | + "command": "python", |
| 43 | + "args": ["date_server.py"], |
| 44 | + }, |
| 45 | + } |
| 46 | + } |
| 47 | + config_path = tmp_path / "mcp.json" |
| 48 | + config_path.write_text(json.dumps(config)) |
| 49 | + return str(config_path) |
| 50 | + |
| 51 | + |
| 52 | +@pytest.fixture |
| 53 | +def factory(tmp_path): |
| 54 | + context = MagicMock() |
| 55 | + context.config_path = str(tmp_path / "uipath.json") |
| 56 | + context.folder_key = "test-folder-key" |
| 57 | + context.mcp_server_id = "test-server-id" |
| 58 | + return UiPathMcpRuntimeFactory(context=context) |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.asyncio |
| 62 | +async def test_exact_match_works(factory, mcp_json_single): |
| 63 | + """Server found by exact name match.""" |
| 64 | + factory._mcp_config = None |
| 65 | + with _UIPATH_PATCH, patch.object(factory, "_load_mcp_config") as mock_load: |
| 66 | + from uipath_mcp._cli._utils._config import McpConfig |
| 67 | + |
| 68 | + mock_load.return_value = McpConfig(mcp_json_single) |
| 69 | + runtime = await factory.new_runtime("math-server", "00000000-0000-0000-0000-000000000001") |
| 70 | + assert runtime._entrypoint == "math-server" |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.asyncio |
| 74 | +async def test_wrong_name_without_force_raises(factory, mcp_json_single): |
| 75 | + """Wrong entrypoint without --force raises SERVER_NOT_FOUND.""" |
| 76 | + with patch.object(factory, "_load_mcp_config") as mock_load: |
| 77 | + from uipath_mcp._cli._utils._config import McpConfig |
| 78 | + |
| 79 | + mock_load.return_value = McpConfig(mcp_json_single) |
| 80 | + with pytest.raises(UiPathMcpRuntimeError) as exc_info: |
| 81 | + await factory.new_runtime("my-mcp2", "00000000-0000-0000-0000-000000000001") |
| 82 | + assert "not found" in str(exc_info.value).lower() |
| 83 | + |
| 84 | + |
| 85 | +@pytest.mark.asyncio |
| 86 | +async def test_force_single_server_uses_config_with_new_entrypoint(factory, mcp_json_single): |
| 87 | + """--force with single server loads its config but uses the requested entrypoint.""" |
| 88 | + with _UIPATH_PATCH, patch.object(factory, "_load_mcp_config") as mock_load: |
| 89 | + from uipath_mcp._cli._utils._config import McpConfig |
| 90 | + |
| 91 | + mock_load.return_value = McpConfig(mcp_json_single) |
| 92 | + runtime = await factory.new_runtime( |
| 93 | + "my-mcp2", "00000000-0000-0000-0000-000000000001", force=True |
| 94 | + ) |
| 95 | + # Entrypoint is the requested name, not the original server name |
| 96 | + assert runtime._entrypoint == "my-mcp2" |
| 97 | + # Server config comes from the existing server |
| 98 | + assert runtime._server.command == "python" |
| 99 | + assert runtime._server.args == ["server.py"] |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.asyncio |
| 103 | +async def test_force_multiple_servers_raises(factory, mcp_json_multiple): |
| 104 | + """--force with multiple servers raises an error.""" |
| 105 | + with patch.object(factory, "_load_mcp_config") as mock_load: |
| 106 | + from uipath_mcp._cli._utils._config import McpConfig |
| 107 | + |
| 108 | + mock_load.return_value = McpConfig(mcp_json_multiple) |
| 109 | + with pytest.raises(UiPathMcpRuntimeError) as exc_info: |
| 110 | + await factory.new_runtime( |
| 111 | + "my-mcp2", "00000000-0000-0000-0000-000000000001", force=True |
| 112 | + ) |
| 113 | + assert "--force requires exactly one server" in str(exc_info.value) |
| 114 | + |
| 115 | + |
| 116 | +@pytest.mark.asyncio |
| 117 | +async def test_force_with_exact_match_still_works(factory, mcp_json_single): |
| 118 | + """--force with a correct entrypoint name works normally.""" |
| 119 | + with _UIPATH_PATCH, patch.object(factory, "_load_mcp_config") as mock_load: |
| 120 | + from uipath_mcp._cli._utils._config import McpConfig |
| 121 | + |
| 122 | + mock_load.return_value = McpConfig(mcp_json_single) |
| 123 | + runtime = await factory.new_runtime( |
| 124 | + "math-server", "00000000-0000-0000-0000-000000000001", force=True |
| 125 | + ) |
| 126 | + assert runtime._entrypoint == "math-server" |
0 commit comments