feat: web.py search() over SearXNG json API

This commit is contained in:
2026-06-28 20:52:36 -05:00
parent 5d7881c4ca
commit 52707d4703
2 changed files with 50 additions and 0 deletions

View File

@@ -68,3 +68,34 @@ def test_request_raises_after_exhausting_retries(monkeypatch):
monkeypatch.setattr(web.time, "sleep", lambda *_: None)
with pytest.raises(web.WebToolsError):
web._request("http://x/y", retries=2)
def test_search_parses_and_caps(monkeypatch):
payload = {"results": [
{"title": "A", "url": "http://a", "content": "snip a", "engine": "ddg"},
{"title": "B", "url": "http://b", "content": "snip b", "engine": "bing"},
{"title": "no-url", "content": "dropped"},
{"title": "C", "url": "http://c", "content": "snip c", "engine": "ddg"},
]}
monkeypatch.setattr(web, "_request", lambda url, **kw: payload)
hits = web.search("demons", max_results=2)
assert hits == [
{"title": "A", "url": "http://a", "snippet": "snip a", "engine": "ddg"},
{"title": "B", "url": "http://b", "snippet": "snip b", "engine": "bing"},
]
def test_search_builds_json_query_url(monkeypatch):
captured = {}
def fake_request(url, **kw):
captured["url"] = url
return {"results": []}
monkeypatch.setattr(web, "_request", fake_request)
monkeypatch.setenv("WEBTOOLS_HOST", "10.10.20.37")
monkeypatch.delenv("SEARXNG_PORT", raising=False)
web.search("demon hierarchy")
assert captured["url"].startswith("http://10.10.20.37:8080/search?")
assert "q=demon+hierarchy" in captured["url"]
assert "format=json" in captured["url"]