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.

39.1 KB
Raw Blame
"""FastAPI re-port of the deleted ``tests/web/routes/test_settings_routes_deep_coverage2.py``.

Same plumbing translation as ``test_settings_routes_deep_coverage_port.py``:
Flask blueprint client -> the ``_*_sync(..., username)`` helpers and the plain
sync route bodies in ``web/routers/settings.py``; ``@settings_limit``-wrapped
routes are reached through ``__wrapped__`` (the technique already used by
``tests/web/routers/test_settings_cache_invalidation.py``).

The redaction-sentinel block at the end of this file is NOT a port. It pins
two properties main enforced in ``web/routes/settings_routes.py`` that have no
implementation anywhere on this branch. They are expected to be RED; see the
class docstrings for the file:line evidence on both sides.
"""

import asyncio
import json
from contextlib import contextmanager
from unittest.mock import MagicMock, Mock, patch

from local_deep_research.security.data_sanitizer import DataSanitizer

S = "local_deep_research.web.routers.settings"


# ---------------------------------------------------------------------------
# Plumbing
# ---------------------------------------------------------------------------


def _make_setting(
    key="test.key",
    value="val",
    ui_element="text",
    name="Test Key",
    editable=True,
    setting_type="app",
    options=None,
    min_value=None,
    max_value=None,
):
    """Build a mock Setting ORM row (port of deep_coverage2's trimmed variant)."""
    s = MagicMock()
    s.key = key
    s.value = value
    s.ui_element = ui_element
    s.name = name
    s.description = "desc"
    s.category = "general"
    s.type = setting_type
    s.editable = editable
    s.visible = True
    s.options = options
    s.min_value = min_value
    s.max_value = max_value
    s.step = None
    s.updated_at = None
    return s


def _patched_db(all_settings=None, first=None, session=None):
    query = MagicMock()
    query.all.return_value = list(all_settings or [])
    query.first.return_value = first
    query.filter.return_value = query
    query.filter_by.return_value = query
    query.distinct.return_value = query
    query.group_by.return_value = query
    query.having.return_value = query
    query.order_by.return_value = query
    query.delete.return_value = 0

    if session is None:
        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
    )


@contextmanager
def _quiet_side_effects():
    with (
        patch(f"{S}.invalidate_settings_caches"),
        patch(f"{S}.reschedule_document_jobs_if_needed"),
        patch(f"{S}.reschedule_zotero_jobs_if_needed"),
    ):
        yield


def _body(resp):
    if isinstance(resp, dict):
        return resp
    return json.loads(resp.body)


def _fake_request(query_params=None, session=None):
    req = MagicMock()
    params = dict(query_params or {})
    req.query_params.get.side_effect = lambda k, d=None: params.get(k, d)
    req.query_params.getlist.side_effect = lambda k: params.get(k, [])
    req.session = {} if session is None else session
    return req


def _unlocked_manager(**kwargs):
    """A SettingsManager mock that answers the route guards honestly.

    A bare MagicMock returns a truthy Mock for ``settings_locked`` and
    ``_is_environment_locked``, so every write route would 403 before
    reaching the branch under test.
    """
    sm = MagicMock()
    sm.settings_locked = False
    sm._is_environment_locked.return_value = False
    sm.default_settings = {}
    for name, value in kwargs.items():
        setattr(sm, name, value)
    return sm


def _run_async_route(route_name, body, username="testuser", **patches):
    """Drive one of the ``async def`` JSON routes to completion.

    ``run_db_sync`` is the only await point, so ``asyncio.run`` is enough.
    """
    import local_deep_research.web.routers.settings as mod

    route = getattr(mod, route_name)
    route = getattr(route, "__wrapped__", route)

    req = _fake_request()

    async def _json():
        return body

    req.json = _json
    return asyncio.run(route(req, username=username))


# ---------------------------------------------------------------------------
# api_delete_setting
# ---------------------------------------------------------------------------


class TestApiDeleteSettingNotEditable:
    """api_delete_setting returns 403 when setting is not editable."""

    def test_non_editable_setting_returns_403(self):
        from local_deep_research.web.routers.settings import api_delete_setting

        locked = _make_setting(key="app.locked", editable=False)
        sm = _unlocked_manager()

        _, db_patch = _patched_db([locked], first=locked)
        with db_patch, patch(f"{S}.get_settings_manager", return_value=sm):
            resp = api_delete_setting.__wrapped__(
                Mock(), "app.locked", username="testuser"
            )

        assert resp.status_code == 403
        assert "not editable" in _body(resp)["error"].lower()
        sm.delete_setting.assert_not_called()


class TestApiDeleteSettingFails:
    """api_delete_setting returns 500 when delete_setting returns False."""

    def test_delete_returns_false_gives_500(self):
        from local_deep_research.web.routers.settings import api_delete_setting

        setting = _make_setting(key="llm.model", editable=True)
        sm = _unlocked_manager()
        sm.delete_setting.return_value = False

        _, db_patch = _patched_db([setting], first=setting)
        with (
            db_patch,
            patch(f"{S}.get_settings_manager", return_value=sm),
            _quiet_side_effects(),
        ):
            resp = api_delete_setting.__wrapped__(
                Mock(), "llm.model", username="testuser"
            )

        assert resp.status_code == 500
        assert "failed" in _body(resp)["error"].lower()


# ---------------------------------------------------------------------------
# api_update_setting
# ---------------------------------------------------------------------------


class TestApiUpdateSettingSetFails:
    """api_update_setting returns 500 when set_setting returns False."""

    def test_set_setting_false_returns_500(self):
        from local_deep_research.web.routers.settings import (
            _api_update_setting_sync,
        )

        setting = _make_setting(
            key="llm.model", value="gpt-4", ui_element="text", editable=True
        )
        setting.type = "llm"

        _, db_patch = _patched_db([setting], first=setting)
        with (
            db_patch,
            patch(
                f"{S}.get_settings_manager", return_value=_unlocked_manager()
            ),
            patch(f"{S}.coerce_setting_for_write", return_value="gpt-3.5"),
            patch(f"{S}.validate_setting", return_value=(True, None)),
            patch(f"{S}.set_setting", return_value=False),
            _quiet_side_effects(),
        ):
            resp = _api_update_setting_sync(
                {"value": "gpt-3.5"}, "llm.model", "testuser"
            )

        assert resp.status_code == 500
        assert "failed" in _body(resp)["error"].lower()


class TestApiUpdateSettingCreateFails:
    """api_update_setting returns 500 when create_or_update_setting returns None."""

    def test_create_returns_none_gives_500(self):
        from local_deep_research.web.routers.settings import (
            _api_update_setting_sync,
        )

        _, db_patch = _patched_db(first=None)
        with (
            db_patch,
            patch(
                f"{S}.get_settings_manager", return_value=_unlocked_manager()
            ),
            patch(f"{S}.create_or_update_setting", return_value=None),
            _quiet_side_effects(),
        ):
            resp = _api_update_setting_sync(
                {"value": "val"}, "llm.new_setting", "testuser"
            )

        assert resp.status_code == 500
        assert "failed" in _body(resp)["error"].lower()


class TestApiUpdateSettingWarningKey:
    """api_update_setting includes warnings for warning-affecting keys."""

    def test_warning_key_includes_warnings_in_response(self):
        from local_deep_research.web.routers.settings import (
            _api_update_setting_sync,
        )

        setting = _make_setting(
            key="llm.provider", value="openai", ui_element="text", editable=True
        )
        setting.type = "llm"
        mock_warnings = [{"type": "info", "message": "Provider changed"}]

        _, db_patch = _patched_db([setting], first=setting)
        with (
            db_patch,
            patch(
                f"{S}.get_settings_manager", return_value=_unlocked_manager()
            ),
            patch(f"{S}.coerce_setting_for_write", return_value="anthropic"),
            patch(f"{S}.validate_setting", return_value=(True, None)),
            patch(f"{S}.set_setting", return_value=True),
            patch(f"{S}.calculate_warnings", return_value=mock_warnings),
            _quiet_side_effects(),
        ):
            resp = _api_update_setting_sync(
                {"value": "anthropic"}, "llm.provider", "testuser"
            )

        data = _body(resp)
        assert "warnings" in data
        assert data["warnings"] == mock_warnings


# ---------------------------------------------------------------------------
# api_toggle_search_favorite
# ---------------------------------------------------------------------------


def _run_favorites(route_name, body, sm, all_settings=None):
    _, db_patch = _patched_db(all_settings)
    with (
        db_patch,
        patch(f"{S}.get_settings_manager", return_value=sm),
        _quiet_side_effects(),
    ):
        return _run_async_route(route_name, body)


class TestApiToggleFavoriteRemove:
    """api_toggle_search_favorite removes engine when already a favorite."""

    def test_toggle_removes_existing_favorite(self):
        sm = _unlocked_manager()
        sm.get_setting.return_value = ["google", "bing"]
        sm.set_setting.return_value = True

        data = _body(
            _run_favorites(
                "api_toggle_search_favorite", {"engine_id": "google"}, sm
            )
        )

        assert data["is_favorite"] is False
        assert "google" not in data["favorites"]


class TestApiToggleFavoriteAdd:
    """api_toggle_search_favorite adds engine when not yet a favorite."""

    def test_toggle_adds_new_favorite(self):
        sm = _unlocked_manager()
        sm.get_setting.return_value = ["bing"]
        sm.set_setting.return_value = True

        data = _body(
            _run_favorites(
                "api_toggle_search_favorite", {"engine_id": "google"}, sm
            )
        )

        assert data["is_favorite"] is True
        assert "google" in data["favorites"]


class TestApiToggleFavoriteFails:
    """api_toggle_search_favorite returns 500 when set_setting fails."""

    def test_set_setting_failure_returns_500(self):
        sm = _unlocked_manager()
        sm.get_setting.return_value = []
        sm.set_setting.return_value = False

        resp = _run_favorites(
            "api_toggle_search_favorite", {"engine_id": "google"}, sm
        )

        assert resp.status_code == 500
        assert "error" in _body(resp)


# ---------------------------------------------------------------------------
# api_update_search_favorites
# ---------------------------------------------------------------------------


class TestApiUpdateSearchFavoritesNoFavorites:
    """api_update_search_favorites returns 400 when favorites key is missing."""

    def test_missing_favorites_key_returns_400(self):
        sm = _unlocked_manager()

        resp = _run_favorites(
            "api_update_search_favorites", {"other": "data"}, sm
        )

        assert resp.status_code == 400
        assert "error" in _body(resp)
        sm.set_setting.assert_not_called()


class TestApiUpdateSearchFavoritesFails:
    """api_update_search_favorites returns 500 when set_setting fails."""

    def test_set_setting_failure_returns_500(self):
        sm = _unlocked_manager()
        sm.set_setting.return_value = False

        resp = _run_favorites(
            "api_update_search_favorites", {"favorites": ["google"]}, sm
        )

        assert resp.status_code == 500
        assert "error" in _body(resp)


# ---------------------------------------------------------------------------
# api_get_all_settings - category filter
# ---------------------------------------------------------------------------


class TestApiGetAllSettingsWithCategory:
    """api_get_all_settings with ?category= query parameter."""

    def test_category_filter_returns_matching_settings(self):
        from local_deep_research.web.routers.settings import (
            api_get_all_settings,
        )

        llm_setting = _make_setting(key="llm.model", value="gpt-4")
        llm_setting.category = "llm_general"
        search_setting = _make_setting(key="search.tool", value="searxng")
        search_setting.category = "search_general"

        sm = _unlocked_manager()
        sm.get_all_settings.return_value = {
            "llm.model": "gpt-4",
            "search.tool": "searxng",
        }

        _, db_patch = _patched_db([llm_setting, search_setting])
        with db_patch, patch(f"{S}.get_settings_manager", return_value=sm):
            resp = api_get_all_settings(
                _fake_request({"category": "llm_general"}), username="testuser"
            )

        data = _body(resp)
        assert data["status"] == "success"
        assert "llm.model" in data["settings"]
        assert "search.tool" not in data["settings"]


# ---------------------------------------------------------------------------
# api_get_db_setting
# ---------------------------------------------------------------------------


class TestApiGetDbSettingNotFound:
    """api_get_db_setting returns 404 when setting not found."""

    def test_not_found_returns_404(self):
        from local_deep_research.web.routers.settings import api_get_db_setting

        _, db_patch = _patched_db(first=None)
        with (
            db_patch,
            patch(
                f"{S}.get_settings_manager", return_value=_unlocked_manager()
            ),
        ):
            resp = api_get_db_setting(
                _fake_request(), "nonexistent.key", username="testuser"
            )

        assert resp.status_code == 404
        assert "not found" in _body(resp)["error"].lower()


class TestApiGetDbSettingTypeValue:
    """api_get_db_setting serializes setting type using .value attribute."""

    def test_enum_type_is_serialized_via_value(self):
        from local_deep_research.web.routers.settings import api_get_db_setting

        setting = _make_setting(
            key="llm.temperature",
            value="0.7",
            ui_element="number",
            editable=True,
        )
        enum_type = MagicMock()
        enum_type.value = "llm"
        setting.type = enum_type

        sm = _unlocked_manager()
        _, db_patch = _patched_db([setting], first=setting)
        with (
            db_patch,
            patch(f"{S}.get_settings_manager", return_value=sm),
            patch(f"{S}._apply_env_override", return_value=("0.7", True)),
        ):
            resp = api_get_db_setting(
                _fake_request(), "llm.temperature", username="testuser"
            )

        assert _body(resp)["type"] == "llm"


# ---------------------------------------------------------------------------
# save_settings (no-JS form POST)
# ---------------------------------------------------------------------------


class TestSaveSettingsBlockedKeys:
    """save_settings (POST form fallback) blocks out-of-namespace keys.

    Flask flashed an error and redirected; the branch's ``_save_settings_sync``
    reports the same refusal in its ``rejected`` counter (which the async
    wrapper turns into the flash). The original's ``status_code == 302``
    assertion is green even with the namespace gate deleted, so this port
    pins the refusal itself.
    """

    def test_blocked_key_is_rejected_and_not_written(self):
        from local_deep_research.web.routers.settings import _save_settings_sync

        sm = _unlocked_manager()
        sm.set_setting.return_value = True

        _, db_patch = _patched_db()
        with (
            db_patch,
            patch(f"{S}.get_settings_manager", return_value=sm),
            _quiet_side_effects(),
        ):
            outcome = _save_settings_sync(
                {"engine.module_path": "/evil/path"}, "testuser"
            )

        assert outcome["rejected"] == 1
        sm.set_setting.assert_not_called()


class TestSaveSettingsOuterException:
    """save_settings must not report success when the save blew up.

    Deviation from the original: Flask let the exception escape into the
    500 handler. The branch deliberately catches it in the route
    (``web/routers/settings.py`` save_settings) so the no-JS user gets a
    flashed error instead of a raw 500 -- but the response must still not be
    a success, and it must still be a redirect back to the settings page.
    """

    def test_outer_exception_flashes_error_and_never_success(self):
        import local_deep_research.web.routers.settings as mod

        route = mod.save_settings.__wrapped__
        req = _fake_request(session={})

        async def _form():
            return {"llm.model": "gpt-4"}

        req.form = _form

        with patch(f"{S}.run_db_sync", side_effect=RuntimeError("unexpected")):
            resp = asyncio.run(route(req, username="testuser"))

        assert resp.status_code == 302
        flashes = req.session.get("_flashes", [])
        assert flashes, "an exception must produce user-visible feedback"
        assert all(category == "error" for category, _msg in flashes)


# ---------------------------------------------------------------------------
# save_all_settings
# ---------------------------------------------------------------------------


class TestSaveAllSettingsMultipleMessage:
    """save_all_settings uses generic message when multiple settings updated."""

    def test_multiple_settings_shows_generic_message(self):
        from local_deep_research.web.routers.settings import (
            _save_all_settings_sync,
        )

        s1 = _make_setting(
            key="llm.model", value="gpt-4", ui_element="text", editable=True
        )
        s1.type = "llm"
        s2 = _make_setting(
            key="search.tool", value="searxng", ui_element="text", editable=True
        )
        s2.type = "search"

        _, db_patch = _patched_db([s1, s2])
        with (
            db_patch,
            patch(
                f"{S}.get_settings_manager",
                return_value=_unlocked_manager(),
            ),
            _quiet_side_effects(),
            patch(f"{S}.coerce_setting_for_write", return_value="new_val"),
            patch(f"{S}.validate_setting", return_value=(True, None)),
            patch(f"{S}.set_setting", return_value=True),
        ):
            resp = _save_all_settings_sync(
                {"llm.model": "gpt-3.5", "search.tool": "google"}, "testuser"
            )

        data = _body(resp)
        assert data["status"] == "success"
        assert "2" in data["message"] or "updated" in data["message"].lower()
        # Pin the count itself: the original's substring test passes on the
        # single-update message too, so it survives this branch being deleted.
        assert "2 updated" in data["message"]


class TestSaveAllSettingsSecretNoop:
    """POST /save_all_settings must treat ""/sentinel for a SECRET setting
    as a no-op (never overwrite the stored value). "Secret" is the same
    predicate the GET redactor uses: ui_element=='password' OR a sensitive
    key suffix -- so a redacted GET round-trip can't destroy the secret."""

    def _post_and_assert_noop(self, setting, submitted_value):
        from local_deep_research.web.routers.settings import (
            _save_all_settings_sync,
        )

        _, db_patch = _patched_db([setting])
        with (
            db_patch,
            patch(
                f"{S}.get_settings_manager",
                return_value=_unlocked_manager(),
            ),
            _quiet_side_effects(),
            patch(
                f"{S}.coerce_setting_for_write",
                side_effect=lambda **kw: kw["value"],
            ),
            patch(f"{S}.validate_setting", return_value=(True, None)),
            patch(f"{S}.set_setting", return_value=True) as set_mock,
        ):
            resp = _save_all_settings_sync(
                {setting.key: submitted_value}, "testuser"
            )

        assert not hasattr(resp, "status_code"), _body(resp)
        assert all(
            call.args[0] != setting.key for call in set_mock.call_args_list
        ), f"{setting.key} was written despite no-op guard"

    def test_empty_password_is_noop(self):
        s = _make_setting(
            key="llm.openai.api_key", ui_element="password", editable=True
        )
        s.type = "llm"
        self._post_and_assert_noop(s, "")

    def test_sentinel_password_is_noop(self):
        s = _make_setting(
            key="llm.openai.api_key", ui_element="password", editable=True
        )
        s.type = "llm"
        self._post_and_assert_noop(s, DataSanitizer.REDACTION_TEXT)

    def test_sensitive_suffix_non_password_sentinel_is_noop(self):
        """Closes the read/write asymmetry: a secret stored with a
        non-password ui_element but a sensitive '.api_key' suffix is
        redacted on GET, so a save round-trip submits the sentinel -- the
        guard must skip it too (it now shares the redactor's predicate)."""
        s = _make_setting(
            key="llm.custom.api_key", ui_element="text", editable=True
        )
        s.type = "llm"
        self._post_and_assert_noop(s, DataSanitizer.REDACTION_TEXT)


class TestSaveAllSettingsRedactsResponse:
    """The POST /save_all_settings response echoes the full settings dict;
    password values must be redacted in it so the endpoint never ships
    plaintext API keys back to the browser (matching GET /settings/api)."""

    def test_password_value_redacted_in_response(self):
        from local_deep_research.web.routers.settings import (
            _save_all_settings_sync,
        )

        secret = _make_setting(
            key="llm.openai.api_key",
            value="sk-super-secret",
            ui_element="password",
            editable=True,
        )
        secret.type = "llm"
        plain = _make_setting(
            key="llm.model", value="gpt-4", ui_element="text", editable=True
        )
        plain.type = "llm"

        _, db_patch = _patched_db([secret, plain])
        with (
            db_patch,
            patch(
                f"{S}.get_settings_manager",
                return_value=_unlocked_manager(),
            ),
            _quiet_side_effects(),
            patch(f"{S}.coerce_setting_for_write", return_value="gpt-3.5"),
            patch(f"{S}.validate_setting", return_value=(True, None)),
            patch(f"{S}.set_setting", return_value=True),
        ):
            resp = _save_all_settings_sync({"llm.model": "gpt-3.5"}, "testuser")

        data = _body(resp)
        echoed = data["settings"]["llm.openai.api_key"]
        assert echoed["value"] == DataSanitizer.REDACTION_TEXT
        # The plaintext secret must never appear anywhere in the response.
        assert "sk-super-secret" not in json.dumps(data, default=str)
        # Non-secret settings pass through unredacted.
        assert data["settings"]["llm.model"]["value"] == "gpt-4"


class TestSaveAllSettingsWarningKey:
    """save_all_settings includes warnings when warning-affecting key changed."""

    def test_warning_affecting_key_includes_warnings(self):
        from local_deep_research.web.routers.settings import (
            _save_all_settings_sync,
        )

        setting = _make_setting(
            key="llm.provider", value="openai", ui_element="text", editable=True
        )
        setting.type = "llm"
        mock_warnings = [{"type": "info", "message": "Check LLM config"}]

        _, db_patch = _patched_db([setting])
        with (
            db_patch,
            patch(
                f"{S}.get_settings_manager",
                return_value=_unlocked_manager(),
            ),
            _quiet_side_effects(),
            patch(f"{S}.coerce_setting_for_write", return_value="anthropic"),
            patch(f"{S}.validate_setting", return_value=(True, None)),
            patch(f"{S}.set_setting", return_value=True),
            patch(f"{S}.calculate_warnings", return_value=mock_warnings),
        ):
            resp = _save_all_settings_sync(
                {"llm.provider": "anthropic"}, "testuser"
            )

        data = _body(resp)
        assert "warnings" in data
        assert data["warnings"] == mock_warnings


# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------


# ---------------------------------------------------------------------------
# api_get_available_models
# ---------------------------------------------------------------------------


@contextmanager
def _available_models_env(
    discovered, setting_side_effect=None, settings_manager=None, session=None
):
    """Common patch stack for the api_get_available_models tests."""
    _, db_patch = _patched_db(session=session)
    stack = [
        db_patch,
        patch(
            "local_deep_research.llm.providers.get_discovered_provider_options",
            return_value=[],
        ),
        patch(
            "local_deep_research.llm.providers.discover_providers",
            return_value=discovered,
        ),
        patch(
            f"{S}._resolve_model_discovery_policy",
            return_value=(MagicMock(require_local_llm=False), {}),
        ),
    ]
    if setting_side_effect is not None:
        stack.append(
            patch(
                f"{S}._get_setting_from_session",
                side_effect=setting_side_effect,
            )
        )
    if settings_manager is not None:
        stack.append(
            patch(f"{S}.get_settings_manager", return_value=settings_manager)
        )

    from contextlib import ExitStack

    with ExitStack() as es:
        for p in stack:
            es.enter_context(p)
        yield


def _call_available_models():
    from local_deep_research.web.routers.settings import (
        api_get_available_models,
    )

    request = _fake_request()
    request.method = "POST"
    request.query_params = {"force_refresh": "true"}
    return asyncio.run(api_get_available_models(request, username="testuser"))


class TestApiGetAvailableModelsAnthropic:
    """api_get_available_models fetches Anthropic models when key is set.

    Anthropic (cloud) models flow through the auto-discovered-provider loop,
    which calls ``AnthropicProvider.list_models_for_api`` (anthropic SDK).
    """

    def test_anthropic_key_triggers_model_fetch(self):
        from local_deep_research.llm.providers.auto_discovery import (
            ProviderInfo,
        )
        from local_deep_research.llm.providers.implementations.anthropic import (
            AnthropicProvider,
        )

        mock_model = MagicMock()
        mock_model.id = "claude-3-opus-20240229"
        mock_model.display_name = "Claude 3 Opus"
        mock_models_resp = MagicMock()
        mock_models_resp.data = [mock_model]

        mock_client = MagicMock()
        mock_client.models.list.return_value = mock_models_resp

        def _setting(key, username=None, default=""):
            if key == "llm.anthropic.api_key":
                return "sk-ant-test"
            return default

        with _available_models_env(
            {"ANTHROPIC": ProviderInfo(AnthropicProvider)},
            setting_side_effect=_setting,
        ):
            with patch("anthropic.Anthropic", return_value=mock_client):
                resp = _call_available_models()

        data = _body(resp)
        assert "providers" in data
        assert "claude-3-opus-20240229" in str(data)
        mock_client.models.list.assert_called_once()


class TestApiGetAvailableModelsAutoDiscoveryUrl:
    """api_get_available_models uses url_setting from provider class if present."""

    def test_auto_discovered_provider_with_url_setting(self):
        provider_class = MagicMock()
        provider_class.api_key_setting = "llm.custom.api_key"
        provider_class.url_setting = "llm.custom.url"
        provider_class.list_models_for_api.return_value = [
            {"value": "custom-model", "label": "Custom Model (Custom)"}
        ]

        provider_info = MagicMock()
        provider_info.provider_name = "Custom Provider"
        provider_info.provider_class = provider_class

        def _setting(key, username=None, default=""):
            if key == "llm.custom.api_key":
                return "custom-key"
            if key == "llm.custom.url":
                return "http://custom.example.com"
            return default

        with _available_models_env(
            {"custom": provider_info}, setting_side_effect=_setting
        ):
            resp = _call_available_models()

        assert not hasattr(resp, "status_code"), _body(resp)
        provider_class.list_models_for_api.assert_called_once_with(
            "custom-key", "http://custom.example.com"
        )


class TestApiGetAvailableModelsCacheSaveError:
    """api_get_available_models continues when saving to cache fails."""

    def test_cache_save_error_still_returns_200(self):
        session = MagicMock()
        session.commit.side_effect = RuntimeError("db locked")

        with _available_models_env(
            {},
            setting_side_effect=lambda key, username=None, default="": default,
            session=session,
        ):
            resp = _call_available_models()

        # Cache-save failure is logged and rolled back, never fatal.
        assert not hasattr(resp, "status_code"), _body(resp)
        assert "providers" in _body(resp)
        session.rollback.assert_called_once()


class TestApiGetAvailableModelsApiKeySettingNone:
    """Auto-discovered providers with api_key_setting=None get api_key="".

    Regression for issue #3800: LMStudioProvider and LlamaCppProvider declare
    ``api_key_setting = None``. The route must not pass a dict of all settings
    to ``list_models_for_api`` -- that would build
    ``Authorization: Bearer <full-settings-dict>`` and leak every cloud
    provider's API key to the local LM Studio/llama-server endpoint.

    This mocks ``get_settings_manager`` (one layer BELOW the helper) rather
    than ``_get_setting_from_session`` itself, so the production helper
    actually runs and its ``key is None`` guard is exercised.
    """

    def test_none_api_key_setting_passes_empty_string_not_dict(self):
        provider_class = MagicMock()
        provider_class.api_key_setting = None
        provider_class.url_setting = "llm.lmstudio.url"
        provider_class.list_models_for_api.return_value = [
            {"value": "local-model", "label": "Local Model"}
        ]

        provider_info = MagicMock()
        provider_info.provider_name = "LM Studio"
        provider_info.provider_class = provider_class

        buggy_dict = {
            "llm.openai.api_key": "sk-leaked-openai-key",
            "llm.anthropic.api_key": "sk-ant-leaked-key",
        }

        def _sm_get_setting(key, default=None, *_a, **_kw):
            if key is None:
                return buggy_dict
            if key == "llm.lmstudio.url":
                return "http://localhost:1234/v1"
            return default if default is not None else ""

        sm = _unlocked_manager()
        sm.get_setting.side_effect = _sm_get_setting

        with _available_models_env(
            {"lmstudio": provider_info}, settings_manager=sm
        ):
            resp = _call_available_models()

        assert not hasattr(resp, "status_code"), _body(resp)
        provider_class.list_models_for_api.assert_called_once()
        call_args = provider_class.list_models_for_api.call_args
        passed_api_key = (
            call_args.args[0]
            if call_args.args
            else call_args.kwargs.get("api_key")
        )
        assert isinstance(passed_api_key, str), (
            f"api_key must be a string, got {type(passed_api_key).__name__}"
        )
        assert not isinstance(passed_api_key, dict)
        assert passed_api_key != buggy_dict
        # Confirm the production helper ran (was not silently mocked away).
        assert any(
            call.args and call.args[0] == "llm.lmstudio.url"
            for call in sm.get_setting.call_args_list
        ), (
            "Expected the production helper to call "
            "SettingsManager.get_setting('llm.lmstudio.url', ...); the mock "
            "manager was never invoked, so the test isn't exercising the "
            "production code path."
        )
        # Conversely, the helper must NOT have called get_setting(None, ...).
        assert not any(
            call.args and call.args[0] is None
            for call in sm.get_setting.call_args_list
        ), (
            "The helper guard at _get_setting_from_session must short-circuit "
            "key=None to default; instead it delegated to the manager."
        )


# ---------------------------------------------------------------------------
# reset_to_defaults
# ---------------------------------------------------------------------------


class TestResetToDefaultsException:
    """reset_to_defaults returns 500 when an exception occurs."""

    def test_exception_returns_500(self):
        from local_deep_research.web.routers.settings import reset_to_defaults

        sm = _unlocked_manager()
        sm.load_from_defaults_file.side_effect = RuntimeError("file not found")

        _, db_patch = _patched_db()
        with (
            db_patch,
            patch(f"{S}.get_settings_manager", return_value=sm),
            _quiet_side_effects(),
        ):
            resp = reset_to_defaults.__wrapped__(
                _fake_request(), username="testuser"
            )

        assert resp.status_code == 500
        data = _body(resp)
        assert data["status"] == "error"
        assert "failed" in data["message"].lower()


# ---------------------------------------------------------------------------
# api_get_data_location
# ---------------------------------------------------------------------------


class TestApiGetDataLocationEncrypted:
    """api_get_data_location reports encryption status when db is encrypted."""

    def test_encrypted_database_path(self):
        from local_deep_research.web.routers.settings import (
            api_get_data_location,
        )

        mock_dbm = MagicMock()
        mock_dbm.has_encryption = True

        with (
            patch(f"{S}.db_manager", mock_dbm),
            patch(f"{S}.get_data_directory", return_value="/data"),
            patch(f"{S}.get_custom_data_directory", return_value=None),
            patch(
                f"{S}.get_encrypted_database_path", return_value="/data/db.enc"
            ),
            patch(
                "local_deep_research.database.sqlcipher_utils.get_sqlcipher_settings",
                return_value={"cipher": "AES-256"},
            ),
            patch(f"{S}.platform") as mock_platform,
        ):
            mock_platform.system.return_value = "Linux"
            resp = api_get_data_location(_fake_request(), username="testuser")

        data = _body(resp)
        assert data["security_notice"]["encrypted"] is True
        assert data["encryption_settings"] == {"cipher": "AES-256"}


# ---------------------------------------------------------------------------
# get_bulk_settings - per-setting exception
# ---------------------------------------------------------------------------


class TestGetBulkSettingsPerSettingError:
    """get_bulk_settings handles per-setting exception gracefully."""

    def test_per_setting_error_included_in_response(self):
        from local_deep_research.web.routers.settings import get_bulk_settings

        def _setting_side_effect(key, *args, **kwargs):
            if key == "llm.model":
                raise RuntimeError("db error")
            return "some_value"

        req = _fake_request({"keys[]": ["llm.model", "search.tool"]})
        with patch(
            f"{S}._get_setting_from_session", side_effect=_setting_side_effect
        ):
            resp = get_bulk_settings(req, username="testuser")

        data = _body(resp)
        assert data["success"] is True
        assert "llm.model" in data["settings"]
        assert data["settings"]["llm.model"]["exists"] is False
        assert "error" in data["settings"]["llm.model"]
        # The healthy key in the same batch is unaffected.
        assert data["settings"]["search.tool"]["exists"] is True


# ---------------------------------------------------------------------------
# _get_setting_from_session - guard against key=None
# ---------------------------------------------------------------------------


class TestGetSettingFromSessionNoneKey:
    """_get_setting_from_session must short-circuit when key is None.

    Regression for issue #3800: providers like LM Studio and Llama.cpp declare
    ``api_key_setting = None``. Without the guard, the helper would delegate to
    ``SettingsManager.get_setting(None, ...)``, which treats None as "return
    all settings" -- leaking every other provider's API key into the
    auto-discovery loop's ``api_key`` argument.
    """

    def test_none_key_returns_default_without_db_call(self):
        from local_deep_research.web.routers.settings import (
            _get_setting_from_session,
        )

        _, db_patch = _patched_db()
        with db_patch, patch(f"{S}.get_settings_manager") as mock_get_sm:
            result = _get_setting_from_session(None, "testuser", "fallback")

        assert result == "fallback"
        mock_get_sm.assert_not_called()


# ---------------------------------------------------------------------------
# Redaction-sentinel source losses: covered elsewhere, deliberately not here
# ---------------------------------------------------------------------------
#
# This file originally ended with a `TestRedactionSentinelSourceLosses` class
# pinning the two redaction-sentinel losses on this branch:
#
#   1. an EMBEDDED sentinel (e.g. "[REDACTED],discord://webhook/tok") is stored
#      verbatim instead of 400'ing -- main's `_embeds_redaction_sentinel`
#      (settings_routes.py:256-279) and `_redaction_sentinel_error` (:228-253)
#      have no counterpart anywhere in src/;
#   2. a non-password sensitive setting can no longer be CLEARED -- main's
#      `_is_secret_empty_noop` (:212-226) gated the `""` no-op on
#      `ui_element == "password"`, the branch applies it to every sensitive
#      setting (routers/settings.py:625-635, :1163-1174, :3325-3339).
#
# Both were real (#5947 / #5960) and are now fixed in
# `web/routers/settings.py`. They are owned in full by
# `test_settings_routes_security_port.py::TestEmbeddedRedactionSentinelIsRejected`
# and `::TestClearingANonPasswordSecretIsNotANoop`, which cover all three write
# paths (PUT /settings/api/{key}, POST /settings/save_all_settings, and the
# no-JS POST /settings/save_settings) at the HTTP boundary and carry a passing
# non-vacuity control. Keeping a second, direct-call copy of the same two
# assertions here would report one loss as many, so it was removed rather than
# duplicated. Nothing was weakened: see that file for the live tests.