~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Termination, cleanup and cancellation contracts of ``research_service``.
Ported from three files the Flask->FastAPI migration deleted:
``tests/web/services/test_research_service_extra_coverage.py``,
``test_research_service_coverage_gaps.py`` and
``test_research_service_deep_coverage.py``.
``cleanup_research_resources`` is the reason this module exists. Every
research run ends in it -- normal completion, user stop, and failure alike --
and on the branch it had **no test that ever called it**. It appears twenty
times across the suite, always as ``patch(f"{MODULE}.cleanup_research_resources")``,
i.e. as something to be mocked away so a *neighbouring* function can be
driven. Nothing pinned:
* that the queue processor is told the run finished, with the run's owner and
``user_password`` forwarded (the notification is what writes the terminal
row; a silently-skipped notify leaves the research stuck "in progress"),
* that a missing username *skips* that notify rather than notifying with
``None``,
* that the final socket message reports the caller's ``final_status`` -- the
regression the ``final_status`` parameter exists for: a hard-coded
COMPLETED made a user-stopped run render as 100%/Completed with an answer
over a stopped state,
* that a failing socket emit cannot escape and abort cleanup,
* that ``remove_subscriptions_for_research`` is scoped to the owner.
The remaining classes cover the branches of ``cancel_research`` that the
branch's own tests reach past (the terminal-state short circuit, the
unclaimed-queued cleanup, the two outer ``except`` arms) and
``handle_termination``'s "queue processor is down, clean up anyway" path.
Translation from main: ``web/routes/globals`` -> ``web/research_state``, and
the ``SocketIOService()`` singleton -> the module-level ``_sio_emit`` /
``_sio_remove`` aliases over ``socketio_asgi``.
"""
from contextlib import ExitStack, contextmanager
from unittest.mock import MagicMock, patch
from local_deep_research.constants import ResearchStatus
MODULE = "local_deep_research.web.services.research_service"
STATE_MOD = "local_deep_research.web.research_state"
QUEUE_PROC_MOD = "local_deep_research.web.queue.processor_v2"
ENV_REGISTRY_MOD = "local_deep_research.settings.env_registry"
def _fake_session_ctx(session=None):
if session is None:
session = MagicMock()
@contextmanager
def ctx(username=None):
yield session
return ctx
@contextmanager
def _apply(patches, extra=()):
"""Enter every ``target -> mock`` in ``patches`` plus any ready-made CMs."""
with ExitStack() as stack:
for cm in extra:
stack.enter_context(cm)
for target, mock_obj in patches.items():
stack.enter_context(patch(target, mock_obj))
yield
def _make_mock_research(status=None):
r = MagicMock()
r.status = status
return r
# ---------------------------------------------------------------------------
# cleanup_research_resources
# ---------------------------------------------------------------------------
class TestCleanupResearchResources:
"""The end-of-run path itself, driven rather than mocked."""
@staticmethod
def _run(
research_id="res-1",
username="testuser",
test_mode=False,
emit_side_effect=None,
**kwargs,
):
from local_deep_research.web.services.research_service import (
cleanup_research_resources,
)
qp = MagicMock()
emit = MagicMock(side_effect=emit_side_effect)
remove = MagicMock()
cleanup = MagicMock()
with (
patch(f"{STATE_MOD}.cleanup_research", cleanup),
patch(f"{QUEUE_PROC_MOD}.queue_processor", qp),
patch(f"{ENV_REGISTRY_MOD}.is_test_mode", return_value=test_mode),
patch(f"{MODULE}._sio_emit", emit),
patch(f"{MODULE}._sio_remove", remove),
patch(f"{MODULE}.time.sleep") as sleep,
):
cleanup_research_resources(research_id, username, **kwargs)
return {
"queue": qp,
"emit": emit,
"remove": remove,
"cleanup": cleanup,
"sleep": sleep,
}
def test_removes_the_run_from_the_active_registry(self):
"""``cleanup_research`` drops the id from ``_active_research`` and the
termination-flag map together. Skipping it strands the id: a rerun of
the same research is refused by ``check_and_start_research`` forever."""
got = self._run(research_id=123)
got["cleanup"].assert_called_once_with(123)
def test_cancel_handoff_can_preserve_the_worker_termination_flag(self):
"""The HTTP stop cleanup releases the slot without eating its signal."""
got = self._run(research_id=123, preserve_termination_flag=True)
got["cleanup"].assert_called_once_with(
123, preserve_termination_flag=True
)
def test_notifies_the_queue_processor_with_owner_and_password(self):
"""The notify is what writes the terminal DB row. ``user_password`` is
forwarded, not defaulted at the call site -- the processor needs it to
reopen the owner's encrypted database from the main thread."""
got = self._run(research_id=123, username="testuser")
got["queue"].notify_research_completed.assert_called_once_with(
"testuser", 123, user_password=None
)
def test_a_supplied_password_reaches_the_queue_processor(self):
got = self._run(research_id=123, user_password="s3cret")
got["queue"].notify_research_completed.assert_called_once_with(
"testuser", 123, user_password="s3cret"
)
def test_no_username_skips_the_notify_entirely(self):
"""Without an owner the processor cannot open a database, so the call
is skipped rather than made with ``None``."""
got = self._run(username=None)
got["queue"].notify_research_completed.assert_not_called()
def test_the_default_final_message_reports_completed_at_100(self):
got = self._run()
got["emit"].assert_called_once()
event, research_id, payload = got["emit"].call_args.args
assert event == "research_progress"
assert research_id == "res-1"
assert payload["status"] == ResearchStatus.COMPLETED
assert payload["progress"] == 100
def test_a_suspended_run_does_not_announce_itself_completed(self):
"""Regression: the termination path passes
``final_status=SUSPENDED``, so the final socket message must report
SUSPENDED at 0% -- NOT a hard-coded 'completed' at 100%. The old
hard-coded COMPLETED made the chat client render an answer over a
stopped state and flipped the progress page to 100%/Completed on a
user stop."""
got = self._run(final_status=ResearchStatus.SUSPENDED)
payload = got["emit"].call_args.args[2]
assert payload["status"] == ResearchStatus.SUSPENDED
assert payload["status"] != ResearchStatus.COMPLETED
assert payload["progress"] == 0
def test_a_failed_run_does_not_announce_itself_completed(self):
got = self._run(final_status=ResearchStatus.FAILED)
payload = got["emit"].call_args.args[2]
assert payload["status"] == ResearchStatus.FAILED
assert payload["progress"] == 0
def test_the_final_message_is_addressed_to_the_runs_owner(self):
"""Subscriptions are keyed ``(owner, research_id)``; a final message
sent without the owner reaches nobody, and the progress UI stalls at
whatever frame arrived last."""
got = self._run(username="alice")
assert got["emit"].call_args.kwargs["owner"] == "alice"
def test_the_subscription_teardown_is_scoped_to_the_owner(self):
"""Benchmark ids autoincrement per user, so run "1" exists for
everyone. Removing by id alone would tear down every other user's
subscription to their own run 1."""
got = self._run(research_id=1, username="alice")
got["remove"].assert_called_once_with(1, "alice")
def test_a_failing_socket_emit_cannot_abort_cleanup(self):
"""The emit is best-effort; an exception must not escape
``cleanup_research_resources`` into the worker's ``finally``."""
got = self._run(emit_side_effect=RuntimeError("socket down"))
# Reached and survived: the notify (before the try) still ran, and no
# exception propagated.
got["queue"].notify_research_completed.assert_called_once()
def test_test_mode_sleeps_before_cleaning_up(self):
"""The deliberate delay that lets concurrency-limit tests observe a
run still holding its slot."""
got = self._run(test_mode=True)
got["sleep"].assert_called_once_with(5)
def test_outside_test_mode_nothing_sleeps(self):
got = self._run(test_mode=False)
got["sleep"].assert_not_called()
# ---------------------------------------------------------------------------
# handle_termination
# ---------------------------------------------------------------------------
class TestHandleTermination:
def test_queues_a_suspended_update_and_cleans_up_as_suspended(self):
from local_deep_research.web.services.research_service import (
handle_termination,
)
qp = MagicMock()
with (
patch(f"{QUEUE_PROC_MOD}.queue_processor", qp),
patch(f"{MODULE}.cleanup_research_resources") as cleanup,
):
handle_termination("res-1", "testuser")
qp.queue_error_update.assert_called_once()
kwargs = qp.queue_error_update.call_args.kwargs
assert kwargs["username"] == "testuser"
assert kwargs["research_id"] == "res-1"
assert kwargs["status"] == ResearchStatus.SUSPENDED
assert kwargs["error_message"] == "Research was terminated by user"
# Cleanup must be told this ended via termination so the final socket
# message reports SUSPENDED, not a spurious "completed".
cleanup.assert_called_once_with(
"res-1", "testuser", final_status=ResearchStatus.SUSPENDED
)
def test_a_dead_queue_processor_still_cleans_up(self):
"""The queue update is wrapped in its own ``except``; losing it must
not leave the run's resources (registry entry, socket subscriptions)
held forever."""
from local_deep_research.web.services.research_service import (
handle_termination,
)
qp = MagicMock()
qp.queue_error_update.side_effect = RuntimeError("queue down")
with (
patch(f"{QUEUE_PROC_MOD}.queue_processor", qp),
patch(f"{MODULE}.cleanup_research_resources") as cleanup,
):
handle_termination("res-1", "testuser")
cleanup.assert_called_once_with(
"res-1", "testuser", final_status=ResearchStatus.SUSPENDED
)
# ---------------------------------------------------------------------------
# cancel_research
# ---------------------------------------------------------------------------
class TestCancelResearch:
def test_an_active_run_is_terminated_through_handle_termination(self):
from local_deep_research.web.services.research_service import (
cancel_research,
)
with (
# Ownership gate reads the caller's DB first; a mock session makes
# the ownership query return a row so the owner is authorized.
patch(f"{MODULE}.get_user_db_session", _fake_session_ctx()),
patch(f"{STATE_MOD}.set_termination_flag") as flag,
patch(f"{STATE_MOD}.is_research_active", return_value=True),
patch(f"{MODULE}.handle_termination") as term,
):
result = cancel_research("res-1", "testuser")
assert result is True
flag.assert_called_once_with("res-1")
term.assert_called_once_with(
"res-1", "testuser", preserve_termination_flag=True
)
def test_a_run_absent_from_the_database_is_not_cancelled(self):
from local_deep_research.web.services.research_service import (
cancel_research,
)
ms = MagicMock()
ms.query.return_value.filter_by.return_value.first.return_value = None
with (
patch(f"{STATE_MOD}.set_termination_flag"),
patch(f"{STATE_MOD}.is_research_active", return_value=False),
patch(f"{MODULE}.get_user_db_session", _fake_session_ctx(ms)),
):
result = cancel_research("no-such", "testuser")
assert result is False
def test_an_already_finished_run_reports_success_without_touching_it(self):
"""Cancelling something already stopped is not an error -- the caller
asked for it to be stopped and it is."""
from local_deep_research.web.services.research_service import (
cancel_research,
)
research = _make_mock_research(status=ResearchStatus.COMPLETED)
ms = MagicMock()
ms.query.return_value.filter_by.return_value.first.return_value = (
research
)
with (
patch(f"{STATE_MOD}.set_termination_flag"),
patch(f"{STATE_MOD}.is_research_active", return_value=False),
patch(f"{MODULE}.get_user_db_session", _fake_session_ctx(ms)),
):
result = cancel_research("res-1", "testuser")
assert result is True
assert research.status == ResearchStatus.COMPLETED
ms.commit.assert_not_called()
def test_a_live_but_unregistered_run_is_suspended_and_committed(self):
from local_deep_research.web.services.research_service import (
cancel_research,
)
research = _make_mock_research(status=ResearchStatus.IN_PROGRESS)
ms = MagicMock()
ms.query.return_value.filter_by.return_value.first.return_value = (
research
)
with (
patch(f"{STATE_MOD}.set_termination_flag"),
patch(f"{STATE_MOD}.is_research_active", return_value=False),
patch(f"{MODULE}.get_user_db_session", _fake_session_ctx(ms)),
):
result = cancel_research("res-1", "testuser")
assert result is True
assert research.status == ResearchStatus.SUSPENDED
ms.commit.assert_called_once()
def test_cancelling_an_unclaimed_queued_run_clears_its_queue_state(self):
"""The queued row, its task metadata and the queue counters are torn
down in the SAME transaction that suspends the parent -- otherwise the
run disappears from the UI while the scheduler still counts it."""
from local_deep_research.web.services.research_service import (
cancel_research,
)
research = _make_mock_research(status=ResearchStatus.QUEUED)
ms = MagicMock()
ms.query.return_value.filter_by.return_value.first.return_value = (
research
)
with (
patch(f"{STATE_MOD}.set_termination_flag"),
patch(f"{STATE_MOD}.is_research_active", return_value=False),
patch(f"{MODULE}.get_user_db_session", _fake_session_ctx(ms)),
patch(
"local_deep_research.web.queue.lifecycle_cleanup"
".cleanup_queued_research_state"
) as cleanup,
):
result = cancel_research("res-1", "testuser")
assert result is True
cleanup.assert_called_once_with(ms, ["res-1"], acting_user="testuser")
def test_a_run_past_the_queue_keeps_its_worker_handoff_state(self):
"""In the spawn-grace window the row is already IN_PROGRESS: the
queued-state teardown must NOT run, or the worker loses the rows it
needs for its own termination path."""
from local_deep_research.web.services.research_service import (
cancel_research,
)
research = _make_mock_research(status=ResearchStatus.IN_PROGRESS)
ms = MagicMock()
ms.query.return_value.filter_by.return_value.first.return_value = (
research
)
with (
patch(f"{STATE_MOD}.set_termination_flag"),
patch(f"{STATE_MOD}.is_research_active", return_value=False),
patch(f"{MODULE}.get_user_db_session", _fake_session_ctx(ms)),
patch(
"local_deep_research.web.queue.lifecycle_cleanup"
".cleanup_queued_research_state"
) as cleanup,
):
result = cancel_research("res-1", "testuser")
assert result is True
cleanup.assert_not_called()
def test_a_database_failure_in_the_inactive_branch_reports_failure(self):
from local_deep_research.web.services.research_service import (
cancel_research,
)
with (
patch(f"{STATE_MOD}.set_termination_flag"),
patch(f"{STATE_MOD}.is_research_active", return_value=False),
patch(
f"{MODULE}.get_user_db_session",
side_effect=RuntimeError("db connection lost"),
),
):
result = cancel_research("res-1", "testuser")
assert result is False
def test_a_failing_termination_flag_reports_failure_not_a_crash(self):
"""The outer ``except`` exists so a broken registry cannot raise into
the HTTP handler that called cancel."""
from local_deep_research.web.services.research_service import (
cancel_research,
)
with (
patch(f"{MODULE}.get_user_db_session", _fake_session_ctx()),
patch(
f"{STATE_MOD}.set_termination_flag",
side_effect=RuntimeError("unexpected failure"),
),
):
result = cancel_research("res-1", "testuser")
assert result is False
def test_a_failing_activity_check_reports_failure_not_a_crash(self):
from local_deep_research.web.services.research_service import (
cancel_research,
)
with (
patch(f"{MODULE}.get_user_db_session", _fake_session_ctx()),
patch(f"{STATE_MOD}.set_termination_flag"),
patch(
f"{STATE_MOD}.is_research_active",
side_effect=RuntimeError("state corrupted"),
),
):
result = cancel_research("res-1", "testuser")
assert result is False
# ---------------------------------------------------------------------------
# save_research_strategy / get_research_strategy
# ---------------------------------------------------------------------------
class TestSaveResearchStrategy:
def test_creates_a_row_when_the_research_has_no_strategy_yet(self):
from local_deep_research.web.services.research_service import (
save_research_strategy,
)
ms = MagicMock()
ms.query.return_value.filter_by.return_value.first.return_value = None
with (
patch(f"{MODULE}.get_user_db_session", _fake_session_ctx(ms)),
patch(f"{MODULE}.ResearchStrategy") as strategy_cls,
):
save_research_strategy(42, "source-based", username="user1")
strategy_cls.assert_called_once_with(
research_id=42, strategy_name="source-based"
)
ms.add.assert_called_once()
ms.commit.assert_called_once()
def test_updates_the_existing_row_instead_of_inserting_a_duplicate(self):
from local_deep_research.web.services.research_service import (
save_research_strategy,
)
existing = MagicMock()
ms = MagicMock()
ms.query.return_value.filter_by.return_value.first.return_value = (
existing
)
with patch(f"{MODULE}.get_user_db_session", _fake_session_ctx(ms)):
save_research_strategy(42, "langgraph-agent", username="user1")
assert existing.strategy_name == "langgraph-agent"
ms.add.assert_not_called()
ms.commit.assert_called_once()
def test_a_database_failure_is_swallowed(self):
"""Recording the strategy is bookkeeping; it must never fail a run."""
from local_deep_research.web.services.research_service import (
save_research_strategy,
)
with patch(
f"{MODULE}.get_user_db_session",
side_effect=RuntimeError("db down"),
):
save_research_strategy(99, "source-based", username="user1")
class TestGetResearchStrategy:
def test_returns_the_stored_name_verbatim(self):
"""Including a strategy that no longer exists in the factory (#4548):
this is a display-only read, so a saved 'mcp' research keeps loading
after the strategy itself is gone."""
from local_deep_research.web.services.research_service import (
get_research_strategy,
)
strategy = MagicMock()
strategy.strategy_name = "mcp"
ms = MagicMock()
ms.query.return_value.filter_by.return_value.first.return_value = (
strategy
)
with patch(f"{MODULE}.get_user_db_session", _fake_session_ctx(ms)):
assert get_research_strategy(7, username="user1") == "mcp"
def test_returns_none_when_no_row_exists(self):
from local_deep_research.web.services.research_service import (
get_research_strategy,
)
ms = MagicMock()
ms.query.return_value.filter_by.return_value.first.return_value = None
with patch(f"{MODULE}.get_user_db_session", _fake_session_ctx(ms)):
assert get_research_strategy(99, username="user1") is None
def test_returns_none_on_a_database_failure(self):
from local_deep_research.web.services.research_service import (
get_research_strategy,
)
with patch(
f"{MODULE}.get_user_db_session", side_effect=Exception("db err")
):
assert get_research_strategy(1, username="user1") is None
# ---------------------------------------------------------------------------
# _generate_report_path
# ---------------------------------------------------------------------------
class TestGenerateReportPath:
def test_produces_a_markdown_report_name(self):
from local_deep_research.web.services.research_service import (
_generate_report_path,
)
path = _generate_report_path("What is machine learning?")
assert path.suffix == ".md"
assert "research_report_" in path.name
def test_survives_unicode_and_shell_metacharacters(self):
from local_deep_research.web.services.research_service import (
_generate_report_path,
)
path = _generate_report_path("日本語クエリ $pecial Ch@rs!")
assert path.suffix == ".md"
assert "research_report_" in path.name
def test_different_queries_do_not_collide(self):
from local_deep_research.web.services.research_service import (
_generate_report_path,
)
assert (
_generate_report_path("query one").name
!= _generate_report_path("query two").name
)
def test_the_same_query_hashes_the_same_way(self):
from local_deep_research.web.services.research_service import (
_generate_report_path,
)
first = _generate_report_path("stable query").name
second = _generate_report_path("stable query").name
# The timestamp and random-uniqueness segments differ between the
# two calls; the hash segment that identifies the query must not.
# (Segments: research_report_<hash>_<ts>_<uniq>.md)
assert first.split("_")[2] == second.split("_")[2]
def test_the_path_stays_under_the_configured_output_directory(self):
from local_deep_research.web.services import research_service
path = research_service._generate_report_path("some query")
assert path.parent == research_service.OUTPUT_DIR
# ---------------------------------------------------------------------------
# The terminal status each of run_research_process's own exits reports
# ---------------------------------------------------------------------------
class TestTheWorkerReportsTheRealTerminalStatus:
"""``final_status`` is a per-call-site tail argument, and only two of the
four ``cleanup_research_resources`` call sites inside
``run_research_process`` pass it.
That is the shape that hides: the parameter exists, its default is
COMPLETED, and dropping the keyword from one call site is a one-line diff
that changes nothing anyone can see from the outside except the status the
user's browser is told. The success sites (quick-summary and normal
completion) correctly rely on the default; the two guarded ones do not,
and neither guard is pinned on the branch --
``tests/security/test_research_service_isolation_fastapi.py`` drives the
pre-start termination exit but asserts only ``call_args.args[1]``, the
username.
Ported from ``test_research_service_coverage_gaps.py``
(``TestRunResearchEarlyTermination``) and
``test_research_service_export_coverage.py``
(``TestRunResearchProcessLlmConfigError``).
"""
def test_a_run_cancelled_before_it_starts_reports_suspended(self):
"""Not "completed at 100%". The user pressed stop before the worker
got going; the progress page must say so."""
from tests.web.services.helpers import (
MODULE as H_MODULE,
RESEARCH_STATE_MOD,
_base_run_patches,
_get_raw_run_research_process,
)
patches = _base_run_patches()
patches[f"{RESEARCH_STATE_MOD}.is_termination_requested"] = MagicMock(
return_value=True
)
patches[f"{H_MODULE}.AdvancedSearchSystem"] = MagicMock()
with _apply(patches):
_get_raw_run_research_process()(
1, "test query", "quick", username="testuser"
)
cleanup = patches[f"{H_MODULE}.cleanup_research_resources"]
cleanup.assert_called_once_with(
1,
"testuser",
user_password=None,
final_status=ResearchStatus.SUSPENDED,
)
# The run really did stop before doing any work.
patches[f"{H_MODULE}.AdvancedSearchSystem"].assert_not_called()
def test_a_run_that_failed_reports_failed(self):
"""The error handler's cleanup passes FAILED. Without it the browser
is told the run completed and then shown an error report."""
from tests.web.services.helpers import (
MODULE as H_MODULE,
_base_run_patches,
_egress_and_search_patches,
_get_raw_run_research_process,
)
patches = _base_run_patches()
patches[f"{H_MODULE}.get_llm"] = MagicMock(
side_effect=RuntimeError("model path not found on llamacpp server")
)
with _apply(patches, extra=_egress_and_search_patches()):
_get_raw_run_research_process()(
3,
"some query",
"quick",
username="testuser",
model="custom-model",
model_provider="llamacpp",
settings_snapshot={"search.tool": "searxng"},
)
cleanup = patches[f"{H_MODULE}.cleanup_research_resources"]
cleanup.assert_called_once_with(
3,
"testuser",
user_password=None,
final_status=ResearchStatus.FAILED,
)
class TestTheWorkerFailsClosedWithoutAConfiguredPrimaryEngine:
"""The CLI, scheduler and queue paths reach the worker without the API's
run-start egress precheck, so this ``raise`` is the only gate they meet.
A snapshot with no ``search.tool`` makes ``build_run_egress_context``
raise; the worker must refuse the run rather than proceed on whatever
default the search factory would pick. Widening the block's trailing
``except ImportError`` to ``except Exception`` -- a one-token change that
reads as defensive -- makes the whole gate permissive and lets the run
proceed unprotected; ``tests/web/routes/test_research_routes_policy.py``
stays green through it, because that covers the API precheck, which these
entry points never reach.
Ported from ``test_research_service_coverage_gaps.py``
(``TestWorkerFailsClosedOnMissingPrimary``).
"""
def test_a_snapshot_without_a_primary_engine_never_reaches_the_search_system(
self,
):
from tests.web.services.helpers import (
MODULE as H_MODULE,
_base_run_patches,
_get_raw_run_research_process,
)
system = MagicMock()
system.analyze_topic.return_value = {
"findings": "x",
"formatted_findings": "x",
}
patches = _base_run_patches()
# The LLM and search factories are stubbed to SUCCEED, so the only
# thing that can stop the run before the search system is the egress
# build refusing the snapshot.
patches[f"{H_MODULE}.get_llm"] = MagicMock(return_value=MagicMock())
patches[f"{H_MODULE}.get_search"] = MagicMock(return_value=MagicMock())
patches[f"{H_MODULE}.AdvancedSearchSystem"] = MagicMock(
return_value=system
)
with _apply(patches):
_get_raw_run_research_process()(
1, "test", "quick", username="user1", settings_snapshot={}
)
system.analyze_topic.assert_not_called()
def test_positive_control_a_snapshot_with_one_does_reach_it(self):
"""Without this, the assertion above would also hold for a worker that
refused every run."""
from tests.web.services.helpers import (
MODULE as H_MODULE,
_base_run_patches,
_get_raw_run_research_process,
)
system = MagicMock()
system.analyze_topic.return_value = {
"findings": "x",
"formatted_findings": "x",
}
patches = _base_run_patches()
patches[f"{H_MODULE}.get_llm"] = MagicMock(return_value=MagicMock())
patches[f"{H_MODULE}.get_search"] = MagicMock(return_value=MagicMock())
patches[f"{H_MODULE}.AdvancedSearchSystem"] = MagicMock(
return_value=system
)
# Same patches as above, same real egress build -- only the snapshot
# differs.
with _apply(patches):
_get_raw_run_research_process()(
1,
"test",
"quick",
username="user1",
settings_snapshot={"search.tool": "searxng"},
)
system.analyze_topic.assert_called_once()
class TestGenerateReportPathHash:
"""The filename carries a deterministic digest of the query.
Ported from ``test_research_service_export_coverage.py``. It is what makes
two reports for the same query land on the same name modulo timestamp; a
random or time-seeded component here would silently break that.
"""
@staticmethod
def _digest(query):
import hashlib
return hashlib.md5( # DevSkim: ignore DS126858
query.encode("utf-8"), usedforsecurity=False
).hexdigest()[:10]
def test_the_filename_embeds_the_querys_digest(self):
from local_deep_research.web.services.research_service import (
_generate_report_path,
)
query = "what is the impact of climate change on biodiversity"
name = _generate_report_path(query).name
assert self._digest(query) in name
assert "research_report" in name
def test_the_digest_is_stable_across_calls(self):
from local_deep_research.web.services.research_service import (
_generate_report_path,
)
query = "deterministic hash test"
assert self._digest(query) in _generate_report_path(query).name
assert self._digest(query) in _generate_report_path(query).name