Skip to content

~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.

8.7 KB
Raw Blame
"""``validate_setting()`` edge cases — min/max, select, unknown ui_element.

Ports main's ``tests/web/routes/test_settings_validation_edge_cases.py``
(``git show origin/main:tests/web/routes/test_settings_validation_edge_cases.py``),
deleted by the FastAPI migration. All 12 originals are ported.

Plumbing translation
--------------------
The original imported ``validate_setting`` / ``DYNAMIC_SETTINGS`` from
``local_deep_research.web.routes.settings_routes``, where they were
re-exports of ``web/services/settings_service.py``. On this branch the
route module ``web/routers/settings.py`` defines its **own**
``validate_setting`` (settings.py:371) and its own ``DYNAMIC_SETTINGS``
(settings.py:114), and those are what every settings write path actually
calls (settings.py:744, :1218, :3379, :3469). So this port targets the
router copy — the code that is live on the request path — not the
service copy, which is now an unreferenced duplicate (see the module-level
note in ``test_router_and_service_validate_setting_have_not_diverged``).

Deliberate divergence, kept honest
----------------------------------
The router copy adds an early ``if value is None: return True, None`` for
``number``/``slider``/``range`` (settings.py:405-416) that the service copy
and main both lack. That is a *deliberate* branch fix (an unset optional
numeric such as ``embeddings.openai.dimensions`` ships as ``null``), not a
loss, and it is pinned here rather than left implicit.

It does, however, change what main's slider test exercised: main patched
``get_typed_setting_value`` because ``"slider"`` is absent from
``UI_ELEMENT_TO_SETTING_TYPE`` and coercion therefore yields ``None``. On
this branch an unpatched ``None`` would now short-circuit to ``(True, None)``
and the min-constraint assertion would pass vacuously. The patch is
therefore retained (retargeted at the router module) so the test still
pins the constraint it was written for.
"""

from unittest.mock import MagicMock, patch

import pytest

from local_deep_research.web.models.settings import BaseSetting, SettingType
from local_deep_research.web.routers.settings import (
    DYNAMIC_SETTINGS,
    validate_setting,
)

MODULE = "local_deep_research.web.routers.settings"


def _make_setting(ui_element, **kwargs):
    """Build a BaseSetting with minimal boilerplate (as in the original)."""
    defaults = dict(
        key="test.key",
        value=0,
        type=SettingType.APP,
        name="Test",
    )
    defaults.update(kwargs)
    defaults["ui_element"] = ui_element
    return BaseSetting(**defaults)


class TestValidateSettingMinMax:
    """number/slider/range min/max constraint validation."""

    def test_number_below_min(self):
        """Number below min_value -> (False, error)."""
        setting = _make_setting("number", min_value=0)
        valid, msg = validate_setting(setting, -1)
        assert valid is False
        assert "at least" in msg

    def test_number_above_max(self):
        """Number above max_value -> (False, error)."""
        setting = _make_setting("number", max_value=100)
        valid, msg = validate_setting(setting, 150)
        assert valid is False
        assert "at most" in msg

    def test_number_no_constraints(self):
        """Number with no min/max -> (True, None)."""
        setting = _make_setting("number")
        valid, msg = validate_setting(setting, 999)
        assert valid is True
        assert msg is None

    def test_number_only_min_passes(self):
        """Number with only min_value, value above -> (True, None)."""
        setting = _make_setting("number", min_value=0)
        valid, msg = validate_setting(setting, 5)
        assert valid is True

    def test_number_only_max_passes(self):
        """Number with only max_value, value below -> (True, None)."""
        setting = _make_setting("number", max_value=100)
        valid, msg = validate_setting(setting, 50)
        assert valid is True

    def test_slider_below_min(self):
        """Slider below min_value -> (False, error).

        ``slider`` is not in ``UI_ELEMENT_TO_SETTING_TYPE``, so
        ``get_typed_setting_value`` returns ``default=None``. Patched to pass
        the numeric value through so the min/max branch is what is under
        test -- see the module docstring for why the patch is now
        load-bearing on this branch and was merely convenient on main.
        """
        setting = _make_setting("slider", min_value=5)
        with patch(f"{MODULE}.get_typed_setting_value", return_value=2):
            valid, msg = validate_setting(setting, 2)
            assert valid is False
            assert "at least" in msg

    def test_range_above_max(self):
        """Range above max_value -> (False, error)."""
        setting = _make_setting("range", max_value=10)
        valid, msg = validate_setting(setting, 15)
        assert valid is False
        assert "at most" in msg


class TestValidateSettingSelect:
    """select ui_element edge cases."""

    def test_select_invalid_option(self):
        """Select with value not in options -> (False, error)."""
        setting = _make_setting(
            "select",
            options=[{"value": "a"}, {"value": "b"}],
        )
        valid, msg = validate_setting(setting, "x")
        assert valid is False
        assert "must be one of" in msg

    def test_select_plain_string_options(self):
        """Select with a plain string options list -> valid option passes.

        The Pydantic model enforces ``List[Dict]``, but at runtime
        ``validate_setting`` handles both dict and non-dict options
        (settings.py:432-435). A mock bypasses Pydantic validation.
        """
        setting = MagicMock()
        setting.key = "test.select"
        setting.ui_element = "select"
        setting.options = ["a", "b", "c"]

        with patch(f"{MODULE}.get_typed_setting_value", return_value="b"):
            valid, msg = validate_setting(setting, "b")
            assert valid is True

    def test_select_empty_options_skips_validation(self):
        """Select with an empty options list -> skipped -> (True, None)."""
        setting = _make_setting("select", options=[])
        valid, msg = validate_setting(setting, "anything")
        assert valid is True

    def test_select_dynamic_setting_skips_validation(self):
        """Select whose key is in DYNAMIC_SETTINGS -> validation skipped."""
        dynamic_key = DYNAMIC_SETTINGS[0]  # "llm.provider"
        setting = _make_setting(
            "select",
            key=dynamic_key,
            options=[{"value": "a"}, {"value": "b"}],
        )
        # Value is not in options, but passes because the key is dynamic.
        valid, msg = validate_setting(setting, "nonexistent_provider")
        assert valid is True


class TestValidateSettingUnknownElement:
    """Unknown ui_element types fail explicitly."""

    def test_unknown_ui_element_is_rejected(self):
        """An unsupported schema cannot silently accept an arbitrary value."""
        setting = _make_setting("custom_widget")
        valid, msg = validate_setting(setting, "any value")
        assert valid is False
        assert msg == "unsupported setting type"


# ---------------------------------------------------------------------------
# Branch-specific additions (not in the original) -- see module docstring
# ---------------------------------------------------------------------------


class TestUnsetOptionalNumericIsValid:
    """The router copy's deliberate ``None`` allowance for numerics.

    Pinned explicitly so that if it is ever reverted (or accidentally
    copied into the service copy) the change is visible, rather than
    silently altering what ``test_slider_below_min`` above exercises.
    """

    @pytest.mark.parametrize("ui_element", ["number", "slider", "range"])
    def test_none_numeric_is_valid_and_skips_min_max(self, ui_element):
        setting = _make_setting(ui_element, min_value=5, max_value=10)
        with patch(f"{MODULE}.get_typed_setting_value", return_value=None):
            valid, msg = validate_setting(setting, "")
        assert valid is True
        assert msg is None

    def test_non_numeric_non_none_is_still_rejected(self):
        """The None allowance must not swallow genuinely bad input."""
        setting = _make_setting("number")
        with patch(f"{MODULE}.get_typed_setting_value", return_value=None):
            valid, msg = validate_setting(setting, "abc")
        assert valid is False
        assert msg == "Value must be a number"

    def test_checkbox_non_boolean_is_rejected(self):
        setting = _make_setting("checkbox")
        with patch(f"{MODULE}.get_typed_setting_value", return_value="yes"):
            valid, msg = validate_setting(setting, "yes")
        assert valid is False
        assert msg == "Value must be a boolean"