~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: settings mutations must invalidate the settings caches.
invalidate_settings_caches() clears the news scheduler's per-user
settings TTLCache and the LLM provider functools.cache. On main every
mutating settings route called it after a successful write; the FastAPI
port orphaned the function entirely (zero call sites), so background
services kept serving stale settings for up to the cache TTL after any
change. These tests pin the re-added call sites.
"""
import json
from contextlib import contextmanager
from unittest.mock import MagicMock, Mock, patch
S = "local_deep_research.web.routers.settings"
def _patched_db(existing_settings=None, first=None):
query = MagicMock()
query.all.return_value = existing_settings or []
query.first.return_value = first
query.filter.return_value = query
query.filter_by.return_value = query
session = MagicMock()
session.query.return_value = query
@contextmanager
def fake_db_session(*a, **kw):
yield session
return session, patch(
f"{S}.get_user_db_session", side_effect=fake_db_session
)
class TestMutationsInvalidateCaches:
def test_save_all_settings_invalidates_on_success(self):
from local_deep_research.web.routers.settings import (
_save_all_settings_sync,
)
created = Mock()
created.type = "LLM"
_, db_patch = _patched_db()
with (
db_patch,
patch(f"{S}.create_or_update_setting", return_value=created),
patch(f"{S}.invalidate_settings_caches") as inval,
):
_save_all_settings_sync({"llm.custom_option": "x"}, "alice")
inval.assert_called_once_with("alice")
def test_save_all_settings_does_not_invalidate_on_validation_error(self):
from local_deep_research.web.routers.settings import (
_save_all_settings_sync,
)
_, db_patch = _patched_db()
with (
db_patch,
patch(f"{S}.invalidate_settings_caches") as inval,
):
resp = _save_all_settings_sync({"security.evil": "x"}, "alice")
assert resp.status_code == 400
inval.assert_not_called()
def test_put_update_invalidates(self):
from local_deep_research.web.routers.settings import (
_api_update_setting_sync,
)
existing = Mock()
existing.key = "llm.model"
existing.editable = True
existing.ui_element = "text"
_, db_patch = _patched_db(first=existing)
with (
db_patch,
patch(f"{S}.validate_setting", return_value=(True, None)),
patch(f"{S}.set_setting", return_value=True),
patch(f"{S}.invalidate_settings_caches") as inval,
):
_api_update_setting_sync({"value": "m"}, "llm.model", "alice")
inval.assert_called_once_with("alice")
def test_put_create_invalidates(self):
from local_deep_research.web.routers.settings import (
_api_update_setting_sync,
)
created = Mock()
created.key = "llm.custom_option"
created.value = "x"
created.type = Mock(value="LLM")
created.name = "Custom Option"
_, db_patch = _patched_db()
with (
db_patch,
patch(f"{S}.create_or_update_setting", return_value=created),
patch(f"{S}.invalidate_settings_caches") as inval,
):
resp = _api_update_setting_sync(
{"value": "x"}, "llm.custom_option", "alice"
)
assert resp.status_code == 201
inval.assert_called_once_with("alice")
def test_delete_invalidates(self):
from local_deep_research.web.routers.settings import (
api_delete_setting,
)
# ``api_delete_setting`` is wrapped by the ``settings_limit`` slowapi
# decorator, which rejects a request that is not a real starlette
# ``Request`` and then reads back rate-limit state the direct call
# never populates. This test is about the cache-invalidation call
# site, not the limiter, so unwrap to the undecorated route body.
delete_setting = api_delete_setting.__wrapped__
existing = Mock()
existing.editable = True
_, db_patch = _patched_db(first=existing)
sm = Mock()
sm.delete_setting.return_value = True
# A bare Mock returns a truthy Mock for every attribute call, so
# the route's settings-lock and environment-lock guards would each
# see "locked" and return 403 before ever reaching the delete.
# Answer both explicitly.
sm.settings_locked = False
sm._is_environment_locked.return_value = False
with (
db_patch,
patch(f"{S}.get_settings_manager", return_value=sm),
patch(f"{S}.invalidate_settings_caches") as inval,
):
result = delete_setting(Mock(), "llm.model", username="alice")
assert "deleted" in json.dumps(result)
inval.assert_called_once_with("alice")
def test_form_save_invalidates_after_commit(self):
from local_deep_research.web.routers.settings import (
_save_settings_sync,
)
existing = Mock()
existing.key = "llm.model"
existing.editable = True
existing.ui_element = "text"
_, db_patch = _patched_db(existing_settings=[existing])
sm = Mock()
sm.set_setting.return_value = True
with (
db_patch,
patch(f"{S}.get_settings_manager", return_value=sm),
patch(f"{S}.invalidate_settings_caches") as inval,
):
_save_settings_sync({"llm.model": "m"}, "alice")
inval.assert_called_once_with("alice")
def test_invalidate_has_call_sites():
"""Fence: the function must never become orphaned again — the FastAPI
port shipped with zero call sites and stale caches in production."""
import inspect
import local_deep_research.web.routers.settings as settings_module
source = inspect.getsource(settings_module)
# At least the six mutation paths pinned above.
assert source.count("invalidate_settings_caches(username)") >= 6