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.

3.5 KB
Raw Blame
"""Direct contract test for ``_classify_error`` -- runs without MCP.

``_classify_error`` has no runtime dependency on the optional ``mcp``
package. The public-tool tests in
``test_policy_denied.py`` / ``test_validation.py`` import the full
``mcp/server.py`` module (which imports ``mcp.server.fastmcp`` at module
level) and are therefore gated behind ``MCP_AVAILABLE``. This file stubs
the optional MCP import so the classifier contract can be verified in any
environment -- in particular, pinning that the non-policy test input
``"connection refused"`` maps to ``connection_error`` and not ``unknown``.

The stub only satisfies the module-level import-time reference to
``FastMCP``; the classifier function itself never touches it.
"""

import sys
from collections.abc import Callable
from unittest.mock import MagicMock

import pytest
from _pytest.fixtures import SubRequest


@pytest.fixture
def classify_error(request: SubRequest) -> Callable[[str], str]:
    """Load ``_classify_error`` without requiring the real MCP package.

    Stubs ``mcp``, ``mcp.server``, and ``mcp.server.fastmcp`` in
    ``sys.modules`` for the duration of the test, then imports (or re-uses)
    the classifier from ``mcp/server.py``. Stubs are removed in teardown so
    other tests' ``try: import mcp`` detection is unaffected.
    """
    stubs: dict[str, MagicMock] = {}
    for mod in ("mcp", "mcp.server", "mcp.server.fastmcp"):
        if mod not in sys.modules:
            stubs[mod] = MagicMock()
            sys.modules[mod] = stubs[mod]
    try:
        from local_deep_research.mcp.server import _classify_error

        yield _classify_error
    finally:
        for mod in stubs:
            sys.modules.pop(mod, None)


def test_classify_connection_refused(classify_error):
    """'connection refused' maps to connection_error via the classifier's
    ``"connection" in error_lower`` branch. This pins the input used by
    ``test_non_policy_exception_keeps_existing_classification`` so it does
    not silently fall through to 'unknown'."""
    assert classify_error("connection refused") == "connection_error"


def test_classify_network_unreachable_is_unknown(classify_error):
    """Negative control: 'network unreachable' does NOT contain the
    classifier's 'connection' substring and therefore maps to 'unknown'.
    This is the latent bug in the original test input -- the classifier
    behaviour is intentional (substring-based), the test input was wrong."""
    assert classify_error("network unreachable") == "unknown"


@pytest.mark.parametrize(
    ("status_code", "expected"),
    [
        ("401", "auth_error"),
        ("404", "model_not_found"),
        ("429", "rate_limit"),
        ("503", "service_unavailable"),
    ],
)
def test_standalone_http_status_codes_are_classified(
    classify_error, status_code, expected
):
    assert classify_error(f"HTTP error {status_code}") == expected


@pytest.mark.parametrize("status_code", ["401", "404", "429", "503"])
def test_status_digits_inside_a_thread_id_are_not_http_codes(
    classify_error, status_code
):
    """An incidental status-code substring in an OS identifier is not HTTP.

    The MCP thread-context RuntimeError includes ``threading.get_ident()``.
    A worker whose numeric ID happened to contain ``404`` made the same test
    alternate between ``unknown`` and ``model_not_found`` across CI runs.
    """
    message = (
        "Database access attempted from background thread "
        f"(ID: 123{status_code}456) with no request context"
    )
    assert classify_error(message) == "unknown"