|
| 1 | +from typing import Any |
| 2 | + |
| 3 | +import pytest |
| 4 | +import pytest_asyncio |
| 5 | + |
| 6 | +from deepset_mcp.client import AsyncDeepsetClient |
| 7 | +from deepset_mcp.transport import AsyncTransport, TransportProtocol |
| 8 | + |
| 9 | + |
| 10 | +class DummyProtocol(TransportProtocol): |
| 11 | + def __init__(self) -> None: |
| 12 | + self.requests: list[dict[str, Any]] = [] |
| 13 | + self.closed: bool = False |
| 14 | + |
| 15 | + async def request(self, method: str, url: str, **kwargs: Any) -> Any: |
| 16 | + # Record the request and return a dummy response |
| 17 | + record: dict[str, Any] = {"method": method, "url": url, **kwargs} |
| 18 | + self.requests.append(record) |
| 19 | + return {"dummy": "response"} |
| 20 | + |
| 21 | + async def close(self) -> None: |
| 22 | + self.closed = True |
| 23 | + |
| 24 | + |
| 25 | +@pytest_asyncio.fixture(autouse=True) # type: ignore |
| 26 | +def clear_env(monkeypatch: pytest.MonkeyPatch) -> None: |
| 27 | + # Ensure DEEPSET_API_KEY is unset by default unless explicitly set |
| 28 | + monkeypatch.delenv("DEEPSET_API_KEY", raising=False) |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.asyncio |
| 32 | +async def test_init_with_api_key_and_transport() -> None: |
| 33 | + dummy: DummyProtocol = DummyProtocol() |
| 34 | + client: AsyncDeepsetClient = AsyncDeepsetClient(api_key="testkey", transport=dummy) |
| 35 | + assert client.api_key == "testkey" |
| 36 | + assert client._transport is dummy |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.asyncio |
| 40 | +async def test_init_without_api_key_raises() -> None: |
| 41 | + # No API key in args or env |
| 42 | + with pytest.raises(ValueError): |
| 43 | + AsyncDeepsetClient() |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.asyncio |
| 47 | +async def test_init_with_env_api_key(monkeypatch: pytest.MonkeyPatch) -> None: |
| 48 | + monkeypatch.setenv("DEEPSET_API_KEY", "envkey") |
| 49 | + client: AsyncDeepsetClient = AsyncDeepsetClient() |
| 50 | + assert client.api_key == "envkey" |
| 51 | + # Default transport is AsyncTransport |
| 52 | + assert isinstance(client._transport, AsyncTransport) |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.asyncio |
| 56 | +async def test_request_default_headers_and_url() -> None: |
| 57 | + dummy: DummyProtocol = DummyProtocol() |
| 58 | + client: AsyncDeepsetClient = AsyncDeepsetClient(api_key="key", base_url="https://api.test", transport=dummy) |
| 59 | + |
| 60 | + resp: Any = await client.request("endpoint") |
| 61 | + # Should normalize endpoint to '/endpoint' |
| 62 | + assert resp == {"dummy": "response"} |
| 63 | + assert len(dummy.requests) == 1 |
| 64 | + |
| 65 | + call: dict[str, Any] = dummy.requests[0] |
| 66 | + assert call["method"] == "GET" |
| 67 | + assert call["url"] == "https://api.test/endpoint" |
| 68 | + headers: dict[str, str] = call["headers"] |
| 69 | + assert headers["Authorization"] == "Bearer key" |
| 70 | + assert headers["Accept"] == "application/json,text/plain,*/*" |
| 71 | + assert "Content-Type" not in headers |
| 72 | + assert call.get("json") is None |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.asyncio |
| 76 | +async def test_request_with_data_and_custom_headers() -> None: |
| 77 | + dummy: DummyProtocol = DummyProtocol() |
| 78 | + client: AsyncDeepsetClient = AsyncDeepsetClient(api_key="key", base_url="https://api.test", transport=dummy) |
| 79 | + |
| 80 | + data: dict[str, Any] = {"foo": "bar"} |
| 81 | + custom: dict[str, str] = {"X-Custom": "value"} |
| 82 | + resp: Any = await client.request("/path", method="POST", data=data, headers=custom) |
| 83 | + assert resp == {"dummy": "response"} |
| 84 | + assert len(dummy.requests) == 1 |
| 85 | + |
| 86 | + call = dummy.requests[0] |
| 87 | + assert call["method"] == "POST" |
| 88 | + assert call["url"] == "https://api.test/path" |
| 89 | + headers = call["headers"] |
| 90 | + # Custom header merged |
| 91 | + assert headers["X-Custom"] == "value" |
| 92 | + assert headers["Content-Type"] == "application/json" |
| 93 | + # Authorization preserved |
| 94 | + assert headers["Authorization"] == "Bearer key" |
| 95 | + assert call.get("json") == data |
| 96 | + |
| 97 | + |
| 98 | +@pytest.mark.asyncio |
| 99 | +async def test_close_and_context_manager() -> None: |
| 100 | + dummy: DummyProtocol = DummyProtocol() |
| 101 | + client: AsyncDeepsetClient = AsyncDeepsetClient(api_key="key", transport=dummy) |
| 102 | + |
| 103 | + # Test close |
| 104 | + await client.close() |
| 105 | + assert dummy.closed is True |
| 106 | + |
| 107 | + # Test async context manager |
| 108 | + dummy2: DummyProtocol = DummyProtocol() |
| 109 | + async with AsyncDeepsetClient(api_key="key", transport=dummy2) as ctx: |
| 110 | + assert isinstance(ctx, AsyncDeepsetClient) |
| 111 | + # After exit, close should be called |
| 112 | + assert dummy2.closed is True |
0 commit comments