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.

14.1 KB
Raw Blame
"""Coverage for ``configure_rag``'s (``POST /library/api/rag/configure``)
settings+index atomicity guarantees.

Ported (with FastAPI adaptation) from the Flask-era
``tests/research_library/routes/test_rag_configure_atomicity.py`` added by
commit 87537d9ec ("fix(security): operator-gate unprotected egress and
harden policy-sensitive consumers (#5148)"). That file imported ``flask``
(not installed on this branch) via the shared
``tests/research_library/routes/_route_helpers_rag.py`` helper (a Flask
test app + blueprint registration), which broke collection of this entire
directory. The SOURCE fix it exercises landed intact in this branch's
FastAPI router, in the nested ``_persist_configuration`` helper inside
``configure_rag`` (``src/local_deep_research/web/routers/rag.py:1271-1374``)
-- only the Flask-era test scaffolding needed porting.

Follows the direct-call idiom established by
``test_rag_routes_cancel_and_worker_wiring.py`` /
``test_rag_routes_collections.py``: ``configure_rag`` is called directly
with ``username`` passed as a keyword (bypassing ``Depends(require_auth)``
resolution). Unlike the sync routes those files cover, ``configure_rag`` is
``async def``, so it is driven with ``asyncio.run(...)``, matching
``tests/web/routers/test_start_research_search_override_validation.py``'s
established pattern for direct-calling async routes. Success paths return a
plain dict (200 implicit); error paths return a starlette ``JSONResponse``,
asserted via ``.status_code`` / ``json.loads(.body)``.

``_persist_configuration`` opens its OWN ``get_user_db_session`` block
rather than reusing a session the test hands in directly, and (unlike the
Flask original, which had an explicit ``except Exception: db_session.
rollback()`` in ``configure_rag`` itself) this branch's real
``get_user_db_session`` context manager now owns that responsibility: on an
exception raised inside its ``with`` block it calls ``safe_rollback`` (->
``session.rollback()``) and re-raises (database/session_context.py
:172-193). ``_configure_context`` below replicates exactly that behaviour
in its fake context manager so the rollback-count assertions below match
production, not a naive stub that would silently pass with the check
removed.

``TestLockedSettingsRefuseWrites`` below is NOT a port of the original 6
Flask methods -- it fills a gap the porting task's source characterization
called out (rag.py:1307-1338: an ``app.lock_settings`` check and a per-key
environment-locked check, each returning 403 before any write). Neither the
original blocked file NOR any other test in this repository exercised that
behaviour for this route (grepped repo-wide for "settings_locked", "RAG
configuration is locked", "are environment-locked" across ``tests/`` and
found no route-level coverage), even though it is real, live behaviour.
Added here rather than left silently unported.
"""

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

import pytest

from local_deep_research.web.routers.rag import configure_rag

MODULE = "local_deep_research.web.routers.rag"
_DB_CTX = "local_deep_research.database.session_context"
_DB_UTILS = "local_deep_research.utilities.db_utils"


_REQUESTED_SETTING_KEYS = [
    "local_search_embedding_model",
    "local_search_embedding_provider",
    "local_search_chunk_size",
    "local_search_chunk_overlap",
    "local_search_splitter_type",
    "local_search_text_separators",
    "local_search_distance_metric",
    "local_search_normalize_vectors",
    "local_search_index_type",
]


def _fake_request(payload):
    """Minimal Starlette ``Request`` stand-in: ``configure_rag`` only reads
    ``await request.json()``."""
    request = Mock()
    request.json = AsyncMock(return_value=payload)
    return request


def _configuration_payload(**overrides):
    payload = {
        "embedding_model": "test-model",
        "embedding_provider": "sentence_transformers",
        "chunk_size": 500,
        "chunk_overlap": 100,
        "collection_id": "collection-1",
    }
    payload.update(overrides)
    return payload


def _make_db_session():
    db_session = Mock()
    db_session.commit = Mock()
    db_session.rollback = Mock()
    return db_session


def _make_settings_mock():
    settings = Mock()
    settings.settings_locked = False
    settings.set_setting = Mock(return_value=True)
    settings.emit_settings_changed_after_commit = Mock()
    return settings


def _rag_service(index_hash: str = "index-hash") -> MagicMock:
    service = MagicMock()
    service.__enter__.return_value = service
    service.__exit__.return_value = False
    service._get_or_create_rag_index.return_value.index_hash = index_hash
    return service


@contextmanager
def _configure_context(
    *, db_session=None, settings=None, extra_patches=None, check_env=None
):
    """Patch ``configure_rag``'s DB/settings/env-lock seams at their SOURCE
    modules (``get_user_db_session`` and ``get_settings_manager`` are
    imported function-locally inside ``configure_rag``, so patching the
    ``local_deep_research.web.routers.rag`` module attribute would miss
    them -- see the module docstring on why the fake session context
    manager also replicates the real rollback-on-exception behaviour).
    """
    db_session = db_session if db_session is not None else _make_db_session()
    settings = settings if settings is not None else _make_settings_mock()

    @contextmanager
    def _fake_get_user_db_session(*_args, **_kwargs):
        try:
            yield db_session
        except Exception:
            db_session.rollback()
            raise

    patches = [
        patch(
            f"{_DB_CTX}.get_user_db_session",
            side_effect=_fake_get_user_db_session,
        ),
        patch(f"{_DB_UTILS}.get_settings_manager", return_value=settings),
        patch(
            f"{MODULE}.check_env_setting",
            side_effect=check_env if check_env is not None else lambda k: None,
        ),
    ]
    if extra_patches:
        patches.extend(extra_patches)

    try:
        for p in patches:
            p.start()
        yield db_session, settings
    finally:
        for p in reversed(patches):
            p.stop()


def _configure(payload, **context_kwargs):
    """Run ``configure_rag`` end-to-end with the given payload and return
    ``(result, db_session, settings)`` for assertions."""
    with _configure_context(**context_kwargs) as (db_session, settings):
        result = asyncio.run(
            configure_rag(_fake_request(payload), username="testuser")
        )
    return result, db_session, settings


class TestConfigureRagAtomicity:
    def test_commits_staged_settings_and_borrowed_index_once_on_success(self):
        rag_service = _rag_service()
        settings = _make_settings_mock()
        db_session = _make_db_session()

        commit_state_at_emit = {}

        def _record_commit_state_at_emit(keys):
            # The invariant this locks: emit fires strictly AFTER the
            # transaction has landed, not interleaved with it.
            commit_state_at_emit["commit_count"] = db_session.commit.call_count
            commit_state_at_emit["keys"] = keys

        settings.emit_settings_changed_after_commit.side_effect = (
            _record_commit_state_at_emit
        )

        result, db_session, settings = _configure(
            _configuration_payload(),
            db_session=db_session,
            settings=settings,
            extra_patches=[
                patch(f"{MODULE}.LibraryRAGService", return_value=rag_service)
            ],
        )

        assert result["success"] is True
        assert result["index_hash"] == "index-hash"
        # Every staged write is batched (commit=False) -- the ONLY commit is
        # the single terminal db_session.commit() below, never a per-key one.
        assert settings.set_setting.call_count == len(_REQUESTED_SETTING_KEYS)
        for call, key in zip(
            settings.set_setting.call_args_list, _REQUESTED_SETTING_KEYS
        ):
            assert call.args[0] == key
            assert call.kwargs == {"commit": False}
        rag_service._get_or_create_rag_index.assert_called_once_with(
            "collection-1", db_session=db_session, commit=False
        )
        db_session.commit.assert_called_once_with()
        db_session.rollback.assert_not_called()
        settings.emit_settings_changed_after_commit.assert_called_once_with(
            _REQUESTED_SETTING_KEYS
        )
        assert commit_state_at_emit == {
            "commit_count": 1,
            "keys": _REQUESTED_SETTING_KEYS,
        }

    def test_emits_once_after_default_settings_commit(self):
        settings = _make_settings_mock()
        db_session = _make_db_session()

        result, db_session, settings = _configure(
            _configuration_payload(collection_id=None),
            db_session=db_session,
            settings=settings,
        )

        assert result["success"] is True
        assert "index_hash" not in result
        db_session.commit.assert_called_once_with()
        settings.emit_settings_changed_after_commit.assert_called_once_with(
            _REQUESTED_SETTING_KEYS
        )

    def test_rolls_back_staged_settings_when_index_creation_fails(self):
        rag_service = _rag_service()
        rag_service._get_or_create_rag_index.side_effect = RuntimeError(
            "index creation failed"
        )
        settings = _make_settings_mock()
        db_session = _make_db_session()

        result, db_session, settings = _configure(
            _configuration_payload(),
            db_session=db_session,
            settings=settings,
            extra_patches=[
                patch(f"{MODULE}.LibraryRAGService", return_value=rag_service)
            ],
        )

        assert result.status_code == 500
        db_session.commit.assert_not_called()
        db_session.rollback.assert_called_once_with()
        settings.emit_settings_changed_after_commit.assert_not_called()

    def test_rolls_back_everything_when_terminal_commit_fails(self):
        rag_service = _rag_service()
        settings = _make_settings_mock()
        db_session = _make_db_session()
        db_session.commit.side_effect = RuntimeError("commit failed")

        result, db_session, settings = _configure(
            _configuration_payload(),
            db_session=db_session,
            settings=settings,
            extra_patches=[
                patch(f"{MODULE}.LibraryRAGService", return_value=rag_service)
            ],
        )

        assert result.status_code == 500
        rag_service._get_or_create_rag_index.assert_called_once_with(
            "collection-1", db_session=db_session, commit=False
        )
        db_session.commit.assert_called_once_with()
        db_session.rollback.assert_called_once_with()
        settings.emit_settings_changed_after_commit.assert_not_called()

    def test_emits_nothing_when_staged_setting_write_fails(self):
        rag_service = _rag_service()
        settings = _make_settings_mock()
        settings.set_setting.return_value = False
        db_session = _make_db_session()

        result, db_session, settings = _configure(
            _configuration_payload(),
            db_session=db_session,
            settings=settings,
            extra_patches=[
                patch(f"{MODULE}.LibraryRAGService", return_value=rag_service)
            ],
        )

        assert result.status_code == 500
        settings.emit_settings_changed_after_commit.assert_not_called()
        rag_service._get_or_create_rag_index.assert_not_called()
        # The explicit rollback at the set_setting-failure call site
        # (rag.py:1342) fires before any index work is attempted.
        db_session.rollback.assert_called_once_with()
        db_session.commit.assert_not_called()

    @pytest.mark.parametrize(
        "text_separators",
        ["not valid json", '{"separator": "\\n"}', ["\\n", 2]],
    )
    def test_rejects_malformed_text_separators_at_request_boundary(
        self, text_separators
    ):
        settings = _make_settings_mock()
        db_session = _make_db_session()

        result, db_session, settings = _configure(
            _configuration_payload(
                collection_id=None, text_separators=text_separators
            ),
            db_session=db_session,
            settings=settings,
        )

        assert result.status_code == 400
        settings.set_setting.assert_not_called()
        # Rejected before _persist_configuration (and therefore
        # get_user_db_session) is ever entered.
        db_session.commit.assert_not_called()
        db_session.rollback.assert_not_called()


class TestLockedSettingsRefuseWrites:
    """See module docstring: gap-fill for rag.py:1307-1338, not present in
    the original 6 Flask methods but confirmed live in this branch's
    ``_persist_configuration``."""

    def test_locked_settings_state_returns_403_and_performs_no_write(self):
        settings = _make_settings_mock()
        settings.settings_locked = True
        db_session = _make_db_session()

        result, db_session, settings = _configure(
            _configuration_payload(), db_session=db_session, settings=settings
        )

        assert result.status_code == 403
        body = json.loads(result.body)
        assert body["success"] is False
        assert "locked" in body["error"].lower()
        settings.set_setting.assert_not_called()
        db_session.commit.assert_not_called()
        db_session.rollback.assert_not_called()

    def test_environment_locked_key_returns_403_naming_key_and_performs_no_write(
        self,
    ):
        settings = _make_settings_mock()
        db_session = _make_db_session()
        locked_key = "local_search_embedding_model"

        def _check_env(key):
            return "env-value" if key == locked_key else None

        result, db_session, settings = _configure(
            _configuration_payload(),
            db_session=db_session,
            settings=settings,
            check_env=_check_env,
        )

        assert result.status_code == 403
        body = json.loads(result.body)
        assert body["success"] is False
        assert locked_key in body["error"]
        settings.set_setting.assert_not_called()
        db_session.commit.assert_not_called()
        db_session.rollback.assert_not_called()