import pytest from unittest.mock import AsyncMock, patch @pytest.mark.asyncio async def test_lookup_wikipedia_hit(): wiki_data = { "title": "QUIC", "extract": "QUIC is a transport layer protocol.", "url": "https://en.wikipedia.org/wiki/QUIC", } with patch("fabledassistant.services.wikipedia.wiki_summary", new_callable=AsyncMock, return_value=wiki_data): from fabledassistant.services.tools.web import lookup_tool result = await lookup_tool(user_id=1, arguments={"query": "QUIC"}) assert result["success"] is True assert result["type"] == "lookup" assert result["source"] == "wikipedia" assert result["data"]["title"] == "QUIC" @pytest.mark.asyncio async def test_lookup_wikipedia_miss_searxng_fallback(): searxng_results = [ {"url": "https://example.com/quic", "title": "QUIC Explained", "snippet": "An overview..."}, ] with patch("fabledassistant.services.wikipedia.wiki_summary", new_callable=AsyncMock, return_value=None), \ patch("fabledassistant.config.Config") as mock_config, \ patch("fabledassistant.services.research._search_searxng", new_callable=AsyncMock, return_value=searxng_results), \ patch("fabledassistant.services.rss._fetch_full_article", new_callable=AsyncMock, return_value="Full article about QUIC..."): mock_config.searxng_enabled.return_value = True from fabledassistant.services.tools.web import lookup_tool result = await lookup_tool(user_id=1, arguments={"query": "QUIC"}) assert result["success"] is True assert result["source"] == "web" assert result["data"]["results"] @pytest.mark.asyncio async def test_lookup_wikipedia_miss_no_searxng(): with patch("fabledassistant.services.wikipedia.wiki_summary", new_callable=AsyncMock, return_value=None), \ patch("fabledassistant.config.Config") as mock_config: mock_config.searxng_enabled.return_value = False from fabledassistant.services.tools.web import lookup_tool result = await lookup_tool(user_id=1, arguments={"query": "xyznonexistent"}) assert result["success"] is True assert result["source"] == "none" @pytest.mark.asyncio async def test_lookup_always_available(): with patch("fabledassistant.services.tools._registry.is_caldav_configured", new_callable=AsyncMock, return_value=False), \ patch("fabledassistant.services.settings.get_setting", new_callable=AsyncMock, return_value="false"): from fabledassistant.services.tools import get_tools_for_user tools = await get_tools_for_user(user_id=1) tool_names = {t["function"]["name"] for t in tools} assert "lookup" in tool_names