~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
"""
Guard-path tests for playwright_html that don't require a real browser.
Targets pure-logic branches in
src/local_deep_research/research_library/downloaders/playwright_html.py:
- Lines 57-62: _run_async ThreadPoolExecutor fallback when an event loop
is already running.
- Lines 117-119: Crawl4AI ImportError path in _fetch_with_crawl4ai.
- Lines 267-269: playwright ImportError path in _fetch_with_playwright.
The existing test_playwright_html.py covers inheritance and SPA-signal
detection but not these ImportError / event-loop fallback guards.
"""
import asyncio
import builtins
from unittest.mock import MagicMock, patch
import pytest
from local_deep_research.research_library.downloaders.playwright_html import (
PlaywrightHTMLDownloader,
_run_async,
)
from local_deep_research.security.egress.policy import (
Decision,
PolicyDeniedError,
)
class TestRunAsyncEventLoopHandling:
"""_run_async detects whether an event loop is already running and
chooses between asyncio.run() and a ThreadPoolExecutor fallback."""
def test_no_running_loop_uses_asyncio_run(self):
async def make_value():
return 7
# Called from plain sync code — no running loop — takes the
# asyncio.run() branch.
result = _run_async(make_value())
assert result == 7
def test_fallback_via_threadpool_inside_active_loop(self):
async def make_value():
await asyncio.sleep(0)
return 42
async def driver():
# Inside a running loop: _run_async must spawn a thread that
# calls asyncio.run on its own loop.
return _run_async(make_value(), timeout=5)
result = asyncio.run(driver())
assert result == 42
def _import_blocker(blocked_prefix):
"""Return a fake __import__ that raises ImportError for blocked_prefix."""
original_import = builtins.__import__
def fake_import(name, globals=None, locals=None, fromlist=(), level=0):
if name == blocked_prefix or name.startswith(f"{blocked_prefix}."):
raise ImportError(f"simulated: {blocked_prefix} not installed")
return original_import(name, globals, locals, fromlist, level)
return fake_import
class TestCrawl4aiImportErrorGuard:
"""When crawl4ai is not installed, _fetch_with_crawl4ai returns None."""
def test_returns_none_when_crawl4ai_missing(self):
dl = PlaywrightHTMLDownloader(timeout=5)
try:
with patch.object(
builtins, "__import__", side_effect=_import_blocker("crawl4ai")
):
result = dl._fetch_with_crawl4ai("https://example.com")
assert result is None
finally:
dl.close()
class TestPlaywrightImportErrorGuard:
"""When playwright is not installed, _fetch_with_playwright returns None
and logs a warning rather than raising."""
def test_returns_none_when_playwright_missing(self):
dl = PlaywrightHTMLDownloader(timeout=5)
try:
# Mock rate_tracker to avoid needing the full rate-limit setup.
dl.rate_tracker = MagicMock()
dl.rate_tracker.apply_rate_limit.return_value = 0
# Stub the guarded robots.txt check so this unit test does not
# fetch https://example.com/robots.txt.
dl._robots_allows = MagicMock(return_value=True)
with patch.object(
builtins,
"__import__",
side_effect=_import_blocker("playwright"),
):
result = dl._fetch_with_playwright("https://example.com")
assert result is None
finally:
dl.close()
_PRIVATE_URL = "https://example.com/private"
_ROBOTS_DISALLOW_PRIVATE = "User-agent: *\nDisallow: /private\n"
def _assert_playwright_not_started(sync_pw):
"""The patched Playwright entry point never launched or opened a page."""
sync_pw.assert_not_called()
browser = (
sync_pw.return_value.start.return_value.chromium.launch.return_value
)
browser.new_page.assert_not_called()
class TestPlainPlaywrightRobotsCheck:
"""The plain-Playwright fetcher honors robots.txt before any browser work.
Crawl4AI refuses a disallowed URL and does not fall through. When
Crawl4AI is missing, or a caller invokes the Playwright fetcher
directly, the same disallow must skip the browser and the rate limit.
"""
def _downloader(self):
dl = PlaywrightHTMLDownloader(timeout=5)
dl.rate_tracker = MagicMock()
dl.rate_tracker.apply_rate_limit.return_value = 0
return dl
def test_disallow_returns_none_without_browser_or_rate_limit(self):
"""A robots.txt Disallow stops the plain-Playwright fetch."""
dl = self._downloader()
robots = MagicMock(status_code=200, text=_ROBOTS_DISALLOW_PRIVATE)
try:
with (
patch.object(dl.session, "get", return_value=robots) as get,
patch("playwright.sync_api.sync_playwright") as sync_pw,
):
result = dl._fetch_with_playwright(_PRIVATE_URL)
finally:
dl.close()
assert result is None
get.assert_called_once_with(
"https://example.com/robots.txt",
timeout=5,
allow_redirects=True,
)
dl.rate_tracker.apply_rate_limit.assert_not_called()
_assert_playwright_not_started(sync_pw)
def test_fetch_html_fallback_honors_disallow_when_crawl4ai_missing(self):
"""With Crawl4AI unavailable, the fallback still obeys robots.txt."""
dl = self._downloader()
robots = MagicMock(status_code=200, text=_ROBOTS_DISALLOW_PRIVATE)
try:
with (
patch("playwright.sync_api.sync_playwright") as sync_pw,
patch.object(dl.session, "get", return_value=robots) as get,
patch.object(
builtins,
"__import__",
side_effect=_import_blocker("crawl4ai"),
),
):
result = dl._fetch_html(_PRIVATE_URL)
finally:
dl.close()
assert result is None
get.assert_called_once_with(
"https://example.com/robots.txt",
timeout=5,
allow_redirects=True,
)
dl.rate_tracker.apply_rate_limit.assert_not_called()
_assert_playwright_not_started(sync_pw)
def test_policy_denied_propagates_without_browser_or_rate_limit(self):
"""A policy denial from the robots check is not swallowed."""
dl = self._downloader()
denied = PolicyDeniedError(
Decision(False, "scope_mismatch"), _PRIVATE_URL
)
dl._robots_allows = MagicMock(side_effect=denied)
try:
with patch("playwright.sync_api.sync_playwright") as sync_pw:
with pytest.raises(PolicyDeniedError) as raised:
dl._fetch_with_playwright(_PRIVATE_URL)
finally:
dl.close()
assert raised.value.decision.reason == "scope_mismatch"
dl.rate_tracker.apply_rate_limit.assert_not_called()
_assert_playwright_not_started(sync_pw)