~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
"""Tests for PlaywrightHTMLDownloader and AutoHTMLDownloader.
Covers: SPA signal detection, inheritance, fallback logic.
Playwright browser tests are not run here (require browser install);
these test the logic around when to use Playwright.
"""
import asyncio
from unittest.mock import AsyncMock, MagicMock, Mock, patch
import pytest
from local_deep_research.research_library.downloaders.base import (
ContentType,
DownloadResult,
)
from local_deep_research.research_library.downloaders.playwright_html import (
AutoHTMLDownloader,
PlaywrightHTMLDownloader,
SPA_SIGNALS,
)
from local_deep_research.research_library.downloaders.html import HTMLDownloader
LONG_PARA = (
"This is a sufficiently long paragraph for content extraction "
"testing purposes that needs to be over two hundred characters long "
"to pass the minimum content length threshold used by the auto downloader."
)
class TestPlaywrightHTMLDownloaderInheritance:
"""Verify PlaywrightHTMLDownloader inherits from HTMLDownloader."""
def test_is_subclass_of_html_downloader(self):
assert issubclass(PlaywrightHTMLDownloader, HTMLDownloader)
def test_has_language(self):
dl = PlaywrightHTMLDownloader(timeout=5)
assert dl.language == "English"
dl.close()
def test_can_handle_http(self):
dl = PlaywrightHTMLDownloader(timeout=5)
assert dl.can_handle("https://example.com") is True
assert dl.can_handle("ftp://example.com") is False
dl.close()
class TestAutoHTMLDownloaderInheritance:
"""Verify AutoHTMLDownloader inherits from HTMLDownloader."""
def test_is_subclass_of_html_downloader(self):
assert issubclass(AutoHTMLDownloader, HTMLDownloader)
def test_has_language(self):
dl = AutoHTMLDownloader(timeout=5)
assert dl.language == "English"
dl.close()
class TestSPASignalDetection:
"""Tests for SPA/JS framework detection heuristics."""
def test_detects_react_root(self):
html = '<html><body><div id="root"></div></body></html>'
assert AutoHTMLDownloader._has_spa_signals(html) is True
def test_detects_vue_app(self):
html = '<html><body><div id="app"></div></body></html>'
assert AutoHTMLDownloader._has_spa_signals(html) is True
def test_detects_nextjs(self):
html = '<html><body><div id="__next"><script>__NEXT_DATA__</script></div></body></html>'
assert AutoHTMLDownloader._has_spa_signals(html) is True
def test_detects_noscript_warning(self):
html = "<html><body><noscript>You need to enable JavaScript to run this app.</noscript></body></html>"
assert AutoHTMLDownloader._has_spa_signals(html) is True
def test_no_signals_in_normal_html(self):
html = (
f"<html><body><article><p>{LONG_PARA}</p></article></body></html>"
)
assert AutoHTMLDownloader._has_spa_signals(html) is False
def test_no_signals_in_empty_html(self):
assert AutoHTMLDownloader._has_spa_signals("") is False
def test_all_known_signals_detected(self):
for signal in SPA_SIGNALS:
html = f"<html><body>{signal}</body></html>"
assert AutoHTMLDownloader._has_spa_signals(html) is True, (
f"Signal not detected: {signal}"
)
class TestAutoDownloaderFallbackLogic:
"""Tests for the static-first, Playwright-fallback logic."""
def test_static_success_skips_playwright(self):
"""When static fetch returns enough content, Playwright is not used."""
dl = AutoHTMLDownloader(timeout=5, min_content_length=50)
html = f"<html><head><title>Test</title></head><body><p>{LONG_PARA}</p></body></html>"
with patch.object(HTMLDownloader, "_fetch_html", return_value=html):
result = dl.download("https://example.com")
assert result is not None
assert len(result) > 50
# Playwright downloader should not have been initialized
assert dl._playwright_downloader is None
dl.close()
def test_short_content_with_spa_triggers_playwright(self):
"""When content is short and SPA signals present, tries Playwright."""
dl = AutoHTMLDownloader(
timeout=5, min_content_length=200, enable_js_rendering=True
)
spa_html = '<html><body><div id="root"></div><noscript>You need to enable JavaScript</noscript></body></html>'
pw_content = f"<html><body><p>{LONG_PARA}</p></body></html>"
# Mock the session.get to return a SPA challenge page
mock_response = Mock()
mock_response.status_code = 403
mock_response.text = spa_html
dl.session = Mock(egress_context=None)
dl.session.get.return_value = mock_response
mock_pw = Mock()
mock_pw.download.return_value = pw_content.encode("utf-8")
dl._playwright_downloader = mock_pw
result = dl.download("https://spa-app.com")
assert result is not None
assert LONG_PARA.encode("utf-8") in result
mock_pw.download.assert_called_once()
dl.close()
class TestHostRateLimitBudget:
"""The chain owner caps per-URL requests to a rate-limiting host.
A host answering 429/503 to everything must see only the static
leg's budgeted attempts (3) for one URL — no probe GET, no browser
navigation — and the terminal outcome must classify as rate-limited.
"""
# ``download`` is the bytes path both extraction pipelines call.
@pytest.mark.parametrize("entry", ["download", "download_with_result"])
def test_rate_limited_host_gets_no_probe_or_navigation(self, entry):
"""429 on every request: 3 static attempts, then the chain stops."""
dl = AutoHTMLDownloader(
timeout=5, min_content_length=200, enable_js_rendering=True
)
calls = []
def _rate_limited_response(url, **kwargs):
calls.append(url)
response = Mock()
response.status_code = 429
response.headers = {}
response.text = ""
response.url = url
return response
session = Mock(egress_context=None)
session.get.side_effect = _rate_limited_response
dl.session = session
# Neutralize adaptive waits so the retried attempts don't sleep.
dl.rate_tracker = Mock()
dl.rate_tracker.apply_rate_limit.return_value = 0.0
pw = Mock()
pw.download.return_value = None
pw.download_with_result.return_value = DownloadResult(
content=None, is_success=False
)
dl._playwright_downloader = pw
try:
result = getattr(dl, entry)(
"https://rate-limited.example.com/paper"
)
finally:
dl.close()
# The host-level budget: exactly the static leg's 3 attempts —
# no probe GET (would be a 4th session.get), no navigation.
assert len(calls) == 3
pw.download.assert_not_called()
pw.download_with_result.assert_not_called()
if entry == "download":
assert result is None
else:
# The terminal outcome classifies as rate-limited.
assert "rate-limited" in (result.skip_reason or "").lower()
def _shared_host_downloader(responses):
"""One downloader reused across URLs, over a session stub keyed by URL.
``responses`` maps each URL to ``(status, html)``. Every request to a
URL is answered the same way, so a 429 URL exhausts the static leg's
three attempts. ``min_content_length`` is far above anything the stub
pages extract to, so every successful fetch is a short one. Returns
``(downloader, requested_urls, browser_stub)``.
"""
dl = AutoHTMLDownloader(
timeout=5, min_content_length=10_000, enable_js_rendering=True
)
requested = []
def _answer(url, **kwargs):
requested.append(url)
status, html = responses[url]
response = Mock()
response.status_code = status
response.headers = {"content-type": "text/html; charset=utf-8"}
response.text = html
response.url = url
return response
session = Mock(egress_context=None)
session.get.side_effect = _answer
dl.session = session
# Neutralize adaptive waits so the retried attempts don't sleep.
dl.rate_tracker = Mock()
dl.rate_tracker.apply_rate_limit.return_value = 0.0
pw = Mock()
pw.download.return_value = None
pw.download_with_result.return_value = DownloadResult(
content=None, is_success=False
)
dl._playwright_downloader = pw
return dl, requested, pw
class TestHostRateLimitBudgetSharedInstance:
"""A host marked by one URL never rewrites a later URL's own result.
``ContentFetcher`` caches one downloader per URL type and
``batch_fetch_and_extract`` shares one across its batch, so the
host-level mark outlives the URL that set it. It only skips the probe
and browser legs for later URLs on that host. Whatever a later URL's
own static fetch produced (a short success, a 404, an unsupported
request) comes back unchanged from both entry points, and only a URL
whose own static fetch gave up on 429/503 is reported as rate-limited.
"""
HOST = "https://shared-host.example.com"
LIMITED = f"{HOST}/limited"
LIMITED_AGAIN = f"{HOST}/limited-again"
SHORT = f"{HOST}/short"
MISSING = f"{HOST}/missing"
RESPONSES = {
LIMITED: (429, ""),
LIMITED_AGAIN: (429, ""),
SHORT: (
200,
f"<html><body><article><p>{LONG_PARA}</p></article></body></html>",
),
MISSING: (404, ""),
}
@classmethod
def _host_marked_by_first_url(cls, entry):
"""A shared downloader whose host an earlier URL already marked."""
dl, requested, pw = _shared_host_downloader(cls.RESPONSES)
getattr(dl, entry)(cls.LIMITED)
# Terminal 429: exactly the static leg's three attempts.
assert requested == [cls.LIMITED] * 3
return dl, requested, pw
@pytest.mark.parametrize("entry", ["download", "download_with_result"])
def test_later_url_keeps_its_short_success(self, entry):
"""A short 200 is below min_content_length but still the answer."""
dl, requested, pw = self._host_marked_by_first_url(entry)
try:
result = getattr(dl, entry)(self.SHORT)
finally:
dl.close()
if entry == "download_with_result":
assert result.is_success
assert result.skip_reason is None
result = result.content
assert result is not None
assert b"sufficiently long paragraph" in result
# The later URL gets its static leg only: no probe, no navigation.
assert requested[3:] == [self.SHORT]
pw.download.assert_not_called()
pw.download_with_result.assert_not_called()
@pytest.mark.parametrize(
("url", "content_type", "reason", "later_requests"),
[
(
MISSING,
ContentType.TEXT,
"Failed to fetch HTML content from URL",
[MISSING],
),
(
SHORT,
ContentType.PDF,
"HTML downloader does not support PDF downloads",
[],
),
],
ids=["404", "pdf-request"],
)
def test_later_url_keeps_its_own_failure_reason(
self, url, content_type, reason, later_requests
):
"""A later failure is its own, not the host's history."""
dl, requested, pw = self._host_marked_by_first_url(
"download_with_result"
)
try:
result = dl.download_with_result(url, content_type)
finally:
dl.close()
assert not result.is_success
assert result.skip_reason == reason
assert requested[3:] == later_requests
pw.download.assert_not_called()
pw.download_with_result.assert_not_called()
@pytest.mark.parametrize("entry", ["download", "download_with_result"])
def test_later_url_that_is_itself_rate_limited_reports_it(self, entry):
"""Its own 429/503 give-up is reported even on an already-marked host."""
dl, requested, pw = self._host_marked_by_first_url(entry)
try:
result = getattr(dl, entry)(self.LIMITED_AGAIN)
finally:
dl.close()
# Its own three static attempts and nothing more.
assert requested[3:] == [self.LIMITED_AGAIN] * 3
pw.download.assert_not_called()
pw.download_with_result.assert_not_called()
if entry == "download":
assert result is None
else:
assert "rate-limited" in (result.skip_reason or "").lower()
class TestPlaywrightDownloaderClose:
"""Tests for resource cleanup."""
def test_close_without_browser(self):
"""close() works when browser was never started."""
dl = PlaywrightHTMLDownloader(timeout=5)
dl.close() # Should not raise
def test_auto_close_without_playwright(self):
"""close() works when Playwright fallback was never used."""
dl = AutoHTMLDownloader(timeout=5)
dl.close() # Should not raise
class TestAutoDownloaderJSRenderingToggle:
"""Regression tests for ``enable_js_rendering`` (issue #3826).
The default Docker production image ships without Chromium, so the
JS-rendering fallback must be skippable to avoid wasted work and
confusing tracebacks on every fetch. With the toggle off, the
downloader must never construct or invoke its Playwright fallback,
even when content is short or SPA signals are present.
"""
def test_disabled_skips_js_when_content_short(self):
"""JS off + short content → static result returned, no Playwright."""
dl = AutoHTMLDownloader(
timeout=5, min_content_length=200, enable_js_rendering=False
)
short_html = "<html><body><p>tiny</p></body></html>"
with patch.object(
HTMLDownloader, "_fetch_html", return_value=short_html
):
with patch.object(
AutoHTMLDownloader,
"_get_playwright_downloader",
side_effect=AssertionError(
"JS fallback must not run when disabled"
),
):
result = dl.download("https://example.com")
# Static result is returned (or None) without raising
assert dl._playwright_downloader is None
# Result is the static (short) extraction result
assert result is None or len(result) < 200
dl.close()
def test_disabled_skips_js_with_spa_signals(self):
"""JS off + SPA signals → static result returned, no Playwright."""
dl = AutoHTMLDownloader(
timeout=5, min_content_length=200, enable_js_rendering=False
)
spa_html = (
'<html><body><div id="root"></div>'
"<noscript>You need to enable JavaScript</noscript>"
"</body></html>"
)
mock_response = Mock()
mock_response.status_code = 403
mock_response.text = spa_html
dl.session = Mock(egress_context=None)
dl.session.get.return_value = mock_response
with patch.object(
AutoHTMLDownloader,
"_get_playwright_downloader",
side_effect=AssertionError(
"JS fallback must not run when disabled"
),
):
result = dl.download("https://spa-app.com")
# Static path returned None (SPA shell), but no Playwright was used
assert dl._playwright_downloader is None
assert result is None or len(result) < 200
dl.close()
def test_disabled_also_skips_in_download_with_result(self):
"""download_with_result must honor the toggle as well."""
dl = AutoHTMLDownloader(
timeout=5, min_content_length=200, enable_js_rendering=False
)
short_html = "<html><body><p>tiny</p></body></html>"
with patch.object(
HTMLDownloader, "_fetch_html", return_value=short_html
):
with patch.object(
AutoHTMLDownloader,
"_get_playwright_downloader",
side_effect=AssertionError(
"JS fallback must not run when disabled"
),
):
result = dl.download_with_result("https://example.com")
# Static DownloadResult was returned without invoking JS
assert dl._playwright_downloader is None
assert result is not None
dl.close()
def test_enabled_still_falls_back_to_js(self):
"""Regression guard: when explicitly enabled, the JS fallback
is still triggered for short SPA pages (existing behaviour)."""
dl = AutoHTMLDownloader(
timeout=5, min_content_length=200, enable_js_rendering=True
)
spa_html = (
'<html><body><div id="root"></div>'
"<noscript>You need to enable JavaScript</noscript>"
"</body></html>"
)
pw_content = f"<html><body><p>{LONG_PARA}</p></body></html>"
mock_response = Mock()
mock_response.status_code = 403
mock_response.text = spa_html
dl.session = Mock(egress_context=None)
dl.session.get.return_value = mock_response
mock_pw = Mock()
mock_pw.download.return_value = pw_content.encode("utf-8")
dl._playwright_downloader = mock_pw
result = dl.download("https://spa-app.com")
assert result is not None
mock_pw.download.assert_called_once()
dl.close()
def test_default_is_false_for_direct_callers(self):
"""The constructor defaults to ``enable_js_rendering=False``,
consistent with every other layer in the stack
(``ContentFetcher``, ``fetch_and_extract``, ``FullSearchResults``,
and the user-facing setting). Callers that explicitly need JS
rendering opt in by passing ``enable_js_rendering=True``.
"""
dl = AutoHTMLDownloader(timeout=5)
assert dl.enable_js_rendering is False
dl.close()
class TestAutoDownloaderPrivateIpSessionSync:
"""AutoHTMLDownloader must keep its static-fetch SafeSession in
lockstep with the ``allow_private_ips`` gate (the browser child
already receives it)."""
def test_forwards_private_ip_opt_in_to_session(self):
dl = AutoHTMLDownloader(allow_private_ips=True)
try:
assert dl.session.allow_private_ips is True
finally:
dl.close()
def test_session_blocks_private_ips_by_default(self):
dl = AutoHTMLDownloader()
try:
assert dl.session.allow_private_ips is False
finally:
dl.close()
def test_forwards_block_link_local_to_session(self):
"""The link-local carve-out reaches the static-fetch SafeSession
so a redirect hop into 169.254.0.0/16 is refused under the grant."""
dl = AutoHTMLDownloader(allow_private_ips=True, block_link_local=True)
try:
assert dl.block_link_local is True
assert dl.session.block_link_local is True
finally:
dl.close()
def test_session_block_link_local_off_by_default(self):
dl = AutoHTMLDownloader()
try:
assert dl.block_link_local is False
assert dl.session.block_link_local is False
finally:
dl.close()
class _RedirectHop:
"""Stand-in for the Playwright APIResponse of a 302 hop, with the
attributes the route guards read (status, headers, headers_array)."""
def __init__(self, location: str):
self.status = 302
self.headers = {"location": location}
self.headers_array = [{"name": "location", "value": location}]
class TestAutoDownloaderBlockLinkLocalReachesJsRoute:
"""``block_link_local`` must reach the lazily-built Playwright child and
both of its route guards, so under an open grant the JS-rendering
fallback refuses a redirect hop into the link-local range exactly like
the static SafeSession route does. Real validator, no browser: the entry
hop is an RFC1918 literal (allowed under the grant, no DNS) that 302s to
a link-local address outside ``ALWAYS_BLOCKED_METADATA_IPS``, so only
the threaded flag can refuse it."""
ENTRY_URL = "http://10.0.0.5/wiki"
LINK_LOCAL_URL = "http://169.254.10.5/latest/meta-data"
@pytest.mark.parametrize("guard", ["playwright", "crawl4ai"])
def test_js_route_refuses_redirect_into_link_local_under_open_grant(
self, guard
):
dl = AutoHTMLDownloader(allow_private_ips=True, block_link_local=True)
try:
child = dl._get_playwright_downloader()
assert child.allow_private_ips is True
assert child.block_link_local is True
hop = _RedirectHop(self.LINK_LOCAL_URL)
route = MagicMock()
route.request.url = self.ENTRY_URL
route.request.method = "GET"
route.request.resource_type = "document"
if guard == "playwright":
route.fetch = MagicMock(return_value=hop)
child._playwright_route_guard(route)
route.abort.assert_called_once_with("blockedbyclient")
route.fulfill.assert_not_called()
else:
route.fetch = AsyncMock(return_value=hop)
route.fulfill = AsyncMock()
route.abort = AsyncMock()
route.fallback = AsyncMock()
asyncio.run(child._crawl4ai_route_guard(route))
route.abort.assert_awaited_once_with("blockedbyclient")
route.fulfill.assert_not_awaited()
finally:
dl.close()
# The entry hop was fetched once; the link-local hop was refused
# before it was fetched.
assert route.fetch.call_count == 1