~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-level guards recovered from main's deleted ``tests/routes/test_settings_routes.py``.
Two pure helpers survived the Flask -> FastAPI migration with their bodies
intact but lost every test that pinned their *outcomes*:
``validate_setting``
On main this was one function, defined in
``web/services/settings_service.py`` and re-exported by
``web/routes/settings_routes.py`` (``settings_routes.py`` line 108 on
``origin/main`` is a plain ``from ..services.settings_service import
validate_setting``). This branch FORKED it: ``web/routers/settings.py``
now carries its own copy (the live one — all four production call sites
are in that module) and the service keeps a second, now-unreferenced
copy. Every assertion below therefore runs against BOTH copies, which
is the faithful translation of main's single function.
The only thing on the branch that mentions the two copies is
``tests/test_settings_service_no_import_cycle.py``, and it asserts
*parity* — that the two agree with each other — not what either one
answers. If both copies dropped the min/max clamp, or the
allowed-options check, or the ``DYNAMIC_SETTINGS`` skip, that probe
would still print ``VALIDATE_PARITY=same`` and pass. These are the
outcome assertions.
``_get_engine_icon_and_category``
Ported to ``web/routers/settings.py`` line 1917 unchanged, but with no
test anywhere on the branch: nothing greps for ``"Local RAG"``,
``"Scientific"``, or the emoji. It decides the icon and the group
label of every entry in the search-engine dropdown
(``api_get_available_search_engines``), and its priority ORDER is
load-bearing — see ``test_public_collection_still_categorized_local_rag``.
"""
from unittest.mock import Mock, patch
import pytest
from local_deep_research.web.routers import settings as settings_router
from local_deep_research.web.services import settings_service
# main had one ``validate_setting``; this branch has two. Run every
# recovered assertion against both so a fix or a regression in either is
# caught, and so the pair cannot silently drift.
_VALIDATE_MODULES = [
pytest.param(settings_router, id="routers.settings"),
pytest.param(settings_service, id="services.settings_service"),
]
# ``validate_setting`` calls ``get_typed_setting_value`` in its own module
# namespace (both copies import the symbol directly), so the patch target
# is per-module.
_CONVERTER = "get_typed_setting_value"
def _converted(module, value):
"""Pin the converter's answer, exactly as main's tests did.
The conversion step is ``settings/manager.get_typed_setting_value`` and
has its own tests; what is under test here is the branch logic that
runs on the CONVERTED value.
"""
return patch(
f"{module.__name__}.{_CONVERTER}",
return_value=value,
)
def _setting(**kwargs):
setting = Mock()
for name, value in kwargs.items():
setattr(setting, name, value)
return setting
class TestValidateSettingCheckbox:
@pytest.mark.parametrize("module", _VALIDATE_MODULES)
@pytest.mark.parametrize("value", [True, False])
def test_boolean_checkbox_is_valid(self, module, value):
setting = _setting(key="test.checkbox", ui_element="checkbox")
with _converted(module, value):
is_valid, error = module.validate_setting(setting, value)
assert is_valid is True
assert error is None
@pytest.mark.parametrize("module", _VALIDATE_MODULES)
def test_non_boolean_checkbox_is_rejected(self, module):
"""The ``isinstance(value, bool)`` guard. Not in main's file, but it
is the only assertion that makes the two rows above non-vacuous: a
``validate_setting`` that returned ``(True, None)`` unconditionally
would satisfy them."""
setting = _setting(key="test.checkbox", ui_element="checkbox")
with _converted(module, "not-a-bool"):
is_valid, error = module.validate_setting(setting, "not-a-bool")
assert is_valid is False
assert error == "Value must be a boolean"
class TestValidateSettingNumeric:
@pytest.mark.parametrize("module", _VALIDATE_MODULES)
def test_number_within_bounds_is_valid(self, module):
setting = _setting(
key="test.number",
ui_element="number",
min_value=1,
max_value=100,
)
with _converted(module, 50):
is_valid, error = module.validate_setting(setting, 50)
assert is_valid is True
assert error is None
@pytest.mark.parametrize("module", _VALIDATE_MODULES)
def test_number_below_min_is_rejected(self, module):
setting = _setting(
key="test.number",
ui_element="number",
min_value=10,
max_value=100,
)
with _converted(module, 5):
is_valid, error = module.validate_setting(setting, 5)
assert is_valid is False
assert "at least" in error
assert "10" in error
@pytest.mark.parametrize("module", _VALIDATE_MODULES)
def test_number_above_max_is_rejected(self, module):
setting = _setting(
key="test.number",
ui_element="number",
min_value=1,
max_value=100,
)
with _converted(module, 200):
is_valid, error = module.validate_setting(setting, 200)
assert is_valid is False
assert "at most" in error
assert "100" in error
@pytest.mark.parametrize("module", _VALIDATE_MODULES)
@pytest.mark.parametrize(
"ui_element, value, min_value, max_value",
[
("slider", 0.5, 0, 1),
("range", 5, 1, 10),
],
)
def test_slider_and_range_share_the_numeric_branch(
self, module, ui_element, value, min_value, max_value
):
"""``slider`` and ``range`` must be handled by the same
``ui_element in ("number", "slider", "range")`` arm as ``number``;
dropping either name from that tuple sends them to the catch-all
``return True, None`` and silently stops enforcing min/max."""
setting = _setting(
key=f"test.{ui_element}",
ui_element=ui_element,
min_value=min_value,
max_value=max_value,
)
with _converted(module, value):
is_valid, error = module.validate_setting(setting, value)
assert is_valid is True
assert error is None
# Out of bounds on the same ui_element must be rejected — this is
# what proves the arm is actually entered.
with _converted(module, max_value + 1):
is_valid, error = module.validate_setting(setting, max_value + 1)
assert is_valid is False
assert "at most" in error
class TestValidateSettingSelect:
@pytest.mark.parametrize("module", _VALIDATE_MODULES)
def test_allowed_option_is_valid(self, module):
setting = _setting(
key="test.select",
ui_element="select",
options=[
{"value": "option1"},
{"value": "option2"},
{"value": "option3"},
],
)
with _converted(module, "option2"):
is_valid, error = module.validate_setting(setting, "option2")
assert is_valid is True
assert error is None
@pytest.mark.parametrize("module", _VALIDATE_MODULES)
def test_option_outside_the_list_is_rejected(self, module):
setting = _setting(
key="test.select",
ui_element="select",
options=[{"value": "option1"}, {"value": "option2"}],
)
with _converted(module, "invalid"):
is_valid, error = module.validate_setting(setting, "invalid")
assert is_valid is False
assert "must be one of" in error
assert "option1" in error and "option2" in error
@pytest.mark.parametrize("module", _VALIDATE_MODULES)
@pytest.mark.parametrize(
"key", ["llm.provider", "llm.model", "search.tool"]
)
def test_dynamic_settings_skip_option_validation(self, module, key):
"""Dropdowns whose options are populated at runtime (the installed
Ollama models, the registered search engines) carry a STALE
``options`` list in the DB. Validating against it would reject every
genuinely-available model. The skip is keyed on ``DYNAMIC_SETTINGS``;
removing a key from that list breaks saving that setting."""
setting = _setting(
key=key,
ui_element="select",
options=[{"value": "stale-option"}],
)
with _converted(
module, "a-model-installed-after-the-options-were-written"
):
is_valid, error = module.validate_setting(
setting, "a-model-installed-after-the-options-were-written"
)
assert is_valid is True
assert error is None
class TestDynamicSettingsConstant:
"""main asserted the membership of ``DYNAMIC_SETTINGS`` itself.
``tests/test_settings_service_no_import_cycle.py`` compares the two
copies to EACH OTHER (``DYNAMIC_PARITY``); it never asserts what is in
them, so emptying both lists passes it.
"""
@pytest.mark.parametrize(
"module",
[
pytest.param(settings_router, id="routers.settings"),
pytest.param(
__import__(
"local_deep_research.settings.manager",
fromlist=["DYNAMIC_SETTINGS"],
),
id="settings.manager",
),
],
)
def test_contains_the_runtime_populated_dropdowns(self, module):
assert "llm.provider" in module.DYNAMIC_SETTINGS
assert "llm.model" in module.DYNAMIC_SETTINGS
assert "search.tool" in module.DYNAMIC_SETTINGS
class TestValidateSettingOptionalNumerics:
"""Both validators distinguish an unset numeric from bad conversion."""
@pytest.mark.parametrize("module", _VALIDATE_MODULES)
@pytest.mark.parametrize("ui_element", ["number", "slider", "range"])
def test_none_is_valid_and_skips_bounds(self, module, ui_element):
setting = _setting(
key=f"optional.{ui_element}",
ui_element=ui_element,
min_value=1,
max_value=4096,
)
with _converted(module, None):
is_valid, error = module.validate_setting(setting, None)
assert is_valid is True
assert error is None
@pytest.mark.parametrize("module", _VALIDATE_MODULES)
@pytest.mark.parametrize("ui_element", ["number", "slider", "range"])
def test_nonblank_failed_conversion_is_rejected(self, module, ui_element):
setting = _setting(
key=f"invalid.{ui_element}",
ui_element=ui_element,
min_value=1,
max_value=4096,
)
with _converted(module, None):
is_valid, error = module.validate_setting(setting, "not-a-number")
assert is_valid is False
assert error == "Value must be a number"
@pytest.mark.parametrize("module", _VALIDATE_MODULES)
@pytest.mark.parametrize("ui_element", ["number", "slider", "range"])
@pytest.mark.parametrize("raw_value", [None, "", " ", "\t\r\n"])
def test_coerce_then_validate_preserves_optional_blanks(
self, module, ui_element, raw_value
):
setting = _setting(
key=f"optional.{ui_element}",
ui_element=ui_element,
min_value=1,
max_value=4096,
)
coerced = settings_router.coerce_setting_for_write(
setting.key, raw_value, ui_element
)
is_valid, error = module.validate_setting(setting, coerced)
assert coerced is None
assert is_valid is True
assert error is None
@pytest.mark.parametrize("module", _VALIDATE_MODULES)
@pytest.mark.parametrize("ui_element", ["number", "slider", "range"])
@pytest.mark.parametrize(
"raw_value",
["not-a-number", True, False, "NaN", "Infinity", "-Infinity"],
)
def test_coerce_then_validate_rejects_hostile_numeric_values(
self, module, ui_element, raw_value
):
setting = _setting(
key=f"invalid.{ui_element}",
ui_element=ui_element,
min_value=None,
max_value=None,
)
coerced = settings_router.coerce_setting_for_write(
setting.key, raw_value, ui_element
)
is_valid, error = module.validate_setting(setting, coerced)
assert is_valid is False
assert error == "Value must be a number"
class TestGetEngineIconAndCategory:
"""``web/routers/settings.py::_get_engine_icon_and_category``.
Untested on this branch. Feeds the label and grouping of every option
in the search-engine dropdown.
"""
@pytest.mark.parametrize(
"flag, icon, category",
[
("is_local", "\N{FILE FOLDER}", "Local RAG"),
("is_scientific", "\N{MICROSCOPE}", "Scientific"),
("is_news", "\N{NEWSPAPER}", "News"),
("is_code", "\N{PERSONAL COMPUTER}", "Code"),
("is_generic", "\N{GLOBE WITH MERIDIANS}", "Web Search"),
],
)
def test_flag_selects_its_icon_and_category(self, flag, icon, category):
result = settings_router._get_engine_icon_and_category({flag: True})
assert result == (icon, category)
def test_unflagged_engine_falls_back_to_the_default(self):
icon, category = settings_router._get_engine_icon_and_category({})
assert icon == "\N{LEFT-POINTING MAGNIFYING GLASS}"
assert category == "Search"
def test_public_collection_still_categorized_local_rag(self):
"""A PUBLIC collection is still local data (two-axis semantics:
``is_public`` and ``is_local`` are independent), so its config —
which always carries ``is_local: True`` — must keep the Local RAG
icon/category rather than falling through to generic.
Recovered verbatim from main; it is the reason ``is_local`` is
checked FIRST in the priority chain.
"""
icon, category = settings_router._get_engine_icon_and_category(
{"is_local": True, "is_public": True}
)
assert icon == "\N{FILE FOLDER}"
assert category == "Local RAG"
def test_local_wins_over_every_other_flag(self):
"""Priority order is load-bearing: an engine that is both local and
scientific must be grouped as Local RAG. Reordering the chain is
invisible to any single-flag test."""
icon, category = settings_router._get_engine_icon_and_category(
{
"is_local": True,
"is_scientific": True,
"is_news": True,
"is_code": True,
"is_generic": True,
}
)
assert (icon, category) == ("\N{FILE FOLDER}", "Local RAG")
def test_engine_class_attributes_take_precedence_over_engine_data(self):
"""When a loaded engine class is supplied, its attributes — not the
config dict — decide. main asserted the class path works; this also
asserts the dict is IGNORED, which is what ``if engine_class:``
actually means."""
engine_class = Mock()
engine_class.is_scientific = True
engine_class.is_generic = False
engine_class.is_local = False
engine_class.is_news = False
engine_class.is_code = False
engine_class.is_books = False
icon, category = settings_router._get_engine_icon_and_category(
{"is_local": True}, engine_class
)
assert icon == "\N{MICROSCOPE}"
assert category == "Scientific"
class TestGetSettingFromSession:
"""``_get_setting_from_session`` — the ``if db_session:`` fallback.
``tests/security/test_followup_and_settings_guards_fastapi.py`` covers
the ``key is None`` short-circuit and the happy path against a real DB;
the "no session available" arm (``return default`` after the ``with``)
is the one main covered and the branch does not.
"""
def test_returns_default_when_no_db_session_is_available(self):
sentinel = object()
with patch(
"local_deep_research.web.routers.settings.get_user_db_session"
) as ctx:
ctx.return_value.__enter__ = Mock(return_value=None)
ctx.return_value.__exit__ = Mock(return_value=False)
result = settings_router._get_setting_from_session(
"test.key", "someuser", sentinel
)
assert result is sentinel