|
| 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 factory(tmp_path): |
| 32 | + context = MagicMock() |
| 33 | + context.config_path = str(tmp_path / "uipath.json") |
| 34 | + context.folder_key = "test-folder-key" |
| 35 | + context.mcp_server_id = "test-server-id" |
| 36 | + return UiPathMcpRuntimeFactory(context=context) |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.asyncio |
| 40 | +async def test_exact_match_works(factory, mcp_json_single): |
| 41 | + """Server found by exact name match.""" |
| 42 | + factory._mcp_config = None |
| 43 | + with _UIPATH_PATCH, patch.object(factory, "_load_mcp_config") as mock_load: |
| 44 | + from uipath_mcp._cli._utils._config import McpConfig |
| 45 | + |
| 46 | + mock_load.return_value = McpConfig(mcp_json_single) |
| 47 | + runtime = await factory.new_runtime( |
| 48 | + "math-server", "00000000-0000-0000-0000-000000000001" |
| 49 | + ) |
| 50 | + assert runtime._entrypoint == "math-server" |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.asyncio |
| 54 | +async def test_wrong_name_raises(factory, mcp_json_single): |
| 55 | + """Wrong entrypoint raises SERVER_NOT_FOUND.""" |
| 56 | + with patch.object(factory, "_load_mcp_config") as mock_load: |
| 57 | + from uipath_mcp._cli._utils._config import McpConfig |
| 58 | + |
| 59 | + mock_load.return_value = McpConfig(mcp_json_single) |
| 60 | + with pytest.raises(UiPathMcpRuntimeError) as exc_info: |
| 61 | + await factory.new_runtime("my-mcp2", "00000000-0000-0000-0000-000000000001") |
| 62 | + assert "not found" in str(exc_info.value).lower() |
0 commit comments