Skip to content

~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.

7.4 KB
Raw Blame
"""Record-shape tests for the Playwright/Crawl4AI guard log events.

The browser route guards, the Set-Cookie parser, the per-hop cookie
re-derivation, and the guarded robots.txt check all log their failure
branches with ``logger.opt(exception=False).debug(...)``: a static
message with NO exception attached. An attached exception would render
the caught error's value — network errors embed the raw, possibly
credentialed URL — into every sink. These tests pin the intended final
record shape: static message, ``record["exception"] is None``, and no
stray ``extra`` fields (the pre-PR ``exc_info=True`` kwarg form stored
``exc_info: True`` in ``extra`` while pretending to attach a traceback).
"""

from __future__ import annotations

import asyncio
from types import SimpleNamespace
from unittest.mock import MagicMock, patch

import pytest
from loguru import logger

from local_deep_research.research_library.downloaders.playwright_html import (
    PlaywrightHTMLDownloader,
)
from local_deep_research.security import ssrf_validator

_SECRET = "credential-do-not-log"
_EXCEPTION_TEXT = "network-error-do-not-log"
_URL = (
    f"https://alice:{_SECRET}@origin.example:8443/private/%2Fencoded"
    f"?token={_SECRET}&next=%2Felsewhere#fragment"
)


@pytest.fixture
def record_sink():
    records = []
    logger.enable("local_deep_research")
    sink_id = logger.add(
        lambda message: records.append(message.record),
        level="DEBUG",
        diagnose=False,
    )
    try:
        yield records
    finally:
        logger.remove(sink_id)
        logger.disable("local_deep_research")


def _event_record(records, event):
    matched = [r for r in records if event in r["message"]]
    assert len(matched) == 1, (
        f"expected exactly one {event!r} record, got {len(matched)}"
    )
    return matched[0]


def _assert_static_no_attachment(records, event):
    record = _event_record(records, event)
    assert record["exception"] is None, (
        f"{event!r} must not attach an exception: {record['exception']!r}"
    )
    assert record["extra"] == {}, (
        f"{event!r} must not carry extra fields: {record['extra']!r}"
    )
    for marker in (_EXCEPTION_TEXT, _SECRET, "Traceback"):
        assert marker not in record["message"]


def _guard_error():
    return RuntimeError(f"{_EXCEPTION_TEXT}: {_URL}")


def _redirect_response():
    return SimpleNamespace(
        status=302,
        headers={"location": "https://origin.example:8443/next"},
    )


class _SyncRoute:
    """Fake Playwright route for the sync (plain-Playwright) guard."""

    def __init__(self, *, fetch_result=None, fetch_error=None, request=None):
        self.request = request if request is not None else MagicMock()
        self.request.url = _URL
        self.request.method = "GET"
        self._fetch_result = fetch_result
        self._fetch_error = fetch_error
        self.aborts = []

    def fetch(self, **_kwargs):
        if self._fetch_error is not None:
            raise self._fetch_error
        return self._fetch_result

    def abort(self, reason):
        self.aborts.append(reason)

    def fulfill(self, **_kwargs):
        raise AssertionError("terminal hop must not be reached here")


class _AsyncRoute:
    """Fake Playwright route for the async (Crawl4AI) guard."""

    def __init__(self, *, fetch_result=None, fetch_error=None):
        self.request = MagicMock()
        self.request.url = _URL
        self.request.method = "GET"
        self._fetch_result = fetch_result
        self._fetch_error = fetch_error
        self.aborts = []

    async def fetch(self, **_kwargs):
        if self._fetch_error is not None:
            raise self._fetch_error
        return self._fetch_result

    async def abort(self, reason):
        self.aborts.append(reason)

    async def fulfill(self, **_kwargs):
        raise AssertionError("terminal hop must not be reached here")


def _hop_request():
    """A request whose ``headers`` blow up once a cookie override exists.

    Iteration 1 of the guard never reads ``request.headers`` (the cookie
    override is still ``None``); on the redirect hop the override becomes
    non-``None`` and ``_apply_hop_cookie_header`` iterates
    ``request.headers.items()`` — which raises, pinning the guarded
    hop-headers failure branch.
    """
    request = MagicMock()
    request.url = _URL
    request.method = "GET"
    request.headers.items.side_effect = _guard_error()
    return request


def _run_guard(downloader, route):
    with patch.object(ssrf_validator, "validate_url", return_value=True):
        downloader._playwright_route_guard(route)


def _run_async_guard(downloader, route):
    with patch.object(ssrf_validator, "validate_url", return_value=True):
        asyncio.run(downloader._crawl4ai_route_guard(route))


def test_sync_guard_fetch_failure_logs_static_debug(record_sink):
    route = _SyncRoute(fetch_error=_guard_error())
    _run_guard(PlaywrightHTMLDownloader(), route)

    assert route.aborts == ["failed"]
    _assert_static_no_attachment(
        record_sink, "Playwright: guarded fetch failed"
    )


def test_sync_guard_hop_and_cookie_failures_log_static_debug(record_sink):
    # The redirect hop makes the cookie re-derivation read the context jar
    # (which raises), then the next hop's header rebuild read fail too.
    route = _SyncRoute(
        fetch_result=_redirect_response(), request=_hop_request()
    )
    route.request.frame.page.context.cookies.side_effect = _guard_error()
    _run_guard(PlaywrightHTMLDownloader(), route)

    assert route.aborts == ["failed"]
    _assert_static_no_attachment(
        record_sink, "Playwright: failed to re-apply redirect-hop cookies"
    )
    _assert_static_no_attachment(
        record_sink, "Playwright: failed to build guarded hop headers"
    )


def test_crawl4ai_guard_fetch_failure_logs_static_debug(record_sink):
    route = _AsyncRoute(fetch_error=_guard_error())
    _run_async_guard(PlaywrightHTMLDownloader(), route)

    assert route.aborts == ["failed"]
    _assert_static_no_attachment(record_sink, "Crawl4AI: guarded fetch failed")


def test_crawl4ai_guard_hop_and_cookie_failures_log_static_debug(record_sink):
    route = _AsyncRoute(fetch_result=_redirect_response())
    route.request.headers.items.side_effect = _guard_error()
    route.request.frame.page.context.cookies.side_effect = _guard_error()
    _run_async_guard(PlaywrightHTMLDownloader(), route)

    assert route.aborts == ["failed"]
    _assert_static_no_attachment(
        record_sink, "Crawl4AI: failed to re-apply redirect-hop cookies"
    )
    _assert_static_no_attachment(
        record_sink, "Crawl4AI: failed to build guarded hop headers"
    )


def test_set_cookie_parse_failure_logs_static_debug(record_sink):
    # A non-str Set-Cookie value makes the parser raise; the guard must log
    # a static debug event and skip the cookie, not attach the exception.
    response = SimpleNamespace(
        headers_array=[{"name": "set-cookie", "value": 123}]
    )

    cookies = PlaywrightHTMLDownloader._cookies_from_response(response, _URL)

    assert cookies == []
    _assert_static_no_attachment(
        record_sink, "Browser guard: failed to parse a Set-Cookie header"
    )


def test_robots_check_failure_fails_open_with_static_debug(record_sink):
    downloader = PlaywrightHTMLDownloader()
    downloader.session = MagicMock()
    downloader.session.get.side_effect = _guard_error()

    assert downloader._robots_allows(_URL) is True

    _assert_static_no_attachment(
        record_sink, "Guarded robots.txt check failed open"
    )