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.

71.3 KB
Raw Blame
"""Port of the deleted ``tests/web/routes/test_settings_routes_gaps_coverage.py``.

Original (on ``origin/main``): 38 test functions over ten classes, driving the
Flask ``settings_bp`` with ``app.test_client()``.  Source under test moved
``web/routes/settings_routes.py`` -> ``web/routers/settings.py``.

WHAT IS **NOT** RE-PORTED (superseded on the branch — successor verified by
reading its assertions, not its name):

* ``TestSaveSettingsEmptyPasswordNoop`` ->
  ``tests/security/test_settings_egress_and_secrets_fastapi.py::
  TestBulkSecretWriteBackAndEcho::test_no_js_form_post_secret_write_back_is_a_noop``
  (parametrised over ``("", "[REDACTED]")``, asserts the STORED row, plus a
  positive control that the same POST wrote a sibling key).
* ``TestApiGetDbSettingRedaction::test_password_value_is_redacted_when_env_overrides_db``
  and ``..._when_env_overrides_default`` ->
  ``tests/security/test_settings_egress_and_secrets_fastapi.py::
  TestSecretRedactionSurvivesEnvOverlay`` (both branches, real ``LDR_*`` env
  var, non-vacuity control on ``editable``).
* ``TestApiTestNotificationUrl::test_success`` ->
  ``tests/web/routers/test_settings_news_hostile_input.py::
  test_test_url_well_formed_body_reaches_test_service_unchanged``.
* ``TestApiGetAvailableModelsEgressPolicy::test_disabled_unprotected_scope_rejects_before_cache_return``
  -> ``tests/security/test_settings_egress_and_secrets_fastapi.py::
  TestModelDiscoveryEgressPolicy::
  test_unresolvable_scope_refuses_before_cache_provider_or_credential[unprotected-...]``
  (same exact JSON body, plus ``provider_options.assert_not_called()``).
* ``...::test_settings_snapshot_failure_refuses_before_cache_return`` ->
  ``tests/security/test_strict_policy_settings_snapshots.py::
  test_model_discovery_rejects_query_failure_before_cache_provider_or_credential``
  (503 + discovery/credential never called).
* ``...::test_fresh_discovery_mixed_dns_never_reads_key_or_calls_provider`` ->
  ``tests/security/test_settings_egress_and_secrets_fastapi.py::
  TestModelDiscoveryEgressPolicy::
  test_mixed_dns_denies_before_credential_read_or_provider_call``.

NOT superseded despite appearances:

* ``tests/security/test_settings_bulk_secret_leak.py`` looks like a successor
  for the bulk-GET redaction but computes the shipped value with its own
  ``_bulk_value()`` helper defined inside the test module — deleting
  ``DataSanitizer.redact_value(...)`` from ``get_bulk_settings`` leaves every
  test in it green.  It pins the sanitizer, not the route.
* ``tests/security/test_settings_secret_redaction_isolation.py`` imports
  ``local_deep_research.web.app_factory`` (deleted) and
  ``...web.routes.settings_routes`` (deleted): 18 of its 20 tests ERROR at
  fixture setup and 2 fail on import, so it pins nothing on this branch.
* ``test_settings_api.py::test_get_bulk_settings`` asserts only
  ``"settings" in data``; ``::test_get_data_location`` asserts only
  ``"data_directory" in data``.  Both stay green with the behaviour deleted.

The restored-behaviour tests below remain active regression coverage.  They
are not skipped or marked xfail; a future divergence from main must fail CI.

HARNESS.  ``TestClient`` against the real FastAPI app with ``require_auth``
overridden, plus a real CSRF token for the write routes (CSRF is
ASGI-middleware-enforced).  ``follow_redirects=False`` everywhere a redirect is
possible: httpx follows by default where Flask's client did not.
"""

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

import pytest
from fastapi.testclient import TestClient

S = "local_deep_research.web.routers.settings"


# ---------------------------------------------------------------------------
# Harness
# ---------------------------------------------------------------------------


@pytest.fixture
def client():
    from local_deep_research.web.dependencies.auth import require_auth
    from local_deep_research.web.fastapi_app import app

    app.dependency_overrides[require_auth] = lambda: "testuser"
    c = TestClient(app, raise_server_exceptions=False)
    token = c.get("/auth/csrf-token").json()["csrf_token"]
    c.headers.update({"X-CSRFToken": token})
    try:
        yield c
    finally:
        app.dependency_overrides.pop(require_auth, None)


def _make_password_db_setting(key="llm.openai.api_key", value="sk-existing"):
    """A mock ``Setting`` row with ``ui_element='password'``.

    All JSON-serialisable fields get concrete values so the GET routes don't
    choke on auto-created MagicMock attributes.
    """
    s = MagicMock()
    s.key = key
    s.value = value
    s.ui_element = "password"
    s.editable = True
    s.visible = True
    s.type = "LLM"
    s.name = key
    s.description = ""
    s.category = "llm_general"
    s.options = None
    s.min_value = None
    s.max_value = None
    s.step = None
    return s


@contextmanager
def _db_with(db_setting, all_settings_overlay=None):
    """Patch ``get_user_db_session`` so ``query(Setting).filter(...).first()``
    returns ``db_setting``, and ``get_settings_manager`` so the single-key
    env overlay (``_apply_env_override`` -> ``get_all_settings()``) returns
    ``all_settings_overlay`` (empty by default = no override)."""
    db = MagicMock()
    db.query.return_value.filter.return_value.first.return_value = db_setting

    @contextmanager
    def _fake_session(*args, **kwargs):
        yield db

    manager = MagicMock()
    manager.settings_locked = False
    manager.get_all_settings.return_value = all_settings_overlay or {}
    manager.default_settings = {}
    # Not globally or env-locked: bare MagicMock attributes are truthy and
    # would make the PUT route return 403 before its secret no-op guard.
    manager._is_environment_locked.return_value = False

    with (
        patch(f"{S}.get_user_db_session", side_effect=_fake_session),
        patch(f"{S}.get_settings_manager", return_value=manager),
    ):
        yield db, manager


# ===========================================================================
# api_get_all_settings — redaction  (GET /settings/api)
# ===========================================================================


class TestApiGetAllSettingsRedaction:
    """``ui_element == 'password'`` values must be masked in the JSON snapshot
    so env-overridden API keys cannot leak through the JSON endpoint.

    ``test_settings_api.py::test_sensitive_setting_get_is_redacted_and_roundtrip_safe``
    covers the redaction itself against a real DB; the two properties it does
    NOT assert — that non-secret values survive, and that the redacted entry
    keeps its metadata — are what make the redaction usable rather than a
    blanket wipe, so they are re-ported here.
    """

    SECRET = "sk-real-leaked-key"
    SAFE_VALUE = "summary_focus_query"

    def _snapshot_with_secret(self):
        return {
            "llm.openai.api_key": {
                "value": self.SECRET,
                "ui_element": "password",
                "type": "LLM",
            },
            "search.fetch.mode": {
                "value": self.SAFE_VALUE,
                "ui_element": "select",
                "type": "SEARCH",
            },
        }

    @contextmanager
    def _patched(self):
        manager = Mock()
        manager.get_all_settings.return_value = self._snapshot_with_secret()

        @contextmanager
        def _fake_session(*args, **kwargs):
            yield MagicMock()

        with (
            patch(f"{S}.get_user_db_session", side_effect=_fake_session),
            patch(f"{S}.get_settings_manager", return_value=manager),
        ):
            yield

    def test_password_value_is_redacted(self, client):
        """The plaintext API key MUST NOT appear in the response."""
        with self._patched():
            resp = client.get("/settings/api")

        assert resp.status_code == 200
        data = resp.json()
        assert data["status"] == "success"
        assert data["settings"]["llm.openai.api_key"]["value"] == "[REDACTED]"
        # Belt-and-braces: the raw secret must be nowhere in the payload.
        assert self.SECRET not in resp.text

    def test_non_secret_values_pass_through(self, client):
        """Non-password settings still come back with their real values."""
        with self._patched():
            resp = client.get("/settings/api")

        data = resp.json()
        assert data["settings"]["search.fetch.mode"]["value"] == self.SAFE_VALUE

    def test_metadata_preserved_for_redacted_entry(self, client):
        """ui_element/type survive so the UI can still render a password input
        even though the value is masked."""
        with self._patched():
            resp = client.get("/settings/api")

        entry = resp.json()["settings"]["llm.openai.api_key"]
        assert entry["ui_element"] == "password"
        assert entry["type"] == "LLM"


# ===========================================================================
# api_update_setting — empty / sentinel secret write is a no-op
# ===========================================================================


class TestApiUpdateSettingEmptyPasswordNoop:
    """``PUT /settings/api/<key>`` with ``value=''`` (or the redaction
    sentinel) on a password setting must be a no-op (200 + message), never a
    write.

    ``test_settings_api.py::test_put_empty_secret_is_noop`` asserts the 200 and
    the ``"unchanged"`` message.  It does not assert that the write step was
    never reached — the exact thing the guard exists to prevent — so the
    ``commit``/stored-value half is re-ported here.
    """

    def test_empty_password_returns_200_without_writing(self, client):
        existing = _make_password_db_setting(value="sk-real-existing")

        with _db_with(existing) as (db, _):
            resp = client.put(
                "/settings/api/llm.openai.api_key", json={"value": ""}
            )

        assert resp.status_code == 200
        # Idempotent message — the route does NOT return an error, because a
        # client save indicator would otherwise show a spurious failure when
        # blurring an empty password field.
        assert "unchanged" in resp.json()["message"].lower()
        # The route never reached the write step.
        db.commit.assert_not_called()
        assert existing.value == "sk-real-existing"

    def test_redacted_sentinel_returns_200_without_writing(self, client):
        """``GET /settings/api`` redacts password values to the sentinel, so a
        stale tab / automation round-trip would otherwise persist the literal
        ``"[REDACTED]"`` over the real secret."""
        from local_deep_research.security.data_sanitizer import DataSanitizer

        existing = _make_password_db_setting(value="sk-real-existing")

        with _db_with(existing) as (db, _):
            resp = client.put(
                "/settings/api/llm.openai.api_key",
                json={"value": DataSanitizer.REDACTION_TEXT},
            )

        assert resp.status_code == 200
        assert "unchanged" in resp.json()["message"].lower()
        db.commit.assert_not_called()
        assert existing.value == "sk-real-existing"

    def test_non_empty_password_does_not_match_noop_guard(self, client):
        """Control: the no-op guard must NOT fire for a real value — otherwise
        a guard that swallowed every password write would score green above.

        The route may still error past the guard (the write path depends on
        machinery this test does not mock); the invariant proved here is only
        that this is NOT the no-op 200 response.
        """
        existing = _make_password_db_setting(value="sk-old")

        with _db_with(existing):
            resp = client.put(
                "/settings/api/llm.openai.api_key", json={"value": "sk-new"}
            )

        if resp.status_code == 200:
            body = resp.json() or {}
            assert "unchanged" not in (body.get("message") or "").lower()


# ===========================================================================
# api_get_db_setting — singular endpoint redaction (GET /settings/api/<key>)
# ===========================================================================


class TestApiGetDbSettingRedaction:
    SECRET = "sk-real-leaked"

    def test_password_value_is_redacted_db_branch(self, client):
        """A password-typed DB row's ``value`` must come back ``[REDACTED]``,
        with the metadata still readable so the front-end can render the right
        input control."""
        existing = _make_password_db_setting(value=self.SECRET)

        with _db_with(existing):
            resp = client.get("/settings/api/llm.openai.api_key")

        assert resp.status_code == 200
        body = resp.json()
        assert body["value"] == "[REDACTED]"
        assert body["ui_element"] == "password"
        assert self.SECRET not in resp.text

    def test_non_password_value_passes_through_db_branch(self, client):
        """Non-password settings stay readable — only secrets are special."""
        plain = _make_password_db_setting(
            key="search.fetch.mode", value="summary_focus_query"
        )
        plain.ui_element = "select"

        with _db_with(plain):
            resp = client.get("/settings/api/search.fetch.mode")

        assert resp.json()["value"] == "summary_focus_query"

    def test_empty_password_value_stays_empty(self, client):
        """Empty/None values are not the secret — leave them readable so the
        front-end can tell 'not configured' from 'configured'."""
        existing = _make_password_db_setting(value="")

        with _db_with(existing):
            resp = client.get("/settings/api/llm.openai.api_key")

        assert resp.json()["value"] == ""

    def test_non_secret_env_override_stays_readable(self, client):
        """The env overlay must only be redacted for SENSITIVE settings: a
        non-secret overridden value ships readable, with the env-lock
        metadata (``editable == False``) intact.

        The env var itself is read by ``SettingsManager.get_all_settings()``;
        ``_apply_env_override`` (settings.py:217) is the overlay this route
        applies on top of the DB row, so the overlay result is injected here.
        The env-var *plumbing* is pinned end-to-end by
        ``tests/security/test_settings_egress_and_secrets_fastapi.py::
        TestSecretRedactionSurvivesEnvOverlay``; what is pinned here is the
        part that file does not assert — that a NON-secret overlaid value is
        not masked.
        """
        plain = _make_password_db_setting(
            key="search.fetch.mode", value="summary_focus_query"
        )
        plain.ui_element = "select"

        overlay = {
            "search.fetch.mode": {"value": "full_page", "editable": False}
        }
        with _db_with(plain, all_settings_overlay=overlay):
            resp = client.get("/settings/api/search.fetch.mode")

        assert resp.status_code == 200
        body = resp.json()
        assert body["value"] == "full_page"
        assert body["editable"] is False


# ===========================================================================
# get_bulk_settings (GET /settings/api/bulk) — caller-controlled keys[]
# ===========================================================================


class TestGetBulkSettingsRedaction:
    """Anyone authenticated can ask for arbitrary keys, including password
    ones: the default key list excludes them but ``?keys[]=llm.openai.api_key``
    does not.  Same suffix-based defence as ``redact_settings_snapshot``.

    ``test_settings_api.py::test_bulk_settings_redacts_secrets`` covers the
    plain exact-key case.  The invisible-padding normalisation, the
    non-sensitive pass-through and the empty-value carve-out are only pinned
    at the ``DataSanitizer`` level (``tests/security/test_settings_bulk_secret_leak.py``
    re-implements the route's expression inside the test module), so they are
    re-ported at the ROUTE level here.
    """

    SECRET = "sk-real-bulk-leak"

    def test_password_key_is_redacted_when_explicitly_requested(self, client):
        with patch(f"{S}._get_setting_from_session", return_value=self.SECRET):
            resp = client.get("/settings/api/bulk?keys[]=llm.openai.api_key")

        assert resp.status_code == 200
        entry = resp.json()["settings"]["llm.openai.api_key"]
        assert entry["value"] == "[REDACTED]"
        # `exists` must still be true so callers can tell the key is set —
        # only the value is masked.
        assert entry["exists"] is True
        assert self.SECRET not in resp.text

    @pytest.mark.parametrize(
        "pad", ["\u200b", "⠀", "\U00013441", "\U00013442", "\U0001d159"]
    )
    def test_invisible_padded_password_key_is_redacted(self, client, pad):
        """Exercise the HTTP route, including query parsing and serialization."""
        key = "llm.openai.api_key" + pad
        with patch(f"{S}._get_setting_from_session", return_value=self.SECRET):
            resp = client.get("/settings/api/bulk", params=[("keys[]", key)])

        assert resp.status_code == 200
        assert resp.json()["settings"][key] == {
            "value": "[REDACTED]",
            "exists": True,
        }
        assert self.SECRET not in resp.text

    def test_non_sensitive_key_passes_through(self, client):
        """Suffix outside DEFAULT_SENSITIVE_KEYS keeps its real value."""
        with patch(f"{S}._get_setting_from_session", return_value="searxng"):
            resp = client.get("/settings/api/bulk?keys[]=search.tool")

        assert resp.json()["settings"]["search.tool"]["value"] == "searxng"

    def test_empty_password_value_stays_empty(self, client):
        with patch(f"{S}._get_setting_from_session", return_value=""):
            resp = client.get("/settings/api/bulk?keys[]=llm.lmstudio.api_key")

        # Empty value is not the secret — stays empty so the front-end can
        # tell "not configured".
        assert resp.json()["settings"]["llm.lmstudio.api_key"]["value"] == ""


# ===========================================================================
# get_bulk_settings — general contract
# ===========================================================================


class TestGetBulkSettings:
    """``test_settings_api.py::test_get_bulk_settings`` asserts only that a
    ``settings`` key exists, so none of the four properties below are pinned
    on the branch."""

    def test_returns_defaults_when_no_keys_specified(self, client):
        with patch(f"{S}._get_setting_from_session", return_value="test-val"):
            resp = client.get("/settings/api/bulk")

        assert resp.status_code == 200
        data = resp.json()
        assert data["success"] is True
        assert "llm.provider" in data["settings"]
        assert "search.tool" in data["settings"]

    def test_returns_specific_keys(self, client):
        with patch(f"{S}._get_setting_from_session", return_value="val"):
            resp = client.get(
                "/settings/api/bulk?keys[]=custom.key1&keys[]=custom.key2"
            )

        assert resp.status_code == 200
        data = resp.json()
        assert "custom.key1" in data["settings"]
        assert "custom.key2" in data["settings"]
        assert data["settings"]["custom.key1"]["value"] == "val"

    def test_returns_exists_false_for_none_value(self, client):
        with patch(f"{S}._get_setting_from_session", return_value=None):
            resp = client.get("/settings/api/bulk?keys[]=missing.key")

        assert resp.json()["settings"]["missing.key"]["exists"] is False

    def test_handles_per_key_errors(self, client):
        """One key raising must not fail the whole response — the per-key
        try/except keeps ``success`` true and marks just that key."""
        with patch(
            f"{S}._get_setting_from_session",
            side_effect=RuntimeError("DB error"),
        ):
            resp = client.get("/settings/api/bulk?keys[]=bad.key")

        data = resp.json()
        assert data["success"] is True
        assert data["settings"]["bad.key"]["exists"] is False
        assert "error" in data["settings"]["bad.key"]


# ===========================================================================
# api_get_data_location
# ===========================================================================


class TestApiGetDataLocation:
    """``test_settings_api.py::test_get_data_location`` asserts only that
    ``data_directory`` is present, so ``is_custom``, the encryption notice and
    the 500 arm are unpinned on the branch."""

    def test_returns_data_location_info(self, client):
        with (
            patch(f"{S}.get_data_directory", return_value="/data/ldr"),
            patch(f"{S}.get_custom_data_directory", return_value=None),
            patch(
                f"{S}.get_encrypted_database_path",
                return_value="/data/ldr/encrypted",
            ),
            patch(f"{S}.db_manager", Mock(has_encryption=True)),
            patch(
                "local_deep_research.database.sqlcipher_utils.get_sqlcipher_settings",
                return_value={"kdf_iterations": 256000},
            ),
        ):
            resp = client.get("/settings/api/data-location")

        assert resp.status_code == 200
        data = resp.json()
        assert data["data_directory"] == "/data/ldr"
        assert data["is_custom"] is False
        assert data["custom_env_value"] is None
        assert data["security_notice"]["encrypted"] is True

    def test_returns_unencrypted_warning(self, client):
        with (
            patch(f"{S}.get_data_directory", return_value="/data/ldr"),
            patch(
                f"{S}.get_custom_data_directory",
                return_value=Path("/custom/dir"),
            ),
            patch(
                f"{S}.get_encrypted_database_path",
                return_value="/data/ldr/db",
            ),
            patch(f"{S}.db_manager", Mock(has_encryption=False)),
        ):
            resp = client.get("/settings/api/data-location")

        data = resp.json()
        assert data["is_custom"] is True
        assert data["custom_env_value"] == str(Path("/custom/dir"))
        assert data["security_notice"]["encrypted"] is False

    def test_ldr_data_dir_marks_the_location_custom(
        self, client, tmp_path, monkeypatch
    ):
        """``LDR_DATA_DIR`` moves the data directory, so it is what the
        response reports as the custom location."""
        custom = tmp_path / "custom-data"
        monkeypatch.setenv("LDR_DATA_DIR", str(custom))

        with patch(f"{S}.db_manager", Mock(has_encryption=False)):
            resp = client.get("/settings/api/data-location")

        assert resp.status_code == 200
        data = resp.json()
        assert data["is_custom"] is True
        assert data["custom_env_var"] == "LDR_DATA_DIR"
        assert data["custom_env_value"] == str(custom.resolve())
        assert data["data_directory"] == data["custom_env_value"]

    def test_bootstrap_data_dir_variable_does_not_mark_the_location_custom(
        self, client, tmp_path, monkeypatch
    ):
        """``LDR_BOOTSTRAP_DATA_DIR`` moves nothing, so it must neither mark
        the location custom nor make the settings page create its directory."""
        platform_default = tmp_path / "platform-default"
        decoy = tmp_path / "decoy"
        monkeypatch.delenv("LDR_DATA_DIR", raising=False)
        monkeypatch.setenv("LDR_BOOTSTRAP_DATA_DIR", str(decoy))
        monkeypatch.setattr(
            "platformdirs.user_data_dir",
            lambda *_args, **_kwargs: str(platform_default),
        )

        with patch(f"{S}.db_manager", Mock(has_encryption=False)):
            resp = client.get("/settings/api/data-location")

        assert resp.status_code == 200
        data = resp.json()
        assert data["data_directory"] == str(platform_default)
        assert data["is_custom"] is False
        assert data["custom_env_value"] is None
        assert not decoy.exists()

    def test_exception_returns_500(self, client):
        with patch(f"{S}.get_data_directory", side_effect=RuntimeError("fail")):
            resp = client.get("/settings/api/data-location")

        assert resp.status_code == 500


# ===========================================================================
# api_test_notification_url
# ===========================================================================


class TestApiTestNotificationUrl:
    def test_missing_service_url_returns_400(self, client):
        """A body without ``service_url`` must return 400, not error out."""
        with patch(f"{S}._get_setting_from_session", return_value=""):
            resp = client.post(
                "/settings/api/notifications/test-url",
                json={"wrong_key": "value"},
            )

        assert resp.status_code == 400
        assert resp.json()["success"] is False

    def test_empty_body_returns_400(self, client):
        with patch(f"{S}._get_setting_from_session", return_value=""):
            resp = client.post("/settings/api/notifications/test-url", json={})

        assert resp.status_code == 400
        assert resp.json()["success"] is False

    def test_service_exception_text_is_not_leaked_to_client(self, client):
        """The response boundary must stay generic when ``test_service`` raises.

        ``SendError`` carries the underlying exception text
        (``notifications/service.py``: ``raise SendError(f"Failed to send
        notification: {str(e)}")``).  This guards the response boundary — the
        right layer for CWE-209 defence — so that if anything in the test-URL
        flow ever raises with sensitive detail, the endpoint's ``except`` keeps
        returning a generic message instead of echoing it.  Fails if someone
        changes the handler to surface ``str(e)``.
        """
        from local_deep_research.notifications.exceptions import SendError

        secret = "SMTP-PASSWORD-do-not-leak-12345"
        mock_svc = Mock()
        mock_svc.test_service.side_effect = SendError(
            f"Failed to send notification: {secret}"
        )

        with patch(
            "local_deep_research.notifications.service.NotificationService",
            return_value=mock_svc,
        ):
            resp = client.post(
                "/settings/api/notifications/test-url",
                json={"service_url": "ntfy://topic"},
            )

        assert resp.status_code == 500
        assert secret not in resp.text
        data = resp.json()
        assert data["success"] is False
        assert secret not in data["error"]


# ===========================================================================
# api_get_available_models
# ===========================================================================


@contextmanager
def _models_env(discovered_options, discovered_providers=None, extra=()):
    """Common patch set for cached reads and POST model discovery."""

    @contextmanager
    def _fake_session(*a, **kw):
        yield MagicMock()

    stack = [
        patch(f"{S}.get_user_db_session", side_effect=_fake_session),
        patch(
            "local_deep_research.llm.providers.get_discovered_provider_options",
            return_value=discovered_options,
        ),
    ]
    if discovered_providers is not None:
        stack.append(
            patch(
                "local_deep_research.llm.providers.discover_providers",
                return_value=discovered_providers,
            )
        )
    stack.extend(extra)

    started = [p.start() for p in stack]
    try:
        yield started
    finally:
        for p in reversed(stack):
            p.stop()


class TestApiGetAvailableModels:
    """Direct non-policy behavior coverage for available-model discovery.

    Before this port, ``force_refresh``, provider-option de-duplication, and
    the single guarded Ollama fetch path had no behavior-sensitive coverage.
    """

    def _cache_vs_fresh(self, client, query, *, post=None):
        """Drive the route with a populated model cache AND a live discovery
        that returns a DIFFERENT model, so the response says which path ran."""
        from local_deep_research.security.egress.policy import (
            EgressContext,
            EgressScope,
        )

        cached = MagicMock()
        cached.provider = "OLLAMA"
        cached.model_key = "cached-only-model"
        cached.model_label = "cached-only-model"

        session = MagicMock()
        session.query.return_value.filter.return_value.all.return_value = [
            cached
        ]

        @contextmanager
        def _session(*a, **kw):
            yield session

        policy_context = EgressContext(
            scope=EgressScope.PUBLIC_ONLY,
            primary_engine="library",
            require_local_llm=False,
            require_local_embeddings=False,
            local_hostnames=(),
            username="testuser",
        )

        with (
            patch(f"{S}.get_user_db_session", side_effect=_session),
            patch(
                f"{S}._resolve_model_discovery_policy",
                return_value=(policy_context, {}),
            ),
            patch(
                "local_deep_research.llm.providers.get_discovered_provider_options",
                return_value=[],
            ),
            patch(
                "local_deep_research.llm.providers.discover_providers",
                return_value={
                    "OLLAMA": self._discovered(
                        "Ollama",
                        [{"value": "fresh-model", "label": "Fresh (Ollama)"}],
                    )
                },
            ),
            patch(f"{S}._get_setting_from_session", return_value=""),
        ):
            # A refresh is a state change: POST-only (CSRF).
            if post is None:
                post = (
                    "refresh_provider" in query or "force_refresh=true" in query
                )
            send = client.post if post else client.get
            resp = send(f"/settings/api/available-models{query}")

        assert resp.status_code == 200, resp.text[:300]
        return resp.json()

    @pytest.mark.parametrize("query", ["", "?force_refresh=false"])
    def test_cache_is_used_when_force_refresh_is_absent(self, client, query):
        """Control for the test below: a GET without ``force_refresh=true``
        returns the cached rows, so 'fresh models' below means the cache was
        genuinely bypassed rather than empty."""
        data = self._cache_vs_fresh(client, query)
        assert [m["value"] for m in data["providers"]["ollama_models"]] == [
            "cached-only-model"
        ]

    def test_force_refresh_bypasses_cache(self, client):
        """POST with an explicit refresh flag (and no target) skips the
        cache and fetches live.

        The original asserted only ``200`` + ``"providers" in body``, which
        stays green with the query-param parsing deleted.  The cached-vs-fresh
        model identity is added so the assertion actually depends on the
        POST refresh branch being taken.
        """
        data = self._cache_vs_fresh(client, "?force_refresh=true")

        assert "providers" in data
        assert [m["value"] for m in data["providers"]["ollama_models"]] == [
            "fresh-model"
        ]

    def test_refresh_provider_bypasses_cache_for_target_only(self, client):
        """``?refresh_provider=<p>`` re-fetches just that provider's catalog
        while other providers keep their cached rows.

        This is the path the research page takes after a provider API key
        is saved: the plain cache read would keep the newly keyed
        provider's stale/empty list for up to 24h, and a full
        ``force_refresh`` re-probes every provider serially.
        """
        data = self._cache_vs_fresh(client, "?refresh_provider=ollama")

        assert "providers" in data
        assert [m["value"] for m in data["providers"]["ollama_models"]] == [
            "fresh-model"
        ]

    def test_post_without_a_refresh_query_reuses_the_cache(self, client):
        """A ``POST`` with neither ``force_refresh=true`` nor
        ``refresh_provider`` is cache-first: it returns the fresh cached rows
        and discovers only on a cache miss.

        This test once asserted the opposite: when only refreshes were
        POSTs, a bare ``POST`` was the full refresh. Every model loader now
        POSTs (a ``GET`` reads only the cache and never discovers, so a cold
        cache would leave the dropdowns empty), so the full refresh is
        ``POST ?force_refresh=true`` and a bare ``POST`` must not bypass a
        fresh cache on every page load.
        """
        data = self._cache_vs_fresh(client, "", post=True)
        assert [m["value"] for m in data["providers"]["ollama_models"]] == [
            "cached-only-model"
        ]

    @pytest.mark.parametrize(
        "query",
        [
            "?refresh_provider=ollama",
            "?force_refresh=true",
            "?force_refresh=TRUE",
            "?force_refresh=true&refresh_provider=ollama",
        ],
    )
    def test_get_with_a_refresh_query_is_refused_before_any_work(
        self, client, query
    ):
        """A refresh calls providers with the stored keys and rewrites
        cached rows (``force_refresh=true`` used to delete every cached
        row), so on ``GET`` (no CSRF check) it answers 405 with
        ``Allow: POST`` before the policy is resolved, the cache is read or
        any provider is discovered."""
        with (
            patch(f"{S}.get_user_db_session") as session,
            patch(f"{S}._resolve_model_discovery_policy") as policy,
            patch(
                "local_deep_research.llm.providers.discover_providers"
            ) as discover,
        ):
            resp = client.get(f"/settings/api/available-models{query}")

        assert resp.status_code == 405, resp.text[:300]
        assert resp.headers["allow"] == "POST"
        session.assert_not_called()
        policy.assert_not_called()
        discover.assert_not_called()

    @pytest.mark.parametrize(
        "query",
        [
            "?refresh_provider=LMSTUDIO",
            "?refresh_provider=LMSTUDIO&force_refresh=true",
        ],
        ids=["targeted", "force-flag-does-not-widen"],
    )
    def test_refresh_provider_fetches_only_the_target(self, client, query):
        """A targeted refresh must not touch any other provider's
        upstream: only the named provider's ``list_models_for_api`` runs,
        and its rows merge with (not replace) the cached catalog. A target
        takes precedence over ``force_refresh=true``, so the flag cannot
        widen a targeted refresh into a full one."""
        from local_deep_research.security.egress.policy import (
            EgressContext,
            EgressScope,
        )

        cached = MagicMock()
        cached.provider = "OLLAMA"
        cached.model_key = "cached-ollama-model"
        cached.model_label = "cached-ollama-model"

        session = MagicMock()
        session.query.return_value.filter.return_value.all.return_value = [
            cached
        ]

        @contextmanager
        def _session(*a, **kw):
            yield session

        ollama_info = self._discovered(
            "Ollama", [{"value": "fresh-ollama", "label": "Fresh (Ollama)"}]
        )
        lmstudio_info = self._discovered(
            "LM Studio",
            [{"value": "fresh-lmstudio", "label": "Fresh (LM Studio)"}],
        )
        policy_context = EgressContext(
            scope=EgressScope.PUBLIC_ONLY,
            primary_engine="library",
            require_local_llm=False,
            require_local_embeddings=False,
            local_hostnames=(),
            username="testuser",
        )

        with (
            patch(f"{S}.get_user_db_session", side_effect=_session),
            patch(
                f"{S}._resolve_model_discovery_policy",
                return_value=(policy_context, {}),
            ),
            patch(
                "local_deep_research.llm.providers.get_discovered_provider_options",
                return_value=[],
            ),
            patch(
                "local_deep_research.llm.providers.discover_providers",
                return_value={
                    "OLLAMA": ollama_info,
                    "LMSTUDIO": lmstudio_info,
                },
            ),
            patch(f"{S}._get_setting_from_session", return_value=""),
        ):
            resp = client.post(f"/settings/api/available-models{query}")

        assert resp.status_code == 200, resp.text[:300]
        data = resp.json()
        lmstudio_info.provider_class.list_models_for_api.assert_called_once()
        ollama_info.provider_class.list_models_for_api.assert_not_called()
        assert [m["value"] for m in data["providers"]["lmstudio_models"]] == [
            "fresh-lmstudio"
        ]
        # Cached Ollama rows are merged through untouched.
        assert [m["value"] for m in data["providers"]["ollama_models"]] == [
            "cached-ollama-model"
        ]

    def test_refresh_provider_unknown_name_returns_cache(self, client):
        """An unknown provider name degrades to the plain cache read —
        no upstream fetch, cached rows intact."""
        data = self._cache_vs_fresh(client, "?refresh_provider=no_such")

        assert [m["value"] for m in data["providers"]["ollama_models"]] == [
            "cached-only-model"
        ]

    def test_refresh_provider_with_cold_cache_falls_back_to_full_discovery(
        self,
        client,
    ):
        """A targeted refresh against an empty model cache (fresh install)
        must NOT return a one-provider catalog — the client would cache it
        as the whole world and blank every other provider's dropdown. It
        falls back to the same full discovery the plain cold-cache path
        performs."""
        from local_deep_research.security.egress.policy import (
            EgressContext,
            EgressScope,
        )

        session = MagicMock()
        session.query.return_value.filter.return_value.all.return_value = []

        @contextmanager
        def _session(*a, **kw):
            yield session

        ollama_info = self._discovered(
            "Ollama", [{"value": "fresh-ollama", "label": "Fresh (Ollama)"}]
        )
        lmstudio_info = self._discovered(
            "LM Studio",
            [{"value": "fresh-lmstudio", "label": "Fresh (LM Studio)"}],
        )
        policy_context = EgressContext(
            scope=EgressScope.PUBLIC_ONLY,
            primary_engine="library",
            require_local_llm=False,
            require_local_embeddings=False,
            local_hostnames=(),
            username="testuser",
        )

        with (
            patch(f"{S}.get_user_db_session", side_effect=_session),
            patch(
                f"{S}._resolve_model_discovery_policy",
                return_value=(policy_context, {}),
            ),
            patch(
                "local_deep_research.llm.providers.get_discovered_provider_options",
                return_value=[],
            ),
            patch(
                "local_deep_research.llm.providers.discover_providers",
                return_value={
                    "OLLAMA": ollama_info,
                    "LMSTUDIO": lmstudio_info,
                },
            ),
            patch(f"{S}._get_setting_from_session", return_value=""),
        ):
            resp = client.post(
                "/settings/api/available-models?refresh_provider=ollama"
            )

        assert resp.status_code == 200, resp.text[:300]
        data = resp.json()
        # Full discovery ran: BOTH providers' models are in the response.
        assert [m["value"] for m in data["providers"]["ollama_models"]] == [
            "fresh-ollama"
        ]
        assert [m["value"] for m in data["providers"]["lmstudio_models"]] == [
            "fresh-lmstudio"
        ]

    def test_refresh_provider_failure_keeps_cached_rows(self, client):
        """A transient upstream failure during a targeted refresh must not
        blank the provider's catalog: the response restores the cached rows
        and the DB rows are never deleted."""
        from local_deep_research.security.egress.policy import (
            EgressContext,
            EgressScope,
        )

        cached = MagicMock()
        cached.provider = "OLLAMA"
        cached.model_key = "cached-ollama-model"
        cached.model_label = "cached-ollama-model"

        session = MagicMock()
        session.query.return_value.filter.return_value.all.return_value = [
            cached
        ]

        @contextmanager
        def _session(*a, **kw):
            yield session

        ollama_info = self._discovered("Ollama", [])
        ollama_info.provider_class.list_models_for_api.side_effect = (
            ConnectionError("transient upstream failure")
        )
        policy_context = EgressContext(
            scope=EgressScope.PUBLIC_ONLY,
            primary_engine="library",
            require_local_llm=False,
            require_local_embeddings=False,
            local_hostnames=(),
            username="testuser",
        )

        with (
            patch(f"{S}.get_user_db_session", side_effect=_session),
            patch(
                f"{S}._resolve_model_discovery_policy",
                return_value=(policy_context, {}),
            ),
            patch(
                "local_deep_research.llm.providers.get_discovered_provider_options",
                return_value=[],
            ),
            patch(
                "local_deep_research.llm.providers.discover_providers",
                return_value={"OLLAMA": ollama_info},
            ),
            patch(f"{S}._get_setting_from_session", return_value=""),
        ):
            resp = client.post(
                "/settings/api/available-models?refresh_provider=ollama"
            )

        assert resp.status_code == 200, resp.text[:300]
        data = resp.json()
        # Cached rows restored to the response...
        assert [m["value"] for m in data["providers"]["ollama_models"]] == [
            "cached-ollama-model"
        ]
        # ...and the DB cache was never touched (no delete on any provider).
        delete_calls = session.query.return_value.filter.return_value.delete
        delete_calls.assert_not_called()

    def test_refresh_provider_empty_result_keeps_cached_rows(self, client):
        """``list_models_for_api`` converts upstream failures into an empty
        list (its catch-all never raises), so a dead endpoint during a key
        re-save arrives here as a *successful* fetch of zero models. That
        must be treated as a failed probe: the cached rows are kept in the
        response and the DB, not deleted."""
        from local_deep_research.security.egress.policy import (
            EgressContext,
            EgressScope,
        )

        cached = MagicMock()
        cached.provider = "OLLAMA"
        cached.model_key = "cached-ollama-model"
        cached.model_label = "cached-ollama-model"

        session = MagicMock()
        session.query.return_value.filter.return_value.all.return_value = [
            cached
        ]

        @contextmanager
        def _session(*a, **kw):
            yield session

        # No side_effect: the fetch "succeeds" with zero models, which is
        # the real-world shape of a transient upstream failure.
        ollama_info = self._discovered("Ollama", [])
        policy_context = EgressContext(
            scope=EgressScope.PUBLIC_ONLY,
            primary_engine="library",
            require_local_llm=False,
            require_local_embeddings=False,
            local_hostnames=(),
            username="testuser",
        )

        with (
            patch(f"{S}.get_user_db_session", side_effect=_session),
            patch(
                f"{S}._resolve_model_discovery_policy",
                return_value=(policy_context, {}),
            ),
            patch(
                "local_deep_research.llm.providers.get_discovered_provider_options",
                return_value=[],
            ),
            patch(
                "local_deep_research.llm.providers.discover_providers",
                return_value={"OLLAMA": ollama_info},
            ),
            patch(f"{S}._get_setting_from_session", return_value=""),
        ):
            resp = client.post(
                "/settings/api/available-models?refresh_provider=ollama"
            )

        assert resp.status_code == 200, resp.text[:300]
        data = resp.json()
        # Cached rows kept in the response...
        assert [m["value"] for m in data["providers"]["ollama_models"]] == [
            "cached-ollama-model"
        ]
        # ...and the DB rows were never deleted.
        session.query.return_value.filter.return_value.delete.assert_not_called()

    def _targeted_refresh(
        self,
        client,
        query,
        cached_rows,
        discovered,
        *,
        require_local_llm=False,
        allowed_providers=None,
        row_times=None,
    ):
        """Drive ``?refresh_provider=`` against a populated cache.

        ``cached_rows`` is ``[(PROVIDER, model_key), ...]``;
        ``row_times`` optionally maps a PROVIDER to its rows'
        ``last_updated``. Returns
        ``(json, session)``; the session mock records the DB writes.
        ``allowed_providers`` (with ``require_local_llm``) stubs the
        endpoint-policy verdict so no provider URL is ever resolved.
        """
        from local_deep_research.security.egress.policy import (
            EgressContext,
            EgressScope,
        )

        rows = []
        for provider, key in cached_rows:
            row = MagicMock()
            row.provider = provider
            row.model_key = key
            row.model_label = key
            if row_times and provider in row_times:
                row.last_updated = row_times[provider]
            rows.append(row)

        session = MagicMock()
        session.query.return_value.filter.return_value.all.return_value = rows

        @contextmanager
        def _session(*a, **kw):
            yield session

        policy_context = EgressContext(
            scope=(
                EgressScope.PRIVATE_ONLY
                if require_local_llm
                else EgressScope.PUBLIC_ONLY
            ),
            primary_engine="library",
            require_local_llm=require_local_llm,
            require_local_embeddings=False,
            local_hostnames=(),
            username="testuser",
        )
        allowed = {p.upper() for p in (allowed_providers or ())}

        with (
            patch(f"{S}.get_user_db_session", side_effect=_session),
            patch(
                f"{S}._resolve_model_discovery_policy",
                return_value=(policy_context, {}),
            ),
            patch(
                f"{S}._model_discovery_provider_allowed",
                side_effect=lambda provider, *a, **kw: (
                    provider.upper() in allowed
                ),
            ),
            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}._get_setting_from_session", return_value=""),
        ):
            # A targeted refresh is a state change: POST-only (CSRF).
            send = client.post if "refresh_provider" in query else client.get
            resp = send(f"/settings/api/available-models{query}")

        assert resp.status_code == 200, resp.text[:300]
        return resp.json(), session

    @staticmethod
    def _deleted_providers(session):
        """Provider names whose cached rows the route deleted (the
        ``ProviderModel.provider == X`` filters; the cache-read filter on
        ``last_updated`` is ignored)."""
        deleted = []
        for call in session.query.return_value.filter.call_args_list:
            expr = call.args[0] if call.args else None
            left = getattr(expr, "left", None)
            if getattr(left, "key", None) == "provider":
                deleted.append(expr.right.value)
        return deleted

    @staticmethod
    def _inserted_providers(session):
        return sorted({c.args[0].provider for c in session.add.call_args_list})

    def test_refresh_provider_success_rewrites_only_target_rows(self, client):
        """A successful targeted refresh deletes and re-inserts ONLY the
        target provider's cached rows. Every other provider keeps its DB
        rows (and their 24h TTL) untouched — re-writing them would reset
        their TTL with stale data and churn the whole cache on each key
        save."""
        discovered = {
            "OLLAMA": self._discovered(
                "Ollama", [{"value": "fresh-ollama", "label": "Fresh"}]
            ),
            "QWEN": self._discovered(
                "QwenCloud", [{"value": "qwen3.8-max", "label": "Qwen Max"}]
            ),
        }
        data, session = self._targeted_refresh(
            client,
            "?refresh_provider=qwen",
            [("OLLAMA", "cached-ollama"), ("QWEN", "cached-qwen")],
            discovered,
        )

        discovered[
            "QWEN"
        ].provider_class.list_models_for_api.assert_called_once()
        discovered[
            "OLLAMA"
        ].provider_class.list_models_for_api.assert_not_called()
        assert [m["value"] for m in data["providers"]["qwen_models"]] == [
            "qwen3.8-max"
        ]
        assert [m["value"] for m in data["providers"]["ollama_models"]] == [
            "cached-ollama"
        ]
        # DB: only QWEN rows deleted and only QWEN rows inserted.
        assert self._deleted_providers(session) == ["QWEN"]
        assert self._inserted_providers(session) == ["QWEN"]
        session.query.return_value.delete.assert_not_called()
        session.commit.assert_called_once()

    def test_refresh_provider_rows_expire_with_the_rest_of_the_cache(
        self, client
    ):
        """The cache read returns ANY fresh rows as the whole catalog, so
        the target's re-fetched rows must not outlive the other providers'
        rows: they are stamped with the oldest other provider's
        ``last_updated``, not ``now``. Otherwise, e.g. 20h after a full
        discovery, a key save would keep only the target's models fresh
        between hours 24 and 44 and hide every other provider's models."""
        from datetime import UTC, datetime, timedelta

        now = datetime.now(UTC)
        ollama_time = now - timedelta(hours=20)
        lmstudio_time = now - timedelta(hours=10)
        discovered = {
            "OLLAMA": self._discovered(
                "Ollama", [{"value": "fresh-ollama", "label": "Fresh"}]
            ),
            "QWEN": self._discovered(
                "QwenCloud", [{"value": "qwen3.8-max", "label": "Qwen Max"}]
            ),
        }
        _data, session = self._targeted_refresh(
            client,
            "?refresh_provider=qwen",
            [
                ("OLLAMA", "cached-ollama"),
                ("LMSTUDIO", "cached-lmstudio"),
                # The target's own old rows never set the timestamp.
                ("QWEN", "cached-qwen"),
            ],
            discovered,
            row_times={
                "OLLAMA": ollama_time,
                "LMSTUDIO": lmstudio_time,
                "QWEN": now - timedelta(hours=23),
            },
        )

        added = [c.args[0] for c in session.add.call_args_list]
        assert [row.provider for row in added] == ["QWEN"]
        assert [row.last_updated for row in added] == [ollama_time]

    def test_refresh_provider_rows_use_now_when_no_other_rows(self, client):
        """With no other provider's cached rows there is nothing to stay
        in step with: the target's rows get a current timestamp."""
        from datetime import UTC, datetime, timedelta

        before = datetime.now(UTC)
        discovered = {
            "QWEN": self._discovered(
                "QwenCloud", [{"value": "qwen3.8-max", "label": "Qwen Max"}]
            ),
        }
        _data, session = self._targeted_refresh(
            client,
            "?refresh_provider=qwen",
            [("QWEN", "cached-qwen")],
            discovered,
            row_times={"QWEN": before - timedelta(hours=23)},
        )

        added = [c.args[0] for c in session.add.call_args_list]
        assert [row.provider for row in added] == ["QWEN"]
        assert added[0].last_updated >= before

    def test_refresh_provider_under_require_local_llm_skips_blocked_target(
        self, client
    ):
        """Local-only posture: a targeted refresh of a cloud provider the
        policy blocks must not contact it, must not leak its cached rows
        into the response, and must not touch the DB — the other (allowed)
        providers' cached rows are returned as-is."""
        discovered = {
            "OLLAMA": self._discovered(
                "Ollama", [{"value": "fresh-ollama", "label": "Fresh"}]
            ),
            "QWEN": self._discovered(
                "QwenCloud", [{"value": "qwen3.8-max", "label": "Qwen Max"}]
            ),
        }
        data, session = self._targeted_refresh(
            client,
            "?refresh_provider=qwen",
            [("OLLAMA", "cached-ollama"), ("QWEN", "cached-qwen")],
            discovered,
            require_local_llm=True,
            allowed_providers={"OLLAMA"},
        )

        discovered[
            "QWEN"
        ].provider_class.list_models_for_api.assert_not_called()
        discovered[
            "OLLAMA"
        ].provider_class.list_models_for_api.assert_not_called()
        assert "qwen_models" not in data["providers"]
        assert [m["value"] for m in data["providers"]["ollama_models"]] == [
            "cached-ollama"
        ]
        assert self._deleted_providers(session) == []
        session.add.assert_not_called()
        session.query.return_value.delete.assert_not_called()

    def test_refresh_provider_under_require_local_llm_refreshes_allowed_target(
        self, client
    ):
        """Local-only posture, allowed target: only that provider is
        re-fetched and re-written; the blocked cloud provider is neither
        contacted nor returned, and its DB rows are left alone."""
        discovered = {
            "OLLAMA": self._discovered(
                "Ollama", [{"value": "fresh-ollama", "label": "Fresh"}]
            ),
            "QWEN": self._discovered(
                "QwenCloud", [{"value": "qwen3.8-max", "label": "Qwen Max"}]
            ),
        }
        data, session = self._targeted_refresh(
            client,
            "?refresh_provider=ollama",
            [("OLLAMA", "cached-ollama"), ("QWEN", "cached-qwen")],
            discovered,
            require_local_llm=True,
            allowed_providers={"OLLAMA"},
        )

        discovered[
            "OLLAMA"
        ].provider_class.list_models_for_api.assert_called_once()
        discovered[
            "QWEN"
        ].provider_class.list_models_for_api.assert_not_called()
        assert [m["value"] for m in data["providers"]["ollama_models"]] == [
            "fresh-ollama"
        ]
        assert "qwen_models" not in data["providers"]
        assert self._deleted_providers(session) == ["OLLAMA"]
        assert self._inserted_providers(session) == ["OLLAMA"]

    def test_force_refresh_uses_one_guarded_ollama_provider_path(self):
        """The router delegates Ollama listing, URL validation, and auth.

        The allowed control must fetch exactly once with authentication. The
        metadata target must be rejected by the real provider before the same
        transport seam is reached. Neither case may use the removed router-
        level ``safe_get`` probe.
        """
        from local_deep_research.llm.providers.auto_discovery import (
            ProviderInfo,
        )
        from local_deep_research.llm.providers.implementations.ollama import (
            OllamaProvider,
        )
        from local_deep_research.security.egress.policy import (
            EgressContext,
            EgressScope,
        )
        from local_deep_research.utilities.llm_utils import (
            OLLAMA_MODEL_LIST_TIMEOUT,
        )
        from local_deep_research.web.routers.settings import (
            api_get_available_models,
        )

        ollama_url = "http://127.0.0.1:11434"
        ollama_key = "configured-ollama-key"
        settings = {
            OllamaProvider.url_setting: ollama_url,
            OllamaProvider.api_key_setting: ollama_key,
        }
        provider_info = ProviderInfo(OllamaProvider)
        provider_fetch = Mock(
            return_value=[{"value": "llama3:latest", "label": "llama3:latest"}]
        )
        router_probe = Mock(
            side_effect=AssertionError(
                "available-models must not issue a router-level Ollama probe"
            )
        )
        policy_context = EgressContext(
            scope=EgressScope.PUBLIC_ONLY,
            primary_engine="library",
            require_local_llm=False,
            require_local_embeddings=False,
            local_hostnames=(),
            username="testuser",
        )

        with _models_env(
            [provider_info.to_dict()],
            discovered_providers={"OLLAMA": provider_info},
            extra=[
                patch(
                    f"{S}._resolve_model_discovery_policy",
                    return_value=(policy_context, {}),
                ),
                patch(
                    f"{S}._get_setting_from_session",
                    side_effect=lambda key, username, default: settings.get(
                        key, default
                    ),
                ),
                patch(
                    "local_deep_research.utilities.llm_utils.fetch_ollama_models",
                    provider_fetch,
                ),
                patch(f"{S}.safe_get", router_probe),
            ],
        ):
            request = MagicMock()
            # A refresh is POST-only: a GET carrying force_refresh=true is a 405.
            request.method = "POST"
            request.query_params.get.side_effect = lambda key, default=None: {
                "force_refresh": "true"
            }.get(key, default)
            response = asyncio.run(
                api_get_available_models(request, username="testuser")
            )

            assert response["providers"]["ollama_models"] == [
                {
                    "value": "llama3:latest",
                    "label": "llama3 (Ollama)",
                    "provider": "OLLAMA",
                }
            ]
            provider_fetch.assert_called_once_with(
                ollama_url,
                timeout=OLLAMA_MODEL_LIST_TIMEOUT,
                auth_headers={"Authorization": f"Bearer {ollama_key}"},
            )

            provider_fetch.reset_mock()
            settings[OllamaProvider.url_setting] = (
                "http://169.254.169.254/latest/meta-data"
            )
            blocked_response = asyncio.run(
                api_get_available_models(request, username="testuser")
            )

            assert blocked_response["providers"]["ollama_models"] == []
            provider_fetch.assert_not_called()
            router_probe.assert_not_called()

    def test_llamacpp_not_duplicated_in_provider_options(self, client):
        """LlamaCppProvider is auto-discovered, so the route must NOT add a
        second hardcoded LLAMACPP entry (regression for the duplicate dropdown
        entry)."""
        discovered = [
            {"value": "OLLAMA", "label": "Ollama 💻 Local", "is_cloud": False},
            {
                "value": "LLAMACPP",
                "label": "llama.cpp 💻 Local",
                "is_cloud": False,
            },
        ]
        with _models_env(
            discovered,
            extra=[patch(f"{S}._get_setting_from_session", return_value=None)],
        ):
            resp = client.post(
                "/settings/api/available-models?force_refresh=true"
            )

        assert resp.status_code == 200
        values = [opt["value"] for opt in resp.json()["provider_options"]]
        assert values.count("LLAMACPP") == 1
        assert len(values) == len(set(values))

    @staticmethod
    def _discovered(name, models):
        """A discovered-provider info whose class returns ``models``."""
        provider_class = MagicMock()
        provider_class.api_key_setting = "x.api_key"
        provider_class.url_setting = None  # falsy -> no base-url fetch
        provider_class.list_models_for_api.return_value = models
        info = MagicMock()
        info.provider_name = name
        info.provider_class = provider_class
        return info

    def _run_models(self, client, local_only):
        from local_deep_research.security.egress.policy import (
            EgressContext,
            EgressScope,
        )

        discovered = {
            "OLLAMA": self._discovered(
                "Ollama", [{"value": "llama3", "label": "llama3 (Ollama)"}]
            ),
            "OPENAI": self._discovered(
                "OpenAI", [{"value": "gpt-4", "label": "GPT-4 (OpenAI)"}]
            ),
        }
        policy_context = EgressContext(
            scope=(
                EgressScope.PRIVATE_ONLY
                if local_only
                else EgressScope.PUBLIC_ONLY
            ),
            primary_engine="library",
            require_local_llm=local_only,
            require_local_embeddings=False,
            local_hostnames=(),
            username="testuser",
        )

        with _models_env(
            [],
            discovered_providers=discovered,
            extra=[
                patch(
                    f"{S}._resolve_model_discovery_policy",
                    return_value=(policy_context, {}),
                ),
                patch(f"{S}._get_setting_from_session", return_value=""),
            ],
        ):
            resp = client.post(
                "/settings/api/available-models?force_refresh=true"
            )

        assert resp.status_code == 200, resp.text[:400]
        return resp.json()["providers"]

    def test_non_local_lists_all_discovered_providers(self, client):
        """Normal posture: every discovered provider is listed."""
        providers = self._run_models(client, local_only=False)
        assert providers["ollama_models"] == [
            {
                "value": "llama3",
                "label": "llama3 (Ollama)",
                "provider": "OLLAMA",
            }
        ]
        assert providers["openai_models"] == [
            {"value": "gpt-4", "label": "GPT-4 (OpenAI)", "provider": "OPENAI"}
        ]

    def test_local_only_lists_only_local_providers(self, client):
        """Local-only posture: discovery is filtered, so Ollama is still listed
        (via its provider class) but the cloud provider is not — key ABSENT,
        not merely empty."""
        providers = self._run_models(client, local_only=True)
        assert providers["ollama_models"] == [
            {
                "value": "llama3",
                "label": "llama3 (Ollama)",
                "provider": "OLLAMA",
            }
        ]
        assert "openai_models" not in providers


class TestApiGetAvailableModelsEgressPolicy:
    """Egress policy on ``GET /settings/api/available-models``.

    The ``provider_options`` half of these tests was RED until #5922 was
    fixed.  Main marked a policy-blocked provider ``disabled=True`` with
    ``disabled_reason='Blocked by "Require Local LLM Endpoint"'`` and KEPT it
    in ``provider_options``; the port hard-filtered those entries out
    instead.  Silently dropping the entry made the "Model Provider" dropdown
    look short after a user configured a cloud API key, leading them to
    assume the key was not being read — the regression main's comment records
    fixing — and left the front-end code that renders the reason unreachable.
    ``web/routers/settings.py`` now marks rather than filters.

    The model map also follows main's single auto-discovery path: a blocked
    provider remains advertised as a disabled option, but is absent from
    ``providers`` because its model-listing method is never called.  The
    obsolete hand-rolled Ollama probe previously injected an unrelated empty
    ``ollama_models`` key after the cached provider was filtered out.
    """

    @staticmethod
    def _settings_manager(snapshot):
        manager = MagicMock()
        manager.get_settings_snapshot.return_value = snapshot
        manager.get_setting.side_effect = lambda key, default=None: (
            snapshot.get(key, default)
        )
        return manager

    @staticmethod
    def _cached_model(provider, key):
        model = MagicMock()
        model.provider = provider
        model.model_key = key
        model.model_label = key
        return model

    def test_cache_hit_filters_cloud_models_and_options_under_private_only(
        self, client
    ):
        # Given a private-only policy and a mixed cache.
        snapshot = {
            "policy.egress_scope": "private_only",
            "search.tool": "library",
        }
        settings_manager = self._settings_manager(snapshot)
        database_session = MagicMock()
        database_session.query.return_value.filter.return_value.all.return_value = [
            self._cached_model("OLLAMA", "llama3"),
            self._cached_model("OPENAI", "gpt-4"),
        ]

        @contextmanager
        def _session(*args, **kwargs):
            yield database_session

        provider_options = [
            {"value": "OLLAMA", "label": "Ollama", "is_cloud": False},
            {"value": "OPENAI", "label": "OpenAI", "is_cloud": True},
        ]

        with (
            patch(f"{S}.get_user_db_session", side_effect=_session),
            patch(f"{S}.get_settings_manager", return_value=settings_manager),
            patch(
                "local_deep_research.llm.providers.get_discovered_provider_options",
                return_value=provider_options,
            ),
            patch(
                "local_deep_research.llm.providers.discover_providers",
                return_value={},
            ),
        ):
            # When the cache is read.
            response = client.get("/settings/api/available-models")

        # Then only the *cached models* for the blocked cloud provider are
        # filtered out (so we never expose a model the backend would refuse to
        # call).  The provider-options list still advertises every provider so
        # the dropdown can show, e.g., OpenAI as disabled with the policy
        # reason.
        assert response.status_code == 200
        payload = response.json()
        assert len(payload["provider_options"]) == 2
        assert payload["provider_options"][0]["value"] == "OLLAMA"
        assert payload["provider_options"][0]["disabled"] is False
        assert payload["provider_options"][1]["value"] == "OPENAI"
        assert payload["provider_options"][1]["disabled"] is True
        assert "Blocked" in payload["provider_options"][1]["disabled_reason"]
        assert payload["providers"] == {
            "ollama_models": [
                {"value": "llama3", "label": "llama3", "provider": "OLLAMA"}
            ]
        }

    def test_fresh_discovery_skips_local_provider_with_remote_url(self, client):
        # Given private-only egress and a remote LM Studio URL.
        snapshot = {
            "policy.egress_scope": "private_only",
            "search.tool": "library",
            "llm.lmstudio.url": "https://8.8.8.8/v1",
        }
        settings_manager = self._settings_manager(snapshot)
        database_session = MagicMock()
        provider_class = MagicMock()
        provider_class.api_key_setting = "llm.lmstudio.api_key"
        provider_class.url_setting = "llm.lmstudio.url"
        provider_info = MagicMock()
        provider_info.provider_name = "LM Studio"
        provider_info.provider_class = provider_class
        provider_options = [
            {"value": "LMSTUDIO", "label": "LM Studio", "is_cloud": False},
            {"value": "OPENAI", "label": "OpenAI", "is_cloud": True},
        ]

        @contextmanager
        def _session(*args, **kwargs):
            yield database_session

        with (
            patch(f"{S}.get_user_db_session", side_effect=_session),
            patch(f"{S}.get_settings_manager", return_value=settings_manager),
            patch(
                "local_deep_research.llm.providers.get_discovered_provider_options",
                return_value=provider_options,
            ),
            patch(
                "local_deep_research.llm.providers.discover_providers",
                return_value={"LMSTUDIO": provider_info},
            ),
        ):
            # When models are refreshed.
            response = client.post(
                "/settings/api/available-models?force_refresh=true"
            )

        # Then the provider is never actually called and no models are
        # returned, but the option is still surfaced in the dropdown marked as
        # disabled with the policy reason so the user can see why their
        # configured LM Studio URL isn't selectable.
        assert response.status_code == 200
        payload = response.json()
        assert len(payload["provider_options"]) == 2
        assert payload["provider_options"][0]["value"] == "LMSTUDIO"
        assert payload["provider_options"][0]["disabled"] is True
        assert "Blocked" in payload["provider_options"][0]["disabled_reason"]
        assert payload["provider_options"][1]["value"] == "OPENAI"
        assert payload["provider_options"][1]["disabled"] is True
        provider_class.list_models_for_api.assert_not_called()

    def test_cache_filters_provider_with_mixed_dns_answers(self, client):
        # Given a cached local-provider model whose endpoint has a public answer.
        snapshot = {
            "policy.egress_scope": "private_only",
            "search.tool": "library",
            "llm.lmstudio.url": "http://mixed.inference.example/v1",
        }
        settings_manager = self._settings_manager(snapshot)
        database_session = MagicMock()
        database_session.query.return_value.filter.return_value.all.return_value = [
            self._cached_model("LMSTUDIO", "local-model")
        ]

        @contextmanager
        def _session(*args, **kwargs):
            yield database_session

        provider_options = [
            {"value": "LMSTUDIO", "label": "LM Studio", "is_cloud": False}
        ]
        with (
            patch(f"{S}.get_user_db_session", side_effect=_session),
            patch(f"{S}.get_settings_manager", return_value=settings_manager),
            patch(
                "local_deep_research.llm.providers.get_discovered_provider_options",
                return_value=provider_options,
            ),
            patch(
                "local_deep_research.llm.providers.discover_providers",
                return_value={"LMSTUDIO": MagicMock()},
            ),
            patch(
                "local_deep_research.security.egress.policy._resolve_with_timeout",
                return_value=[
                    (None, None, None, None, ("10.0.0.42", 0)),
                    (None, None, None, None, ("8.8.8.8", 0)),
                ],
            ) as resolve,
        ):
            # When model discovery reads the cache.
            response = client.get("/settings/api/available-models")

        # Then the stale cached model is filtered out so we never serve it to a
        # UI that would have to refuse to use it.  The provider option itself
        # is still advertised (disabled, with the policy reason) so the user
        # understands why their configured endpoint isn't selectable.
        assert response.status_code == 200
        payload = response.json()
        assert len(payload["provider_options"]) == 1
        assert payload["provider_options"][0]["value"] == "LMSTUDIO"
        assert payload["provider_options"][0]["disabled"] is True
        assert "Blocked" in payload["provider_options"][0]["disabled_reason"]
        assert payload["providers"] == {}
        resolve.assert_called_once_with("mixed.inference.example")