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.

10.7 KB
Raw Blame
"""Component coverage for the ``LibraryRAGService.search()`` dimension-mismatch
translator (PR #5284 review follow-up).

The translator converts ``FaissVectorStore._prepare``'s ``ValueError`` (raised
when a query embedding's dimension differs from the index's, or when it is
otherwise degenerate -- non-finite, zero-magnitude, or overflow-magnitude)
into a service-level ``RuntimeError`` carrying collection context, preserving
the legacy exception contract that pre-#5284 callers/logs depended on.

The existing skip-path test mocks ``VectorIndex`` wholesale, so the real
``search()`` translator branch was never exercised. This module:
  * confirms the real ``FaissVectorStore._prepare`` raises a ``ValueError``
    with the exact dimension-mismatch message recognized by the translator;
  * drives the REAL ``LibraryRAGService.search()`` translator branch with a
    realistic dimension-mismatch ``ValueError``, asserting it becomes a
    ``RuntimeError`` with collection context + chained cause;
  * pins that NON-dimension ``ValueError``s pass through unchanged;
  * drives a REAL ``FaissVectorStore`` (not a stand-in) dimension mismatch
    through the REAL translator end to end, so the translator's regex is
    pinned against ``_prepare``'s ACTUAL wording rather than a copy of it
    (a two-step drift -- changing ``_prepare``'s message and the store-level
    test's literal together -- would still fail this one); and
  * drives a REAL, EMPTY ``FaissVectorStore``'s zero-magnitude / non-finite
    query rejection through that same real translator: ``_prepare`` now runs
    BEFORE the ``ntotal == 0`` early return (see ``faiss_store.py``'s
    ``search()``), so those degenerate-embedding rejections must become the
    same shape of service-level ``RuntimeError`` as a dimension mismatch, not
    a raw ``ValueError`` escaping to the caller.
"""

from __future__ import annotations

import threading

import numpy as np
import pytest
from langchain_core.embeddings import Embeddings

from local_deep_research.database.models.library import EmbeddingProvider
from local_deep_research.research_library.services.library_rag_service import (
    LibraryRAGService,
)
from local_deep_research.vector_stores.facade import VectorIndex
from local_deep_research.vector_stores.implementations.faiss_store import (
    FaissVectorStore,
)


def _vecs(n: int, dim: int = 4, seed: int = 0) -> np.ndarray:
    rng = np.random.default_rng(seed)
    return rng.random((n, dim), dtype=np.float32)


class _RaisingVectorIndex:
    """Stand-in vector index whose ``search`` raises a given error.

    Drives the REAL ``LibraryRAGService.search()`` translator branch with a
    realistic dimension-mismatch ``ValueError`` (the same shape the real
    ``FaissVectorStore._prepare`` produces), so the translator's
    try/except/raise executes against the real method rather than being mocked
    away. Only the vector-index leaf is faked; the service method is real.
    """

    def __init__(self, error: BaseException) -> None:
        self._error = error

    def search(self, query: object, top_k: int) -> list:  # noqa: ANN001
        raise self._error


def _bare_service() -> LibraryRAGService:
    """A ``LibraryRAGService`` with ``__init__`` bypassed (only search() runs)."""
    service = object.__new__(LibraryRAGService)
    service.username = "search-user"
    return service


# --- the real FaissVectorStore really raises ValueError("...dimension...") ----


def test_faiss_prepare_raises_dimension_valueerror(tmp_path) -> None:
    """Pin the exact store message recognized by the service translator."""
    store = FaissVectorStore.create(
        dimension=4,
        index_type="flat",
        metric="cosine",
        normalize=True,
        path=tmp_path / "coll.faiss",
        lock=threading.Lock(),
        integrity_record=lambda _p: None,
        integrity_verify=lambda _p: (True, None),
    )
    store.add([1], _vecs(1, dim=4))

    with pytest.raises(
        ValueError, match=r"^vector dimension 8 != index dimension 4$"
    ):
        store.search(_vecs(1, dim=8)[0], 1)


# --- the real LibraryRAGService.search() translator --------------------------


def test_search_translates_dimension_valueerror_to_runtimeerror(
    monkeypatch,
) -> None:
    """Drive the REAL ``search()`` method: a dimension-mismatch ``ValueError``
    from the vector index is translated to a ``RuntimeError`` carrying the
    collection context."""
    service = _bare_service()
    monkeypatch.setattr(
        service,
        "_get_vector_index",
        lambda _cid, _name: _RaisingVectorIndex(
            ValueError("vector dimension 8 != index dimension 4")
        ),
    )

    with pytest.raises(RuntimeError) as exc_info:
        service.search("any query", "coll-123", 5)

    msg = str(exc_info.value).lower()
    assert "dimension mismatch" in msg
    assert "coll-123" in str(exc_info.value)  # collection context preserved
    # The underlying ValueError is chained, not discarded.
    assert isinstance(exc_info.value.__cause__, ValueError)


@pytest.mark.parametrize(
    "message",
    [
        "zero-magnitude query",
        "unsupported dimensions parameter",
        "provider refused vector dimension 8 != index dimension 4",
    ],
)
def test_search_passes_through_non_dimension_valueerror(
    monkeypatch, message
) -> None:
    """Non-dimension ``ValueError``s pass through unchanged. Each parametrized
    message is deliberately a NEAR-miss of a real ``_prepare`` message
    (different wording, an added prefix, etc.) -- the translator matches
    exact formats only, both for the dimension regex and for the fixed-text
    degenerate-embedding messages (see the real-store tests below), so a
    provider ``ValueError`` that merely resembles one of those keeps passing
    through unchanged rather than being rewired."""
    service = _bare_service()
    monkeypatch.setattr(
        service,
        "_get_vector_index",
        lambda _cid, _name: _RaisingVectorIndex(ValueError(message)),
    )

    with pytest.raises(ValueError) as exc_info:
        service.search("any query", "coll-123", 5)

    assert str(exc_info.value) == message


# --- real end-to-end: a genuine FaissVectorStore driving the real search() --


class _FixedVectorEmbeddings(Embeddings):
    """Returns the SAME fixed vector for every query, regardless of the query
    text. Lets a test pin exactly which vector reaches the real
    ``FaissVectorStore._prepare`` without depending on any real embedding
    model (no network, no model load -- see ``_FakeEmbeddings`` in
    ``tests/vector_stores/test_facade.py`` for the same pattern)."""

    def __init__(self, vector: np.ndarray) -> None:
        self._vector = vector

    def embed_documents(self, texts):  # pragma: no cover - unused by search()
        return [self._vector.tolist() for _ in texts]

    def embed_query(self, text):
        return self._vector.tolist()


def _make_real_vector_index(
    tmp_path, *, store_dimension: int, query_vector: np.ndarray
) -> VectorIndex:
    """A REAL ``VectorIndex`` wired to a REAL, freshly-created (empty)
    ``FaissVectorStore`` on disk under ``tmp_path``. Only the embeddings
    backend is faked (fixed output); ``_prepare`` itself is never mocked, so
    whatever the real store does with ``query_vector`` is what ``search()``
    really sees.
    """
    return VectorIndex(
        username="testuser",
        db_password="pw",  # gitleaks:allow
        embeddings=_FixedVectorEmbeddings(query_vector),
        embedding_model="fake-model",
        embedding_model_type=EmbeddingProvider.SENTENCE_TRANSFORMERS,
        collection_name="collection_coll-123",
        dimension=store_dimension,
        path=tmp_path / "coll.faiss",
        lock=threading.Lock(),
        integrity_record=lambda _p: None,
        integrity_verify=lambda _p: (True, None),
        index_type="flat",
        metric="cosine",
        normalize=True,
    )


def test_search_real_store_dimension_mismatch_translates_end_to_end(
    monkeypatch, tmp_path
) -> None:
    """R5: no ``_RaisingVectorIndex`` stand-in, no mocked ``_prepare`` -- a
    REAL ``FaissVectorStore`` (dimension 4) raises its real
    dimension-mismatch ``ValueError`` for a real dimension-8 query, and the
    REAL ``LibraryRAGService.search()`` translator turns it into a
    ``RuntimeError``. This pins the translator's regex against ``_prepare``'s
    ACTUAL wording end to end, unlike
    ``test_search_translates_dimension_valueerror_to_runtimeerror`` above
    (which only drives the translator against a fake vector index whose
    error text is hand-written, not produced by a real store)."""
    service = _bare_service()
    vindex = _make_real_vector_index(
        tmp_path, store_dimension=4, query_vector=_vecs(1, dim=8)[0]
    )
    monkeypatch.setattr(
        service, "_get_vector_index", lambda _cid, _name: vindex
    )

    with pytest.raises(RuntimeError) as exc_info:
        service.search("any query", "coll-123", 5)

    msg = str(exc_info.value)
    assert "dimension mismatch" in msg.lower()
    assert "coll-123" in msg
    cause = exc_info.value.__cause__
    assert isinstance(cause, ValueError)
    assert str(cause) == "vector dimension 8 != index dimension 4"


@pytest.mark.parametrize(
    "make_query",
    [
        pytest.param(lambda: np.zeros(4, dtype="float32"), id="zero_magnitude"),
        pytest.param(
            lambda: np.full(4, np.nan, dtype="float32"), id="non_finite"
        ),
    ],
)
def test_search_real_empty_store_degenerate_query_translates_end_to_end(
    monkeypatch, tmp_path, make_query
) -> None:
    """R2: on an EMPTY store, ``_prepare`` runs BEFORE the ``ntotal == 0``
    early return (see ``faiss_store.py``'s ``search()``), so a
    zero-magnitude or non-finite query is rejected instead of silently
    returning ``[]`` for a never-indexed collection -- and the real
    ``search()`` translator turns that ``ValueError`` into the same shape of
    service-level ``RuntimeError`` as a dimension mismatch, not a raw
    exception escaping to the caller. Reverting the translator's widening to
    cover these degenerate-embedding messages (as opposed to only the
    dimension regex) fails this test: the pre-widening translator would let
    this ``ValueError`` propagate raw instead of becoming a
    ``RuntimeError``."""
    service = _bare_service()
    vindex = _make_real_vector_index(
        tmp_path, store_dimension=4, query_vector=make_query()
    )
    assert vindex._store.count() == 0  # genuinely empty/never-indexed
    monkeypatch.setattr(
        service, "_get_vector_index", lambda _cid, _name: vindex
    )

    with pytest.raises(RuntimeError) as exc_info:
        service.search("any query", "coll-123", 5)

    msg = str(exc_info.value)
    assert "coll-123" in msg
    assert "degenerate" in msg.lower()
    assert isinstance(exc_info.value.__cause__, ValueError)