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.7 KB
Raw Blame
"""The timeout hint must name a real control and not misdirect.

The hint is the only user-facing text that explains what to do about a
timeout, and nothing pinned it before: deleting it, or restoring the
earlier ``Settings -> LLM -> Request timeout`` wording, left every other
test green. These tests fail on either.

No network: ``openai`` and ``httpx`` exceptions are constructed directly.
"""

import httpx
import pytest
from openai import APIConnectionError, APITimeoutError

from local_deep_research.error_handling.openai_compat_errors import (
    friendly_openai_compatible_error,
)
from local_deep_research.llm.providers._helpers import CONNECT_TIMEOUT_SECONDS
from local_deep_research.settings.manager import SettingsManager


def _req():
    return httpx.Request("POST", "http://localhost:8000/v1/chat/completions")


def _message(exc):
    return friendly_openai_compatible_error(
        exc,
        provider="vllm",
        base_url="http://localhost:8000/v1",
        model="llama-3-8b",
    )


def _chain(outer, *causes):
    current = outer
    for cause in causes:
        current.__cause__ = cause
        current = cause
    return outer


@pytest.fixture(scope="module")
def ui_label():
    """The label the settings UI actually renders for the knob."""
    defaults = SettingsManager(db_session=None).default_settings
    return defaults["llm.request_timeout"]["name"]


class TestReadTimeoutHint:
    def test_the_hint_names_the_label_the_ui_renders(self, ui_label):
        """A user has to be able to find the control by that name."""
        # Given
        exc = _chain(
            APITimeoutError(request=_req()),
            httpx.ReadTimeout("timed out"),
        )

        # When
        message = _message(exc)

        # Then
        assert ui_label == "LLM Request Inactivity Timeout (seconds)"
        assert ui_label in message

    def test_the_hint_names_the_setting_key_and_environment_variable(self):
        # Given
        exc = _chain(
            APITimeoutError(request=_req()),
            httpx.ReadTimeout("timed out"),
        )

        # When
        message = _message(exc)

        # Then
        assert "llm.request_timeout" in message
        assert "LDR_LLM_REQUEST_TIMEOUT" in message

    def test_no_settings_navigation_path_is_invented(self):
        """``Settings -> LLM -> Request timeout`` is not a real path.

        Restoring that wording fails here.
        """
        # Given
        exc = _chain(
            APITimeoutError(request=_req()),
            httpx.ReadTimeout("timed out"),
        )

        # When
        message = _message(exc)

        # Then
        assert "Request timeout" not in message
        assert "Settings ->" not in message


class TestConnectTimeoutIsNotMisdirected:
    """A connect timeout arrives as ``APITimeoutError`` too.

    ``openai._base_client`` wraps every ``httpx.TimeoutException`` —
    ``ConnectTimeout`` included — in ``APITimeoutError``, so it takes the
    same branch as a read timeout. Without the connect sentence the hint
    tells a user whose endpoint is unreachable to raise a setting that
    cannot affect them. Deleting that sentence fails both tests here.
    """

    def _connect_timeout(self):
        return _chain(
            APITimeoutError(request=_req()),
            httpx.ConnectTimeout("timed out"),
        )

    def test_the_user_is_told_the_setting_does_not_move_the_connect_bound(
        self,
    ):
        # Given
        exc = self._connect_timeout()

        # When
        message = _message(exc)

        # Then
        assert "Error type: openai_timeout" in message
        assert "Connection attempts are bounded at most at 5 seconds" in message
        assert "will not help if the endpoint cannot be reached" in message

    def test_the_stated_connect_bound_matches_the_shipped_constant(self):
        """Raising CONNECT_TIMEOUT_SECONDS must break this, not go unsaid."""
        # Given
        exc = self._connect_timeout()

        # When
        message = _message(exc)

        # Then
        assert CONNECT_TIMEOUT_SECONDS == 5.0
        assert f"{CONNECT_TIMEOUT_SECONDS:g} seconds" in message


class TestNonTimeoutFailures:
    def test_a_refused_connection_gets_no_timeout_hint(self):
        """Nothing about a raisable timeout when nothing timed out."""
        # Given
        exc = _chain(
            APIConnectionError(message="Connection error.", request=_req()),
            httpx.ConnectError("All connection attempts failed"),
        )

        # When
        message = _message(exc)

        # Then
        assert "Error type: openai_connection_refused" in message
        assert "llm.request_timeout" not in message
        assert "LDR_LLM_REQUEST_TIMEOUT" not in message