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.

4.8 KB
Raw Blame
"""
Smoke tests for the parallel-indexing wiring in the routes + scheduler.

Heavy mock-driven route integration tests would have to patch lazy
imports (``from ...database.session_context import get_user_db_session``
inside worker functions), which isn't reliable across the
``unittest.mock.patch`` boundary in this test setup. Instead, this
file pins four observable contracts:

  1. ``LibraryRAGService.index_documents_parallel`` exists with the
     documented signature so the six call sites can rely on it.
  2. ``_background_index_worker`` accepts a ``max_workers`` parameter
     so the request-context caller can forward the resolved setting.
  3. ``_auto_index_documents_worker`` accepts a ``max_workers``
     parameter for the same reason.
  4. The default ``rag.indexing_max_parallel_docs`` value in the
     settings JSON is in the documented range — defaults shape user
     experience; if this drifts we want a test to scream.

Detailed behavioural coverage of the parallel helper lives in
``test_index_documents_parallel.py``.
"""

import inspect

import pytest


class TestParallelHelperSignature:
    """The route-side wiring relies on this exact signature being stable."""

    def test_index_documents_parallel_exists_with_expected_signature(self):
        from local_deep_research.research_library.services.library_rag_service import (
            LibraryRAGService,
        )

        assert hasattr(LibraryRAGService, "index_documents_parallel")
        sig = inspect.signature(LibraryRAGService.index_documents_parallel)
        params = sig.parameters

        # ``doc_info`` is the only positional (after self).
        assert "doc_info" in params
        assert "collection_id" in params
        # Keyword-only behaviour we'd break if changed carelessly.
        assert "force_reindex" in params
        assert "max_workers" in params
        assert "progress_callback" in params
        assert "is_cancelled" in params

    def test_index_documents_parallel_max_workers_defaults_to_four(self):
        """A regression-safe default so a missing kwargs argument still works."""
        from local_deep_research.research_library.services.library_rag_service import (
            LibraryRAGService,
        )

        sig = inspect.signature(LibraryRAGService.index_documents_parallel)
        assert sig.parameters["max_workers"].default == 4


class TestWorkerParameters:
    """The route resolves max_workers and forwards it; the worker must accept it."""

    def test_background_worker_accepts_max_workers(self):
        from local_deep_research.web.routers.rag import (
            _background_index_worker,
        )

        sig = inspect.signature(_background_index_worker)
        assert "max_workers" in sig.parameters
        # Default is intentionally permissive; the route is the
        # one that knows the user's preference.
        assert sig.parameters["max_workers"].default == 4

    def test_auto_index_worker_accepts_max_workers(self):
        from local_deep_research.web.routers.rag import (
            _auto_index_documents_worker,
        )

        sig = inspect.signature(_auto_index_documents_worker)
        assert "max_workers" in sig.parameters
        assert sig.parameters["max_workers"].default == 4


@pytest.mark.parametrize(
    "key,expected_default",
    [
        ("rag.indexing_max_parallel_docs", 4),
        ("rag.indexing_batch_size", 15),
    ],
)
class TestSettingDefaults:
    """Defaults in the settings JSON drive first-run UX."""

    def test_default_value_matches_spec(self, key, expected_default):
        """Verify the JSON default matches the user-visible spec."""
        import json
        from pathlib import Path

        settings_path = (
            Path(__file__).resolve().parents[3]
            / "src/local_deep_research/defaults/research_library/library_settings.json"
        )
        with settings_path.open(encoding="utf-8") as fh:
            settings = json.load(fh)
        assert key in settings, f"missing setting: {key}"
        assert settings[key]["value"] == expected_default, (
            f"{key} default drifted: got {settings[key]['value']}, "
            f"want {expected_default}; update docs/CONFIGURATION.md too."
        )

    def test_default_min_value_is_one(self, key, expected_default):
        """A worker count below 1 makes no semantic sense; clamp protects us."""
        import json
        from pathlib import Path

        settings_path = (
            Path(__file__).resolve().parents[3]
            / "src/local_deep_research/defaults/research_library/library_settings.json"
        )
        with settings_path.open(encoding="utf-8") as fh:
            settings = json.load(fh)
        assert settings[key]["min_value"] >= 1, (
            f"{key} min_value < 1 — index_documents_parallel's "
            "max(1, ...) clamp would be a silent UX surprise."
        )