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.

6.2 KB
Raw Blame
from contextlib import ExitStack
from dataclasses import dataclass
from unittest.mock import MagicMock, patch

import pytest

from tests.web.services.helpers import (
    MODULE,
    _base_run_patches,
    _get_raw_run_research_process,
)


@dataclass(frozen=True, slots=True)
class _WorkerSearchLimits:
    max_results: int | None
    time_period: str | None


@pytest.fixture(autouse=True)
def _clear_raw_worker_egress_context():
    from local_deep_research.security.egress.audit_hook import (
        clear_active_context,
    )

    clear_active_context()
    yield
    clear_active_context()


def _dispatch_search_limits(
    limits: _WorkerSearchLimits | None,
) -> tuple[MagicMock, MagicMock]:
    persisted = {
        "llm.provider": "openai_endpoint",
        "llm.model": "persisted-model",
        "llm.openai_endpoint.url": "http://persisted.invalid/v1",
        "search.tool": "searxng",
        "search.max_results": 7,
        "search.time_period": "y",
        "search.engine.web.serpapi.default_params.time_period": "y",
    }
    effective = {
        "llm.provider": "openai_endpoint",
        "llm.model": "effective-model",
        "llm.openai_endpoint.url": "http://effective.example/v1",
        "search.tool": "searxng",
        "search.max_results": 13,
        "search.time_period": "m",
        "search.engine.web.serpapi.default_params.time_period": "y",
    }
    search_factory = MagicMock(return_value=MagicMock())
    system = MagicMock()
    system.analyze_topic.return_value = {
        "findings": "test",
        "formatted_findings": "result",
    }
    system_cls = MagicMock(return_value=system)
    patches = _base_run_patches()
    patches[f"{MODULE}.apply_environment_overrides_to_snapshot"] = MagicMock(
        return_value=effective
    )
    patches[f"{MODULE}.get_llm"] = MagicMock(return_value=MagicMock())
    patches[f"{MODULE}.AdvancedSearchSystem"] = system_cls
    worker_kwargs = {"username": "user1", "settings_snapshot": persisted}
    if limits is not None:
        worker_kwargs["max_results"] = limits.max_results
        worker_kwargs["time_period"] = limits.time_period

    with ExitStack() as stack:
        stack.enter_context(
            patch(
                "local_deep_research.config.search_config.factory_get_search",
                search_factory,
            )
        )
        stack.enter_context(
            patch(
                "local_deep_research.security.egress.policy.context_from_snapshot",
                return_value=MagicMock(),
            )
        )
        stack.enter_context(
            patch(
                "local_deep_research.security.egress.run_classification.audit_run_from_snapshot",
                return_value=MagicMock(allowed=True),
            )
        )
        for target, mock_obj in patches.items():
            stack.enter_context(patch(target, mock_obj))
        _get_raw_run_research_process()(1, "test", "quick", **worker_kwargs)

    return search_factory, system_cls


def test_explicit_search_limits_override_effective_snapshot_at_consumer():
    # Given: explicit limits distinct from persisted and effective snapshots.
    limits = _WorkerSearchLimits(max_results=19, time_period="w")

    # When: the worker dispatches the run with explicit limits.
    search_factory, system_cls = _dispatch_search_limits(limits)

    # Then: the snapshot carries the explicit limits; time_period is NOT
    # forwarded as a kwarg (create_search_engine resolves it from the
    # snapshot since #5587), and the per-request override resets per-engine
    # time_period choices to "inherit" so it wins for this run.
    search_factory.assert_called_once()
    assert search_factory.call_args.kwargs["max_results"] == 19
    assert "time_period" not in search_factory.call_args.kwargs
    factory_snapshot = search_factory.call_args.kwargs["settings_snapshot"]
    assert factory_snapshot["search.time_period"] == "w"
    assert (
        factory_snapshot["search.engine.web.serpapi.default_params.time_period"]
        == "inherit"
    )
    system_snapshot = system_cls.call_args.kwargs["settings_snapshot"]
    assert system_snapshot["search.max_results"] == 19
    assert system_snapshot["search.time_period"] == "w"
    assert (
        system_snapshot["search.engine.web.serpapi.default_params.time_period"]
        == "inherit"
    )


def test_omitted_search_limits_preserve_effective_snapshot():
    # Given: no worker search-limit overrides.
    limits = None

    # When: the worker dispatches the run.
    search_factory, system_cls = _dispatch_search_limits(limits)

    # Then: the real search consumer and system snapshot use 13 and "m".
    search_factory.assert_called_once()
    assert search_factory.call_args.kwargs["max_results"] == 13
    assert "time_period" not in search_factory.call_args.kwargs
    factory_snapshot = search_factory.call_args.kwargs["settings_snapshot"]
    assert factory_snapshot["search.time_period"] == "m"
    # No per-request override: the per-engine choice is left untouched.
    assert (
        factory_snapshot["search.engine.web.serpapi.default_params.time_period"]
        == "y"
    )
    system_snapshot = system_cls.call_args.kwargs["settings_snapshot"]
    assert system_snapshot["search.max_results"] == 13
    assert system_snapshot["search.time_period"] == "m"


def test_none_search_limits_preserve_effective_snapshot():
    # Given: legacy explicit None search-limit overrides.
    limits = _WorkerSearchLimits(max_results=None, time_period=None)

    # When: the worker dispatches the run.
    search_factory, system_cls = _dispatch_search_limits(limits)

    # Then: the real search consumer and system snapshot use 13 and "m".
    search_factory.assert_called_once()
    assert search_factory.call_args.kwargs["max_results"] == 13
    assert "time_period" not in search_factory.call_args.kwargs
    factory_snapshot = search_factory.call_args.kwargs["settings_snapshot"]
    assert factory_snapshot["search.time_period"] == "m"
    # No per-request override: the per-engine choice is left untouched.
    assert (
        factory_snapshot["search.engine.web.serpapi.default_params.time_period"]
        == "y"
    )
    system_snapshot = system_cls.call_args.kwargs["settings_snapshot"]
    assert system_snapshot["search.max_results"] == 13
    assert system_snapshot["search.time_period"] == "m"