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.

11.2 KB
Raw Blame
"""Routes delegate full replacements without mutating the published state.

Real commit/rollback and post-commit file removal are exercised by
services/test_collection_rebuild.py. These tests pin the three entry points,
their incremental controls, error responses and short setup-session lifetime.
"""

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

import pytest
from starlette.requests import Request

from local_deep_research.web.routers import rag as rag_module

_DB_CTX = "local_deep_research.database.session_context"
_DB_UTILS = "local_deep_research.utilities.db_utils"
_SESSION_PASSWORDS = "local_deep_research.database.session_passwords"
SETTINGS_SNAPSHOT = {}


def _request(path="/library/api/rag/index-all", query_string=b""):
    return Request(
        {
            "type": "http",
            "method": "GET",
            "path": path,
            "headers": [],
            "query_string": query_string,
            "session": {"session_id": "sid"},
        }
    )


def _db_session():
    session = MagicMock()
    collection = Mock(id="coll-1", embedding_model="already-set")
    collection.name = "Test"
    session.query.return_value.filter_by.return_value.first.return_value = (
        collection
    )
    return session


def _settings_manager():
    manager = Mock()
    manager.get_setting.side_effect = lambda key, default=None: default
    manager.get_all_settings.return_value = SETTINGS_SNAPSHOT
    return manager


def _drain(response):
    import asyncio

    async def collect():
        chunks = []
        async for chunk in response.body_iterator:
            chunks.append(
                chunk if isinstance(chunk, bytes) else str(chunk).encode()
            )
        return b"".join(chunks).decode()

    return asyncio.run(collect())


@pytest.mark.parametrize(
    "site", ["index_all", "index_collection", "background"]
)
@pytest.mark.parametrize("force", [False, True])
@pytest.mark.parametrize("failed", [False, True])
def test_routes_use_the_rebuild_boundary_without_early_metadata_writes(
    site, force, failed
):
    db = _db_session()
    service = MagicMock()
    service.__enter__.return_value = service
    depth = 0
    aggregate = {
        "successful": 1,
        "skipped": 0,
        "failed": 0,
        "errors": [],
        "results": {"doc-1": {"status": "success"}},
        "cancelled": False,
        "total": 1,
    }
    service.reconcile_collection_index.return_value = {
        "indexed_documents": 1,
        "indexed_chunks": 1,
        "live_vectors": 1,
        "orphan_vectors": 0,
    }

    @contextmanager
    def session(*_args, **_kwargs):
        nonlocal depth
        depth += 1
        try:
            yield db
        finally:
            depth -= 1

    def run(doc_info, collection_id, **kwargs):
        assert depth == 0, "Embedding must not hold the route's setup session"
        assert doc_info == [("doc-1", "First")]
        assert collection_id == "coll-1"
        if failed:
            raise RuntimeError("Embedding backend unavailable")
        if kwargs.get("progress_callback"):
            kwargs["progress_callback"](1, 1, "First", "success")
        return aggregate

    selected = (
        service.rebuild_collection
        if force
        else service.index_documents_parallel
    )
    selected.side_effect = run
    doc = Mock(id="doc-1", filename="First", title="First")
    with (
        patch(f"{_DB_CTX}.get_user_db_session", side_effect=session),
        patch(
            f"{_DB_UTILS}.get_settings_manager",
            return_value=_settings_manager(),
        ),
        patch.object(
            rag_module, "get_settings_manager", return_value=_settings_manager()
        ),
        patch.object(rag_module, "get_rag_service", return_value=service),
        patch.object(
            rag_module, "_get_rag_service_for_thread", return_value=service
        ),
        patch.object(
            rag_module, "_store_collection_embedding_metadata"
        ) as store,
        patch.object(
            rag_module,
            "_query_documents_to_index",
            return_value=[(Mock(), doc)],
        ),
        patch.object(rag_module, "_register_sse_indexer", return_value=True),
        patch.object(rag_module, "_update_task_status") as status,
        patch.object(rag_module, "_is_task_cancelled", return_value=False),
        patch(
            f"{_SESSION_PASSWORDS}.session_password_store",
            Mock(get_session_password=Mock(return_value=None)),
        ),
    ):
        if site == "background":
            rag_module._background_index_worker(
                "task-1", "coll-1", "alice", "pw", force
            )
            assert status.call_args.kwargs["status"] == (
                "failed" if failed else "completed"
            )
        else:
            request = _request(
                query_string=f"collection_id=coll-1&force_reindex={str(force).lower()}".encode()
            )
            response = (
                rag_module.index_all(request, username="alice")
                if site == "index_all"
                else rag_module.index_collection(
                    request, "coll-1", username="alice"
                )
            )
            events = _sse_events(_drain(response))
            assert events[-1]["type"] == ("error" if failed else "complete")
    selected.assert_called_once()
    other = (
        service.index_documents_parallel
        if force
        else service.rebuild_collection
    )
    other.assert_not_called()
    store.assert_not_called()
    db.commit.assert_not_called()
    if failed:
        service.reconcile_collection_index.assert_not_called()


CLEARED_AGGREGATE = {
    "successful": 0,
    "skipped": 1,
    "failed": 0,
    "errors": [],
    "results": {"doc-1": {"status": "cleared"}},
    "cancelled": False,
    "total": 1,
}


def _sse_events(body):
    return [
        json.loads(line[6:])
        for line in body.split("\n")
        if line.startswith("data: ")
    ]


def test_cleared_counts_as_skipped_in_the_index_all_sse_loop():
    """``rag.py``'s own per-document classification (the ``index_all``
    batch loop), which reads ``result["status"]`` directly rather than
    trusting the aggregate's counters."""
    db_session = _db_session()
    rag_service = MagicMock()
    rag_service.index_documents_parallel.return_value = dict(CLEARED_AGGREGATE)

    @contextmanager
    def fake_session(*args, **kwargs):
        yield db_session

    with (
        patch(f"{_DB_CTX}.get_user_db_session", side_effect=fake_session),
        patch(
            f"{_DB_UTILS}.get_settings_manager",
            return_value=_settings_manager(),
        ),
        patch.object(rag_module, "_store_collection_embedding_metadata"),
        patch.object(
            rag_module,
            "_query_documents_to_index",
            return_value=[(Mock(), Mock(id="doc-1", title="t"))],
        ),
        patch.object(rag_module, "get_rag_service", return_value=rag_service),
    ):
        body = _drain(
            rag_module.index_all(
                _request(query_string=b"collection_id=coll-1"),
                username="alice",
            )
        )

    complete = [e for e in _sse_events(body) if e.get("type") == "complete"][0]
    assert complete["results"]["skipped"] == 1, (
        "a 'cleared' document (empty-text purge) is a handled non-failure "
        f"and must be counted as skipped: {complete['results']}"
    )
    assert complete["results"]["failed"] == 0, complete["results"]
    assert complete["results"]["errors"] == [], (
        "a cleared document must not produce a blank-error failure row: "
        f"{complete['results']['errors']}"
    )


def _parallel_with_per_doc_status(status, **extra):
    """Run ``index_documents_parallel`` over one document whose per-doc
    result is ``status``.

    ``_index_one`` is patched on the INSTANCE: that is the documented seam
    the helper checks (``"_index_one" in self.__dict__``) to bypass the
    prepared/serialised pipeline. A class-level patch does not take it --
    ``cls._index_one is LibraryRAGService._index_one`` stays true once the
    class attribute itself has been replaced -- and the call falls through
    to ``_prepare_document`` instead.
    """
    from local_deep_research.research_library.services.library_rag_service import (
        LibraryRAGService,
    )

    service = object.__new__(LibraryRAGService)
    service.username = "alice"
    service._db_password = "pw"
    service._index_one = Mock(return_value={"status": status, **extra})

    return LibraryRAGService.index_documents_parallel(
        service, [("doc-1", "Emptied paper")], "coll-1", max_workers=1
    )


def test_cleared_counts_as_skipped_in_index_documents_parallel():
    """``library_rag_service.index_documents_parallel`` -- the aggregate the
    ``index_collection`` SSE route and the background worker both copy their
    counters from, so this one site covers both of them."""
    result = _parallel_with_per_doc_status("cleared", chunk_count=0)

    assert result["skipped"] == 1, (
        f"'cleared' must aggregate into skipped, not failed: {result}"
    )
    assert result["failed"] == 0, result
    assert result["errors"] == [], (
        f"a cleared document must not be reported as an error: {result}"
    )


def test_cleared_counts_as_skipped_in_index_all_documents():
    """``library_rag_service.index_all_documents`` -- the third copy of the
    same ``in ("skipped", "cleared")`` check."""
    from local_deep_research.research_library.services.library_rag_service import (
        LibraryRAGService,
    )

    service = object.__new__(LibraryRAGService)
    service.username = "alice"
    service._db_password = "pw"
    service.index_document = Mock(
        return_value={"status": "cleared", "chunk_count": 0}
    )

    doc_collection = Mock()
    doc_collection.document_id = "doc-1"

    db_session = MagicMock()
    query = db_session.query.return_value
    query.filter_by.return_value = query
    query.filter.return_value = query
    query.all.return_value = [doc_collection]
    query.first.return_value = Mock(title="Emptied paper")

    @contextmanager
    def fake_session(*args, **kwargs):
        yield db_session

    with patch(
        "local_deep_research.research_library.services."
        "library_rag_service.get_user_db_session",
        side_effect=fake_session,
    ):
        result = LibraryRAGService.index_all_documents(service, "coll-1")

    assert result["skipped"] == 1, (
        f"'cleared' must aggregate into skipped, not failed: {result}"
    )
    assert result["failed"] == 0, result


@pytest.mark.parametrize("status", ["error", "failed"])
def test_a_real_failure_is_still_counted_as_failed(status):
    """Discriminator for the three tests above: the classification must not
    have been widened into "everything is skipped"."""
    result = _parallel_with_per_doc_status(status, error="boom")

    assert result["failed"] == 1, result
    assert result["skipped"] == 0, result


def test_a_success_is_still_counted_as_successful():
    """Second discriminator: the counters are not all wired to one bucket."""
    result = _parallel_with_per_doc_status("success", chunk_count=3)

    assert result["successful"] == 1, result
    assert result["skipped"] == 0, result
    assert result["failed"] == 0, result