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.

12.6 KB
Raw Blame
"""Route-boundary tests for search overrides on ``POST /api/start_research``.

Ports main's Flask-era ``tests/web/routes/test_start_research_search_override_
validation.py`` (visible via ``git show origin/main:...``) onto the FastAPI
route in ``web/routers/research.py``. The Flask original imported ``flask``
(not installed here) and its ``app``/``client`` fixtures from
``tests.web.routes.test_research_routes_extracted_helpers``, a Flask helper
module this merge deleted — both broke collection of the whole
``tests/web/routes/`` directory.

Why this lives under ``tests/web/routers/`` rather than ``tests/web/routes/``:
that's where every other direct-call FastAPI router test for this exact
route already lives (``test_start_research_ssrf.py``,
``test_export_research_logs.py``, ``test_async_handlers_offload.py``), and
this suite follows their established idiom of calling the route function
directly with ``username=...`` to bypass ``Depends(require_auth)`` rather
than driving a ``TestClient``.

``start_research`` is also wrapped by the ``api_rate_limit`` slowapi
decorator (see ``web/dependencies/rate_limit.py``), which expects a real
Starlette ``Request`` to key off of. A direct call with a ``Mock`` request
is exactly the situation ``test_settings_cache_invalidation.py`` and
``test_notes_rate_limit_keys.py`` already unwrap via ``.__wrapped__`` for
the same reason: this suite is about the search-override guard, not the
limiter (which has its own suite, ``test_auth_rate_limits.py``).

Guard under test: ``validate_search_overrides`` (``web/routes/
research_validation.py``) is called synchronously at the top of the async
``start_research`` handler (research.py:629), BEFORE the only dispatch seam
in that function — ``await run_db_sync(_start_research_sync, ...)``
(research.py:637-639). Patching ``run_db_sync`` and asserting it was never
awaited is therefore direct proof the 400 happens before any work is
offloaded, not just a status-code coincidence.

Scope note vs. main: main's invalid-override cases were parametrized over
``active_count in (0, 5)`` because its Flask guard ran after the DB session/
queue-state was already wired up for the request. On this branch the guard
is a pure function of the parsed JSON body, called before any DB session is
opened (research.py:611-634) — so active-research count cannot affect
whether it fires. That axis is dropped here as redundant, not silently
lost.

Queue provenance: ``_start_research_sync`` still builds
``submission_overrides`` from the explicit request fields (research.py:
867-885) and ``_queue_research`` still forwards the full ``research_settings``
dict — which embeds ``submission_overrides`` — as ``settings_snapshot`` to
``queue_processor.notify_research_queued`` (research.py:565-581). That
behaviour is unchanged from main, so it is ported rather than dropped.

Does NOT duplicate ``tests/web/queue/test_persisted_search_override_
validation.py``, which covers ``validate_search_overrides`` being
re-applied when a *persisted* queued row is later claimed and dispatched
by ``QueueProcessorV2._start_research``. This file covers the earlier,
separate call at the initial HTTP request boundary.
"""

import asyncio
import json
from contextlib import ExitStack, contextmanager
from unittest.mock import AsyncMock, MagicMock, Mock, patch

import pytest

from local_deep_research.constants import ResearchStatus
from local_deep_research.web.routers.research import start_research
from local_deep_research.web.routes.research_validation import (
    validate_search_overrides,
)

# The undecorated route body -- see module docstring for why direct calls
# must unwrap past the slowapi ``api_rate_limit`` decorator.
_start_research = start_research.__wrapped__

ROUTER = "local_deep_research.web.routers.research"
_SETTINGS_MANAGER = "local_deep_research.settings.SettingsManager"
_SAVE_STRATEGY = (
    "local_deep_research.web.services.research_service.save_research_strategy"
)
_RECLAIM_STALE = (
    "local_deep_research.web.routes.globals.reclaim_stale_user_active_research"
)
_QUEUE_PROCESSOR = "local_deep_research.web.queue.processor_v2.queue_processor"


# ---------------------------------------------------------------------------
# Request / dependency-seam builders
# ---------------------------------------------------------------------------


def _make_request(payload, session_id="sid-1"):
    """Minimal stand-in for the Starlette ``Request`` the handler reads:
    the Content-Type header, ``await request.json()``,
    ``request.session.get(...)``, and ``str(request.base_url)``.
    """
    request = Mock()
    request.headers = {"content-type": "application/json"}
    request.json = AsyncMock(return_value=payload)
    request.session = {"session_id": session_id}
    request.base_url = "http://testserver/"
    return request


def _make_settings_manager():
    """SettingsManager stub (Flask-suite idiom, ported to this branch)."""
    sm = MagicMock()
    lookup = {
        "llm.provider": "ollama",
        "llm.model": "llama3",
        "llm.ollama.url": "http://localhost:11434",
        "search.tool": "searxng",
        "search.iterations": 5,
        "search.questions_per_iteration": 5,
        "search.search_strategy": "source-based",
        "app.max_concurrent_researches": 3,
    }
    sm.get_setting.side_effect = lambda key, default=None: lookup.get(
        key, default
    )
    sm.get_all_settings.return_value = {"setting_key": "setting_val"}
    # NOT a dict -> _precheck_engine_policy skips (documented behavior for
    # test doubles); the egress-policy precheck has its own suite.
    sm.get_settings_snapshot.return_value = MagicMock()
    return sm


def _mock_db_session(active_count=0):
    """MagicMock standing in for the per-user SQLAlchemy session. The same
    ``query().filter_by()`` chain backs both the active-research count
    check and ``_queue_research``'s max-position lookup.
    """
    ms = MagicMock()
    chain = ms.query.return_value.filter_by.return_value
    chain.count.return_value = active_count
    chain.first.return_value = MagicMock()
    chain.scalar.return_value = 0
    return ms


@contextmanager
def _dispatch_mocks(active_count=0):
    """Patch every seam of ``_start_research_sync`` so a *validated*
    request can run the real function body end-to-end, past
    ``validate_search_overrides`` (already exercised in the async wrapper).

    Yields ``(start_research_process, queue_processor)`` mocks so tests can
    assert which of direct-dispatch or queueing actually happened.
    """
    ms = _mock_db_session(active_count=active_count)
    sm = _make_settings_manager()

    @contextmanager
    def _session_ctx(*args, **kwargs):
        yield ms

    fake_thread = MagicMock()
    fake_thread.ident = 42

    with ExitStack() as stack:
        stack.enter_context(
            patch(f"{ROUTER}.get_user_db_session", _session_ctx)
        )
        stack.enter_context(patch(_SETTINGS_MANAGER, return_value=sm))
        spawn = stack.enter_context(
            patch(f"{ROUTER}.start_research_process", return_value=fake_thread)
        )
        stack.enter_context(
            patch(f"{ROUTER}.resolve_user_password", return_value=("pw", False))
        )
        stack.enter_context(patch(f"{ROUTER}.log_settings"))
        stack.enter_context(patch(f"{ROUTER}.ResearchHistory"))
        stack.enter_context(patch(f"{ROUTER}.UserActiveResearch"))
        stack.enter_context(patch(_SAVE_STRATEGY))
        stack.enter_context(patch(_RECLAIM_STALE, return_value=False))
        queue_processor = stack.enter_context(patch(_QUEUE_PROCESSOR))
        yield spawn, queue_processor


def _run(request, **kwargs):
    return asyncio.run(_start_research(request, **kwargs))


# ---------------------------------------------------------------------------
# Invalid overrides -> 400 before the dispatch seam is ever reached
# ---------------------------------------------------------------------------

INVALID_OVERRIDES = (
    ("max_results", 0),
    ("max_results", 51),
    ("max_results", True),
    ("max_results", False),
    ("max_results", 1.0),
    ("max_results", "50"),
    ("time_period", ""),
    ("time_period", "7d"),
    ("time_period", "30d"),
    ("time_period", "day"),
    ("time_period", "Y"),
    ("time_period", 1),
)


class TestInvalidSearchOverrideRejectedBeforeDispatch:
    @pytest.mark.parametrize(
        "field,value",
        INVALID_OVERRIDES,
        ids=[f"{field}-{value!r}" for field, value in INVALID_OVERRIDES],
    )
    def test_invalid_search_override_returns_400_before_dispatch(
        self, field, value
    ):
        # Given: a request whose only problem is one invalid override.
        payload = {
            "query": "override validation",
            "model": "llama3",
            field: value,
        }
        request = _make_request(payload)

        # When: the request reaches the start-research handler. run_db_sync
        # is the ONLY seam between the async wrapper and any DB/dispatch
        # work (research.py:637-639) -- mocking it and asserting it was
        # never awaited proves the guard runs before dispatch, not just
        # that the response happens to carry a 400.
        with patch(
            f"{ROUTER}.run_db_sync", new_callable=AsyncMock
        ) as run_db_sync:
            result = _run(request, username="testuser")

        # Then: rejected before any offload, with the real validator's
        # own message (not a hand-duplicated string).
        run_db_sync.assert_not_called()
        assert result.status_code == 400
        body = json.loads(result.body)
        assert body["status"] == "error"
        assert body["message"] == validate_search_overrides({field: value})


# ---------------------------------------------------------------------------
# Valid boundary overrides -> validation passes, direct dispatch proceeds
# ---------------------------------------------------------------------------

VALID_BOUNDARIES = tuple(
    (max_results, time_period)
    for max_results in (1, 50)
    for time_period in ("d", "w", "m", "y", "all")
)


class TestValidSearchOverrideBoundariesDispatchDirectly:
    @pytest.mark.parametrize(
        "max_results,time_period",
        VALID_BOUNDARIES,
        ids=[f"max-{m}-period-{t}" for m, t in VALID_BOUNDARIES],
    )
    def test_valid_search_override_boundaries_dispatch_directly(
        self, max_results, time_period
    ):
        # Given: a direct-dispatch request (no queued backlog) with
        # canonical boundary values.
        payload = {
            "query": "valid override",
            "model": "llama3",
            "max_results": max_results,
            "time_period": time_period,
        }
        request = _make_request(payload)

        # When: the request runs the full (mocked-seams) pipeline.
        with _dispatch_mocks(active_count=0) as (spawn, queue_processor):
            result = _run(request, username="testuser")

        # Then: the research starts exactly once with the explicit
        # boundary values, and never touches the queue path.
        assert result["status"] == "success"
        spawn.assert_called_once()
        assert spawn.call_args.kwargs["max_results"] == max_results
        assert spawn.call_args.kwargs["time_period"] == time_period
        queue_processor.notify_research_queued.assert_not_called()


# ---------------------------------------------------------------------------
# Valid overrides retain queue provenance when queued instead of dispatched
# ---------------------------------------------------------------------------


class TestValidSearchOverridesRetainQueueProvenance:
    def test_valid_search_overrides_retain_queue_provenance(self):
        # Given: a queue-bound request (active count already at the
        # configured max) with both explicit valid overrides.
        payload = {
            "query": "queued override",
            "max_results": 50,
            "time_period": "all",
        }
        request = _make_request(payload)

        # When: the client submits values while the active-research limit
        # is reached (active_count=5 >= app.max_concurrent_researches=3).
        with _dispatch_mocks(active_count=5) as (spawn, queue_processor):
            result = _run(request, username="testuser")

        # Then: the request queues instead of dispatching, and the
        # settings snapshot handed to the queue processor retains exactly
        # both override names (research.py:867-885, :565-581).
        assert result["status"] == ResearchStatus.QUEUED
        spawn.assert_not_called()
        queue_processor.notify_research_queued.assert_called_once()
        snapshot = queue_processor.notify_research_queued.call_args.kwargs[
            "settings_snapshot"
        ]
        assert snapshot["submission_overrides"] == [
            "max_results",
            "time_period",
        ]