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.

28.7 KB
Raw Blame
"""
Tests for research_service core functionality.

Tests cover:
- Research process validation
- Settings context setup
- LLM instantiation
- Search engine setup
- Research analysis phase
"""

from unittest.mock import Mock, patch
import pytest

from local_deep_research.settings.manager import SnapshotSettingsContext
from local_deep_research.web.services.research_service import (
    run_research_process,
)
from tests.web.services.helpers import (
    run_quick_mode_with_analyze_result,
    run_quick_mode_with_search_error,
)


class TestResearchProcessValidation:
    """Tests for research process input validation.

    These call the real ``run_research_process`` directly. Its username
    check (``if not username: raise ValueError(...)``) runs before any
    thread/DB/search setup, so it is reachable with no mocking at all --
    see research_service.py's ``run_research_process``, first lines.
    """

    def test_run_research_process_missing_username_raises_value_error(self):
        """Username is required - a missing username kwarg raises."""
        with pytest.raises(ValueError, match="Username is required"):
            run_research_process(123, "test query", "quick")

    def test_run_research_process_empty_username_raises_value_error(self):
        """Username is required - empty string raises."""
        with pytest.raises(ValueError, match="Username is required"):
            run_research_process(123, "test query", "quick", username="")

    def test_run_research_process_none_username_raises_value_error(self):
        """Username is required - explicit None raises."""
        with pytest.raises(ValueError, match="Username is required"):
            run_research_process(123, "test query", "quick", username=None)

    def test_run_research_process_valid_username_proceeds(self):
        """A non-empty username does not trip the "Username is required"
        guard (it may still fail later for unrelated reasons in this
        unmocked environment -- e.g. no DB/search configured -- but that
        is a different, later failure than the one this test pins)."""
        try:
            run_research_process(
                123, "test query", "quick", username="validuser"
            )
        except ValueError as e:
            assert "Username is required" not in str(e), (
                "a valid username should not trip the username-required "
                f"guard, but raised: {e}"
            )
        except Exception:
            # Any other exception is a later-stage failure (unmocked
            # DB/search), which is fine -- this test only pins the
            # username guard itself.
            pass

    def test_run_research_process_whitespace_username_validation(self):
        """Documents actual (not assumed) behavior: a whitespace-only
        username is truthy in Python, so ``if not username:`` does NOT
        treat it as missing -- run_research_process does not raise the
        "Username is required" error for "   ". This is current,
        unchanged behavior (not something this test asserts SHOULD be
        true); if the guard is ever tightened to also reject blank-after-
        strip usernames, this test's expectation must flip along with it.
        """
        try:
            run_research_process(123, "test query", "quick", username="   ")
        except ValueError as e:
            assert "Username is required" not in str(e), (
                "whitespace-only username unexpectedly tripped the "
                f"username-required guard: {e}"
            )
        except Exception:
            pass

    @pytest.mark.skip(
        reason=(
            "self-referential: asserted on a `termination_flags` dict "
            "built inside the test body, never passed into "
            "run_research_process. That kwarg is Flask-era plumbing -- "
            "grep confirms `termination_flags` is not referenced anywhere "
            "in research_service.py on this branch; termination is now "
            "tracked via web.research_state.is_termination_requested(), "
            "keyed by research_id in module state, not a caller-supplied "
            "dict. No production symbol corresponds to this test's premise."
        )
    )
    def test_run_research_process_research_id_validation(self):
        """Research ID is tracked correctly."""

    @pytest.mark.skip(
        reason=(
            "self-referential and tautological: builds a `query` string "
            'locally and asserts `"<script>" in query` against that same '
            "string -- true by construction regardless of any production "
            "code. run_research_process passes the query straight through "
            "to AdvancedSearchSystem with no sanitization step to pin, so "
            "there is no production symbol this could bind to."
        )
    )
    def test_run_research_process_query_sanitization(self):
        """Query with special characters is handled."""

    @pytest.mark.skip(
        reason=(
            "self-referential and tautological: `for mode in valid_modes: "
            "assert mode in valid_modes` is true for any list by "
            "construction. run_research_process does branch on "
            "mode == 'quick' vs other, but that branching is covered "
            "behaviorally elsewhere (e.g. TestResearchAnalysisPhase below, "
            "and test_research_service_progress_integration.py); this "
            "test's own assertion cannot fail no matter what the "
            "production code does."
        )
    )
    def test_run_research_process_mode_validation(self):
        """Both quick and detailed modes are valid."""


class TestSettingsContextSetup:
    """Tests for settings context initialization in research threads."""

    def test_settings_context_from_snapshot_dict(self):
        """SettingsContext extracts values from dictionary snapshot."""
        # Import the internal class by running code that defines it
        snapshot = {
            "llm.provider": "ollama",
            "llm.model": "mistral",
            "search.tool": "google",
        }

        ctx = SnapshotSettingsContext(snapshot, username="testuser")

        assert ctx.get_setting("llm.provider") == "ollama"
        assert ctx.get_setting("llm.model") == "mistral"
        assert ctx.get_setting("search.tool") == "google"

    def test_settings_context_from_snapshot_full_objects(self):
        """SettingsContext extracts values from full setting objects."""
        snapshot = {
            "llm.provider": {"value": "openai", "ui_element": "select"},
            "llm.model": {"value": "gpt-4", "ui_element": "text"},
        }

        ctx = SnapshotSettingsContext(snapshot, username="testuser")

        assert ctx.get_setting("llm.provider") == "openai"
        assert ctx.get_setting("llm.model") == "gpt-4"

    def test_settings_context_extract_value_from_full_setting(self):
        """SettingsContext properly extracts value field from setting objects."""
        snapshot = {
            "llm.temperature": {"value": 0.5, "ui_element": "slider"},
            "llm.max_tokens": {"value": 4096, "ui_element": "number"},
        }

        ctx = SnapshotSettingsContext(snapshot, username="testuser")

        assert ctx.get_setting("llm.temperature") == 0.5
        assert ctx.get_setting("llm.max_tokens") == 4096

    def test_settings_context_malformed_snapshot_handling(self):
        """SettingsContext handles malformed snapshots gracefully."""
        snapshot = {
            "valid.key": "value",
            "malformed.dict": {"not_value": "test"},  # Missing 'value' key
            "another.key": None,
        }

        ctx = SnapshotSettingsContext(snapshot, username="testuser")

        assert ctx.get_setting("valid.key") == "value"
        # Malformed dict stored as-is (no 'value' key)
        assert ctx.get_setting("malformed.dict") == {"not_value": "test"}
        assert ctx.get_setting("another.key") is None

    def test_settings_context_empty_snapshot_uses_defaults(self):
        """SettingsContext returns defaults for empty snapshot."""

        ctx = SnapshotSettingsContext({}, username="testuser")

        assert (
            ctx.get_setting("nonexistent.key", "default_value")
            == "default_value"
        )
        assert ctx.get_setting("another.missing", 42) == 42

    def test_settings_context_missing_key_uses_default(self):
        """SettingsContext returns default when key not found."""
        snapshot = {"existing.key": "value"}

        ctx = SnapshotSettingsContext(snapshot, username="testuser")

        assert ctx.get_setting("missing.key", "fallback") == "fallback"
        assert ctx.get_setting("missing.key") is None

    def test_settings_context_type_conversion_during_setup(self):
        """SettingsContext preserves types from snapshot."""
        snapshot = {
            "int_value": {"value": 100},
            "float_value": {"value": 3.14},
            "bool_value": {"value": True},
            "list_value": {"value": [1, 2, 3]},
            "dict_value": {"value": {"nested": "data"}},
        }

        ctx = SnapshotSettingsContext(snapshot, username="testuser")

        assert ctx.get_setting("int_value") == 100
        assert ctx.get_setting("float_value") == 3.14
        assert ctx.get_setting("bool_value") is True
        assert ctx.get_setting("list_value") == [1, 2, 3]
        assert ctx.get_setting("dict_value") == {"nested": "data"}

    def test_settings_context_thread_local_isolation(self):
        """SettingsContext maintains isolation for different users."""

        ctx1 = SnapshotSettingsContext({"key": "user1_value"}, username="user1")
        ctx2 = SnapshotSettingsContext({"key": "user2_value"}, username="user2")

        assert ctx1.get_setting("key") == "user1_value"
        assert ctx2.get_setting("key") == "user2_value"
        assert ctx1.username == "user1"
        assert ctx2.username == "user2"

    def test_settings_context_cleanup_on_error(self):
        """SettingsContext handles errors gracefully during initialization."""

        # None snapshot should be handled
        ctx = SnapshotSettingsContext(None, username="testuser")
        assert ctx.get_setting("any.key", "default") == "default"

    def test_settings_context_nested_settings_extraction(self):
        """SettingsContext handles nested key structures."""
        snapshot = {
            "llm.provider": {"value": "openai"},
            "llm.openai.api_key": {"value": "sk-test"},
            "llm.openai.model": {"value": "gpt-4"},
            "search.google.api_key": {"value": "google-key"},
        }

        ctx = SnapshotSettingsContext(snapshot, username="testuser")

        assert ctx.get_setting("llm.provider") == "openai"
        assert ctx.get_setting("llm.openai.api_key") == "sk-test"
        assert ctx.get_setting("llm.openai.model") == "gpt-4"
        assert ctx.get_setting("search.google.api_key") == "google-key"


class TestLLMInstantiation:
    """Tests for LLM instantiation during research.

    Note: These tests verify the LLM configuration logic without making
    actual API calls or requiring external services.
    """

    @pytest.mark.skip(
        reason=(
            "self-referential: builds `provider`/`is_available` locals and "
            "asserts on an if/else computed from them, calling no "
            "production code. There is also no retry/fallback-on-provider- "
            "unavailability mechanism in get_llm() to bind to -- "
            "llm_config.py's get_llm() calls provider.create_llm() once, "
            "with no `is_available()`-gated retry path (confirmed by "
            "reading get_llm() top to bottom: no `fallback`/`retry` token "
            "appears in the file). The nearest real behavior -- "
            "classifying a *search-time* Ollama failure -- lives in "
            "run_research_process and is covered by TestErrorClassification "
            "in test_research_service_execution.py."
        )
    )
    def test_llm_instantiation_success_logic(self):
        """LLM instantiation success scenario."""

    @pytest.mark.skip(
        reason=(
            "self-referential, and its premise ('LLM instantiation' retries "
            "on a 503) does not correspond to any real code path -- see "
            "test_llm_instantiation_success_logic's skip reason. The real "
            "503-classification logic runs when a *search* fails, not "
            "during LLM construction; it is covered by "
            "TestErrorClassification in test_research_service_execution.py."
        )
    )
    def test_llm_instantiation_ollama_503_error_logic(self):
        """LLM handles Ollama 503 service unavailable."""

    @pytest.mark.skip(
        reason=(
            "self-referential: builds `model_name`/`available_models` "
            "locals and asserts on a membership check against its own "
            "list -- calls no production code. get_llm() does not "
            "validate model_name against a fetched list of available "
            "Ollama models before constructing the client."
        )
    )
    def test_llm_instantiation_ollama_404_model_not_found_logic(self):
        """LLM handles Ollama 404 model not found."""

    @pytest.mark.skip(
        reason=(
            "self-referential, same unfounded 'LLM instantiation retries' "
            "premise as test_llm_instantiation_success_logic. The real "
            "connection-error classification is search-time, not "
            "instantiation-time, and is covered by TestErrorClassification "
            "in test_research_service_execution.py."
        )
    )
    def test_llm_instantiation_connection_timeout_logic(self):
        """LLM handles connection timeout."""

    @pytest.mark.skip(
        reason=(
            "self-referential: builds `provider`/`api_key` locals and "
            "asserts on an if/else computed from them. get_llm() does not "
            "itself check for a missing API key and 'use a fallback' -- "
            "each cloud provider's create_llm() raises its own "
            "not-configured error (e.g. openai_base.py's pattern), which "
            "is a different, provider-specific contract than the boolean "
            "'should_use_fallback' this test invents."
        )
    )
    def test_llm_instantiation_api_key_missing_logic(self):
        """LLM handles missing API key for cloud providers."""

    @patch("local_deep_research.config.llm_config.get_setting_from_snapshot")
    def test_llm_instantiation_invalid_provider(self, mock_get_setting):
        """get_llm raises for invalid provider."""
        from local_deep_research.config.llm_config import get_llm

        mock_get_setting.side_effect = lambda key, default=None, **kwargs: {
            "llm.model": "model",
            "llm.temperature": 0.7,
            "llm.provider": "invalid_provider",
        }.get(key, default)

        # A (default-scope) snapshot is required so the egress-policy PEP —
        # which fails closed for snapshot-less non-local providers — lets the
        # call reach the provider-name validation this test exercises.
        with pytest.raises(ValueError) as exc_info:
            get_llm(
                provider="invalid_provider",
                settings_snapshot={"search.tool": "searxng"},
            )

        assert "Invalid provider" in str(exc_info.value)

    @pytest.mark.skip(
        reason=(
            "self-referential: `override_model if override_model else "
            "settings_model` is evaluated and asserted on directly, not "
            "produced by get_llm(). The real precedence (`if model_name is "
            "None: model_name = get_setting_from_snapshot(...)`) does "
            "exist in llm_config.get_llm(), but exercising it end-to-end "
            "requires either a live provider registry or mocking "
            "is_llm_registered/get_llm_from_registry deeply enough that "
            "the resulting test would mostly be pinning the mock, not the "
            "precedence rule -- not attempted here; left as a gap rather "
            "than a manufactured pass."
        )
    )
    def test_llm_instantiation_model_name_override_logic(self):
        """Model name override takes precedence over settings."""

    @pytest.mark.skip(
        reason=(
            "self-referential, same reasoning as "
            "test_llm_instantiation_model_name_override_logic: the real "
            "`if temperature is None: temperature = get_setting(...)` "
            "precedence lives in get_llm(), but reaching it requires the "
            "same disproportionate provider-registry mocking."
        )
    )
    def test_llm_instantiation_temperature_setting_logic(self):
        """Temperature setting is applied correctly."""

    def test_llm_instantiation_context_window_calculation_logic(self):
        """Context window is calculated correctly for local providers.

        Calls the real ``get_context_window_for_provider`` helper (the
        function llm_config.get_llm() and every provider's create_llm()
        actually use — see llm/providers/_helpers.py) instead of a local
        if/else copy of its local-vs-cloud branch.
        """
        from local_deep_research.llm.providers._helpers import (
            get_context_window_for_provider,
        )

        assert (
            get_context_window_for_provider("ollama", settings_snapshot={})
            == 8192
        )

    def test_llm_instantiation_thinking_mode_detection_logic(self):
        """Thinking mode is configured for supported models.

        Calls the real ``OllamaProvider.create_llm`` and reads the
        ``reasoning`` kwarg it passes to ``ChatOllama`` — that is where
        ``llm.ollama.enable_thinking`` actually takes effect (see
        llm/providers/implementations/ollama.py).
        """
        from local_deep_research.llm.providers.implementations.ollama import (
            OllamaProvider,
        )

        snapshot = {
            "llm.ollama.url": "http://localhost:11434",
            "llm.ollama.enable_thinking": True,
        }
        with patch(
            "local_deep_research.llm.providers.implementations.ollama.ChatOllama"
        ) as mock_chat_ollama:
            OllamaProvider.create_llm(
                model_name="deepseek-r1",
                temperature=0.5,
                settings_snapshot=snapshot,
            )
        assert mock_chat_ollama.call_args.kwargs["reasoning"] is True

    def test_llm_max_tokens_calculation(self):
        """Max tokens is calculated as 80% of context window.

        Calls the real ``compute_max_tokens`` helper instead of a local
        copy of its ``min(raw, int(context_window * 0.8))`` cap.
        """
        from local_deep_research.llm.providers._helpers import (
            compute_max_tokens,
        )

        snapshot = {
            "llm.supports_max_tokens": True,
            "llm.max_tokens": 100000,
        }
        max_tokens = compute_max_tokens(
            settings_snapshot=snapshot, context_window_size=4096
        )
        assert max_tokens == int(4096 * 0.8)  # 3276

    def test_llm_provider_normalization(self):
        """Provider name is normalized to lowercase.

        Calls the real ``normalize_provider`` (llm/providers/base.py) —
        the single function every provider comparison in this codebase is
        supposed to route through — instead of asserting a `.lower()` call
        against itself.
        """
        from local_deep_research.llm.providers.base import normalize_provider

        assert normalize_provider("OLLAMA") == "ollama"
        assert normalize_provider("OpenAI") == "openai"
        assert normalize_provider("Anthropic") == "anthropic"
        assert normalize_provider(None) is None


class TestSearchEngineSetup:
    """Tests for search engine setup during research."""

    @patch("local_deep_research.config.search_config.get_search")
    def test_search_engine_creation_success(self, mock_get_search):
        """get_search successfully creates search engine."""
        mock_search = Mock()
        mock_get_search.return_value = mock_search

        from local_deep_research.config.search_config import get_search

        result = get_search(
            search_tool="google",
            llm_instance=Mock(),
            username="testuser",
        )

        assert result == mock_search

    @patch("local_deep_research.config.search_config.get_search")
    def test_search_engine_creation_failure_fallback(self, mock_get_search):
        """get_search handles creation failure."""
        mock_get_search.side_effect = Exception("Search engine error")

        from local_deep_research.config.search_config import get_search

        with pytest.raises(Exception) as exc_info:
            get_search(search_tool="invalid", llm_instance=Mock())

        assert "Search engine error" in str(exc_info.value)

    @patch("local_deep_research.config.search_config.get_search")
    def test_search_engine_with_llm_instance(self, mock_get_search):
        """get_search passes LLM instance correctly."""
        mock_search = Mock()
        mock_get_search.return_value = mock_search
        mock_llm = Mock()

        from local_deep_research.config.search_config import get_search

        get_search(
            search_tool="google",
            llm_instance=mock_llm,
            username="testuser",
        )

        mock_get_search.assert_called_once()
        call_kwargs = mock_get_search.call_args
        assert call_kwargs[1].get("llm_instance") == mock_llm

    @patch("local_deep_research.config.search_config.get_search")
    def test_search_engine_settings_propagation(self, mock_get_search):
        """get_search propagates settings snapshot."""
        mock_search = Mock()
        mock_get_search.return_value = mock_search
        settings = {"search.max_results": 10}

        from local_deep_research.config.search_config import get_search

        get_search(
            search_tool="google",
            llm_instance=Mock(),
            settings_snapshot=settings,
        )

        call_kwargs = mock_get_search.call_args
        assert call_kwargs[1].get("settings_snapshot") == settings

    @patch("local_deep_research.config.search_config.get_search")
    def test_search_engine_cache_integration(self, mock_get_search):
        """get_search integrates with cache system."""
        mock_search = Mock()
        mock_get_search.return_value = mock_search

        from local_deep_research.config.search_config import get_search

        # Should not raise
        result = get_search(
            search_tool="google",
            llm_instance=Mock(),
        )

        assert result is not None

    @patch("local_deep_research.config.search_config.get_search")
    def test_search_engine_rate_limiting_config(self, mock_get_search):
        """get_search applies rate limiting configuration."""
        mock_search = Mock()
        mock_get_search.return_value = mock_search

        from local_deep_research.config.search_config import get_search

        get_search(
            search_tool="google",
            llm_instance=Mock(),
        )

        # Search should be created
        mock_get_search.assert_called_once()

    @patch("local_deep_research.config.search_config.get_search")
    def test_search_engine_invalid_config_handling(self, mock_get_search):
        """get_search handles invalid configuration."""
        mock_get_search.side_effect = ValueError("Invalid search configuration")

        from local_deep_research.config.search_config import get_search

        with pytest.raises(ValueError) as exc_info:
            get_search(
                search_tool="invalid_engine",
                llm_instance=Mock(),
            )

        assert "Invalid search configuration" in str(exc_info.value)

    @patch("local_deep_research.config.search_config.get_search")
    def test_search_engine_timeout_configuration(self, mock_get_search):
        """get_search applies timeout configuration."""
        mock_search = Mock()
        mock_get_search.return_value = mock_search

        from local_deep_research.config.search_config import get_search

        result = get_search(
            search_tool="google",
            llm_instance=Mock(),
        )

        assert result is not None


class TestResearchAnalysisPhase:
    """Tests for research analysis phase."""

    def test_analysis_phase_success(self):
        """Analysis phase completes successfully with results.

        Calls the real ``run_research_process`` (via the shared quick-mode
        harness) with ``analyze_topic`` returning these exact results, and
        asserts on what the production code does with them -- the clean
        markdown handed to the citation formatter -- instead of asserting
        that a locally-built Mock returns what it was told to return.
        """
        result = run_quick_mode_with_analyze_result(
            {
                "findings": [
                    {"content": "Test finding", "phase": "Final synthesis"}
                ],
                "formatted_findings": "# Test Results\n\nTest finding",
                "iterations": 3,
                "current_knowledge": "",
            }
        )
        assert result == {"clean_markdown": "Test finding"}

    def test_analysis_phase_ollama_unavailable_error_classification(self):
        """Analysis phase classifies Ollama unavailable errors.

        Drives the real search-error classification in
        run_research_process (research_service.py's
        ``except Exception as search_error`` / ``except Exception as e``
        pair) via the shared harness, instead of a local copy of the
        `if "status code: 503" in error_message` check.
        """
        message = run_quick_mode_with_search_error(
            "Request failed with status code: 503"
        )
        assert "Ollama AI service is unavailable" in message

    def test_analysis_phase_model_not_found_error_classification(self):
        """Analysis phase classifies model not found errors."""
        message = run_quick_mode_with_search_error("status code: 404 not found")
        assert "Ollama model not found" in message or (
            "model not found" in message.lower()
        )

    def test_analysis_phase_connection_error_classification(self):
        """Analysis phase classifies connection errors."""
        message = run_quick_mode_with_search_error(
            "Connection refused: localhost:11434"
        )
        assert "Connection error" in message

    def test_analysis_phase_api_error_classification(self):
        """Analysis phase classifies API errors."""
        message = run_quick_mode_with_search_error(
            "status code: 500 internal error"
        )
        assert "language model API rejected the request" in message

    def test_analysis_phase_error_message_transformation(self):
        """Analysis phase transforms error messages to user-friendly format.

        The original (vacuous) version of this test asserted that the raw
        "HTTP 503" detail survived into the user-facing message. The real
        code deliberately does NOT do that: the second classification
        stage (research_service.py ~2667) replaces the first stage's
        "(HTTP 503)"-bearing message with a scrubbed category string
        before it reaches ErrorReportGenerator / the client (CWE-209 --
        raw provider text can carry internal hosts/paths). This test now
        pins the ACTUAL user-facing behavior instead of the assumed one.
        """
        message = run_quick_mode_with_search_error(
            "Request failed with status code: 503"
        )
        assert "Ollama AI service is unavailable" in message
        assert "HTTP 503" not in message, (
            "raw status-code detail should not reach the user-facing "
            "message (CWE-209) -- if this now fails, either the scrubbing "
            "was removed (a regression) or deliberately relaxed (update "
            "this test's expectation to match)"
        )

    def test_analysis_phase_partial_results_handling(self):
        """Analysis phase handles partial results.

        Drives the real synthesis-fallback logic (research_service.py
        ~1736-1900) via the shared harness: an error-shaped
        ``formatted_findings`` with one valid and one error-shaped
        finding should fall back to a "Fallback Mode" report built only
        from the valid finding, not the local re-filter this test used
        to perform on its own dict.
        """
        result = run_quick_mode_with_analyze_result(
            {
                "findings": [
                    {"content": "Finding 1", "phase": "search"},
                    {"content": "Error: LLM failed", "phase": "synthesis"},
                ],
                "formatted_findings": "Error: Final synthesis failed",
                "iterations": 2,
                "current_knowledge": "",
            }
        )
        markdown = result["clean_markdown"]
        assert "Fallback Mode" in markdown
        assert "Finding 1" in markdown
        assert "Error: LLM failed" not in markdown, (
            "the error-shaped finding should have been filtered out of "
            "the fallback, leaving only the valid finding"
        )