From b959ae6d8965ca58b8dde49247137403e09ecea1 Mon Sep 17 00:00:00 2001 From: Xubin Ren Date: Sat, 11 Apr 2026 08:48:42 +0000 Subject: [PATCH] test(web): cover Kagi search provider Add focused coverage for the Kagi web search provider, including the request format and the DuckDuckGo fallback when no API key is configured. --- tests/tools/test_web_search_tool.py | 38 +++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/tools/test_web_search_tool.py b/tests/tools/test_web_search_tool.py index e33dd7e6c..790d8adcd 100644 --- a/tests/tools/test_web_search_tool.py +++ b/tests/tools/test_web_search_tool.py @@ -120,6 +120,27 @@ async def test_jina_search(monkeypatch): assert "https://jina.ai" in result +@pytest.mark.asyncio +async def test_kagi_search(monkeypatch): + async def mock_get(self, url, **kw): + assert "kagi.com/api/v0/search" in url + assert kw["headers"]["Authorization"] == "Bot kagi-key" + assert kw["params"] == {"q": "test", "limit": 2} + return _response(json={ + "data": [ + {"t": 0, "title": "Kagi Result", "url": "https://kagi.com", "snippet": "Premium search"}, + {"t": 1, "list": ["ignored related search"]}, + ] + }) + + monkeypatch.setattr(httpx.AsyncClient, "get", mock_get) + tool = _tool(provider="kagi", api_key="kagi-key") + result = await tool.execute(query="test", count=2) + assert "Kagi Result" in result + assert "https://kagi.com" in result + assert "ignored related search" not in result + + @pytest.mark.asyncio async def test_unknown_provider(): tool = _tool(provider="unknown") @@ -189,6 +210,23 @@ async def test_jina_422_falls_back_to_duckduckgo(monkeypatch): assert "DuckDuckGo fallback" in result +@pytest.mark.asyncio +async def test_kagi_fallback_to_duckduckgo_when_no_key(monkeypatch): + class MockDDGS: + def __init__(self, **kw): + pass + + def text(self, query, max_results=5): + return [{"title": "Fallback", "href": "https://ddg.example", "body": "DuckDuckGo fallback"}] + + monkeypatch.setattr("ddgs.DDGS", MockDDGS) + monkeypatch.delenv("KAGI_API_KEY", raising=False) + + tool = _tool(provider="kagi", api_key="") + result = await tool.execute(query="test") + assert "Fallback" in result + + @pytest.mark.asyncio async def test_jina_search_uses_path_encoded_query(monkeypatch): calls = {}