|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import sys |
| 5 | +from types import ModuleType, SimpleNamespace |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from nanobot.agent.tools.mcp import MCPToolWrapper |
| 10 | + |
| 11 | + |
| 12 | +class _FakeTextContent: |
| 13 | + def __init__(self, text: str) -> None: |
| 14 | + self.text = text |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture(autouse=True) |
| 18 | +def _fake_mcp_module(monkeypatch: pytest.MonkeyPatch) -> None: |
| 19 | + mod = ModuleType("mcp") |
| 20 | + mod.types = SimpleNamespace(TextContent=_FakeTextContent) |
| 21 | + monkeypatch.setitem(sys.modules, "mcp", mod) |
| 22 | + |
| 23 | + |
| 24 | +def _make_wrapper(session: object, *, timeout: float = 0.1) -> MCPToolWrapper: |
| 25 | + tool_def = SimpleNamespace( |
| 26 | + name="demo", |
| 27 | + description="demo tool", |
| 28 | + inputSchema={"type": "object", "properties": {}}, |
| 29 | + ) |
| 30 | + return MCPToolWrapper(session, "test", tool_def, tool_timeout=timeout) |
| 31 | + |
| 32 | + |
| 33 | +@pytest.mark.asyncio |
| 34 | +async def test_execute_returns_text_blocks() -> None: |
| 35 | + async def call_tool(_name: str, arguments: dict) -> object: |
| 36 | + assert arguments == {"value": 1} |
| 37 | + return SimpleNamespace(content=[_FakeTextContent("hello"), 42]) |
| 38 | + |
| 39 | + wrapper = _make_wrapper(SimpleNamespace(call_tool=call_tool)) |
| 40 | + |
| 41 | + result = await wrapper.execute(value=1) |
| 42 | + |
| 43 | + assert result == "hello\n42" |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.asyncio |
| 47 | +async def test_execute_returns_timeout_message() -> None: |
| 48 | + async def call_tool(_name: str, arguments: dict) -> object: |
| 49 | + await asyncio.sleep(1) |
| 50 | + return SimpleNamespace(content=[]) |
| 51 | + |
| 52 | + wrapper = _make_wrapper(SimpleNamespace(call_tool=call_tool), timeout=0.01) |
| 53 | + |
| 54 | + result = await wrapper.execute() |
| 55 | + |
| 56 | + assert result == "(MCP tool call timed out after 0.01s)" |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.asyncio |
| 60 | +async def test_execute_handles_server_cancelled_error() -> None: |
| 61 | + async def call_tool(_name: str, arguments: dict) -> object: |
| 62 | + raise asyncio.CancelledError() |
| 63 | + |
| 64 | + wrapper = _make_wrapper(SimpleNamespace(call_tool=call_tool)) |
| 65 | + |
| 66 | + result = await wrapper.execute() |
| 67 | + |
| 68 | + assert result == "(MCP tool call was cancelled)" |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.asyncio |
| 72 | +async def test_execute_re_raises_external_cancellation() -> None: |
| 73 | + started = asyncio.Event() |
| 74 | + |
| 75 | + async def call_tool(_name: str, arguments: dict) -> object: |
| 76 | + started.set() |
| 77 | + await asyncio.sleep(60) |
| 78 | + return SimpleNamespace(content=[]) |
| 79 | + |
| 80 | + wrapper = _make_wrapper(SimpleNamespace(call_tool=call_tool), timeout=10) |
| 81 | + task = asyncio.create_task(wrapper.execute()) |
| 82 | + await started.wait() |
| 83 | + |
| 84 | + task.cancel() |
| 85 | + |
| 86 | + with pytest.raises(asyncio.CancelledError): |
| 87 | + await task |
| 88 | + |
| 89 | + |
| 90 | +@pytest.mark.asyncio |
| 91 | +async def test_execute_handles_generic_exception() -> None: |
| 92 | + async def call_tool(_name: str, arguments: dict) -> object: |
| 93 | + raise RuntimeError("boom") |
| 94 | + |
| 95 | + wrapper = _make_wrapper(SimpleNamespace(call_tool=call_tool)) |
| 96 | + |
| 97 | + result = await wrapper.execute() |
| 98 | + |
| 99 | + assert result == "(MCP tool call failed: RuntimeError)" |
0 commit comments