|
| 1 | +"""Tests for McpConfig and McpServer.""" |
| 2 | + |
| 3 | +import json |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from uipath_mcp._cli._utils._config import McpConfig, McpServer |
| 9 | + |
| 10 | + |
| 11 | +def test_server_defaults(): |
| 12 | + s = McpServer("svc", {}) |
| 13 | + assert s.name == "svc" |
| 14 | + assert s.type is None |
| 15 | + assert s.transport == "stdio" |
| 16 | + assert s.url is None |
| 17 | + assert s.command == "None" |
| 18 | + assert s.args == [] |
| 19 | + assert s.env == {} |
| 20 | + assert s.file_path is None |
| 21 | + assert s.is_streamable_http is False |
| 22 | + |
| 23 | + |
| 24 | +def test_server_streamable_http_and_args(): |
| 25 | + s = McpServer( |
| 26 | + "svc", |
| 27 | + { |
| 28 | + "type": "remote", |
| 29 | + "transport": "streamable-http", |
| 30 | + "url": "https://x", |
| 31 | + "command": "node", |
| 32 | + "args": ["index.js"], |
| 33 | + "env": {}, |
| 34 | + }, |
| 35 | + ) |
| 36 | + assert s.is_streamable_http is True |
| 37 | + assert s.file_path == "index.js" |
| 38 | + d = s.to_dict() |
| 39 | + assert d["transport"] == "streamable-http" |
| 40 | + assert d["url"] == "https://x" |
| 41 | + assert d["command"] == "node" |
| 42 | + assert "McpServer" in repr(s) |
| 43 | + |
| 44 | + |
| 45 | +def test_server_env_overlay_from_os(monkeypatch: pytest.MonkeyPatch): |
| 46 | + monkeypatch.setenv("FOO", "from-env") |
| 47 | + s = McpServer("svc", {"env": {"FOO": "default", "BAR": "keep"}}) |
| 48 | + assert s.env["FOO"] == "from-env" |
| 49 | + assert s.env["BAR"] == "keep" |
| 50 | + |
| 51 | + |
| 52 | +def test_validate_server_name_ok(): |
| 53 | + McpConfig.validate_server_name("good-name-1") |
| 54 | + |
| 55 | + |
| 56 | +@pytest.mark.parametrize("bad", ["with space", "under_score", "dots.bad", ""]) |
| 57 | +def test_validate_server_name_bad(bad: str): |
| 58 | + with pytest.raises(ValueError): |
| 59 | + McpConfig.validate_server_name(bad) |
| 60 | + |
| 61 | + |
| 62 | +def test_config_not_exists(tmp_path: Path): |
| 63 | + cfg = McpConfig(str(tmp_path / "missing.json")) |
| 64 | + assert cfg.exists is False |
| 65 | + assert cfg.get_servers() == [] |
| 66 | + assert cfg.get_server_names() == [] |
| 67 | + assert cfg.get_server("anything") is None |
| 68 | + |
| 69 | + |
| 70 | +def test_config_load_and_lookup(tmp_path: Path): |
| 71 | + path = tmp_path / "mcp.json" |
| 72 | + path.write_text( |
| 73 | + json.dumps( |
| 74 | + { |
| 75 | + "servers": { |
| 76 | + "alpha": {"command": "a", "args": ["a.py"]}, |
| 77 | + "beta": {"command": "b"}, |
| 78 | + } |
| 79 | + } |
| 80 | + ) |
| 81 | + ) |
| 82 | + cfg = McpConfig(str(path)) |
| 83 | + assert cfg.exists is True |
| 84 | + assert set(cfg.get_server_names()) == {"alpha", "beta"} |
| 85 | + assert len(cfg.get_servers()) == 2 |
| 86 | + assert cfg.get_server("alpha").name == "alpha" # type: ignore[union-attr] |
| 87 | + assert cfg.get_server("missing") is None |
| 88 | + raw = cfg.load_config() |
| 89 | + assert "servers" in raw |
| 90 | + |
| 91 | + |
| 92 | +def test_config_single_server_returned_regardless_of_name(tmp_path: Path): |
| 93 | + path = tmp_path / "mcp.json" |
| 94 | + path.write_text(json.dumps({"servers": {"only": {"command": "x"}}})) |
| 95 | + cfg = McpConfig(str(path)) |
| 96 | + assert cfg.get_server("does-not-matter").name == "only" # type: ignore[union-attr] |
| 97 | + |
| 98 | + |
| 99 | +def test_config_load_invalid_json(tmp_path: Path): |
| 100 | + path = tmp_path / "mcp.json" |
| 101 | + path.write_text("{not json") |
| 102 | + with pytest.raises(json.JSONDecodeError): |
| 103 | + McpConfig(str(path)) |
| 104 | + |
| 105 | + |
| 106 | +def test_config_load_invalid_name(tmp_path: Path): |
| 107 | + path = tmp_path / "mcp.json" |
| 108 | + path.write_text(json.dumps({"servers": {"bad name": {}}})) |
| 109 | + with pytest.raises(ValueError): |
| 110 | + McpConfig(str(path)) |
| 111 | + |
| 112 | + |
| 113 | +def test_load_config_when_missing_raises(tmp_path: Path): |
| 114 | + cfg = McpConfig(str(tmp_path / "nope.json")) |
| 115 | + with pytest.raises(FileNotFoundError): |
| 116 | + cfg.load_config() |
0 commit comments