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.

9.6 KB
Raw Blame
"""Real SQL-count and index-scope regressions for collection search eligibility."""

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

import pytest
from sqlalchemy import create_engine, event
from sqlalchemy.orm import sessionmaker

from local_deep_research.database.models import Base
from local_deep_research.database.models.library import (
    Collection,
    EmbeddingProvider,
    RAGIndex,
    RagDocumentStatus,
)
from local_deep_research.research_library.services.library_rag_service import (
    LibraryRAGService,
)
from local_deep_research.web_search_engines.engines.search_engine_collection import (
    CollectionSearchEngine,
)

_ENGINE = "local_deep_research.web_search_engines.engines"
_SERVICE = "local_deep_research.research_library.services.library_rag_service"
_COLLECTION_ID = "collection-query-count"
_CONFIG = {
    "embedding_model": "test-model",
    "embedding_provider": "sentence_transformers",
    "chunk_size": 700,
    "chunk_overlap": 0,
    "splitter_type": "recursive",
    "text_separators": ["\n", ""],
    "distance_metric": "dot_product",
    "normalize_vectors": False,
    "index_type": "flat",
}


class _NoEmbeddingIO:
    @property
    def embeddings(self):
        raise AssertionError(
            "Eligibility must not initialize or probe embeddings"
        )


@pytest.fixture
def search_world():
    """Real collection, configuration resolution and EXISTS; no embedding IO."""
    engine = create_engine("sqlite:///:memory:")
    Base.metadata.create_all(engine)
    session = sessionmaker(bind=engine, expire_on_commit=False)()
    collection_config = dict(_CONFIG)
    collection_config["embedding_model_type"] = EmbeddingProvider(
        collection_config.pop("embedding_provider")
    )
    session.add(
        Collection(
            id=_COLLECTION_ID,
            name="Query count collection",
            **collection_config,
        )
    )
    session.commit()

    @contextmanager
    def get_session(*_args, **_kwargs):
        yield session

    services = []

    def make_service(**kwargs):
        service = LibraryRAGService(
            **kwargs, embedding_manager=_NoEmbeddingIO()
        )
        service.get_rag_stats = Mock(wraps=service.get_rag_stats)
        service.search = Mock(return_value=[])
        services.append(service)
        return service

    with (
        patch(
            f"{_ENGINE}.search_engine_library.get_setting_from_snapshot",
            return_value=None,
        ),
        patch(
            f"{_ENGINE}.search_engine_library.get_server_url",
            return_value="http://localhost:5000",
        ),
        patch(
            f"{_ENGINE}.search_engine_collection.get_user_db_session",
            get_session,
        ),
        patch(f"{_SERVICE}.get_user_db_session", get_session),
        patch(f"{_SERVICE}.FileIntegrityManager"),
        patch(
            f"{_ENGINE}.search_engine_collection.LibraryRAGService",
            side_effect=make_service,
        ),
    ):

        def add_index(collection_id=_COLLECTION_ID, legacy=False, **overrides):
            config = {**_CONFIG, **overrides}
            with LibraryRAGService(
                username="query-count-user",
                embedding_manager=_NoEmbeddingIO(),
                **config,
            ) as service:
                index_hash = service._get_index_hash(
                    f"collection_{collection_id}",
                    service.embedding_model,
                    service.embedding_provider,
                )
            if legacy:
                index_hash = "legacy-" + index_hash
            config["embedding_model_type"] = EmbeddingProvider(
                config.pop("embedding_provider")
            )
            index = RAGIndex(
                collection_name=f"collection_{collection_id}",
                embedding_dimension=4,
                index_path=f"/unused/{index_hash}.faiss",
                index_hash=index_hash,
                is_current=True,
                **config,
            )
            session.add(index)
            session.commit()
            return index

        def add_status(index, collection_id=_COLLECTION_ID):
            session.add(
                RagDocumentStatus(
                    document_id=f"document-for-index-{index.id}",
                    collection_id=collection_id,
                    rag_index_id=index.id,
                    chunk_count=1,
                )
            )
            session.commit()

        def search(collection_id=_COLLECTION_ID):
            collection_engine = CollectionSearchEngine(
                collection_id=collection_id,
                collection_name="Query count collection",
                settings_snapshot={"_username": "query-count-user"},
            )
            statements = []

            def capture(_conn, _cursor, statement, _params, _context, _many):
                statements.append(statement)

            event.listen(engine, "before_cursor_execute", capture)
            try:
                assert collection_engine.search("test query") == []
            finally:
                event.remove(engine, "before_cursor_execute", capture)
            return statements

        yield session, add_index, add_status, search, services

    session.close()
    engine.dispose()


@pytest.mark.parametrize("indexed", [False, True])
@pytest.mark.parametrize("legacy", [False, True])
def test_search_eligibility_uses_only_configuration_and_exists_queries(
    search_world, indexed, legacy
):
    _session, add_index, add_status, search, services = search_world
    index = add_index(legacy=legacy)
    if indexed:
        add_status(index)

    statements = search()

    service = services[-1]
    assert len(statements) == 3 + int(legacy), statements
    service.get_rag_stats.assert_not_called()
    assert sum("FROM collections" in sql for sql in statements) == 1
    assert sum("FROM rag_indices" in sql for sql in statements) == 1 + int(
        legacy
    )
    assert (
        sum(
            "SELECT EXISTS" in sql and "FROM rag_document_status" in sql
            for sql in statements
        )
        == 1
    )
    assert not any(
        "count(" in sql.lower() or "sum(" in sql.lower() for sql in statements
    )
    if indexed:
        service.search.assert_called_once_with("test query", _COLLECTION_ID, 10)
    else:
        service.search.assert_not_called()


@pytest.mark.parametrize("active_index_exists", [False, True])
@pytest.mark.parametrize(
    "foreign_config",
    [
        {"collection_id": "other-collection"},
        {"embedding_model": "other-model"},
        {"chunk_overlap": 20},
        {"normalize_vectors": True},
        {"distance_metric": "cosine"},
        {"text_separators": ["\n\n", "\n", ""]},
    ],
)
def test_foreign_status_rows_do_not_make_this_configuration_searchable(
    search_world, active_index_exists, foreign_config
):
    session, add_index, add_status, search, services = search_world
    if active_index_exists:
        add_index()
    foreign_index = add_index(**foreign_config)
    add_status(
        foreign_index, foreign_config.get("collection_id", _COLLECTION_ID)
    )
    original_indices = session.query(RAGIndex).count()

    search()

    services[-1].get_rag_stats.assert_not_called()
    services[-1].search.assert_not_called()
    assert session.query(RAGIndex).count() == original_indices


def test_missing_collection_does_not_fall_back_to_another_collection(
    search_world,
):
    """Locks the premise this change relies on -- a missing collection_id
    never falls back to another collection (e.g. the default library) --
    rather than detecting a regression of this change itself. ``search()``
    returns at the Collection lookup before ``LibraryRAGService`` (and so
    ``has_indexed_documents``) is ever constructed, so ``services == []``
    and this passes byte-identically under a full revert of #5285. See
    ``test_has_indexed_documents_scopes_status_rows_to_the_resolved_index``
    below for a test that actually exercises ``has_indexed_documents``.
    """
    _session, add_index, add_status, search, services = search_world
    add_status(add_index())

    statements = search(collection_id=None)

    assert len(statements) == 1, statements
    assert services == []


def test_has_indexed_documents_scopes_status_rows_to_the_resolved_index(
    search_world,
):
    """Direct test of ``has_indexed_documents`` itself, since ``search()``
    only reaches it indirectly through ``CollectionSearchEngine`` and no
    test in this tree calls it directly otherwise.

    Fails if the ``RagDocumentStatus.rag_index_id`` scoping is dropped in
    favor of a ``collection_id``-only ``EXISTS`` -- as it briefly was in
    this branch's own history (commit 3df8c2c7f, reverted by 6770ae75e):
    the second assertion below would then see the status row (moved to a
    foreign index but still in the same collection) as making this
    configuration eligible, and return True instead of False.
    """
    session, add_index, add_status, _search, _services = search_world
    index = add_index()
    add_status(index)

    with LibraryRAGService(
        username="query-count-user",
        embedding_manager=_NoEmbeddingIO(),
        **_CONFIG,
    ) as service:
        assert service.has_indexed_documents(_COLLECTION_ID) is True

        # Re-point the same status row at a foreign-configuration index
        # without changing its collection_id.
        status_row = (
            session.query(RagDocumentStatus)
            .filter_by(collection_id=_COLLECTION_ID)
            .one()
        )
        foreign_index = add_index(embedding_model="other-model")
        status_row.rag_index_id = foreign_index.id
        session.commit()

        assert service.has_indexed_documents(_COLLECTION_ID) is False