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.

12.3 KB
Raw Blame
"""Coverage for the FastAPI ``rag`` router's ``/library/api/collections``
endpoints: collection-type validation on create (POST) and the aggregated
document/indexed counts on list (GET).

Both tests below are ported (with FastAPI adaptation) from Flask-era test
files on main that this branch's FastAPI migration deleted wholesale, even
though the SOURCE fixes each test pins landed intact:

- 30b00eb91 "fix(collections): validate collection type input (#5397)" --
  ``tests/notes/test_notes_routes_review_fixes.py`` ::
  ``TestCreateCollectionTypeAllowlist::test_rejects_unhashable_collection_types``.
  (That Flask test lived under ``tests/notes/`` because the Flask notes and
  rag blueprints shared a test-client fixture module; the endpoint under
  test is the same ``POST /api/collections`` handler covered here.)
- 92c80718b "perf(library): aggregate collection document counts (#5378)" --
  ``tests/research_library/routes/test_rag_routes.py`` ::
  ``TestGetCollectionsIndexedCounts::test_document_link_counts_use_one_grouped_query``.

This file is new rather than an addition to one of the existing
``test_rag_routes_*.py`` files in this directory: those cover cancel/SSE
wiring, parallel-indexing settings plumbing, and standalone indexing
helpers, none of which touch the collection list/create endpoints these two
tests exercise. Grouping both under the ``/api/collections`` surface they
share keeps the file focused.

Follows the direct-call idiom established by
``tests/research_library/routes/test_rag_routes_cancel_and_worker_wiring.py``:
the router's route functions (and their ``_sync`` helpers) are plain
callables, called directly here with ``username`` passed as a keyword
(bypassing ``Depends(require_auth)`` resolution). Success paths return a
plain dict (FastAPI implies status 200); error paths return a starlette
``JSONResponse``, asserted via ``.status_code`` / ``json.loads(.body)``
rather than the Flask original's ``resp.get_json()``.
"""

import json
from types import SimpleNamespace
from unittest.mock import Mock, patch

import pytest

MODULE = "local_deep_research.web.routers.rag"
_DB_CTX = "local_deep_research.database.session_context"


def _fake_request():
    """Minimal stand-in for a Starlette ``Request``.

    Neither ``_create_collection_sync`` nor ``get_collections`` reads
    anything off the request object itself (the former takes the already-
    parsed body as a plain ``data`` dict; the latter only needs ``username``),
    so an empty stub is sufficient -- matching ``_fake_request`` in
    ``test_rag_routes_cancel_and_worker_wiring.py``.
    """
    return SimpleNamespace(session={}, query_params={})


def _build_mock_query(all_result=None, first_result=None):
    q = Mock()
    q.all.return_value = all_result if all_result is not None else []
    q.first.return_value = first_result
    q.filter_by.return_value = q
    q.filter.return_value = q
    q.join.return_value = q
    q.options.return_value = q
    q.order_by.return_value = q
    return q


def _make_db_session(query_side_effect=None):
    db_session = Mock()
    if query_side_effect is not None:
        db_session.query = Mock(side_effect=query_side_effect)
    else:
        db_session.query = Mock(return_value=_build_mock_query())
    db_session.commit = Mock()
    db_session.add = Mock()
    return db_session


# ---------------------------------------------------------------------------
# POST /api/collections: collection-type validation (#5397)
# ---------------------------------------------------------------------------


class TestCreateCollectionRejectsUnhashableTypes:
    """``_create_collection_sync`` must reject a non-string ``type`` with a
    clean 400 before the allowlist membership check ever runs.

    Ported from main's deleted Flask-era
    ``tests/notes/test_notes_routes_review_fixes.py`` (commit 30b00eb91,
    "fix(collections): validate collection type input (#5397)") ::
    ``TestCreateCollectionTypeAllowlist::test_rejects_unhashable_collection_types``.

    ``[]``/``{}`` are unhashable, so without this guard a naive
    ``collection_type not in allowed_types`` (a ``set`` membership test)
    would raise an uncaught-by-design ``TypeError: unhashable type`` --
    hence "unhashable" in the original name. The ``isinstance`` guard added
    by #5397 (now at the top of ``_create_collection_sync``, before the
    allowlist check) rejects them earlier with a clean, structured 400
    instead.
    """

    @pytest.mark.parametrize("invalid_type", [[], {}])
    def test_rejects_unhashable_collection_types(self, invalid_type):
        from local_deep_research.web.routers.rag import (
            _create_collection_sync,
        )

        db_session = _make_db_session()

        with patch(f"{_DB_CTX}.get_user_db_session") as mock_get_session:
            mock_get_session.return_value.__enter__ = Mock(
                return_value=db_session
            )
            mock_get_session.return_value.__exit__ = Mock(return_value=False)
            result = _create_collection_sync(
                {"name": "Trap", "type": invalid_type}, "testuser"
            )

        assert result.status_code == 400
        payload = json.loads(result.body)
        assert payload["success"] is False
        assert "type must be a string" in payload["error"].lower()
        # The isinstance guard runs before the DB session is even opened --
        # an invalid type is rejected without touching the allowlist check
        # or the name-uniqueness lookup.
        mock_get_session.assert_not_called()


# ---------------------------------------------------------------------------
# GET /api/collections: aggregated document/indexed counts (#5378)
# ---------------------------------------------------------------------------


class TestGetCollectionsAggregatedCounts:
    """``get_collections`` must compute each collection's document/indexed
    counts via ONE grouped aggregate query, not a per-collection lazy load
    (an N+1 query regression).

    Ported from main's deleted Flask-era
    ``tests/research_library/routes/test_rag_routes.py`` (commit 92c80718b,
    "perf(library): aggregate collection document counts (#5378)") ::
    ``TestGetCollectionsIndexedCounts::test_document_link_counts_use_one_grouped_query``.
    That Flask test file was replaced wholesale by this branch's FastAPI
    migration, dropping the test even though the SOURCE fix -- the single
    grouped ``func.count(...).group_by(DocumentCollection.collection_id)``
    query now in ``get_collections`` -- landed intact.

    This is a PERFORMANCE regression test: it captures every SQL statement
    executed against ``document_collections`` on a real (in-memory SQLite)
    session and asserts there is exactly ONE such query, with the expected
    ``GROUP BY`` clause. That pins the query COUNT itself, not just the
    returned numbers -- a revert to per-collection counting (looping over
    collections and querying/lazy-loading ``document_links`` for each) would
    still return correct totals but would fail this test by issuing N
    queries instead of 1, which a pure output-comparison test could never
    catch.
    """

    @staticmethod
    def _seed_session(request):
        """Return an in-memory session seeded with TWO collections.

        Collection A: 3 docs, 2 indexed (1 pending).
        Collection B: 2 docs, 1 indexed (1 pending).

        Two collections with *different* indexed/total splits are required
        so a missing ``.group_by(DocumentCollection.collection_id)`` (which
        would collapse the aggregate into a single global count) actually
        fails an assertion rather than coincidentally matching one
        collection's value. Returns ``(session, collection_a_id,
        collection_b_id)``.
        """
        import hashlib
        import uuid
        from sqlalchemy import create_engine
        from sqlalchemy.orm import sessionmaker
        from local_deep_research.database.models import Base
        from local_deep_research.database.models.library import (
            Collection,
            Document,
            DocumentCollection,
            DocumentStatus,
            SourceType,
        )

        engine = create_engine("sqlite:///:memory:")
        request.addfinalizer(engine.dispose)
        Base.metadata.create_all(engine)
        session = sessionmaker(bind=engine)()
        request.addfinalizer(session.close)

        source_type = SourceType(
            id=str(uuid.uuid4()),
            name="user_upload",
            display_name="User Upload",
            description="Uploaded by user",
            icon="fas fa-upload",
        )
        session.add(source_type)
        session.commit()

        doc_counter = [0]

        def _add_collection(name, indexed_flags):
            collection = Collection(
                id=str(uuid.uuid4()),
                name=name,
                description="Mixed indexed/unindexed links",
                is_default=False,
                collection_type="user_collection",
            )
            session.add(collection)
            session.commit()

            for indexed in indexed_flags:
                i = doc_counter[0]
                doc_counter[0] += 1
                content = f"document body {i}"
                doc = Document(
                    id=str(uuid.uuid4()),
                    source_type_id=source_type.id,
                    document_hash=hashlib.sha256(
                        f"{i}{content}".encode()
                    ).hexdigest(),
                    file_size=len(content),
                    file_type="text",
                    text_content=content,
                    title=f"Doc {i}",
                    status=DocumentStatus.COMPLETED,
                )
                session.add(doc)
                session.commit()
                session.add(
                    DocumentCollection(
                        document_id=doc.id,
                        collection_id=collection.id,
                        indexed=indexed,
                    )
                )
                session.commit()
            return collection.id

        # A: 3 docs, 2 indexed. B: 2 docs, 1 indexed -- distinct splits.
        collection_a_id = _add_collection(
            "Indexed Status Collection A", (True, True, False)
        )
        collection_b_id = _add_collection(
            "Indexed Status Collection B", (True, False)
        )

        return session, collection_a_id, collection_b_id

    def _call_route(self, session):
        """Invoke the real route function directly, with
        ``get_user_db_session`` patched to yield the seeded session --
        matching the direct-call idiom used throughout this directory
        instead of the Flask original's test client + blueprint app."""
        from local_deep_research.web.routers.rag import get_collections

        with patch(f"{_DB_CTX}.get_user_db_session") as mock_get_session:
            mock_get_session.return_value.__enter__ = Mock(return_value=session)
            mock_get_session.return_value.__exit__ = Mock(return_value=False)
            return get_collections(_fake_request(), username="testuser")

    def test_document_link_counts_use_one_grouped_query(self, request):
        """Collection totals must not lazy-load document_links per
        collection."""
        from sqlalchemy import event

        session, _collection_a_id, _collection_b_id = self._seed_session(
            request
        )
        document_collection_queries = []

        def capture_statement(
            _conn,
            _cursor,
            statement,
            _parameters,
            _context,
            _executemany,
        ):
            normalized = " ".join(statement.lower().split())
            if "from document_collections" in normalized:
                document_collection_queries.append(normalized)

        engine = session.get_bind()
        event.listen(engine, "before_cursor_execute", capture_statement)
        try:
            result = self._call_route(session)
        finally:
            event.remove(engine, "before_cursor_execute", capture_statement)
            session.close()

        # FastAPI idiom: the success path returns a plain dict (200 is
        # implicit in the return value, unlike the Flask original's
        # `response.status_code == 200` + `response.get_json()`).
        assert result["success"] is True
        assert len(document_collection_queries) == 1
        group_by_clause = "group by document_collections.collection_id"
        assert group_by_clause in document_collection_queries[0]