~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
"""Unit tests for research_service.start_research_process wiring.
The function front-loads the global concurrency semaphore (HTTP 429 before any
DB state), wraps the worker callback so the semaphore is released on every
exit path, copies the caller's contextvars into the worker thread (so
get_current_username() resolves inside it — the Flask app-context wrapper this
replaced was removed in d9168614b), and refuses to double-spawn via the atomic
check_and_start_research. Each of those behaviours is pinned here with the
semaphore and research-state seams mocked.
"""
import threading
from unittest.mock import Mock, patch
import pytest
from local_deep_research.exceptions import (
DuplicateResearchError,
SystemAtCapacityError,
)
from local_deep_research.utilities.request_context import (
get_current_username,
reset_request_user,
set_request_user,
)
SERVICE = "local_deep_research.web.services.research_service"
CHECK_AND_START = (
"local_deep_research.web.research_state.check_and_start_research"
)
def _semaphore(acquire_ok=True):
sem = Mock()
sem.acquire.return_value = acquire_ok
return sem
class TestCapacityAndSpawnGuards:
def test_at_capacity_raises_without_spawning(self):
from local_deep_research.web.services.research_service import (
start_research_process,
)
sem = _semaphore(acquire_ok=False)
with patch(f"{SERVICE}._global_research_semaphore", sem):
with patch(f"{CHECK_AND_START}") as mock_start:
with pytest.raises(SystemAtCapacityError):
start_research_process("rid", "q", "quick", Mock())
mock_start.assert_not_called()
# Nothing acquired → nothing to release.
sem.release.assert_not_called()
def test_duplicate_research_releases_semaphore(self):
"""check_and_start_research returning False (a live thread already
exists) must release the already-held semaphore slot."""
from local_deep_research.web.services.research_service import (
start_research_process,
)
sem = _semaphore()
with patch(f"{SERVICE}._global_research_semaphore", sem):
with patch(f"{CHECK_AND_START}", return_value=False):
with pytest.raises(DuplicateResearchError):
start_research_process("rid", "q", "quick", Mock())
sem.release.assert_called_once()
def test_check_and_start_exception_releases_semaphore(self):
"""If the atomic check-and-start itself raises before any worker ran,
the slot must be released here (no worker will ever release it)."""
from local_deep_research.web.services.research_service import (
start_research_process,
)
sem = _semaphore()
with patch(f"{SERVICE}._global_research_semaphore", sem):
with patch(
f"{CHECK_AND_START}", side_effect=RuntimeError("db down")
):
with pytest.raises(RuntimeError, match="db down"):
start_research_process("rid", "q", "quick", Mock())
sem.release.assert_called_once()
def test_success_registers_thread_and_returns_it(self):
from local_deep_research.web.services.research_service import (
start_research_process,
)
captured = {}
def fake_check_and_start(research_id, data):
captured["research_id"] = research_id
captured["data"] = data
return True
sem = _semaphore()
with patch(f"{SERVICE}._global_research_semaphore", sem):
with patch(f"{CHECK_AND_START}", side_effect=fake_check_and_start):
thread = start_research_process(
"rid", "q", "quick", Mock(), model="m1"
)
assert captured["research_id"] == "rid"
assert captured["data"]["thread"] is thread
assert captured["data"]["settings"] == {"model": "m1"}
assert isinstance(thread, threading.Thread)
assert thread.daemon is True
# The spawn registration alone must not release the slot — the worker
# does that on exit.
sem.release.assert_not_called()
class TestWorkerThreadWiring:
"""Run the prepared (not yet started) thread and assert the callback
invocation contract."""
def _spawn(self, callback, **kwargs):
from local_deep_research.web.services.research_service import (
start_research_process,
)
sem = _semaphore()
with patch(f"{SERVICE}._global_research_semaphore", sem):
with patch(f"{CHECK_AND_START}", return_value=True):
thread = start_research_process(
"rid", "q", "quick", callback, **kwargs
)
# Run the worker while the semaphore patch is still active so
# _release_semaphore_on_exit releases OUR mock.
thread.start()
thread.join(timeout=10)
assert not thread.is_alive()
return sem
def test_callback_receives_args_and_kwargs(self):
seen = {}
def callback(research_id, query, mode, **kw):
seen["args"] = (research_id, query, mode)
seen["kwargs"] = kw
self._spawn(callback, username="alice", model="m1")
# No vestigial Flask app-context placeholder is prepended (d9168614b).
assert seen["args"] == ("rid", "q", "quick")
assert seen["kwargs"] == {"username": "alice", "model": "m1"}
def test_semaphore_released_after_callback_returns(self):
sem = self._spawn(lambda *a, **kw: None)
sem.release.assert_called_once()
def test_semaphore_released_when_callback_raises(self):
def callback(*a, **kw):
raise RuntimeError("worker exploded")
sem = self._spawn(callback)
sem.release.assert_called_once()
def test_caller_contextvars_propagate_into_worker(self):
"""The worker runs inside a copy of the caller's contextvars, so
get_current_username() resolves the authenticated user that started
the research (metrics/token attribution depend on this)."""
seen = {}
def callback(*a, **kw):
seen["username"] = get_current_username()
handles = set_request_user("alice", "sess-1")
try:
self._spawn(callback)
finally:
reset_request_user(handles)
assert seen["username"] == "alice"