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.

10.3 KB
Raw Blame
"""Shared support for fetch-tool sink-leak tests.

Provides constants, mock builders, a real-sink harness context manager,
and assertion helpers used by both ``test_fetch_success_sinks`` and
``test_fetch_error_sinks``.
"""

from __future__ import annotations

import io
import re
from contextlib import contextmanager
from types import SimpleNamespace
from unittest.mock import MagicMock, patch

from loguru import logger

from local_deep_research.utilities import log_utils
from local_deep_research.utilities.log_utils import (
    database_sink,
    frontend_progress_sink,
)

# ---------------------------------------------------------------------------
# Distinctive markers — any leak is unambiguous in assertion failures.
# ---------------------------------------------------------------------------

FOCUS = "UNIQUE_FOCUS_MARKER_xyz789"
QUERY = "UNIQUE_QUERY_MARKER_abc456"
TITLE = "UNIQUE_TITLE_MARKER_def012"
BODY = "UNIQUE_BODY_CONTENT_markerghi345"
SUMMARY = "UNIQUE_SUMMARY_TEXT_markerjkl678"
CRED_SECRET = "CRED_SECRET_mno901"
QUERY_TOKEN = "QUERY_TOKEN_pqr234"
EXC_TEXT = "UNIQUE_EXCEPTION_TEXT_stu567"

CREDENTIALED_URL = (
    f"https://user:{CRED_SECRET}@safe.example:8443/secretpath?key={QUERY_TOKEN}"
)
REDACTED_ORIGIN = "https://safe.example:8443"

ENCODED_URL = (
    f"https://user%40domain:{CRED_SECRET}@safe.example:8443"
    f"/secretpath?key={QUERY_TOKEN}"
)
DOUBLE_ENCODED_URL = (
    f"https://user%2540domain:{CRED_SECRET}@safe.example:8443"
    f"/secretpath?key={QUERY_TOKEN}"
)

FORBIDDEN = [
    FOCUS,
    QUERY,
    TITLE,
    BODY,
    SUMMARY,
    CRED_SECRET,
    QUERY_TOKEN,
    EXC_TEXT,
    "secretpath",
    # Userinfo shapes exactly as they appear in the three URLs above:
    # ``user:<cred>@`` (CREDENTIALED_URL) and the percent-encoded
    # ``user%40domain`` / ``user%2540domain`` forms (ENCODED_URL /
    # DOUBLE_ENCODED_URL). A marker that no input can produce asserts
    # nothing.
    "user:",
    "user%40domain",
    "user%2540domain",
]

FETCH_EVENT_RE = re.compile(
    r"^\[FETCH\] mode=(?P<mode>\S+) status=(?P<status>\S+)"
    r" url=(?P<url>\S+)"
    r"(?: page_chars=(?P<page_chars>\d+))?"
    r"(?: summary_chars=(?P<summary_chars>\d+))?\s*$"
)


# ---------------------------------------------------------------------------
# Sink harness — real Loguru sinks, patched external seams.
# ---------------------------------------------------------------------------


@contextmanager
def create_sink_harness(tmp_path):
    """Configure real Loguru sinks + patch external seams.

    Adds five handlers (stderr StringIO with ``enqueue=True``, file,
    ``database_sink``, ``frontend_progress_sink``, capture) on top of
    whatever handlers already exist — does **not** call
    ``logger.remove()`` globally.  Only the added handler IDs are
    removed in the ``finally`` block.

    Yields a SimpleNamespace:
        db_entries, socket_payloads, records, stderr, file_path, flush
    """
    stderr_buf = io.StringIO()
    log_file = tmp_path / "fetch_test.log"

    db_entries = []
    socket_payloads = []
    records = []

    def capture_sink(message):
        records.append(message.record)

    handler_ids = [
        logger.add(
            stderr_buf,
            level="DEBUG",
            format="{message}",
            diagnose=False,
            enqueue=True,
        ),
        logger.add(
            str(log_file), level="DEBUG", format="{message}", diagnose=False
        ),
        logger.add(database_sink, level="DEBUG", diagnose=False),
        logger.add(frontend_progress_sink, level="DEBUG", diagnose=False),
        logger.add(capture_sink, level="DEBUG", diagnose=False),
    ]
    # ``src/local_deep_research/__init__.py`` disables the package's loguru
    # logging by default; re-enable it for the harness and restore that
    # default in the ``finally`` below (same pairing as ``loguru_caplog`` in
    # tests/conftest.py).
    logger.enable("local_deep_research")

    def _capture_emit(event, research_id, payload, **kw):
        socket_payloads.append((event, research_id, payload))

    try:
        # Post-FastAPI-migration, `database_sink` no longer gates on Flask's
        # `has_app_context()`; it instead checks whether it is running on the
        # event loop or off the main thread (see log_utils.database_sink).
        # Under plain synchronous pytest execution (no event loop, thread
        # name "MainThread") it takes the *direct-write* branch and calls
        # `_write_log_to_database` synchronously rather than enqueuing via
        # `_log_queue` -- so patching `_log_queue` (the pre-migration
        # strategy, back when Flask's `has_app_context()` was False in tests
        # and always forced the queueing branch) is no longer on the
        # exercised path. Patch `_write_log_to_database` directly instead,
        # matching the current synchronous code path.
        with patch.object(
            log_utils, "_write_log_to_database", db_entries.append
        ):
            # `frontend_progress_sink` no longer goes through a Flask-era
            # `SocketIOService` class; it calls the module-level
            # `emit_to_subscribers` function imported into log_utils.
            with patch.object(
                log_utils, "emit_to_subscribers", side_effect=_capture_emit
            ):
                with logger.contextualize(
                    research_id="test-rid", username="test-user"
                ):
                    yield SimpleNamespace(
                        db_entries=db_entries,
                        socket_payloads=socket_payloads,
                        records=records,
                        stderr=stderr_buf,
                        file_path=log_file,
                        flush=lambda: logger.complete(),
                    )
    finally:
        logger.complete()
        for hid in handler_ids:
            try:
                logger.remove(hid)
            except ValueError:
                pass
        logger.disable("local_deep_research")


# ---------------------------------------------------------------------------
# Mock builders
# ---------------------------------------------------------------------------


def fetcher_cm(*, status="success", title="", content=""):
    """Build a ContentFetcher context-manager mock."""
    fetcher = MagicMock()
    fetcher.fetch.return_value = {
        "status": status,
        "title": title,
        "content": content,
    }
    cm = MagicMock()
    cm.__enter__.return_value = fetcher
    cm.__exit__.return_value = False
    return cm


def model_returning(text: str):
    """Model whose ``invoke`` returns a message with ``.content == text``."""
    msg = MagicMock()
    msg.content = text
    model = MagicMock()
    model.invoke.return_value = msg
    return model


def model_raising(exc: Exception):
    """Model whose ``invoke`` raises."""
    model = MagicMock()
    model.invoke.side_effect = exc
    return model


# ---------------------------------------------------------------------------
# Extraction helpers
# ---------------------------------------------------------------------------


def fetch_events_from(records):
    """Return only records whose message is a [FETCH] event."""
    return [r for r in records if r["message"].lstrip().startswith("[FETCH]")]


def fetch_db_messages(db_entries):
    """Extract [FETCH] messages from DB queue entries."""
    return [e["message"] for e in db_entries if "[FETCH]" in e["message"]]


def fetch_socket_messages(socket_payloads):
    """Extract [FETCH] messages from frontend socket payloads."""
    return [
        payload["log_entry"]["message"]
        for _event, _rid, payload in socket_payloads
        if "[FETCH]" in payload["log_entry"]["message"]
    ]


def fetch_file_lines(file_path):
    """Read the file sink and return [FETCH] lines."""
    content = file_path.read_text()
    return [line for line in content.splitlines() if "[FETCH]" in line]


def fetch_stderr_lines(stderr_buf):
    """Extract [FETCH] lines from the stderr buffer."""
    content = stderr_buf.getvalue()
    return [line for line in content.splitlines() if "[FETCH]" in line]


# ---------------------------------------------------------------------------
# Assertion helpers
# ---------------------------------------------------------------------------


def assert_no_forbidden(text, context):
    """Assert none of the FORBIDDEN markers appear in *text*."""
    for marker in FORBIDDEN:
        assert marker not in text, (
            f"FORBIDDEN value {marker!r} found in {context}:\n{text}"
        )


def assert_safe_origin(text, context):
    """Assert the redacted origin appears exactly once."""
    origin_count = text.count(REDACTED_ORIGIN)
    assert origin_count == 1, (
        f"Expected exactly 1 redacted origin {REDACTED_ORIGIN!r} "
        f"in {context}, got {origin_count}:\n{text}"
    )


def assert_record_exception_none(record, context):
    """Assert the loguru record has no exception attached."""
    assert record["exception"] is None, (
        f"Record exception should be None in {context}, "
        f"got {record['exception']!r}"
    )


def assert_all_sinks_clean(harness):
    """Assert every [FETCH] event across all sinks is leak-free.

    Checks DB queue entries, frontend socket payloads, stderr buffer,
    file output, and captured records (message, extra, exception).
    """
    for msg in fetch_db_messages(harness.db_entries):
        assert_no_forbidden(msg, "database_sink queued message")
        assert_safe_origin(msg, "database_sink queued message")

    for msg in fetch_socket_messages(harness.socket_payloads):
        assert_no_forbidden(msg, "frontend_progress_sink socket payload")
        assert_safe_origin(msg, "frontend_progress_sink socket payload")

    for line in fetch_stderr_lines(harness.stderr):
        assert_no_forbidden(line, "stderr StringIO")
        assert_safe_origin(line, "stderr StringIO")

    for line in fetch_file_lines(harness.file_path):
        assert_no_forbidden(line, "file sink output")
        assert_safe_origin(line, "file sink output")

    for record in fetch_events_from(harness.records):
        assert_no_forbidden(record["message"], "record message")
        assert_no_forbidden(str(record["extra"]), "record extra")
        assert_no_forbidden(str(record["exception"]), "record exception")
        assert_safe_origin(record["message"], "record message")
        assert_record_exception_none(record, "record exception")