|
| 1 | +from types import SimpleNamespace |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +import astrbot.core.tools.registry as tool_registry |
| 6 | +import astrbot.core.tools.web_search_tools as web_search_tools |
| 7 | +from astrbot.core.knowledge_base.parsers.url_parser import URLExtractor |
| 8 | +from astrbot.core.tools.web_search_tools import ExaWebSearchTool |
| 9 | + |
| 10 | + |
| 11 | +def _make_tool_context(provider_settings: dict) -> SimpleNamespace: |
| 12 | + cfg = {"provider_settings": provider_settings} |
| 13 | + return SimpleNamespace( |
| 14 | + context=SimpleNamespace( |
| 15 | + context=SimpleNamespace(get_config=lambda umo=None: cfg), |
| 16 | + event=SimpleNamespace(unified_msg_origin="test:private:session"), |
| 17 | + ) |
| 18 | + ) |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.asyncio |
| 22 | +@pytest.mark.parametrize( |
| 23 | + ("search_type", "expected"), |
| 24 | + [ |
| 25 | + ("deep-lite", "deep-lite"), |
| 26 | + ("deep-reasoning", "deep-reasoning"), |
| 27 | + ("instant", "instant"), |
| 28 | + ("unsupported", "auto"), |
| 29 | + ], |
| 30 | +) |
| 31 | +async def test_exa_web_search_tool_normalizes_search_type( |
| 32 | + monkeypatch: pytest.MonkeyPatch, |
| 33 | + search_type: str, |
| 34 | + expected: str, |
| 35 | +): |
| 36 | + captured: dict[str, object] = {} |
| 37 | + |
| 38 | + async def fake_exa_search(provider_settings: dict, payload: dict, timeout: int): |
| 39 | + captured["provider_settings"] = provider_settings |
| 40 | + captured["payload"] = payload |
| 41 | + captured["timeout"] = timeout |
| 42 | + return [] |
| 43 | + |
| 44 | + monkeypatch.setattr(web_search_tools, "_exa_search", fake_exa_search) |
| 45 | + |
| 46 | + tool = ExaWebSearchTool() |
| 47 | + result = await tool.call( |
| 48 | + _make_tool_context({"websearch_exa_key": ["test-key"]}), |
| 49 | + query="AstrBot", |
| 50 | + search_type=search_type, |
| 51 | + ) |
| 52 | + |
| 53 | + assert result == "Error: Exa web searcher does not return any results." |
| 54 | + assert captured["payload"]["type"] == expected |
| 55 | + |
| 56 | + |
| 57 | +def test_get_exa_base_url_rejects_endpoint_path(): |
| 58 | + with pytest.raises(ValueError) as exc_info: |
| 59 | + web_search_tools._get_exa_base_url( |
| 60 | + {"websearch_exa_base_url": "https://api.exa.ai/search"} |
| 61 | + ) |
| 62 | + |
| 63 | + assert str(exc_info.value) == ( |
| 64 | + "Error: Exa API Base URL must be a base URL or proxy prefix, " |
| 65 | + "not a specific endpoint path. Received: 'https://api.exa.ai/search'." |
| 66 | + ) |
| 67 | + |
| 68 | + |
| 69 | +def test_url_extractor_rejects_endpoint_base_url(): |
| 70 | + with pytest.raises(ValueError) as exc_info: |
| 71 | + URLExtractor( |
| 72 | + ["test-key"], |
| 73 | + tavily_base_url="https://api.tavily.com/extract", |
| 74 | + ) |
| 75 | + |
| 76 | + assert str(exc_info.value) == ( |
| 77 | + "Error: Tavily API Base URL must be a base URL or proxy prefix, " |
| 78 | + "not a specific endpoint path. Received: 'https://api.tavily.com/extract'." |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +def test_bocha_builtin_config_statuses_are_registered(): |
| 83 | + rule = tool_registry._BUILTIN_TOOL_CONFIG_RULES.get("web_search_bocha") |
| 84 | + |
| 85 | + assert rule is not None |
| 86 | + statuses = rule.evaluate( |
| 87 | + { |
| 88 | + "provider_settings": { |
| 89 | + "web_search": True, |
| 90 | + "websearch_provider": "bocha", |
| 91 | + } |
| 92 | + } |
| 93 | + ) |
| 94 | + |
| 95 | + assert statuses == [ |
| 96 | + { |
| 97 | + "key": "provider_settings.web_search", |
| 98 | + "operator": "equals", |
| 99 | + "expected": True, |
| 100 | + "actual": True, |
| 101 | + "matched": True, |
| 102 | + "message": None, |
| 103 | + }, |
| 104 | + { |
| 105 | + "key": "provider_settings.websearch_provider", |
| 106 | + "operator": "equals", |
| 107 | + "expected": "bocha", |
| 108 | + "actual": "bocha", |
| 109 | + "matched": True, |
| 110 | + "message": None, |
| 111 | + } |
| 112 | + ] |
0 commit comments