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.

5.6 KB
Raw Blame
"""Sink-leak tests for fetch-tool error-path events.

Verifies that full-fetch outer failure, summary-model failure, and
summary outer failure emit static ``logger.error`` events — no
traceback, no exception attachment, no forbidden values in any sink.
"""

from __future__ import annotations

from unittest.mock import MagicMock, patch

import pytest

from local_deep_research.advanced_search_system.strategies.langgraph_agent_strategy import (
    SearchResultsCollector,
)
from local_deep_research.advanced_search_system.tools.fetch import (
    build_fetch_tool,
)

from ._fetch_sink_support import (
    BODY,
    CRED_SECRET,
    create_sink_harness,
    CREDENTIALED_URL,
    EXC_TEXT,
    FOCUS,
    FETCH_EVENT_RE,
    fetch_events_from,
    fetcher_cm,
    model_raising,
    model_returning,
    QUERY,
    REDACTED_ORIGIN,
    SUMMARY,
    TITLE,
    assert_all_sinks_clean,
    assert_record_exception_none,
)


@pytest.fixture
def sink_harness(tmp_path):
    with create_sink_harness(tmp_path) as harness:
        yield harness


# ---------------------------------------------------------------------------
# Test 5: full-fetch outer failure
# ---------------------------------------------------------------------------


def test_full_fetch_outer_failure_emits_error_without_traceback(sink_harness):
    """A full-mode fetch exception must emit a static logger.error event
    with status=error — no traceback, no exception attachment."""
    collector = SearchResultsCollector([])
    tool = build_fetch_tool("full", collector)
    assert tool is not None

    fetcher = MagicMock()
    fetcher.fetch.side_effect = Exception(EXC_TEXT)
    cm = MagicMock()
    cm.__enter__.return_value = fetcher
    cm.__exit__.return_value = False

    with patch(
        "local_deep_research.content_fetcher.ContentFetcher", return_value=cm
    ):
        out = tool.invoke({"url": CREDENTIALED_URL})

    sink_harness.flush()

    assert "Error fetching" in out
    assert CRED_SECRET not in out

    fetch_records = fetch_events_from(sink_harness.records)
    assert len(fetch_records) == 1
    event_msg = fetch_records[0]["message"].strip()
    match = FETCH_EVENT_RE.match(event_msg)
    assert match is not None, f"Event does not match schema: {event_msg!r}"
    assert match.group("mode") == "full"
    assert match.group("status") == "error"
    assert match.group("url") == REDACTED_ORIGIN

    assert_record_exception_none(
        fetch_records[0], "full-fetch outer failure record"
    )

    assert_all_sinks_clean(sink_harness)


# ---------------------------------------------------------------------------
# Test 6: summary-model failure (LLM invoke raises)
# ---------------------------------------------------------------------------


def test_summary_model_failure_emits_error_without_traceback(sink_harness):
    """A summary-mode LLM invoke exception must emit a static
    logger.error event with status=summary_error — no traceback."""
    collector = SearchResultsCollector([])
    model = model_raising(Exception(EXC_TEXT))
    tool = build_fetch_tool(
        "summary_focus_query",
        collector,
        model=model,
        overall_query=QUERY,
    )
    assert tool is not None
    cm = fetcher_cm(title=TITLE, content=BODY * 5)

    with patch(
        "local_deep_research.content_fetcher.ContentFetcher", return_value=cm
    ):
        out = tool.invoke({"url": CREDENTIALED_URL, "focus": FOCUS})

    sink_harness.flush()

    assert "Error summarizing" in out
    assert CRED_SECRET not in out

    fetch_records = fetch_events_from(sink_harness.records)
    assert len(fetch_records) == 1
    event_msg = fetch_records[0]["message"].strip()
    match = FETCH_EVENT_RE.match(event_msg)
    assert match is not None, f"Event does not match schema: {event_msg!r}"
    assert match.group("mode") == "summary_focus_query"
    assert match.group("status") == "summary_error"
    assert match.group("url") == REDACTED_ORIGIN

    assert_record_exception_none(
        fetch_records[0], "summary-model failure record"
    )

    assert_all_sinks_clean(sink_harness)


# ---------------------------------------------------------------------------
# Test 7: summary outer failure
# ---------------------------------------------------------------------------


def test_summary_outer_failure_emits_error_without_traceback(sink_harness):
    """A summary-mode outer exception must emit a static logger.error
    event with status=error — no traceback, no exception attachment."""
    collector = SearchResultsCollector([])
    model = model_returning(SUMMARY)
    tool = build_fetch_tool(
        "summary_focus_query",
        collector,
        model=model,
        overall_query=QUERY,
    )
    assert tool is not None

    fetcher = MagicMock()
    fetcher.fetch.side_effect = Exception(EXC_TEXT)
    cm = MagicMock()
    cm.__enter__.return_value = fetcher
    cm.__exit__.return_value = False

    with patch(
        "local_deep_research.content_fetcher.ContentFetcher", return_value=cm
    ):
        out = tool.invoke({"url": CREDENTIALED_URL, "focus": FOCUS})

    sink_harness.flush()

    assert "Error fetching" in out
    assert CRED_SECRET not in out

    fetch_records = fetch_events_from(sink_harness.records)
    assert len(fetch_records) == 1
    event_msg = fetch_records[0]["message"].strip()
    match = FETCH_EVENT_RE.match(event_msg)
    assert match is not None, f"Event does not match schema: {event_msg!r}"
    assert match.group("mode") == "summary_focus_query"
    assert match.group("status") == "error"
    assert match.group("url") == REDACTED_ORIGIN

    assert_record_exception_none(
        fetch_records[0], "summary outer failure record"
    )

    assert_all_sinks_clean(sink_harness)