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.

test(api): real-path regression tests for /generate_report user-context (#4396) (#4552)

LearningCircuita83fe433aa16
diff --git a/tests/web/test_api_coverage.py b/tests/web/test_api_coverage.py
index d56038ded8..99a8162b12 100644
--- a/tests/web/test_api_coverage.py
+++ b/tests/web/test_api_coverage.py
@@ -1444,6 +1444,110 @@ class TestRestToResearchFunctionCallPath:
f"setting key. args: {write_call['args']!r}"
)
+ # ---------------------------------------------------------------
+ # /generate_report real-path tests (issue #4396)
+ # ---------------------------------------------------------------
+ # Unlike analyze_documents (a flat get_search -> get_llm -> summarize
+ # function), generate_report runs the whole research engine
+ # (_init_search_system -> AdvancedSearchSystem.analyze_topic ->
+ # IntegratedReportGenerator), so get_llm/get_search alone is the wrong
+ # seam — it would run the full engine. The deepest function specific to
+ # the generate_report wrapper is _init_search_system, and its
+ # get_llm(settings_snapshot=...) call (research_functions.py) is the
+ # EXACT line that 500'd in #4396 when the REST endpoint failed to inject
+ # the user's snapshot (no provider/api_key -> LLM init failure). So mock
+ # _init_search_system + IntegratedReportGenerator and let the real
+ # generate_report() body run: snapshot threading, search-context setup,
+ # report assembly, and the REST handler's truncation/response logic.
+
+ _GR_REPORT = {"content": "Final report body.", "metadata": {"query": "q"}}
+
+ def _post_generate_report(self, authed_client):
+ """Fire a real /api/v1/generate_report request WITHOUT mocking
+ generate_report itself — only _init_search_system (one layer down,
+ whose get_llm call is the #4396 failure point) and the report
+ generator. Returns ``(response, init_kwargs)`` where ``init_kwargs``
+ is the kwargs _init_search_system actually received (or None if it
+ was never called), so individual tests assert on the response or on
+ the user context that reached it."""
+ captured = {}
+
+ def _capture_init(*args, **kwargs):
+ captured["kwargs"] = kwargs
+ stub_system = MagicMock()
+ stub_system.analyze_topic.return_value = {
+ "findings": [],
+ "current_knowledge": "",
+ }
+ return stub_system
+
+ with _mock_access_control():
+ with (
+ patch(
+ "local_deep_research.api.research_functions._init_search_system",
+ side_effect=_capture_init,
+ ),
+ patch(
+ "local_deep_research.api.research_functions.IntegratedReportGenerator"
+ ) as mock_rg_cls,
+ # _close_system runs in generate_report's finally; with a
+ # MagicMock system it would call safe_close on auto-created
+ # attributes. No-op it to keep the test focused.
+ patch(
+ "local_deep_research.api.research_functions._close_system"
+ ),
+ ):
+ mock_rg_cls.return_value.generate_report.return_value = (
+ self._GR_REPORT
+ )
+ resp = authed_client.post(
+ "/api/v1/generate_report", json={"query": "q"}
+ )
+ return resp, captured.get("kwargs")
+
+ def test_generate_report_full_call_path(self, authed_client):
+ """The real generate_report() body runs end-to-end via REST (only
+ _init_search_system + the report generator are mocked) and returns
+ 200 — not the original #4396 500 — with the report content passed
+ back to the client. The per-input user-context assertions live in
+ the next test."""
+ resp, _ = self._post_generate_report(authed_client)
+ assert resp.status_code == 200, (
+ f"REST endpoint failed to invoke generate_report() end-to-end. "
+ f"Issue #4396 was a 500 here (no provider/api_key reached LLM "
+ f"init). Response: {resp.get_data(as_text=True)}"
+ )
+ assert resp.get_json()["content"] == self._GR_REPORT["content"]
+
+ def test_generate_report_threads_user_context_to_search_system(
+ self, authed_client
+ ):
+ """The #4396 fix, verified at the failure site: the authenticated
+ user's settings_snapshot (and username) must reach
+ _init_search_system, whose get_llm(settings_snapshot=...) call is
+ what raised when the snapshot was missing. A future revert of the
+ user-context injection in api_generate_report fails loudly here —
+ not as a silent green mock test."""
+ resp, init_kwargs = self._post_generate_report(authed_client)
+ assert resp.status_code == 200, resp.get_data(as_text=True)
+ assert init_kwargs is not None, (
+ "_init_search_system was never called — generate_report "
+ "returned 200 without initializing 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 (no provider/api_key -> LLM init "
+ "500)."
+ )
+ assert snapshot.get("_ldr_test_tracer") == "tracer-value", (
+ f"settings_snapshot reaching _init_search_system is not the "
+ f"user's snapshot. Got: {snapshot!r}"
+ )
+ assert init_kwargs.get("username") == "testuser", (
+ "username did not reach _init_search_system"
+ )
+
# ===================================================================
# Research-function signature compatibility