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.3 KB
Raw Blame
"""Sink-leak tests for fetch-tool success and empty-status events.

Verifies that summary success, empty-content, and empty-summary log
events emitted to real Loguru sinks contain ONLY bounded metadata:
mode, status, redacted origin, and page/summary character counts.
"""

from __future__ import annotations

from unittest.mock import 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,
    CREDENTIALED_URL,
    create_sink_harness,
    DOUBLE_ENCODED_URL,
    ENCODED_URL,
    FOCUS,
    FETCH_EVENT_RE,
    fetch_events_from,
    fetch_db_messages,
    fetch_file_lines,
    fetch_socket_messages,
    fetch_stderr_lines,
    fetcher_cm,
    model_returning,
    QUERY,
    REDACTED_ORIGIN,
    SUMMARY,
    TITLE,
    assert_all_sinks_clean,
)


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


# ---------------------------------------------------------------------------
# Test 1: summary success — encoded credentialed URL
# ---------------------------------------------------------------------------


def test_summary_success_encoded_url_emits_safe_event_to_all_sinks(
    sink_harness,
):
    """Summary success must emit one bounded event per sink with only
    mode, status, redacted origin, and char counts."""
    collector = SearchResultsCollector([])
    model = model_returning(SUMMARY)
    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": ENCODED_URL, "focus": FOCUS})

    sink_harness.flush()

    assert out.startswith("[1] ")
    assert SUMMARY in out

    db_msgs = fetch_db_messages(sink_harness.db_entries)
    socket_msgs = fetch_socket_messages(sink_harness.socket_payloads)
    stderr_lines = fetch_stderr_lines(sink_harness.stderr)
    file_lines = fetch_file_lines(sink_harness.file_path)
    fetch_records = fetch_events_from(sink_harness.records)

    assert len(db_msgs) == 1, f"Expected 1 DB event, got {db_msgs}"
    assert len(socket_msgs) == 1, f"Expected 1 socket event, got {socket_msgs}"
    assert len(file_lines) == 1, f"Expected 1 file line, got {file_lines}"
    assert len(fetch_records) == 1, f"Expected 1 record, got {fetch_records}"
    assert len(stderr_lines) >= 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") == "success"
    assert match.group("url") == REDACTED_ORIGIN
    assert match.group("page_chars") is not None
    assert match.group("summary_chars") is not None

    assert_all_sinks_clean(sink_harness)


# ---------------------------------------------------------------------------
# Test 2: summary success — double-encoded credentialed URL
# ---------------------------------------------------------------------------


def test_summary_success_double_encoded_url_emits_safe_origin(sink_harness):
    """A double-encoded credentialed URL must still redact to the same
    safe scheme://host:port origin across every sink."""
    collector = SearchResultsCollector([])
    model = model_returning(SUMMARY)
    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 * 3)

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

    sink_harness.flush()

    assert out.startswith("[1] ")
    assert SUMMARY 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("url") == REDACTED_ORIGIN

    assert_all_sinks_clean(sink_harness)


# ---------------------------------------------------------------------------
# Test 3: empty page content
# ---------------------------------------------------------------------------


def test_empty_content_emits_bounded_empty_content_status(sink_harness):
    """An empty page must emit status=empty_content with only mode and
    redacted origin — no LLM call, no collector registration."""
    collector = SearchResultsCollector([])
    model = model_returning(SUMMARY)
    tool = build_fetch_tool(
        "summary_focus_query",
        collector,
        model=model,
        overall_query=QUERY,
    )
    assert tool is not None
    cm = fetcher_cm(title=TITLE, content="")

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

    sink_harness.flush()

    assert out.startswith("NOT RELEVANT")
    assert collector.results == []
    model.invoke.assert_not_called()

    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") == "empty_content"
    assert match.group("url") == REDACTED_ORIGIN

    assert_all_sinks_clean(sink_harness)


# ---------------------------------------------------------------------------
# Test 4: empty summary (page has content, LLM returns empty)
# ---------------------------------------------------------------------------


def test_empty_summary_emits_bounded_empty_summary_status(sink_harness):
    """A non-empty page whose LLM summary comes back empty must emit
    status=empty_summary with mode, redacted origin, and page_chars."""
    collector = SearchResultsCollector([])
    model = model_returning("")
    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 * 4)

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

    sink_harness.flush()

    assert out.startswith("NOT RELEVANT")
    assert collector.results == []

    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") == "empty_summary"
    assert match.group("url") == REDACTED_ORIGIN
    assert match.group("page_chars") is not None

    assert_all_sinks_clean(sink_harness)