|
| 1 | +"""Unit tests for client abstractions.""" |
| 2 | + |
| 3 | +# pylint: disable=protected-access,invalid-overridden-method |
| 4 | + |
| 5 | +import asyncio |
| 6 | +import json |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from flowllm.components.client import BaseClient, HttpClient, MCPClient |
| 11 | +from flowllm.constants import FLOWLLM_DEFAULT_HOST, FLOWLLM_DEFAULT_PORT, FLOWLLM_SERVICE_INFO |
| 12 | + |
| 13 | + |
| 14 | +class _FakeClient(BaseClient): |
| 15 | + """Concrete BaseClient for exercising wrapper behavior.""" |
| 16 | + |
| 17 | + def __init__(self, actions=None, **kwargs): |
| 18 | + super().__init__(**kwargs) |
| 19 | + self.actions = actions or [{"action": "search"}] |
| 20 | + self.payloads = [] |
| 21 | + |
| 22 | + async def list_actions(self): |
| 23 | + return self.actions |
| 24 | + |
| 25 | + async def _execute(self, action: str, payload: dict): |
| 26 | + self.payloads.append((action, payload)) |
| 27 | + yield "chunk" |
| 28 | + |
| 29 | + |
| 30 | +def test_base_client_list_action_returns_pretty_json(): |
| 31 | + """The synthetic list action uses list_actions instead of _execute.""" |
| 32 | + |
| 33 | + async def run(): |
| 34 | + client = _FakeClient(actions=[{"action": "health_check", "method": "GET"}]) |
| 35 | + chunks = [chunk async for chunk in client("list")] |
| 36 | + assert json.loads(chunks[0]) == [{"action": "health_check", "method": "GET"}] |
| 37 | + assert not client.payloads |
| 38 | + |
| 39 | + asyncio.run(run()) |
| 40 | + |
| 41 | + |
| 42 | +def test_base_client_delegates_non_list_actions(): |
| 43 | + """Regular actions pass through to _execute with kwargs as payload.""" |
| 44 | + |
| 45 | + async def run(): |
| 46 | + client = _FakeClient() |
| 47 | + assert [chunk async for chunk in client("search", query="hi")] == ["chunk"] |
| 48 | + assert client.payloads == [("search", {"query": "hi"})] |
| 49 | + |
| 50 | + asyncio.run(run()) |
| 51 | + |
| 52 | + |
| 53 | +def test_http_client_reads_service_info_from_environment(monkeypatch): |
| 54 | + """HTTP client host/port default from FLOWLLM_SERVICE_INFO when present.""" |
| 55 | + monkeypatch.setenv(FLOWLLM_SERVICE_INFO, json.dumps({"host": "10.0.0.1", "port": 8123})) |
| 56 | + |
| 57 | + client = HttpClient() |
| 58 | + |
| 59 | + assert client.base_url == "http://10.0.0.1:8123" |
| 60 | + |
| 61 | + |
| 62 | +def test_http_client_falls_back_on_invalid_service_info(monkeypatch): |
| 63 | + """Invalid service info is ignored in favor of built-in defaults.""" |
| 64 | + monkeypatch.setenv(FLOWLLM_SERVICE_INFO, "not-json") |
| 65 | + |
| 66 | + client = HttpClient() |
| 67 | + |
| 68 | + assert client.base_url == f"http://{FLOWLLM_DEFAULT_HOST}:{FLOWLLM_DEFAULT_PORT}" |
| 69 | + |
| 70 | + |
| 71 | +def test_http_client_format_for_display_handles_response_envelope(): |
| 72 | + """Response envelopes render answer first with status and metadata.""" |
| 73 | + rendered = HttpClient._format_for_display( |
| 74 | + json.dumps({"answer": "done", "success": True, "metadata": {"cost": 1}, "extra": "x"}), |
| 75 | + ) |
| 76 | + |
| 77 | + assert rendered.splitlines()[0] == "done" |
| 78 | + assert "\u2705" in rendered |
| 79 | + assert '{"cost": 1}' in rendered |
| 80 | + assert '"extra": "x"' in rendered |
| 81 | + |
| 82 | + |
| 83 | +def test_mcp_client_rejects_unknown_transport(): |
| 84 | + """Unsupported MCP transport names fail during construction.""" |
| 85 | + with pytest.raises(ValueError, match="Unknown transport"): |
| 86 | + MCPClient(transport="websocket") |
0 commit comments