~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
import sys
import types
from io import StringIO
from types import SimpleNamespace
from unittest.mock import MagicMock, patch
import pytest
from loguru import logger
from local_deep_research.research_library.downloaders.base import (
ContentType,
rate_limit_authority,
)
from local_deep_research.research_library.downloaders.html import HTMLDownloader
from local_deep_research.research_library.downloaders.playwright_html import (
AutoHTMLDownloader,
PlaywrightHTMLDownloader,
)
_SECRET = "credential-do-not-log"
_EXCEPTION_TEXT = "network-error-do-not-log"
_TITLE = "title-do-not-log"
_URL = (
f"https://alice:{_SECRET}@origin.example:8443/private/%2Fencoded"
f"?token={_SECRET}&next=%2Felsewhere#fragment"
)
_ORIGIN = "https://origin.example:8443"
_FORBIDDEN = (
"alice",
_SECRET,
"/private/",
"%2Fencoded",
"token=",
"next=",
"fragment",
_EXCEPTION_TEXT,
_TITLE,
"Traceback",
)
@pytest.fixture
def rendered_log():
output = StringIO()
logger.enable("local_deep_research")
sink_id = logger.add(
output,
level="DEBUG",
format="{message}\n{exception}",
backtrace=True,
diagnose=False,
)
try:
yield output
finally:
logger.remove(sink_id)
logger.disable("local_deep_research")
def _assert_safe_event(output, event):
lines = [line for line in output.getvalue().splitlines() if event in line]
assert len(lines) == 1
assert lines[0].count(_ORIGIN) == 1
for marker in _FORBIDDEN:
assert marker not in output.getvalue()
def _html_downloader(response=None, error=None):
downloader = HTMLDownloader()
rate_tracker = MagicMock()
rate_tracker.apply_rate_limit.return_value = 0
downloader.rate_tracker = rate_tracker
downloader.session = MagicMock()
downloader.session.get.return_value = response
downloader.session.get.side_effect = error
return downloader, rate_tracker
@pytest.mark.parametrize(
("response", "error", "event", "rate_limit_calls"),
[
(
SimpleNamespace(
status_code=200,
headers={"content-type": "text/html"},
text="<html>ok</html>",
),
None,
"html.fetch_succeeded",
1,
),
(
SimpleNamespace(
status_code=200,
headers={"content-type": "application/pdf"},
text="",
content=b"%PDF-1.4 fake pdf bytes",
url=_ORIGIN,
),
None,
"html.fetch_pdf_recovery",
1,
),
(
SimpleNamespace(
status_code=200,
headers={"content-type": "text/plain"},
text="plain text body",
content=b"plain text body",
url=_ORIGIN,
),
None,
"html.fetch_text_recovery",
1,
),
(
SimpleNamespace(
status_code=200,
headers={"content-type": "application/json"},
text='{"key": "value"}',
content=b'{"key": "value"}',
url=_ORIGIN,
),
None,
"html.fetch_unexpected_content_type",
1,
),
(
SimpleNamespace(status_code=503, headers={}, text=""),
None,
"html.fetch_failed",
3,
),
(
None,
RuntimeError(f"{_EXCEPTION_TEXT}: {_URL}"),
"html.fetch_error",
1,
),
],
)
def test_static_fetch_branches_render_one_safe_origin(
rendered_log, response, error, event, rate_limit_calls
):
downloader, rate_tracker = _html_downloader(response=response, error=error)
with downloader:
downloader._fetch_html(_URL)
assert rate_tracker.apply_rate_limit.call_count == rate_limit_calls
rate_tracker.apply_rate_limit.assert_called_with(
"html_download_origin.example:8443"
)
_assert_safe_event(rendered_log, event)
@pytest.mark.parametrize(
("method_name", "event"),
[
("download", "html.download_failed"),
("download_with_result", "html.download_result_failed"),
],
)
def test_html_download_exception_branches_do_not_render_exception(
rendered_log, method_name, event
):
downloader = HTMLDownloader()
with patch.object(
downloader,
"_fetch_html",
side_effect=RuntimeError(f"{_EXCEPTION_TEXT}: {_URL}"),
):
getattr(downloader, method_name)(_URL, ContentType.TEXT)
_assert_safe_event(rendered_log, event)
def test_hostile_title_is_omitted_from_success_log(rendered_log):
downloader = HTMLDownloader()
with patch(
"local_deep_research.research_library.downloaders.html.extract_content_with_metadata",
return_value={"title": _TITLE, "content": "useful content"},
):
extracted = downloader._extract_content("<html></html>", _URL)
assert extracted is not None
assert extracted["title"] == _TITLE
_assert_safe_event(rendered_log, "html.extract_succeeded")
def test_metadata_exception_uses_safe_static_event(rendered_log):
downloader = HTMLDownloader()
with (
patch.object(downloader, "_fetch_html", return_value="<html></html>"),
patch(
"local_deep_research.research_library.downloaders.html.BeautifulSoup",
side_effect=RuntimeError(f"{_EXCEPTION_TEXT}: {_URL}"),
),
):
metadata = downloader.get_metadata(_URL)
assert metadata == {"url": _URL}
_assert_safe_event(rendered_log, "html.metadata_extract_failed")
def test_extract_exception_renders_safe_static_event(rendered_log):
downloader = HTMLDownloader()
with patch(
"local_deep_research.research_library.downloaders.html.extract_content_with_metadata",
side_effect=RuntimeError(f"{_EXCEPTION_TEXT}: {_URL}"),
):
assert downloader._extract_content("<html></html>", _URL) is None
_assert_safe_event(rendered_log, "html.extract_failed")
def _install_crawl4ai(monkeypatch, result):
class Config:
def __init__(self, **_kwargs):
pass
class Crawler:
def __init__(self, **_kwargs):
# The downloader installs its SSRF route guard via
# crawler_strategy.set_hook() before navigating; a guard-install
# failure fails closed (returns None) and emits none of the
# crawl4ai.* events. Provide a no-op strategy so the guarded
# fetch path proceeds and its event is rendered.
self.crawler_strategy = SimpleNamespace(
set_hook=lambda *_args, **_kwargs: None
)
async def __aenter__(self):
return self
async def __aexit__(self, *_args):
return False
async def arun(self, **_kwargs):
if isinstance(result, Exception):
raise result
return result
module = types.ModuleType("crawl4ai")
module.__dict__.update(
AsyncWebCrawler=Crawler,
BrowserConfig=Config,
CrawlerRunConfig=Config,
)
monkeypatch.setitem(sys.modules, "crawl4ai", module)
@pytest.mark.parametrize(
("result", "event"),
[
(
SimpleNamespace(success=True, html="<html>ok</html>"),
"crawl4ai.fetch_succeeded",
),
(
SimpleNamespace(
success=False, html="", error_message="robots.txt denied"
),
"crawl4ai.robots_denied",
),
(
SimpleNamespace(success=False, html="", status_code=503),
"crawl4ai.fetch_failed",
),
(RuntimeError(f"{_EXCEPTION_TEXT}: {_URL}"), "crawl4ai.fetch_error"),
],
)
def test_crawl4ai_branches_render_safe_events(
rendered_log, monkeypatch, result, event
):
_install_crawl4ai(monkeypatch, result)
downloader = PlaywrightHTMLDownloader()
# Close the robots.txt network seam: the real check fetches
# ``/robots.txt`` through SafeSession before the key is applied.
monkeypatch.setattr(downloader, "_robots_allows", lambda _url: True)
downloader.rate_tracker = MagicMock()
downloader.rate_tracker.apply_rate_limit.return_value = 0
with downloader:
downloader._fetch_with_crawl4ai(_URL)
downloader.rate_tracker.apply_rate_limit.assert_called_once_with(
"crawl4ai_download_origin.example:8443"
)
_assert_safe_event(rendered_log, event)
def _install_playwright(monkeypatch, page):
class Browser:
def new_page(self, **_kwargs):
return page
def close(self):
return None
class Playwright:
chromium = SimpleNamespace(launch=lambda **_kwargs: Browser())
def stop(self):
return None
package = types.ModuleType("playwright")
sync_api = types.ModuleType("playwright.sync_api")
sync_api.__dict__["sync_playwright"] = lambda: SimpleNamespace(
start=lambda: Playwright()
)
monkeypatch.setitem(sys.modules, "playwright", package)
monkeypatch.setitem(sys.modules, "playwright.sync_api", sync_api)
@pytest.mark.parametrize(
("content", "error", "event"),
[
("<html>ok</html>", None, "playwright.fetch_succeeded"),
("", None, "playwright.fetch_empty"),
(
None,
RuntimeError(f"{_EXCEPTION_TEXT}: {_URL}"),
"playwright.fetch_error",
),
],
)
def test_playwright_branches_render_safe_events(
rendered_log, monkeypatch, content, error, event
):
page = MagicMock()
page.goto.side_effect = error
page.content.return_value = content
_install_playwright(monkeypatch, page)
downloader = PlaywrightHTMLDownloader()
# Close the robots.txt network seam: the real check fetches
# ``/robots.txt`` through SafeSession before the key is applied.
monkeypatch.setattr(downloader, "_robots_allows", lambda _url: True)
downloader.rate_tracker = MagicMock()
downloader.rate_tracker.apply_rate_limit.return_value = 0
with downloader:
downloader._fetch_with_playwright(_URL)
downloader.rate_tracker.apply_rate_limit.assert_called_once_with(
"playwright_download_origin.example:8443"
)
_assert_safe_event(rendered_log, event)
def test_auto_html_success_fallback_and_raw_exception_events_are_safe(
rendered_log,
):
downloader = AutoHTMLDownloader(
min_content_length=10, enable_js_rendering=True
)
downloader.rate_tracker = MagicMock()
downloader.rate_tracker.apply_rate_limit.return_value = 0
downloader._playwright_downloader = MagicMock()
downloader._playwright_downloader.download.return_value = (
b"long enough body"
)
def static_download(*_args):
downloader._last_raw_html = '<div id="root"></div>'
return b"short"
with patch.object(HTMLDownloader, "download", side_effect=static_download):
assert downloader.download(_URL) == b"long enough body"
downloader.session = MagicMock()
downloader.session.get.side_effect = RuntimeError(
f"{_EXCEPTION_TEXT}: {_URL}"
)
try:
assert downloader._fetch_html(_URL) is None
finally:
downloader.close()
_assert_safe_event(rendered_log, "auto_html.js_fallback_started")
_assert_safe_event(rendered_log, "auto_html.js_succeeded")
_assert_safe_event(rendered_log, "auto_html.raw_fetch_failed")
@pytest.mark.parametrize(
("static_result", "event"),
[
(b"long enough body", "auto_html.static_fetch_succeeded"),
(None, "auto_html.js_fallback_disabled"),
],
)
def test_auto_html_static_success_and_disabled_failure_events_are_safe(
rendered_log, static_result, event
):
downloader = AutoHTMLDownloader(
min_content_length=10, enable_js_rendering=False
)
with patch.object(HTMLDownloader, "download", return_value=static_result):
downloader.download(_URL)
downloader.close()
_assert_safe_event(rendered_log, event)
# ---------------------------------------------------------------------------
# Rate-limit engine keys — one shared derivation across the three sites.
# ---------------------------------------------------------------------------
# Same matrix as tests/research_library/downloaders/
# test_direct_pdf_logging_sinks.py's ``pdf_download_`` cases: all four keys
# come from ``base.rate_limit_authority`` so they cannot drift apart.
_AUTHORITY_CASES = [
("https://origin.example:8443/paper", "origin.example:8443"),
(_URL, "origin.example:8443"),
# Leading whitespace survives to the fetch (``requests`` lstrips it when
# preparing), so the per-host bucket must survive too. The previous
# derivation re-parsed ``redact_url_for_log(url)`` — which renders
# ``?:// https`` for this input — and produced an empty authority,
# merging every whitespace-prefixed host into one bucket.
(" https://slow-host.example/paper", "slow-host.example"),
("http://[2001:db8::1]:8080/paper", "[2001:db8::1]:8080"),
(
f"https://alice:{_SECRET}@origin.example:99999/paper",
"invalid_authority",
),
(f"http://alice:{_SECRET}@[/paper", "invalid_authority"),
]
def _drive_html(url, _monkeypatch):
# Same (url, monkeypatch) signature as the two browser drivers so the
# parametrisation below can treat all three uniformly; the static path
# needs no module stubbing, so its monkeypatch argument goes unused.
# A 200 HTML response: these tests pin the engine-key derivation, not
# the fetch retry loop (a 503 here would apply rate limiting 3 times).
downloader, rate_tracker = _html_downloader(
response=SimpleNamespace(
status_code=200,
headers={"content-type": "text/html"},
text="<html>ok</html>",
)
)
with downloader:
downloader._fetch_html(url)
return rate_tracker
def _drive_crawl4ai(url, monkeypatch):
_install_crawl4ai(
monkeypatch, SimpleNamespace(success=True, html="<html>ok</html>")
)
downloader = PlaywrightHTMLDownloader()
# Close the robots.txt network seam: the real check fetches
# ``/robots.txt`` through SafeSession before the key is applied.
monkeypatch.setattr(downloader, "_robots_allows", lambda _url: True)
downloader.rate_tracker = MagicMock()
downloader.rate_tracker.apply_rate_limit.return_value = 0
with downloader:
downloader._fetch_with_crawl4ai(url)
return downloader.rate_tracker
def _drive_playwright(url, monkeypatch):
page = MagicMock()
page.content.return_value = "<html>ok</html>"
_install_playwright(monkeypatch, page)
downloader = PlaywrightHTMLDownloader()
# Close the robots.txt network seam: the real check fetches
# ``/robots.txt`` through SafeSession before the key is applied.
monkeypatch.setattr(downloader, "_robots_allows", lambda _url: True)
downloader.rate_tracker = MagicMock()
downloader.rate_tracker.apply_rate_limit.return_value = 0
with downloader:
downloader._fetch_with_playwright(url)
return downloader.rate_tracker
@pytest.mark.parametrize(("url", "expected_authority"), _AUTHORITY_CASES)
@pytest.mark.parametrize(
("driver", "prefix"),
[
(_drive_html, "html_download_"),
(_drive_crawl4ai, "crawl4ai_download_"),
(_drive_playwright, "playwright_download_"),
],
)
def test_engine_keys_come_from_the_shared_authority_helper(
monkeypatch, driver, prefix, url, expected_authority
):
rate_tracker = driver(url, monkeypatch)
rate_tracker.apply_rate_limit.assert_called_once_with(
f"{prefix}{expected_authority}"
)
key = rate_tracker.apply_rate_limit.call_args[0][0]
assert key == prefix + rate_limit_authority(url)
assert "alice" not in key
assert _SECRET not in key
assert not any(ch.isspace() or not ch.isprintable() for ch in key)
@pytest.mark.parametrize(
("driver", "prefix"),
[
(_drive_html, "html_download_"),
(_drive_crawl4ai, "crawl4ai_download_"),
(_drive_playwright, "playwright_download_"),
],
)
def test_whitespace_prefixed_urls_keep_per_host_buckets(
monkeypatch, driver, prefix
):
keys = [
driver(
f" https://{host}/paper", monkeypatch
).apply_rate_limit.call_args[0][0]
for host in ("slow-host.example", "fast-host.example")
]
assert keys == [
f"{prefix}slow-host.example",
f"{prefix}fast-host.example",
]