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.

21.8 KB
Raw Blame
"""REST -> research-function call path, with the research function NOT mocked.

Recovered from main's deleted ``tests/web/test_api_coverage.py``
(``TestRestToResearchFunctionCallPath``, ``TestResearchFunctionSignatures``,
``TestHttpMethods``). Those classes have no successor anywhere on this
branch: every other ``/api/v1`` test replaces ``quick_summary`` /
``generate_report`` / ``analyze_documents`` with a mock at exactly the seam
this file refuses to mock.

Why that matters. ``MagicMock(return_value=...)`` accepts ANY kwargs. So a
handler that passes a keyword the research function's signature rejects —
the #4396 bug class — produces a green mock test and a 500 in production.
``tests/security/test_api_v1_boundary_fastapi.py`` is thorough about what
the endpoint *sends*; it patches ``analyze_documents`` / ``generate_report``
themselves, so it cannot see whether the callee can receive it.

The seams here are one layer BELOW the research function:

* ``analyze_documents`` is a flat ``get_llm -> get_search -> summarise``
  function, so ``get_llm``/``get_search`` are the right seam and the real
  ``analyze_documents`` body runs. Both are patched with ``autospec=True``
  so ``analyze_documents``' own calls into them are signature-checked too.
* ``generate_report`` drives the whole research engine, so the seam is
  ``_init_search_system`` — whose ``get_llm(settings_snapshot=...)`` call is
  the EXACT line that 500'd in #4396 when the REST endpoint failed to inject
  the user's snapshot — plus ``IntegratedReportGenerator``.

The fixture shape (real registration, real encrypted per-user DB, real
CSRF, real ``require_api_access``) follows
``tests/security/test_api_v1_boundary_fastapi.py``.
"""

from __future__ import annotations

import inspect
import itertools
import uuid
from unittest.mock import MagicMock, patch

import pytest

pytestmark = pytest.mark.real_session_check

ACCOUNT_PASSWORD = "Api-CallPath-Probe-1!"  # noqa: S105

QUICK_SUMMARY_PATH = "/api/v1/quick_summary"
GENERATE_REPORT_PATH = "/api/v1/generate_report"
ANALYZE_DOCUMENTS_PATH = "/api/v1/analyze_documents"
HEALTH_PATH = "/api/v1/health"
DOCS_PATH = "/api/v1/"

# Planted in the user's own encrypted settings DB with a value unique to that
# user. Finding that value in the snapshot that reaches the layer below
# proves the snapshot is THIS user's, with their stored value unchanged —
# not an empty dict, the JSON defaults or another user's settings.
#
# Not underscore-prefixed on purpose: names starting with "_" are reserved
# for run metadata written by the code that owns it (such as the
# openai_endpoint key-owner marker), and SettingsManager.get_all_settings
# skips settings rows with such names. RESERVED_ROW_KEY is planted the same
# way and must NOT arrive.
TRACER_KEY = "test.ldr_call_path_tracer"
RESERVED_ROW_KEY = "_ldr_call_path_tracer"

QUERY = "What is quantum computing?"
COLLECTION = "physics_papers"
STUB_LLM_SUMMARY = "Qubits [1] enable superposition and entanglement [2]."
STUB_DOCUMENTS = [
    {
        "content": "Qubits are quantum bits.",
        "title": "Qubit Basics",
        "link": "https://example.com/qubit",
    },
    {
        "content": "Superposition allows simultaneous states.",
        "title": "Superposition",
        "link": "https://example.com/super",
    },
]

# Rate limiting is keyed per client IP and the limiter's enabled flag is
# resolved at import time, so give every client its own peer address.
_peer_counter = itertools.count(1)


@pytest.fixture
def live_app(tmp_path, monkeypatch):
    """The real assembled FastAPI app on a temp data dir."""
    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
    monkeypatch.setenv("LDR_TESTING_WITH_MOCKS", "true")
    monkeypatch.setenv("LDR_DISABLE_RATE_LIMITING", "true")
    monkeypatch.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1000")

    from local_deep_research.database.auth_db import init_auth_database
    from local_deep_research.database.encrypted_db import db_manager
    from local_deep_research.database.session_passwords import (
        session_password_store,
    )
    from local_deep_research.web.fastapi_app import app as fastapi_app
    import local_deep_research.web.routers.auth as auth_routes

    original_data_dir = db_manager.data_dir
    created_users: list[str] = []
    try:
        db_manager.data_dir = tmp_path / "encrypted_databases"
        init_auth_database()
        monkeypatch.setattr(
            auth_routes,
            "_perform_post_login_tasks",
            lambda _u, _p, _sid=None: None,
        )
        yield fastapi_app, created_users
    finally:
        for username in created_users:
            session_password_store.clear_all_for_user(username)
        db_manager.close_all_databases()
        db_manager.data_dir = original_data_dir


def _client(app):
    from fastapi.testclient import TestClient

    peer = next(_peer_counter)
    client = TestClient(app, raise_server_exceptions=False)
    client.headers.update(
        {"X-Forwarded-For": f"10.{peer // 254 % 254 + 1}.{peer % 254 + 1}.9"}
    )
    return client


def _csrf(client):
    client.get("/auth/login")
    resp = client.get("/auth/csrf-token")
    return resp.json().get("csrf_token", "") if resp.status_code == 200 else ""


def _api_user(live_app):
    """Register a fresh user; return (client, username)."""
    app, created_users = live_app
    username = f"apicp_{uuid.uuid4().hex[:8]}"
    created_users.append(username)
    client = _client(app)
    token = _csrf(client)
    resp = client.post(
        "/auth/register",
        data={
            "username": username,
            "password": ACCOUNT_PASSWORD,
            "confirm_password": ACCOUNT_PASSWORD,
            "acknowledge": "true",
            "csrf_token": token,
        },
        headers={"X-CSRFToken": token},
        follow_redirects=False,
    )
    assert resp.status_code in (200, 302), (
        f"registration failed: {resp.status_code} / {resp.text[:400]}"
    )
    assert client.get("/auth/check").status_code == 200
    return client, username


def _plant_tracer(username):
    """Store ``TRACER_KEY`` and ``RESERVED_ROW_KEY`` in the user's encrypted
    DB, both with a value unique to ``username``; return that value."""
    from local_deep_research.database.session_context import get_user_db_session
    from local_deep_research.settings import SettingsManager

    tracer_value = f"tracer-{username}"
    with get_user_db_session(username, password=ACCOUNT_PASSWORD) as db_session:
        manager = SettingsManager(db_session)
        for key in (TRACER_KEY, RESERVED_ROW_KEY):
            assert manager.set_setting(key, tracer_value), (
                f"could not seed {key} for {username}"
            )
    return tracer_value


def _post(client, path, body):
    token = _csrf(client)
    return client.post(path, json=body, headers={"X-CSRFToken": token})


def _stub_llm(content=STUB_LLM_SUMMARY):
    llm = MagicMock()
    response = MagicMock()
    response.content = content
    llm.invoke.return_value = response
    return llm


def _stub_search(results=None):
    search = MagicMock()
    search.run.return_value = STUB_DOCUMENTS if results is None else results
    return search


def _post_analyze_documents(client, extra_body=None, extra_patches=()):
    """Fire a real POST with only ``get_llm``/``get_search`` replaced.

    Returns ``(response, llm, search)``.
    """
    from contextlib import ExitStack

    llm = _stub_llm()
    search = _stub_search()
    body = {"query": QUERY, "collection_name": COLLECTION}
    body.update(extra_body or {})

    with ExitStack() as stack:
        stack.enter_context(
            patch(
                "local_deep_research.api.research_functions.get_llm",
                autospec=True,
                return_value=llm,
            )
        )
        stack.enter_context(
            patch(
                "local_deep_research.api.research_functions.get_search",
                autospec=True,
                return_value=search,
            )
        )
        for extra in extra_patches:
            stack.enter_context(extra)
        response = _post(client, ANALYZE_DOCUMENTS_PATH, body)

    return response, llm, search


class TestAnalyzeDocumentsRealCallPath:
    """The real ``analyze_documents()`` body runs, invoked over HTTP."""

    def test_endpoint_can_actually_invoke_the_research_function(self, live_app):
        """A 200 here means every kwarg ``api_analyze_documents`` passes is
        accepted by ``analyze_documents``' signature. The handler catches
        ``Exception`` broadly, so a rejected kwarg surfaces as a 500, not a
        traceback."""
        client, _username = _api_user(live_app)

        response, _llm, _search = _post_analyze_documents(client)

        assert response.status_code == 200, (
            "REST endpoint failed to invoke analyze_documents() with the "
            f"kwargs it passes. Response: {response.text[:600]}"
        )
        assert "summary" in response.json()

    def test_response_shape_is_exactly_the_four_documented_keys(self, live_app):
        """No unexpected keys added, none silently removed."""
        client, _username = _api_user(live_app)

        response, _llm, _search = _post_analyze_documents(client)

        assert response.status_code == 200, response.text[:600]
        assert response.json() == {
            "summary": STUB_LLM_SUMMARY,
            "documents": STUB_DOCUMENTS,
            "collection": COLLECTION,
            "document_count": len(STUB_DOCUMENTS),
        }

    def test_summary_equals_the_llm_content_verbatim(self, live_app):
        client, _username = _api_user(live_app)

        response, _llm, _search = _post_analyze_documents(client)

        assert response.status_code == 200, response.text[:600]
        assert response.json()["summary"] == STUB_LLM_SUMMARY

    def test_summary_preserves_citation_markers(self, live_app):
        """``[1]``/``[2]`` must reach the client unrewritten. If a future
        refactor adds citation processing here, this flags it."""
        client, _username = _api_user(live_app)

        response, _llm, _search = _post_analyze_documents(client)

        assert response.status_code == 200, response.text[:600]
        summary = response.json()["summary"]
        assert "[1]" in summary, f"citation [1] stripped: {summary!r}"
        assert "[2]" in summary, f"citation [2] stripped: {summary!r}"

    def test_documents_pass_through_unfiltered_and_unreordered(self, live_app):
        client, _username = _api_user(live_app)

        response, _llm, _search = _post_analyze_documents(client)

        assert response.status_code == 200, response.text[:600]
        assert response.json()["documents"] == STUB_DOCUMENTS

    def test_collection_is_echoed_back(self, live_app):
        client, _username = _api_user(live_app)

        response, _llm, _search = _post_analyze_documents(client)

        assert response.status_code == 200, response.text[:600]
        assert response.json()["collection"] == COLLECTION

    def test_document_count_matches_the_documents_length(self, live_app):
        client, _username = _api_user(live_app)

        response, _llm, _search = _post_analyze_documents(client)

        assert response.status_code == 200, response.text[:600]
        assert response.json()["document_count"] == len(STUB_DOCUMENTS)

    def test_search_engine_receives_the_query_verbatim(self, live_app):
        client, _username = _api_user(live_app)

        response, _llm, search = _post_analyze_documents(client)

        assert response.status_code == 200, response.text[:600]
        search.run.assert_called_once_with(QUERY)

    def test_llm_prompt_embeds_the_query(self, live_app):
        client, _username = _api_user(live_app)

        response, llm, _search = _post_analyze_documents(client)

        assert response.status_code == 200, response.text[:600]
        prompt = llm.invoke.call_args[0][0]
        assert QUERY in prompt

    def test_llm_prompt_embeds_every_document_body(self, live_app):
        client, _username = _api_user(live_app)

        response, llm, _search = _post_analyze_documents(client)

        assert response.status_code == 200, response.text[:600]
        prompt = llm.invoke.call_args[0][0]
        for doc in STUB_DOCUMENTS:
            assert doc["content"] in prompt, (
                f"document content missing from LLM prompt: {doc['content']!r}"
            )

    def test_output_file_is_rejected_before_the_research_function_runs(
        self, live_app, tmp_path
    ):
        """``output_file`` used to reach ``analyze_documents`` -> the same
        bare-``open()`` ``write_file_verified`` sink as generate_report,
        behind the same unset-by-default ``api.allow_file_output`` gate.
        Post-merge review of this follow-up found that gap and closed it
        the same way as generate_report/quick_summary: ``output_file`` is
        now subtracted from ``_ANALYZE_DOCUMENTS_PARAMS`` (api_v1.py) and
        rejected with a 400 at the REST boundary, before ``get_llm``/
        ``get_search`` — and therefore analyze_documents' own body — ever
        run. This supersedes the old version of this test, which asserted
        the (now-unreachable) 200 success path threaded the user's
        snapshot into ``write_file_verified``.
        """
        client, _username = _api_user(live_app)

        response, llm, search = _post_analyze_documents(
            client,
            extra_body={"output_file": str(tmp_path / "analysis.md")},
        )

        assert response.status_code == 400, response.text[:600]
        assert response.json()["allowed_parameters"] == sorted(
            {"force_reindex", "max_results", "temperature"}
        ), (
            "output_file must not be advertised as an accepted "
            "analyze_documents parameter any more"
        )
        # The rejection happens before analyze_documents (and therefore the
        # stubbed get_llm/get_search) is ever reached.
        llm.invoke.assert_not_called()
        search.run.assert_not_called()


def _post_generate_report(client, body=None):
    """Fire a real POST with only ``_init_search_system`` and the report
    generator replaced, so the real ``generate_report()`` body runs.

    Returns ``(response, init_kwargs_or_None)``.
    """
    captured = {}

    def _capture_init(*_args, **kwargs):
        captured["kwargs"] = kwargs
        stub_system = MagicMock()
        stub_system.analyze_topic.return_value = {
            "findings": [],
            "current_knowledge": "",
        }
        return stub_system

    report = {"content": "Final report body.", "metadata": {"query": QUERY}}

    with (
        patch(
            "local_deep_research.api.research_functions._init_search_system",
            side_effect=_capture_init,
        ),
        patch(
            "local_deep_research.api.research_functions."
            "IntegratedReportGenerator"
        ) as report_generator_cls,
        # _close_system runs in generate_report's finally; with a MagicMock
        # system it would call safe_close on auto-created attributes.
        patch("local_deep_research.api.research_functions._close_system"),
    ):
        report_generator_cls.return_value.generate_report.return_value = report
        response = _post(client, GENERATE_REPORT_PATH, body or {"query": QUERY})

    return response, captured.get("kwargs"), report


class TestGenerateReportRealCallPath:
    """#4396, verified at the failure site."""

    def test_endpoint_runs_generate_report_end_to_end(self, live_app):
        """200, not the original #4396 500 (no provider/api_key reached LLM
        init), with the report content passed back to the client."""
        client, _username = _api_user(live_app)

        response, _init_kwargs, report = _post_generate_report(client)

        assert response.status_code == 200, (
            "REST endpoint failed to invoke generate_report() end-to-end. "
            f"Response: {response.text[:600]}"
        )
        assert response.json()["content"] == report["content"]

    def test_users_settings_snapshot_reaches_init_search_system(self, live_app):
        """``_init_search_system``'s ``get_llm(settings_snapshot=...)`` call
        is what raised in #4396. A revert of the user-context injection in
        ``api_generate_report`` fails loudly here rather than passing as a
        green mock test.

        The tracer was once named ``_ldr_call_path_tracer``. When
        ``get_all_settings`` started skipping ``_``-named settings rows
        (that namespace is run metadata, so a stored row must not be able
        to pose as it), that tracer stopped reaching any snapshot even
        though the caller's own snapshot still arrived. The tracer is now an
        ordinary key whose per-user value has to arrive unchanged. The
        ``_``-named row is still planted, and must stay out of the
        snapshot."""
        client, username = _api_user(live_app)
        tracer_value = _plant_tracer(username)

        response, init_kwargs, _report = _post_generate_report(client)

        assert response.status_code == 200, response.text[:600]
        assert init_kwargs is not None, (
            "_init_search_system was never called — generate_report returned "
            "200 without initialising the search system."
        )
        snapshot = init_kwargs.get("settings_snapshot")
        assert snapshot is not None, (
            "_init_search_system received no settings_snapshot — this is "
            "exactly the #4396 regression."
        )
        assert snapshot.get(TRACER_KEY) == tracer_value, (
            "the snapshot reaching _init_search_system is not the caller's "
            f"own: {TRACER_KEY}={snapshot.get(TRACER_KEY)!r}, "
            f"first keys {sorted(snapshot)[:10]}"
        )
        assert RESERVED_ROW_KEY not in snapshot, (
            f"the stored row {RESERVED_ROW_KEY!r} reached _init_search_system;"
            " '_'-named keys are run metadata and never come from settings "
            "rows"
        )

    def test_username_reaches_init_search_system(self, live_app):
        client, username = _api_user(live_app)

        response, init_kwargs, _report = _post_generate_report(client)

        assert response.status_code == 200, response.text[:600]
        assert init_kwargs is not None
        assert init_kwargs.get("username") == username


class TestResearchFunctionSignatures:
    """Static: the kwargs the endpoints pass must BIND to the signatures.

    No mocking, no HTTP — pure ``inspect.signature``. ``bind_partial``
    raises ``TypeError`` if any keyword is rejected, including for
    functions that have ``**kwargs`` (where a naive "is the name in
    ``parameters``" check would short-circuit and validate nothing).

    ``grep -rl bind_partial tests/`` finds nothing else on this branch.
    """

    @pytest.mark.parametrize(
        "fn_name, extra_required",
        [
            ("quick_summary", {}),
            ("generate_report", {}),
            ("analyze_documents", {"collection_name": "c"}),
        ],
    )
    def test_endpoint_call_binds_to_function_signature(
        self, fn_name, extra_required
    ):
        from local_deep_research.api import research_functions

        fn = getattr(research_functions, fn_name)
        signature = inspect.signature(fn)

        # Exactly what _load_user_context_into_params writes into params,
        # plus the positional query the handlers pass.
        endpoint_call_kwargs = {
            "query": "q",
            "username": "u",
            "settings_snapshot": {},
            "programmatic_mode": False,
            **extra_required,
        }

        try:
            signature.bind_partial(**endpoint_call_kwargs)
        except TypeError as exc:
            pytest.fail(
                f"REST endpoint cannot call {fn_name}{signature}: {exc}. "
                "This bug class — the endpoint passes kwargs the function "
                "rejects — is invisible to mock-based tests because "
                "MagicMock swallows arbitrary kwargs."
            )


class TestHttpMethods:
    """Wrong verb must be 405, not 200/404/500.

    Framework-provided, but nothing on this branch asserts it, and a route
    accidentally declared with ``methods=["GET", "POST"]`` (or a duplicate
    registration under the other verb) would go unnoticed.
    """

    def test_health_rejects_post(self, live_app):
        """Anonymous, but WITH a CSRF token.

        A bare POST is 403 here, not 405: this branch deliberately dropped
        Flask's blanket ``csrf.exempt`` for the ``api_v1`` blueprint, so
        ``CSRFMiddleware`` answers before routing. Sending a real token
        gets the request past the middleware so the routing layer — the
        thing under test — is the one that answers.
        """
        app, _ = live_app
        client = _client(app)
        token = _csrf(client)

        response = client.post(HEALTH_PATH, headers={"X-CSRFToken": token})

        assert response.status_code == 405

    def test_docs_rejects_post(self, live_app):
        client, _username = _api_user(live_app)
        response = _post(client, DOCS_PATH, {})
        assert response.status_code == 405

    @pytest.mark.parametrize(
        "path",
        [QUICK_SUMMARY_PATH, GENERATE_REPORT_PATH, ANALYZE_DOCUMENTS_PATH],
    )
    def test_post_only_endpoints_reject_get(self, live_app, path):
        client, _username = _api_user(live_app)
        response = client.get(path)
        assert response.status_code == 405, path


class TestHealthTimestamp:
    """``/api/v1/health`` returns a *current* unix timestamp.

    ``tests/web/routers/test_health_diagnostics.py`` and
    ``test_api_v1_external_contract.py`` between them pin the key set and
    ``isinstance(timestamp, float)`` — a hardcoded ``0.0`` satisfies both.
    """

    def test_timestamp_is_recent(self, live_app):
        import time

        app, _ = live_app
        response = _client(app).get(HEALTH_PATH)

        assert response.status_code == 200
        timestamp = response.json()["timestamp"]
        assert abs(timestamp - time.time()) < 5, (
            f"health timestamp is not current: {timestamp}"
        )