|
| 1 | +import json |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from astrbot.core.provider import func_tool_manager |
| 6 | +from astrbot.core.provider.func_tool_manager import FunctionToolManager |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def mcp_init_harness( |
| 11 | + monkeypatch: pytest.MonkeyPatch, |
| 12 | + tmp_path, |
| 13 | +): |
| 14 | + manager = FunctionToolManager() |
| 15 | + data_dir = tmp_path / "data" |
| 16 | + data_dir.mkdir() |
| 17 | + |
| 18 | + (data_dir / "mcp_server.json").write_text( |
| 19 | + json.dumps({"mcpServers": {"demo": {"active": True}}}), |
| 20 | + encoding="utf-8", |
| 21 | + ) |
| 22 | + monkeypatch.setattr( |
| 23 | + func_tool_manager, |
| 24 | + "get_astrbot_data_path", |
| 25 | + lambda: data_dir, |
| 26 | + ) |
| 27 | + |
| 28 | + called = {} |
| 29 | + |
| 30 | + async def fake_start_mcp_server(*, name, cfg, shutdown_event, timeout_seconds): |
| 31 | + called[name] = { |
| 32 | + "cfg": cfg, |
| 33 | + "shutdown_event_type": type(shutdown_event).__name__, |
| 34 | + "timeout_seconds": timeout_seconds, |
| 35 | + } |
| 36 | + |
| 37 | + monkeypatch.setattr(manager, "_start_mcp_server", fake_start_mcp_server) |
| 38 | + return manager, called |
| 39 | + |
| 40 | + |
| 41 | +def assert_demo_init_result(summary, called, *, timeout_seconds: float) -> None: |
| 42 | + assert summary.total == 1 |
| 43 | + assert summary.success == 1 |
| 44 | + assert summary.failed == [] |
| 45 | + assert called["demo"]["cfg"] == {"active": True} |
| 46 | + assert called["demo"]["shutdown_event_type"] == "Event" |
| 47 | + assert called["demo"]["timeout_seconds"] == timeout_seconds |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.asyncio |
| 51 | +async def test_init_mcp_clients_passes_timeout_seconds_keyword(mcp_init_harness): |
| 52 | + manager, called = mcp_init_harness |
| 53 | + |
| 54 | + summary = await manager.init_mcp_clients() |
| 55 | + |
| 56 | + assert_demo_init_result( |
| 57 | + summary, |
| 58 | + called, |
| 59 | + timeout_seconds=manager._init_timeout_default, |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +@pytest.mark.asyncio |
| 64 | +async def test_init_mcp_clients_passes_overridden_init_timeout( |
| 65 | + mcp_init_harness, |
| 66 | +): |
| 67 | + manager, called = mcp_init_harness |
| 68 | + |
| 69 | + summary = await manager.init_mcp_clients(init_timeout=3.5) |
| 70 | + |
| 71 | + assert_demo_init_result(summary, called, timeout_seconds=3.5) |
| 72 | + |
| 73 | + |
| 74 | +@pytest.mark.asyncio |
| 75 | +async def test_init_mcp_clients_reads_env_timeout_when_not_overridden( |
| 76 | + mcp_init_harness, |
| 77 | + monkeypatch: pytest.MonkeyPatch, |
| 78 | +): |
| 79 | + manager, called = mcp_init_harness |
| 80 | + manager._init_timeout_default = 20.0 # ensure env override is observable |
| 81 | + monkeypatch.setenv("ASTRBOT_MCP_INIT_TIMEOUT", "3.5") |
| 82 | + |
| 83 | + summary = await manager.init_mcp_clients() |
| 84 | + |
| 85 | + assert_demo_init_result(summary, called, timeout_seconds=3.5) |
| 86 | + |
| 87 | + |
| 88 | +@pytest.mark.asyncio |
| 89 | +async def test_init_mcp_clients_prefers_explicit_timeout_over_env( |
| 90 | + mcp_init_harness, |
| 91 | + monkeypatch: pytest.MonkeyPatch, |
| 92 | +): |
| 93 | + manager, called = mcp_init_harness |
| 94 | + monkeypatch.setenv("ASTRBOT_MCP_INIT_TIMEOUT", "7.0") |
| 95 | + |
| 96 | + summary = await manager.init_mcp_clients(init_timeout=3.5) |
| 97 | + |
| 98 | + assert_demo_init_result(summary, called, timeout_seconds=3.5) |
0 commit comments