File size: 3,138 Bytes
881f9f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""Tests for the Cell B MCP tool builder."""

from __future__ import annotations

import pathlib
import sys

import pytest


def test_mcp_client_timeout_default(monkeypatch):
    from scripts.aat_tools_mcp import _client_timeout_seconds

    monkeypatch.delenv("AAT_MCP_CLIENT_TIMEOUT_SECONDS", raising=False)

    assert _client_timeout_seconds() == 30


def test_mcp_client_timeout_env(monkeypatch):
    from scripts.aat_tools_mcp import _client_timeout_seconds

    monkeypatch.setenv("AAT_MCP_CLIENT_TIMEOUT_SECONDS", "45.5")

    assert _client_timeout_seconds() == 45.5


def test_mcp_client_timeout_rejects_invalid_env(monkeypatch):
    from scripts.aat_tools_mcp import _client_timeout_seconds

    monkeypatch.setenv("AAT_MCP_CLIENT_TIMEOUT_SECONDS", "nope")

    with pytest.raises(ValueError, match="must be numeric"):
        _client_timeout_seconds()


def test_server_launch_mode_rejects_invalid_env(monkeypatch):
    from scripts.aat_tools_mcp import _server_launch_mode

    monkeypatch.setenv("AAT_MCP_SERVER_LAUNCH_MODE", "bogus")

    with pytest.raises(ValueError, match="must be either"):
        _server_launch_mode()


def test_server_params_python_uses_bootstrap(monkeypatch):
    from scripts.aat_tools_mcp import _server_params

    repo_root = pathlib.Path.cwd()
    server_path = repo_root / "mcp_servers/iot_server/server.py"
    monkeypatch.setenv("AAT_MCP_SERVER_LAUNCH_MODE", "python")
    monkeypatch.setenv("AAT_MCP_SERVER_PYTHON", sys.executable)

    params = _server_params(repo_root, server_path)

    assert params["command"] == sys.executable
    assert params["args"][:2] == [
        "-u",
        str(repo_root / "scripts/aat_mcp_server_bootstrap.py"),
    ]
    assert params["args"][-1] == str(server_path)
    assert params["cwd"] == str(repo_root)
    assert params["env"] == {
        "PYTHONUNBUFFERED": "1",
        "AAT_MCP_REPO_ROOT": str(repo_root),
    }


def test_server_params_python_requires_server_python(monkeypatch):
    from scripts.aat_tools_mcp import _server_params

    repo_root = pathlib.Path.cwd()
    server_path = repo_root / "mcp_servers/iot_server/server.py"
    monkeypatch.setenv("AAT_MCP_SERVER_LAUNCH_MODE", "python")
    monkeypatch.delenv("AAT_MCP_SERVER_PYTHON", raising=False)

    with pytest.raises(ValueError, match="requires AAT_MCP_SERVER_PYTHON"):
        _server_params(repo_root, server_path)


def test_server_params_uv_ignores_server_python(monkeypatch):
    from scripts.aat_tools_mcp import _server_params

    repo_root = pathlib.Path.cwd()
    server_path = repo_root / "mcp_servers/iot_server/server.py"
    monkeypatch.setenv("AAT_MCP_SERVER_LAUNCH_MODE", "uv")
    monkeypatch.setenv("AAT_MCP_SERVER_PYTHON", "/usr/bin/python3")

    params = _server_params(repo_root, server_path)

    assert params["command"] == "uv"
    assert "python" in params["args"]
    assert "-u" in params["args"]
    assert str(repo_root / "scripts/aat_mcp_server_bootstrap.py") in params["args"]
    assert params["args"][-1] == str(server_path)
    assert params["env"] == {
        "PYTHONUNBUFFERED": "1",
        "AAT_MCP_REPO_ROOT": str(repo_root),
    }