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.

17.2 KB
Raw Blame
"""The search-favorites API: ``/settings/api/search-favorites`` (+ toggle)
and the favorites half of ``/settings/api/available-search-engines``.

Ported from the Flask-era ``tests/web/routes/test_search_favorites.py``,
deleted by the FastAPI migration. The handlers moved to
``src/local_deep_research/web/routers/settings.py`` unchanged -- same
paths, same hand-rolled validation (so the 400s are still 400s, not
FastAPI's 422), same response shapes.

WHAT WAS LEFT UNPINNED. The branch does carry three real successors:
``test_settings_persistence_contracts.py`` pins that PUT and toggle write
to the database and that a refused ``set_setting`` is not reported as
success, and ``test_js_fetch_parity.py`` pins the toggle's ``engine_id``
field name. Everything else in this file's surface reduced to
``status_code == 200`` / ``isinstance(data, list)``:

* **the toggle-OFF branch was entirely dead.** No test on the branch ever
  toggles an engine that is already a favorite, so
  ``favorites.remove(engine_id); is_favorite = False`` could be replaced
  with an unconditional ``append`` and the whole suite stayed green.
* **every 400 on PUT** (``favorites`` absent, null, or not a list; a
  non-object body) had no test.
* **every ``except Exception -> 500``** on the three handlers had no test.
* **every favorites assertion on ``available-search-engines``** was gone:
  the ``favorites`` key, ``is_favorite`` on the options and on the
  ``engines`` dict, and the favorites-first band ordering. The one
  ordering test on the branch
  (``tests/web_search_engines/test_engine_groups.py``) re-implements the
  sort inside its own body over synthetic dicts, so deleting the
  endpoint's ``engine_options.sort(...)`` leaves it green.
* **the two ``isinstance`` coercions** that turn a corrupt non-list
  ``search.favorites`` row into ``[]`` (one on read, one in the toggle).

These run against the real app and the real per-user database through
``authenticated_client``, so a favorites list is written and read back
rather than asserted against a mock of itself.
"""

from contextlib import contextmanager
from unittest.mock import MagicMock, patch

import pytest

SETTINGS_PREFIX = "/settings"
FAVORITES = f"{SETTINGS_PREFIX}/api/search-favorites"
TOGGLE = f"{FAVORITES}/toggle"
ENGINES = f"{SETTINGS_PREFIX}/api/available-search-engines"

_SETTINGS_MOD = "local_deep_research.web.routers.settings"


@pytest.fixture(autouse=True)
def _reset_rate_limiter_storage():
    """The mutating routes carry ``@settings_limit`` ("30 per minute",
    keyed by user). Each ``authenticated_client`` registers its own user
    so the buckets are already per-test, but a few of these tests make
    several writes in a row -- clearing the shared storage keeps them
    independent of anything else in the process."""
    try:
        from local_deep_research.web.dependencies.rate_limit import limiter

        storage = getattr(limiter, "_storage", None)
        if storage is not None and hasattr(storage, "reset"):
            storage.reset()
    except Exception:
        pass
    yield


def _set_favorites(client, values):
    """PUT ``values`` and return the response, asserting it landed."""
    response = client.put(FAVORITES, json={"favorites": values})
    assert response.status_code == 200, response.text[:300]
    return response


def _get_favorites(client):
    response = client.get(FAVORITES)
    assert response.status_code == 200, response.text[:300]
    return response.get_json()["favorites"]


def _exploding_session():
    """Patch the router's ``get_user_db_session`` so opening a session
    raises -- the only way to reach the handlers' ``except`` arms."""

    @contextmanager
    def _boom(*_args, **_kwargs):
        raise RuntimeError("database unavailable")
        yield  # pragma: no cover

    return patch(f"{_SETTINGS_MOD}.get_user_db_session", side_effect=_boom)


def _manager_returning(value):
    """Patch ``get_settings_manager`` so ``get_setting`` hands back
    ``value`` -- used to drive the non-list coercion branches, which a
    well-formed database cannot produce."""
    manager = MagicMock()
    manager.settings_locked = False
    manager.get_setting.return_value = value
    manager.set_setting.return_value = True
    return patch(
        f"{_SETTINGS_MOD}.get_settings_manager", return_value=manager
    ), manager


# ---------------------------------------------------------------------------
# GET /settings/api/search-favorites
# ---------------------------------------------------------------------------


def test_get_favorites_requires_authentication(client):
    response = client.get(FAVORITES, follow_redirects=False)
    assert response.status_code == 401, response.status_code


def test_get_returns_the_stored_favorites(authenticated_client):
    """The read must echo what was stored. The branch's only successor
    asserts ``isinstance(favorites, list)``, which a handler returning a
    constant ``[]`` satisfies."""
    _set_favorites(authenticated_client, ["arxiv", "github"])
    assert _get_favorites(authenticated_client) == ["arxiv", "github"]


def test_get_coerces_a_non_list_stored_value_to_an_empty_list(
    authenticated_client,
):
    """A corrupt ``search.favorites`` row must read back as ``[]``, not
    be handed to the frontend as a string it will try to iterate."""
    patcher, _manager = _manager_returning("not-a-list")
    with patcher:
        response = authenticated_client.get(FAVORITES)
    assert response.status_code == 200, response.status_code
    assert response.get_json()["favorites"] == []


def test_get_favorites_answers_500_when_the_database_is_unavailable(
    authenticated_client,
):
    with _exploding_session():
        response = authenticated_client.get(FAVORITES)
    assert response.status_code == 500, response.status_code
    assert response.get_json()["error"] == "Failed to retrieve favorites"


# ---------------------------------------------------------------------------
# PUT /settings/api/search-favorites
# ---------------------------------------------------------------------------


def test_put_requires_authentication(client):
    """Anonymous writes are refused. The CSRF middleware runs before
    routing and fails closed on an unsafe method with no session token,
    so this is a 403 where the read is a 401 -- either way the write
    never reaches the handler."""
    response = client.put(
        FAVORITES, json={"favorites": []}, follow_redirects=False
    )
    assert response.status_code in (401, 403), response.status_code


def test_put_rejects_a_non_object_body(authenticated_client):
    response = authenticated_client.put(FAVORITES, json=["arxiv"])
    assert response.status_code == 400, response.status_code


def test_put_requires_a_favorites_field(authenticated_client):
    response = authenticated_client.put(FAVORITES, json={})
    assert response.status_code == 400, response.status_code
    assert response.get_json()["error"] == "No favorites provided"


def test_put_rejects_a_null_favorites_value(authenticated_client):
    """``{"favorites": null}`` must not be stored as a null row."""
    response = authenticated_client.put(FAVORITES, json={"favorites": None})
    assert response.status_code == 400, response.status_code
    assert response.get_json()["error"] == "No favorites provided"


def test_put_rejects_a_non_list_favorites_value(authenticated_client):
    response = authenticated_client.put(FAVORITES, json={"favorites": "arxiv"})
    assert response.status_code == 400, response.status_code
    assert response.get_json()["error"] == "Favorites must be a list"


def test_put_accepts_an_empty_list_and_clears_the_favorites(
    authenticated_client,
):
    """Clearing every favorite is a legitimate write, not a missing
    field -- the ``is None`` guard must not swallow ``[]``."""
    _set_favorites(authenticated_client, ["arxiv"])
    _set_favorites(authenticated_client, [])
    assert _get_favorites(authenticated_client) == []


def test_put_preserves_the_submitted_order(authenticated_client):
    """The list is user-ordered; the write path must not sort or
    normalise it."""
    ordered = ["wikipedia", "arxiv", "github", "pubmed"]
    response = _set_favorites(authenticated_client, ordered)
    assert response.get_json()["favorites"] == ordered
    assert _get_favorites(authenticated_client) == ordered


def test_put_replaces_rather_than_merges(authenticated_client):
    """A second PUT is a full replacement -- the earlier entries must be
    gone, not merged in."""
    _set_favorites(authenticated_client, ["arxiv", "github"])
    _set_favorites(authenticated_client, ["pubmed"])
    assert _get_favorites(authenticated_client) == ["pubmed"]


def test_put_answers_500_when_the_database_is_unavailable(
    authenticated_client,
):
    with _exploding_session():
        response = authenticated_client.put(
            FAVORITES, json={"favorites": ["arxiv"]}
        )
    assert response.status_code == 500, response.status_code
    assert response.get_json()["error"] == "Failed to update favorites"


# ---------------------------------------------------------------------------
# POST /settings/api/search-favorites/toggle
# ---------------------------------------------------------------------------


def test_toggle_requires_authentication(client):
    response = client.post(
        TOGGLE, json={"engine_id": "arxiv"}, follow_redirects=False
    )
    assert response.status_code in (401, 403), response.status_code


def test_toggle_rejects_a_non_object_body(authenticated_client):
    response = authenticated_client.post(TOGGLE, json=["arxiv"])
    assert response.status_code == 400, response.status_code


def test_toggle_requires_an_engine_id(authenticated_client):
    response = authenticated_client.post(TOGGLE, json={})
    assert response.status_code == 400, response.status_code
    assert response.get_json()["error"] == "No engine_id provided"


def test_toggle_rejects_an_empty_string_engine_id(authenticated_client):
    """Present but falsy is still no engine -- the guard is ``if not
    engine_id``, not ``if engine_id is None``. Without it an empty string
    is appended to the favorites list."""
    response = authenticated_client.post(TOGGLE, json={"engine_id": ""})
    assert response.status_code == 400, response.status_code
    assert response.get_json()["error"] == "No engine_id provided"


def test_toggle_adds_an_engine_that_was_not_a_favorite(authenticated_client):
    _set_favorites(authenticated_client, [])
    response = authenticated_client.post(TOGGLE, json={"engine_id": "arxiv"})
    assert response.status_code == 200, response.text[:300]
    body = response.get_json()
    assert body["is_favorite"] is True
    assert body["favorites"] == ["arxiv"]
    assert _get_favorites(authenticated_client) == ["arxiv"]


def test_toggle_removes_an_engine_that_was_already_a_favorite(
    authenticated_client,
):
    """The toggle-OFF branch. Nothing else on the branch exercises it:
    replace ``favorites.remove(...)`` with an unconditional append and
    every other favorites test still passes."""
    _set_favorites(authenticated_client, ["arxiv", "github"])
    response = authenticated_client.post(TOGGLE, json={"engine_id": "arxiv"})
    assert response.status_code == 200, response.text[:300]
    body = response.get_json()
    assert body["is_favorite"] is False
    assert body["favorites"] == ["github"]
    assert _get_favorites(authenticated_client) == ["github"]


def test_toggle_does_not_create_duplicates(authenticated_client):
    """Toggling the same engine twice returns to the starting state
    rather than accumulating two copies."""
    _set_favorites(authenticated_client, [])
    authenticated_client.post(TOGGLE, json={"engine_id": "arxiv"})
    authenticated_client.post(TOGGLE, json={"engine_id": "arxiv"})
    assert _get_favorites(authenticated_client) == []

    authenticated_client.post(TOGGLE, json={"engine_id": "arxiv"})
    assert _get_favorites(authenticated_client) == ["arxiv"]


def test_toggle_appends_to_the_shipped_default_without_a_prior_write(
    authenticated_client,
):
    """A user who has never PUT a favorites list still has the shipped
    default (``settings_search_config.json``); the toggle must extend
    that list rather than start from an empty one and silently drop it.
    """
    baseline = _get_favorites(authenticated_client)
    assert baseline, (
        "premise: a fresh account ships a non-empty search.favorites "
        "default; without one this test proves nothing"
    )
    fresh = next(
        engine
        for engine in ("pubmed", "wikipedia", "semantic_scholar", "github")
        if engine not in baseline
    )

    response = authenticated_client.post(TOGGLE, json={"engine_id": fresh})
    assert response.status_code == 200, response.text[:300]
    body = response.get_json()
    assert body["is_favorite"] is True
    assert body["favorites"] == baseline + [fresh]
    assert _get_favorites(authenticated_client) == baseline + [fresh]


def test_toggle_coerces_a_non_list_stored_value(authenticated_client):
    """The toggle has its own ``isinstance`` guard, separate from the
    read handler's. A corrupt row must be replaced by a fresh list, not
    concatenated onto a string."""
    patcher, manager = _manager_returning("not-a-list")
    with patcher:
        response = authenticated_client.post(
            TOGGLE, json={"engine_id": "arxiv"}
        )
    assert response.status_code == 200, response.text[:300]
    body = response.get_json()
    assert body["is_favorite"] is True
    assert body["favorites"] == ["arxiv"]
    manager.set_setting.assert_called_once_with("search.favorites", ["arxiv"])


def test_toggle_answers_500_when_the_database_is_unavailable(
    authenticated_client,
):
    with _exploding_session():
        response = authenticated_client.post(
            TOGGLE, json={"engine_id": "arxiv"}
        )
    assert response.status_code == 500, response.status_code
    assert response.get_json()["error"] == "Failed to toggle favorite"


# ---------------------------------------------------------------------------
# GET /settings/api/available-search-engines -- the favorites overlay
# ---------------------------------------------------------------------------


def _engines_payload(client):
    response = client.get(ENGINES)
    assert response.status_code == 200, response.text[:300]
    return response.get_json()


def test_available_search_engines_requires_authentication(client):
    response = client.get(ENGINES, follow_redirects=False)
    assert response.status_code == 401, response.status_code


def test_available_search_engines_reports_the_favorites_list(
    authenticated_client,
):
    """The endpoint's own ``favorites`` key. The successors on the branch
    assert only ``status_code == 200`` / ``isinstance(data, (dict, list))``,
    which a payload with no ``favorites`` key at all satisfies."""
    payload = _engines_payload(authenticated_client)
    assert "engine_options" in payload
    known = payload["engine_options"][0]["value"]

    _set_favorites(authenticated_client, [known])
    assert _engines_payload(authenticated_client)["favorites"] == [known]


def test_engine_options_and_engines_carry_is_favorite(authenticated_client):
    """``is_favorite`` is stamped onto every option AND onto every entry
    of the ``engines`` dict -- two separate assignments in the handler,
    neither pinned elsewhere."""
    payload = _engines_payload(authenticated_client)
    known = payload["engine_options"][0]["value"]
    _set_favorites(authenticated_client, [known])

    payload = _engines_payload(authenticated_client)

    options = {opt["value"]: opt for opt in payload["engine_options"]}
    assert all("is_favorite" in opt for opt in options.values())
    assert options[known]["is_favorite"] is True
    assert all(
        opt["is_favorite"] is False
        for value, opt in options.items()
        if value != known
    )

    engines = payload["engines"]
    assert all("is_favorite" in entry for entry in engines.values())
    assert engines[known]["is_favorite"] is True


def test_favouriting_an_engine_moves_it_to_the_top_of_the_options(
    authenticated_client,
):
    """The endpoint's own ``engine_options.sort(...)`` puts the favorites
    band first. The branch's ordering test re-implements this sort inside
    its own body over synthetic dicts, so deleting the call in the
    handler leaves it green; this drives the real endpoint.

    A non-first engine is chosen deliberately: starring whatever already
    sorts first would prove nothing.
    """
    payload = _engines_payload(authenticated_client)
    values = [opt["value"] for opt in payload["engine_options"]]
    assert len(values) > 1, "need at least two engines to prove an ordering"
    candidate = values[-1]

    _set_favorites(authenticated_client, [candidate])
    payload = _engines_payload(authenticated_client)

    reordered = [opt["value"] for opt in payload["engine_options"]]
    assert reordered[0] == candidate, (
        f"{candidate} was starred but did not move to the front of "
        f"engine_options: {reordered[:5]}"
    )
    first = payload["engine_options"][0]
    assert first["is_favorite"] is True
    # The favorites band is an overlay: base_group must still record the
    # engine's real category so un-starring can send it back.
    assert first["base_group"] != first["group"]
    assert first["group_order"] <= payload["engine_options"][1]["group_order"]