~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
"""Ported from ``origin/main:tests/web/services/test_research_service.py``.
That module was deleted by the Flask->FastAPI migration. The module under
test, ``web/services/research_service.py``, survived the migration almost
unchanged (68 insertions / 22 deletions, identical set of top-level
functions), so nearly every assertion in the deleted file still describes a
real, live contract.
What changed, and how the plumbing was translated
-------------------------------------------------
* ``research_service`` no longer imports ``thread_context`` /
``thread_with_app_context`` — the Flask app-context wrapper around the
worker callback was deleted (d9168614b) and replaced by a
``contextvars.copy_context()`` hand-off. Main's ``@patch`` of those two
names would now raise ``AttributeError`` at decoration time, so those
patches were dropped. Nothing was asserted about them; they were pure
plumbing.
* ``web/routes/globals`` is now a re-export shim over
``web/research_state``. ``start_research_process`` /
``cleanup_research_resources`` / ``cancel_research`` all do a
function-local ``from ..research_state import ...``, so the shim is NOT a
working patch target for them — every such target was retargeted at
``local_deep_research.web.research_state``. (Same trap documented in
``tests/web/services/helpers.py``.)
* ``web/services/socket_service.SocketIOService`` is gone. The worker and
the cleanup path now emit through the module-level aliases
``research_service._sio_emit`` / ``._sio_remove`` (and the
``_socket_emitter`` adapter). Main's ``@patch(...SocketIOService)`` in the
cleanup tests was socket ISOLATION, not an assertion, so it was retargeted
at those two aliases — with ``create_autospec``, matching the branch idiom
in ``helpers.py``, so a signature drift (e.g. the required ``owner=``
kwarg) fails loudly instead of being swallowed by the surrounding
``except``.
* ``start_research_process`` front-loads a global concurrency semaphore.
Main's tests let the real semaphore leak a slot per test (the mocked
thread never runs, so nothing releases it). It is mocked here so the file
cannot exhaust the process-wide limit for later tests.
Tests from the source file that are NOT here are covered by an existing
branch test; the per-test verdicts are in the port report. In summary:
``TestGetCitationFormatter`` -> ``test_research_service_helpers.py``;
``TestSaveResearchStrategy`` / ``TestGetResearchStrategy`` ->
``tests/security/test_research_service_isolation_fastapi.py::TestStrategyUsernameBarrier``;
``TestGenerateReportPathUniqueHash`` -> ``...::TestGenerateReportPathContainment``;
the kwargs-into-``settings`` cases -> ``test_research_service_start_process.py::
TestCapacityAndSpawnGuards::test_success_registers_thread_and_returns_it``;
the inactive/terminal ``cancel_research`` cases ->
``...::TestCancelResearchIsolation``; and most of
``TestParseResearchMetadata`` -> ``test_research_service_helpers.py``.
"""
import hashlib
from unittest.mock import MagicMock, Mock, create_autospec, patch
import pytest
from local_deep_research.web.services import research_service
SERVICE = "local_deep_research.web.services.research_service"
RESEARCH_STATE = "local_deep_research.web.research_state"
QUEUE_PROC = "local_deep_research.web.queue.processor_v2"
ENV_REGISTRY = "local_deep_research.settings.env_registry"
def _mock_semaphore():
"""A stand-in for ``_global_research_semaphore`` that always grants."""
sem = Mock()
sem.acquire.return_value = True
return sem
def _socket_isolation_patches():
"""Autospecced replacements for the two socketio_asgi aliases.
The Flask ``SocketIOService`` class main patched here no longer exists;
these are the names ``cleanup_research_resources`` actually binds.
"""
return (
patch(
f"{SERVICE}._sio_emit",
create_autospec(research_service._sio_emit, spec_set=True),
),
patch(
f"{SERVICE}._sio_remove",
create_autospec(research_service._sio_remove, spec_set=True),
),
)
# ---------------------------------------------------------------------------
# export_report_to_memory — end-to-end through the REAL exporter registry.
#
# The branch's TestExportReportToMemory (test_research_service_helpers.py)
# mocks ``ExporterRegistry`` wholesale, so it pins the delegation but not one
# byte of real output: with every exporter broken it would still be green.
# These are the format/mimetype/magic-byte contracts.
# ---------------------------------------------------------------------------
class TestExportReportToMemory:
"""Ported from ``origin/main:...::TestExportReportToMemory``.
Delete the LaTeX/RIS/Quarto/PDF exporter registrations and every one of
these goes red; the branch's mocked-registry successor would not.
"""
def test_export_latex_format(self):
"""export_report_to_memory generates LaTeX content."""
content, filename, mimetype = research_service.export_report_to_memory(
"# Test Report\n\nThis is test content.",
"latex",
title="Test Report",
)
assert filename.endswith(".tex")
assert mimetype == "text/plain"
assert isinstance(content, bytes)
def test_export_ris_format(self):
"""export_report_to_memory generates RIS content."""
content, filename, mimetype = research_service.export_report_to_memory(
"# Test Report\n\nThis is test content.",
"ris",
title="Test Report",
)
assert filename.endswith(".ris")
assert mimetype == "text/plain"
assert isinstance(content, bytes)
def test_export_unsupported_format_raises(self):
"""Unsupported format raises ValueError.
Unlike the branch's successor this goes through the REAL registry,
so it also pins that no catch-all exporter answers for an unknown
name.
"""
with pytest.raises(ValueError, match="Unsupported export format"):
research_service.export_report_to_memory(
"# Test Report", "unsupported"
)
def test_export_quarto_format(self):
"""export_report_to_memory generates Quarto zip content."""
content, filename, mimetype = research_service.export_report_to_memory(
"# Test Report\n\nThis is test content.",
"quarto",
title="Test Report",
)
assert filename.endswith(".zip")
assert mimetype == "application/zip"
assert isinstance(content, bytes)
def test_export_pdf_format(self):
"""export_report_to_memory generates PDF content."""
content, filename, mimetype = research_service.export_report_to_memory(
"# Test Report\n\nThis is test content.",
"pdf",
title="Test Report",
)
assert filename.endswith(".pdf")
assert mimetype == "application/pdf"
# PDF files start with %PDF
assert content.startswith(b"%PDF")
class TestExportQuartoFormat:
"""Ported from ``origin/main:...::TestExportQuartoFormat``."""
def test_export_quarto_creates_zip(self):
"""The quarto payload is a real archive, not just a .zip name."""
content, filename, mimetype = research_service.export_report_to_memory(
"# Test Report\n\nThis is test content.",
"quarto",
title="Test Report",
)
assert filename.endswith(".zip")
assert mimetype == "application/zip"
assert isinstance(content, bytes)
# Verify it's a valid zip file by checking magic bytes
assert content[:2] == b"PK"
class TestTitlePrepending:
"""Ported from ``origin/main:...::TestTitlePrepending``.
``export_report_to_memory`` delegates title handling to each exporter's
``_prepend_title_if_needed``. ``tests/exporters/test_base_exporter.py``
unit-tests that helper on ``BaseExporter``; it does NOT pin which
concrete exporters call it. These tests pin the end-to-end result: a
markdown title must never leak into a structured format (RIS records,
a LaTeX preamble, Quarto front matter), and the markdown-rendering
formats must still produce valid output when it is applied.
"""
def test_pdf_gets_title_prepended(self):
"""PDF format should get markdown title prepended."""
content, filename, mimetype = research_service.export_report_to_memory(
"This is content without a title heading.",
"pdf",
title="My Report Title",
)
assert content.startswith(b"%PDF")
assert filename.endswith(".pdf")
def test_odt_gets_title_prepended(self):
"""ODT format should get markdown title prepended."""
try:
import pypandoc
pypandoc.get_pandoc_version()
except (ImportError, OSError):
pytest.skip("Pandoc is not installed (required for ODT export)")
content, filename, mimetype = research_service.export_report_to_memory(
"This is content without a title heading.",
"odt",
title="My Report Title",
)
# ODT should be valid ZIP and have the title
assert content[:2] == b"PK"
assert filename.endswith(".odt")
def test_ris_does_not_get_title_prepended(self):
"""RIS is a bibliographic format with strict structure; a markdown
title prepended to it corrupts the first record."""
markdown_content = """# Research Report
Some content here.
## Sources
[1] First Source
URL: https://example.com/1
"""
content, filename, mimetype = research_service.export_report_to_memory(
markdown_content, "ris", title="My Report Title"
)
ris_text = content.decode("utf-8")
assert not ris_text.startswith("# My Report Title")
assert filename.endswith(".ris")
def test_latex_does_not_get_title_prepended(self):
r"""LaTeX has its own document structure (\documentclass, ...)."""
content, filename, mimetype = research_service.export_report_to_memory(
"Some content here.", "latex", title="My Report Title"
)
assert filename.endswith(".tex")
assert isinstance(content, bytes)
# Verify the content is valid (can be decoded)
assert content.decode("utf-8")
def test_quarto_does_not_get_title_prepended(self):
"""Quarto carries the title in YAML front matter already."""
content, filename, mimetype = research_service.export_report_to_memory(
"Some content here.", "quarto", title="My Report Title"
)
assert content[:2] == b"PK"
assert filename.endswith(".zip")
def test_title_not_duplicated_if_already_present(self):
"""Content already starting with the title heading is left alone."""
title = "My Report Title"
content, filename, mimetype = research_service.export_report_to_memory(
f"# {title}\n\nThis content already has the title.",
"pdf",
title=title,
)
assert content.startswith(b"%PDF")
def test_title_not_prepended_if_content_starts_with_heading(self):
"""Any leading heading suppresses the prepend, not just a match."""
content, filename, mimetype = research_service.export_report_to_memory(
"# Different Heading\n\nSome content here.",
"pdf",
title="My Report Title",
)
assert content.startswith(b"%PDF")
# ---------------------------------------------------------------------------
# _generate_report_path
# ---------------------------------------------------------------------------
class TestGenerateReportPath:
"""Ported from ``origin/main:...::TestGenerateReportPath``.
``TestGenerateReportPathContainment`` (tests/security/
test_research_service_isolation_fastapi.py) pins the shape of the name
(``research_report_<10 hex>_<ts>_<8 hex>.md``) and that the hash segment is a
stable function of the query — but not WHICH digest. Swapping md5 for
sha256 would keep all of those green while silently changing every
previously generated report path. That is what this test pins.
"""
def test_generate_report_path_creates_unique_path(
self, tmp_path, monkeypatch
):
"""The path carries md5(query)[:10] and the research_report prefix."""
monkeypatch.setattr(research_service, "OUTPUT_DIR", tmp_path)
query = "test research query"
result = research_service._generate_report_path(query)
query_hash = hashlib.md5( # DevSkim: ignore DS126858
query.encode("utf-8"), usedforsecurity=False
).hexdigest()[:10]
assert query_hash in str(result)
assert "research_report" in str(result)
# ---------------------------------------------------------------------------
# start_research_process
# ---------------------------------------------------------------------------
class TestStartResearchProcess:
"""Ported from ``origin/main:...::TestStartResearchProcess``.
The branch successor (``test_research_service_start_process.py``) pins
the semaphore accounting, the returned thread and the ``settings`` key,
but NOT the ``status`` field of the registry entry and NOT that the
caller leaves the thread unstarted. ``check_and_start_research`` owns
the ``.start()`` call; a caller that started the thread itself would
reintroduce the double-spawn window the atomic helper exists to close,
and no existing test would notice.
"""
def test_start_research_process_creates_thread(self):
"""The registry entry is in_progress and carries the unstarted thread."""
with patch(f"{SERVICE}._global_research_semaphore", _mock_semaphore()):
with patch(
f"{RESEARCH_STATE}.check_and_start_research", return_value=True
) as mock_check_start:
with patch(f"{SERVICE}.threading.Thread") as mock_thread_class:
mock_thread = Mock()
mock_thread_class.return_value = mock_thread
research_service.start_research_process(
research_id=123,
query="test query",
mode="quick",
run_research_callback=Mock(),
)
# The dedup helper owns starting the thread — not the caller.
mock_thread.start.assert_not_called()
mock_check_start.assert_called_once()
call_args = mock_check_start.call_args
assert call_args[0][0] == 123
assert call_args[0][1]["status"] == "in_progress"
assert call_args[0][1]["thread"] is mock_thread
def test_start_research_process_raises_on_duplicate(self):
"""A refused check-and-start raises and spawns nothing."""
from local_deep_research.exceptions import DuplicateResearchError
with patch(f"{SERVICE}._global_research_semaphore", _mock_semaphore()):
with patch(
f"{RESEARCH_STATE}.check_and_start_research", return_value=False
):
with patch(f"{SERVICE}.threading.Thread") as mock_thread_class:
mock_thread = Mock()
mock_thread_class.return_value = mock_thread
with pytest.raises(DuplicateResearchError):
research_service.start_research_process(
research_id=123,
query="test query",
mode="quick",
run_research_callback=Mock(),
)
# Thread prepared but never started — check_and_start_research
# owns the .start() call and refused to make it.
mock_thread.start.assert_not_called()
# ---------------------------------------------------------------------------
# cleanup_research_resources
# ---------------------------------------------------------------------------
class TestCleanupResearchResources:
"""Ported from ``origin/main:...::TestCleanupResearchResources``.
No branch test calls ``cleanup_research_resources`` directly; every
reference to it in ``tests/`` mocks it out. So nothing on the branch
pins that it de-registers the research from the global state dicts, or
that it hands the queue processor the username/password it needs. Drop
either call and the whole suite stays green: an abandoned entry in
``_active_research`` blocks every retry of that id, and a missed
``notify_research_completed`` leaves the row stuck in_progress.
"""
@patch(f"{ENV_REGISTRY}.is_test_mode", return_value=False)
@patch(f"{QUEUE_PROC}.queue_processor")
@patch(f"{RESEARCH_STATE}.cleanup_research")
def test_cleanup_calls_cleanup_research(
self, mock_cleanup, mock_queue, mock_test_mode
):
"""cleanup_research_resources calls cleanup_research to remove from dicts."""
emit_patch, remove_patch = _socket_isolation_patches()
with emit_patch, remove_patch:
research_service.cleanup_research_resources(
123, username="testuser"
)
mock_cleanup.assert_called_once_with(123)
@patch(f"{ENV_REGISTRY}.is_test_mode", return_value=False)
@patch(f"{QUEUE_PROC}.queue_processor")
@patch(f"{RESEARCH_STATE}.cleanup_research")
def test_cleanup_notifies_queue_processor(
self, mock_cleanup, mock_queue, mock_test_mode
):
"""cleanup_research_resources notifies queue processor."""
emit_patch, remove_patch = _socket_isolation_patches()
with emit_patch, remove_patch:
research_service.cleanup_research_resources(
123, username="testuser"
)
mock_queue.notify_research_completed.assert_called_once_with(
"testuser", 123, user_password=None
)
# ---------------------------------------------------------------------------
# cancel_research
# ---------------------------------------------------------------------------
class TestCancelResearch:
"""Ported from ``origin/main:...::TestCancelResearch``.
Only the ACTIVE branch is ported. The inactive and already-terminal
branches are covered end-to-end by
``tests/security/test_research_service_isolation_fastapi.py::
TestCancelResearchIsolation``. The active branch's nearest successor,
``tests/security/test_research_terminate_cross_user.py::
test_cancel_research_terminates_your_own_active_research``, asserts only
``handle.assert_called_once()`` — it never checks the ARGUMENTS, so a
``handle_termination(research_id)`` that dropped the username (and thus
could not open the terminating user's database to persist SUSPENDED)
would keep it green.
"""
@patch(f"{SERVICE}.get_user_db_session", MagicMock())
@patch(f"{RESEARCH_STATE}.is_research_active", return_value=True)
@patch(f"{RESEARCH_STATE}.set_termination_flag")
@patch(f"{SERVICE}.handle_termination")
def test_cancel_research_sets_termination_flag(
self, mock_handle_termination, mock_set_flag, mock_is_active
):
"""cancel_research sets the termination flag and forwards the username.
The ownership gate reads the caller's DB first; a MagicMock session
makes the ownership query return a row so the owner is authorized.
"""
result = research_service.cancel_research(123, username="testuser")
assert result is True
mock_set_flag.assert_called_once_with(123)
mock_handle_termination.assert_called_once_with(
123, "testuser", preserve_termination_flag=True
)
# ---------------------------------------------------------------------------
# handle_termination
# ---------------------------------------------------------------------------
class TestHandleTermination:
"""Ported from ``origin/main:...::TestHandleTermination``.
Every branch reference to ``handle_termination`` mocks it out; nothing
exercises its body. The second test is the important one: if
``final_status`` were dropped, ``cleanup_research_resources`` would fall
back to its ``COMPLETED`` default and the terminal socket message for a
user-stopped research would read "completed" at 100% — the exact
regression the ``final_status`` parameter was added to fix.
"""
@patch(f"{QUEUE_PROC}.queue_processor")
@patch(f"{SERVICE}.cleanup_research_resources")
def test_handle_termination_queues_update(self, mock_cleanup, mock_queue):
"""handle_termination queues suspension update."""
research_service.handle_termination(123, username="testuser")
mock_queue.queue_error_update.assert_called_once()
call_kwargs = mock_queue.queue_error_update.call_args[1]
assert call_kwargs["status"] == "suspended"
assert call_kwargs["research_id"] == 123
@patch(f"{QUEUE_PROC}.queue_processor")
@patch(f"{SERVICE}.cleanup_research_resources")
def test_handle_termination_calls_cleanup(self, mock_cleanup, mock_queue):
"""handle_termination calls cleanup with the SUSPENDED terminal status."""
research_service.handle_termination(123, username="testuser")
# Termination must report SUSPENDED to cleanup so the final socket
# message is not a spurious "completed".
mock_cleanup.assert_called_once_with(
123, "testuser", final_status="suspended"
)
# ---------------------------------------------------------------------------
# _parse_research_metadata — the cases the branch successor does not cover
# ---------------------------------------------------------------------------
class TestParseResearchMetadata:
"""Ported from ``origin/main:...::TestParseResearchMetadata``.
``test_research_service_helpers.py::TestParseResearchMetadata`` covers
dict-copy, nested dict, simple JSON string, invalid JSON, ``None``,
``int`` and ``""``. The four cases below are the ones it does not: a
JSON string carrying nested structures, the literal ``"{}"``, a list
(the third input KIND, distinct from int only in that a list is
iterable and a naive ``dict(x)`` attempt on it raises rather than
returning ``{}``), and an already-empty dict.
"""
def test_parse_complex_json_string(self):
"""Nested structures survive parsing from a JSON string."""
json_str = '{"iterations": 5, "metadata": {"model": "gpt-4"}, "sources": ["a", "b"]}'
result = research_service._parse_research_metadata(json_str)
assert result["iterations"] == 5
assert result["metadata"]["model"] == "gpt-4"
assert result["sources"] == ["a", "b"]
def test_parse_empty_json_string(self):
"""The literal '{}' parses to an empty dict, not to a failure."""
assert research_service._parse_research_metadata("{}") == {}
def test_parse_list_returns_empty_dict(self):
"""A list input returns {} instead of raising."""
assert research_service._parse_research_metadata([1, 2, 3]) == {}
def test_parse_empty_dict_returns_empty_dict(self):
"""An empty dict round-trips as an empty dict."""
assert research_service._parse_research_metadata({}) == {}