|
| 1 | +import pytest |
| 2 | + |
| 3 | +import src.tools.misc as misc |
| 4 | + |
| 5 | + |
| 6 | +@pytest.mark.asyncio |
| 7 | +async def test_search_redis_documents_url_not_configured(monkeypatch): |
| 8 | + """Return a clear error if MCP_DOCS_SEARCH_URL is not set for search_redis_documents.""" |
| 9 | + monkeypatch.setattr(misc, "MCP_DOCS_SEARCH_URL", "") |
| 10 | + |
| 11 | + result = await misc.search_redis_documents("What is Redis?") |
| 12 | + |
| 13 | + assert isinstance(result, dict) |
| 14 | + assert ( |
| 15 | + result["error"] == "MCP_DOCS_SEARCH_URL environment variable is not configured" |
| 16 | + ) |
| 17 | + |
| 18 | + |
| 19 | +@pytest.mark.asyncio |
| 20 | +async def test_search_redis_documents_empty_question(monkeypatch): |
| 21 | + """Reject empty/whitespace-only questions for search_redis_documents.""" |
| 22 | + monkeypatch.setattr(misc, "MCP_DOCS_SEARCH_URL", "https://example.com/docs") |
| 23 | + |
| 24 | + result = await misc.search_redis_documents(" ") |
| 25 | + |
| 26 | + assert isinstance(result, dict) |
| 27 | + assert result["error"] == "Question parameter cannot be empty" |
| 28 | + |
| 29 | + |
| 30 | +@pytest.mark.asyncio |
| 31 | +async def test_search_redis_documents_success_json_response(monkeypatch): |
| 32 | + """Return parsed JSON when the docs API responds with JSON for search_redis_documents.""" |
| 33 | + |
| 34 | + class DummyResponse: |
| 35 | + async def __aenter__(self): |
| 36 | + return self |
| 37 | + |
| 38 | + async def __aexit__(self, exc_type, exc, tb): |
| 39 | + return False |
| 40 | + |
| 41 | + async def json(self): |
| 42 | + return {"results": [{"title": "Redis Intro"}]} |
| 43 | + |
| 44 | + class DummySession: |
| 45 | + async def __aenter__(self): |
| 46 | + return self |
| 47 | + |
| 48 | + async def __aexit__(self, exc_type, exc, tb): |
| 49 | + return False |
| 50 | + |
| 51 | + def get(self, *_, **__): # pragma: no cover - trivial wrapper |
| 52 | + return DummyResponse() |
| 53 | + |
| 54 | + monkeypatch.setattr(misc, "MCP_DOCS_SEARCH_URL", "https://example.com/docs") |
| 55 | + monkeypatch.setattr(misc.aiohttp, "ClientSession", DummySession) |
| 56 | + |
| 57 | + result = await misc.search_redis_documents("What is a Redis stream?") |
| 58 | + |
| 59 | + assert isinstance(result, dict) |
| 60 | + assert result["results"][0]["title"] == "Redis Intro" |
| 61 | + |
| 62 | + |
| 63 | +@pytest.mark.asyncio |
| 64 | +async def test_search_redis_documents_non_json_response(monkeypatch): |
| 65 | + """If the response is not JSON, surface the text content in an error from search_redis_documents.""" |
| 66 | + |
| 67 | + class DummyContentTypeError(Exception): |
| 68 | + pass |
| 69 | + |
| 70 | + class DummyResponse: |
| 71 | + async def __aenter__(self): |
| 72 | + return self |
| 73 | + |
| 74 | + async def __aexit__(self, exc_type, exc, tb): |
| 75 | + return False |
| 76 | + |
| 77 | + async def json(self): |
| 78 | + raise DummyContentTypeError("Not JSON") |
| 79 | + |
| 80 | + async def text(self): |
| 81 | + return "<html>not json</html>" |
| 82 | + |
| 83 | + class DummySession: |
| 84 | + async def __aenter__(self): |
| 85 | + return self |
| 86 | + |
| 87 | + async def __aexit__(self, exc_type, exc, tb): |
| 88 | + return False |
| 89 | + |
| 90 | + def get(self, *_, **__): # pragma: no cover - trivial wrapper |
| 91 | + return DummyResponse() |
| 92 | + |
| 93 | + monkeypatch.setattr(misc, "MCP_DOCS_SEARCH_URL", "https://example.com/docs") |
| 94 | + # Patch aiohttp.ContentTypeError to our dummy so the except block matches |
| 95 | + monkeypatch.setattr(misc.aiohttp, "ContentTypeError", DummyContentTypeError) |
| 96 | + monkeypatch.setattr(misc.aiohttp, "ClientSession", DummySession) |
| 97 | + |
| 98 | + result = await misc.search_redis_documents("Explain Redis JSON") |
| 99 | + |
| 100 | + assert isinstance(result, dict) |
| 101 | + assert "Non-JSON response" in result["error"] |
| 102 | + assert "not json" in result["error"] |
| 103 | + |
| 104 | + |
| 105 | +@pytest.mark.asyncio |
| 106 | +async def test_search_redis_documents_http_client_error(monkeypatch): |
| 107 | + """HTTP client errors from search_redis_documents are caught and returned in an error dict.""" |
| 108 | + |
| 109 | + class DummyClientError(Exception): |
| 110 | + pass |
| 111 | + |
| 112 | + class ErrorResponse: |
| 113 | + async def __aenter__(self): |
| 114 | + raise DummyClientError("boom") |
| 115 | + |
| 116 | + async def __aexit__(self, exc_type, exc, tb): |
| 117 | + return False |
| 118 | + |
| 119 | + class DummySession: |
| 120 | + async def __aenter__(self): |
| 121 | + return self |
| 122 | + |
| 123 | + async def __aexit__(self, exc_type, exc, tb): |
| 124 | + return False |
| 125 | + |
| 126 | + def get(self, *_, **__): # pragma: no cover - trivial wrapper |
| 127 | + return ErrorResponse() |
| 128 | + |
| 129 | + monkeypatch.setattr(misc, "MCP_DOCS_SEARCH_URL", "https://example.com/docs") |
| 130 | + monkeypatch.setattr(misc.aiohttp, "ClientError", DummyClientError) |
| 131 | + monkeypatch.setattr(misc.aiohttp, "ClientSession", DummySession) |
| 132 | + |
| 133 | + result = await misc.search_redis_documents("What is Redis?") |
| 134 | + |
| 135 | + assert isinstance(result, dict) |
| 136 | + assert result["error"] == "HTTP client error: boom" |
0 commit comments