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.

11.2 KB
Raw Blame
"""The search query must not reach the log on the empty-result path.

``BaseSearchEngine.run()`` logs "returned no preview results" without the
query, but it only reaches that line *after* calling ``_get_previews()`` and
it records metrics in its ``finally`` block — so the engine implementations
and ``SearchTracker`` are part of the same leak surface and are covered here
too. ``.pre-commit-hooks/check-sensitive-logging.py`` enforces the rule
statically; these tests pin the runtime behaviour.
"""

from unittest.mock import Mock, call, patch

import pytest

from local_deep_research.web_search_engines.engines.search_engine_guardian import (
    GuardianSearchEngine,
)
from local_deep_research.web_search_engines.engines.search_engine_mojeek import (
    MojeekSearchEngine,
)
from local_deep_research.web_search_engines.engines.search_engine_pubchem import (
    PubChemSearchEngine,
)
from local_deep_research.web_search_engines.engines.search_engine_semantic_scholar import (
    SemanticScholarSearchEngine,
)
from local_deep_research.web_search_engines.engines.search_engine_searxng import (
    SearXNGSearchEngine,
)
from local_deep_research.web_search_engines.search_engine_base import (
    BaseSearchEngine,
)

QUERY = "unique-query-must-not-be-logged"

# The fallback ladders below log *parts* of the query rather than the whole
# string, so ``QUERY not in ...`` would pass while a term the user typed was
# written to the sink. Every word here is long enough to survive both of
# Semantic Scholar's length filters (>6 chars for the key-terms rung, >5 for
# the single-longest-term rung) and none of them occurs in any fixed message.
PROBE_QUERY = "paleobotany triskelion zymurgy"
PROBE_WORDS = PROBE_QUERY.split()


def _assert_no_probe_word(mock_logger):
    """No word of PROBE_QUERY may appear in any captured logger call."""
    logged = repr(mock_logger.mock_calls)
    leaked = [word for word in PROBE_WORDS if word in logged]
    assert not leaked, f"query terms reached the log: {leaked}\n{logged}"


class _NoPreviewSearchEngine(BaseSearchEngine):
    def _get_previews(self, query):
        return []

    def _get_full_content(self, relevant_items):
        return relevant_items


def test_run_does_not_log_query_when_no_previews_are_returned():
    engine = _NoPreviewSearchEngine(programmatic_mode=True)
    engine.rate_tracker = Mock(enabled=False)

    with patch(
        "local_deep_research.web_search_engines.search_engine_base.logger"
    ) as mock_logger:
        results = engine.run(QUERY)

    assert results == []
    # Positive assertion first: without it every remaining assertion would
    # also pass if run() raised early, if the empty branch became
    # unreachable, or if the logger.info call were deleted outright.
    assert (
        call.info(
            "Search engine _NoPreviewSearchEngine returned no preview results"
        )
        in mock_logger.mock_calls
    )
    # repr(mock_calls) covers positional args, keyword args, and chained
    # calls (e.g. logger.bind(...).info(...)) in one flat string.
    assert QUERY not in repr(mock_logger.mock_calls)


@pytest.mark.parametrize(
    "engine_cls, module, entry_message, empty_message",
    [
        (
            SearXNGSearchEngine,
            "search_engine_searxng",
            "Getting SearXNG previews",
            "No SearXNG results found",
        ),
        (
            MojeekSearchEngine,
            "search_engine_mojeek",
            "Getting Mojeek previews",
            "No Mojeek results found",
        ),
    ],
)
def test_engine_get_previews_omits_query_on_empty_results(
    engine_cls, module, entry_message, empty_message
):
    """The engine's own entry log and empty-result warning must stay quiet.

    Both fire before ``run()`` reaches its sanitised message, so a query left
    in either of them makes the base-class fix worthless for that engine.
    """
    # Bypass __init__: it wants settings/DB access, and _get_previews only
    # needs the availability flag and the results helper.
    engine = engine_cls.__new__(engine_cls)
    engine._is_available = True
    engine._get_search_results = Mock(return_value=[])

    with patch(
        f"local_deep_research.web_search_engines.engines.{module}.logger"
    ) as mock_logger:
        previews = engine._get_previews(QUERY)

    assert previews == []
    engine._get_search_results.assert_called_once_with(QUERY)
    assert call.info(entry_message) in mock_logger.mock_calls
    assert call.warning(empty_message) in mock_logger.mock_calls
    assert QUERY not in repr(mock_logger.mock_calls)


@pytest.mark.parametrize("missing", ["username", "user_password"])
def test_search_tracker_omits_query_when_context_is_incomplete(missing):
    """run()'s finally block records metrics; its bail-outs logged the query.

    The no-password branch additionally paired the query with the username.
    """
    from local_deep_research.metrics import search_tracker

    context = {
        "research_id": "1",
        "username": "alice",
        "user_password": "s3cret",
    }
    context.pop(missing)

    with (
        patch.object(
            search_tracker, "get_search_context", return_value=context
        ),
        patch.object(search_tracker, "logger") as mock_logger,
    ):
        search_tracker.SearchTracker.record_search(
            engine_name="searxng", query=QUERY, results_count=0
        )

    assert mock_logger.warning.call_count == 1
    logged = repr(mock_logger.mock_calls)
    assert "Cannot save search metrics" in logged
    assert "searxng" in logged
    assert QUERY not in logged
    if missing == "user_password":
        # The username is kept deliberately - it is what an operator needs to
        # find the broken research context. What is removed is the pairing of
        # that username with the text the user searched for.
        assert "alice" in logged


def test_semantic_scholar_fallback_ladder_omits_every_query_term():
    """The empty-result ladder logged a word taken straight from the query.

    ``_adaptive_search`` walks four rungs when the direct search comes back
    empty; the last two build their retry string out of the user's own words
    (``key_terms_query``, then ``longest_word``). Those are substrings of the
    query, so an assertion on the whole query string cannot see them.

    Revert that fails this test: restore
    ``logger.info("Trying with single key term: {}", longest_word)`` at
    ``search_engine_semantic_scholar.py:495``.
    """
    engine = SemanticScholarSearchEngine.__new__(SemanticScholarSearchEngine)
    # No LLM: _optimize_query returns the raw user query unchanged, which is
    # the configuration in which this path carries verbatim user text.
    engine.llm = None
    engine._direct_search = Mock(return_value=[])

    with patch(
        "local_deep_research.web_search_engines.engines."
        "search_engine_semantic_scholar.logger"
    ) as mock_logger:
        papers, strategy = engine._adaptive_search(PROBE_QUERY)

    assert papers == []
    assert strategy == "standard"
    # Positive assertions: without them this test would also pass if the
    # ladder stopped running, or if both fallback log lines were deleted.
    assert engine._direct_search.call_count == 3
    assert (
        call.info("Trying with key terms extracted from the query")
        in mock_logger.mock_calls
    )
    assert (
        call.info("Trying with the single longest key term")
        in mock_logger.mock_calls
    )
    _assert_no_probe_word(mock_logger)


def test_guardian_success_path_omits_the_query_from_search_metadata():
    """Guardian overrides run(), so the base-class fix never applies to it.

    ``_search_metadata`` carried ``original_query`` and ``optimized_query``,
    and ``run()`` logs the whole dict at INFO whenever previews are non-empty
    — i.e. on every Guardian search that returns something.

    Revert that fails this test: restore ``"original_query": query`` and
    ``"optimized_query": optimized_query`` in the ``_search_metadata`` dict
    built by ``search_engine_guardian.py::_get_previews``.
    """
    optimized = "triskelion zymurgy"
    article = {
        "id": "a1",
        "title": "t",
        "link": "https://example.invalid/a1",
        "snippet": "s",
        "publication_date": "2026-01-01",
        "section": "World",
        "author": "a",
        "full_content": "c",
    }

    engine = GuardianSearchEngine.__new__(GuardianSearchEngine)
    BaseSearchEngine.__init__(engine, programmatic_mode=True)
    engine.llm = None
    engine.max_filtered_results = None
    engine.search_snippets_only = False
    engine.from_date = "2026-01-01"
    engine.to_date = "2026-01-02"
    engine.section = None
    engine.order_by = "relevance"
    engine._original_date_params = {
        "from_date": engine.from_date,
        "to_date": engine.to_date,
    }
    engine._optimize_query_for_guardian = Mock(return_value=optimized)
    engine._adapt_dates_for_query_type = Mock()
    engine._adaptive_search = Mock(return_value=([article], "standard"))

    with patch(
        "local_deep_research.web_search_engines.engines."
        "search_engine_guardian.logger"
    ) as mock_logger:
        results = engine.run(PROBE_QUERY)

    assert results == [article]
    # Positive assertion: the metadata line must still be reached, otherwise
    # the absence assertions below are vacuous.
    logged = repr(mock_logger.mock_calls)
    assert "Search metadata:" in logged
    assert "'strategy': 'standard'" in logged
    _assert_no_probe_word(mock_logger)
    # The optimized query is derived from the user's text and was logged
    # under its own key; its terms must be gone too.
    for word in optimized.split():
        assert word not in logged


def test_pubchem_fallback_processing_error_omits_the_query():
    """The autocomplete-empty fallback re-uses the raw query as a "name".

    When ``_search_compounds`` finds nothing, ``_get_previews`` tries a
    direct lookup and, if that succeeds, sets ``compound_names = [query]``
    (``search_engine_pubchem.py:265``) — the query itself becomes the only
    "compound name" the per-compound loop processes. If that processing then
    raises, the exception-logging call site logged that name verbatim.

    Revert that fails this test: restore
    ``f"Error processing PubChem compound: {name} ..."`` at
    ``search_engine_pubchem.py:361``.
    """
    engine = PubChemSearchEngine.__new__(PubChemSearchEngine)
    engine.engine_type = "PubChemSearchEngine"
    engine.rate_tracker = Mock()
    engine.max_results = 10
    engine._search_compounds = Mock(return_value=[])
    # First call: the direct-lookup probe that seeds compound_names=[query].
    # Second call: the per-compound loop re-fetching that same "name", which
    # fails and reaches the exception-logging call site under test.
    engine._get_compound_by_name = Mock(
        side_effect=[{"cid": 1}, ValueError("boom")]
    )

    with patch(
        "local_deep_research.web_search_engines.search_engine_base.logger"
    ) as mock_logger:
        previews = engine._search_previews(PROBE_QUERY)

    assert previews == []
    # Positive assertion: the exception path must actually be reached,
    # otherwise the absence check below is vacuous.
    assert mock_logger.warning.call_count == 1
    mock_logger.exception.assert_not_called()
    _assert_no_probe_word(mock_logger)