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.

7.8 KB
Raw Blame
import pytest

from local_deep_research.settings.env_registry import get_env_setting
from local_deep_research.settings.env_settings import (
    IntegerSetting,
    SettingsRegistry,
)
from local_deep_research.settings.exceptions import (
    EnvironmentValueRangeError,
    InvalidEnvironmentValueError,
)


_TRUSTED_PROXY_ENV_VAR = "LDR_SERVER_TRUSTED_PROXY_COUNT"


def test_trusted_proxy_count_raises_specific_error_for_malformed_value(
    monkeypatch,
):
    # Given
    monkeypatch.setenv(_TRUSTED_PROXY_ENV_VAR, "not-an-integer")

    # When / Then
    with pytest.raises(InvalidEnvironmentValueError) as error:
        get_env_setting("server.trusted_proxy_count", 0)
    assert error.value.env_var == _TRUSTED_PROXY_ENV_VAR
    assert str(error.value) == (
        "LDR_SERVER_TRUSTED_PROXY_COUNT: value 'not-an-integer' is not a valid integer"
    )
    assert isinstance(error.value.__cause__, ValueError)


def test_trusted_proxy_count_raises_range_error_for_negative_value(monkeypatch):
    # Given
    monkeypatch.setenv(_TRUSTED_PROXY_ENV_VAR, "-1")

    # When / Then
    with pytest.raises(EnvironmentValueRangeError) as error:
        get_env_setting("server.trusted_proxy_count", 0)
    assert error.value.env_var == _TRUSTED_PROXY_ENV_VAR
    assert str(error.value) == (
        "LDR_SERVER_TRUSTED_PROXY_COUNT: value -1 is below minimum 0"
    )


@pytest.mark.parametrize("configured_count", ["0", "2"])
def test_trusted_proxy_count_returns_valid_non_negative_value(
    monkeypatch, configured_count
):
    # Given
    monkeypatch.setenv(_TRUSTED_PROXY_ENV_VAR, configured_count)

    # When
    value = get_env_setting("server.trusted_proxy_count", 0)

    # Then
    assert value == int(configured_count)


def test_trusted_proxy_count_uses_zero_default_when_unset(monkeypatch):
    # Given
    monkeypatch.delenv(_TRUSTED_PROXY_ENV_VAR, raising=False)

    # When
    value = get_env_setting("server.trusted_proxy_count", 0)

    # Then
    assert value == 0


def test_non_strict_integer_setting_keeps_malformed_value_fallback(monkeypatch):
    # Given
    setting = IntegerSetting(
        key="testing.non_strict_integer",
        description="Test-only non-strict integer setting.",
        default=7,
    )
    monkeypatch.setenv("LDR_TESTING_NON_STRICT_INTEGER", "not-an-integer")

    # When
    value = setting.get_value()

    # Then
    assert value == 7


@pytest.mark.parametrize("raw_value", ["not-an-integer", "-1"])
def test_registry_propagates_opted_in_integer_validation_errors(
    monkeypatch, raw_value
):
    # Given
    registry = SettingsRegistry()
    setting = IntegerSetting(
        key="testing.opted_in_integer",
        description="Test-only opted-in integer setting.",
        default=7,
        min_value=0,
        raise_on_validation_error=True,
    )
    registry.register_category("testing", [setting])
    monkeypatch.setenv("LDR_TESTING_OPTED_IN_INTEGER", raw_value)

    # When / Then
    with pytest.raises(
        (InvalidEnvironmentValueError, EnvironmentValueRangeError)
    ):
        registry.get("testing.opted_in_integer", 7)


@pytest.mark.parametrize("raw_value", ["not-an-integer", "-1"])
def test_registry_keeps_non_opted_in_integer_validation_fallback(
    monkeypatch, raw_value
):
    # Given
    registry = SettingsRegistry()
    setting = IntegerSetting(
        key="testing.non_opted_in_integer",
        description="Test-only non-opted-in integer setting.",
        default=7,
        min_value=0,
    )
    registry.register_category("testing", [setting])
    monkeypatch.setenv("LDR_TESTING_NON_OPTED_IN_INTEGER", raw_value)

    # When
    value = registry.get("testing.non_opted_in_integer", 7)

    # Then
    assert value == 7


def test_trusted_proxy_count_treats_empty_value_as_unset(monkeypatch):
    """``LDR_SERVER_TRUSTED_PROXY_COUNT=""`` must fall back to the default.

    The repo-wide contract, pinned by
    ``tests/test_config_env_var_contracts.py::
    test_empty_env_var_is_treated_as_unset_not_as_false``: "``LDR_X=""`` must
    fall back to the DB/default, not override it. Orchestrators (Unraid,
    Compose, k8s manifests) frequently emit an empty string for an
    unconfigured variable." Opting a setting into
    ``raise_on_validation_error`` must not turn that routine emission into a
    refusal to boot -- every other IntegerSetting warns and defaults.
    """
    # Given
    monkeypatch.setenv(_TRUSTED_PROXY_ENV_VAR, "")

    # When
    value = get_env_setting("server.trusted_proxy_count", 0)

    # Then
    assert value == 0


@pytest.mark.parametrize(
    "raw_value",
    [
        "1_0",  # int() reads 10; reviewers read "one hop"
        "+3",  # int() reads 3
        "٣",  # ARABIC-INDIC DIGIT THREE; int() reads 3
        "1",  # FULLWIDTH DIGIT ONE; int() reads 1
        " 2x ",
    ],
)
def test_trusted_proxy_count_rejects_non_plain_ascii_spellings(
    monkeypatch, raw_value
):
    """A trust-boundary hop count must mean what it looks like.

    ``int()`` accepts underscore separators, a leading ``+`` and any Unicode
    decimal digit, so a value that reads as one hop in review can resolve to
    ten at runtime -- silently shifting which X-Forwarded-For entry is
    believed. Reject the lot rather than normalising them.
    """
    # Given
    monkeypatch.setenv(_TRUSTED_PROXY_ENV_VAR, raw_value)

    # When / Then
    with pytest.raises(InvalidEnvironmentValueError) as error:
        get_env_setting("server.trusted_proxy_count", 0)
    assert error.value.env_var == _TRUSTED_PROXY_ENV_VAR


@pytest.mark.parametrize("raw_value", ["17", "5000", "99999999999999999999"])
def test_trusted_proxy_count_rejects_values_above_the_hop_bound(
    monkeypatch, raw_value
):
    """An absurd hop count is a typo, and silently accepting it collapses
    every client behind the proxy into the direct peer's single bucket: no
    real X-Forwarded-For chain has that many entries, so ``_select_trusted_
    hop`` returns None on every request and the limiter falls back to the
    peer. Fail at startup, naming the variable, instead."""
    # Given
    monkeypatch.setenv(_TRUSTED_PROXY_ENV_VAR, raw_value)

    # When / Then
    with pytest.raises(EnvironmentValueRangeError) as error:
        get_env_setting("server.trusted_proxy_count", 0)
    assert error.value.env_var == _TRUSTED_PROXY_ENV_VAR
    assert "above maximum 16" in str(error.value)


@pytest.mark.parametrize("raw_value", ["0", "1", "16", " 4 "])
def test_trusted_proxy_count_accepts_plain_values_in_range(
    monkeypatch, raw_value
):
    """Positive control for the two rejections above: the ordinary
    spellings, including the upper bound and surrounding whitespace, still
    resolve."""
    # Given
    monkeypatch.setenv(_TRUSTED_PROXY_ENV_VAR, raw_value)

    # When / Then
    assert get_env_setting("server.trusted_proxy_count", 0) == int(
        raw_value.strip()
    )


def test_non_strict_integer_setting_still_accepts_int_spellings(monkeypatch):
    """The strict parse is opt-in, not a change to every IntegerSetting.

    Negative control for the parametrized rejection above: a setting that
    did not ask for ``strict_digits`` keeps ``int()``'s own grammar.
    """
    # Given
    setting = IntegerSetting(
        key="testing.lenient_integer",
        description="Test-only lenient integer setting.",
        default=7,
    )
    monkeypatch.setenv("LDR_TESTING_LENIENT_INTEGER", "1_0")

    # When / Then
    assert setting.get_value() == 10


def test_non_strict_integer_setting_treats_empty_value_as_unset(monkeypatch):
    """The ``LDR_X=""`` contract applies to every IntegerSetting, not just
    the opted-in one."""
    # Given
    setting = IntegerSetting(
        key="testing.lenient_empty_integer",
        description="Test-only lenient integer setting.",
        default=7,
    )
    monkeypatch.setenv("LDR_TESTING_LENIENT_EMPTY_INTEGER", "")

    # When / Then
    assert setting.get_value() == 7