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.

25.6 KB
Raw Blame
"""Time-period settings must preserve operator policy and reject bad inputs."""

from types import SimpleNamespace
from unittest.mock import Mock, patch

import pytest

from local_deep_research.config.search_config import get_search
from local_deep_research.security.egress.policy import Decision
from local_deep_research.settings.manager import (
    apply_environment_overrides_to_snapshot,
)
from local_deep_research.utilities.type_utils import overlay_runtime_settings
from local_deep_research.web_search_engines.search_engine_factory import (
    apply_time_period_override,
)


class CapturingEngine:
    def __init__(self, time_period="y", max_results=10, **kwargs):
        self.time_period = time_period


@pytest.mark.parametrize("wrapped", [False, True])
@pytest.mark.parametrize(
    (
        "global_env",
        "per_engine_env",
        "request_period",
        "saved_per_engine",
        "expected",
    ),
    [
        pytest.param(
            "w", None, "d", "y", "w", id="global-operator-pin-survives-request"
        ),
        pytest.param(
            "w", None, None, "inherit", "w", id="global-pin-no-request-control"
        ),
        pytest.param(None, None, "d", "y", "d", id="unlocked-request-control"),
        pytest.param(None, "w", "d", "y", "w", id="per-engine-pin-control"),
        # The same rule without a request (the common case): an operator's
        # global pin outranks a per-engine choice saved in Settings, and the
        # engine's own pin outranks the global one, request or not.
        pytest.param(
            "w", None, None, "y", "w", id="global-pin-outranks-saved-choice"
        ),
        pytest.param(
            "w", "m", None, "y", "m", id="engine-pin-outranks-global-pin"
        ),
        pytest.param(
            "w", "m", "d", "y", "m", id="engine-pin-outranks-global-pin-request"
        ),
        pytest.param(
            None, None, None, "y", "y", id="saved-choice-without-any-pin"
        ),
    ],
)
def test_operator_period_dispatch(
    monkeypatch,
    global_env,
    per_engine_env,
    request_period,
    saved_per_engine,
    expected,
    wrapped,
):
    for name, value in [
        ("LDR_SEARCH_TIME_PERIOD", global_env),
        (
            "LDR_SEARCH_ENGINE_WEB_SERPAPI_DEFAULT_PARAMS_TIME_PERIOD",
            per_engine_env,
        ),
    ]:
        if value is None:
            monkeypatch.delenv(name, raising=False)
        else:
            monkeypatch.setenv(name, value)
    snapshot = {
        "search.time_period": {
            "value": "all",
            "ui_element": "select",
            "editable": True,
        },
        "search.engine.web.serpapi.default_params.time_period": {
            "value": saved_per_engine,
            "ui_element": "select",
            "editable": True,
        },
        "search.engine.library.enabled": False,
    }
    if not wrapped:
        snapshot = {
            key: value["value"] if isinstance(value, dict) else value
            for key, value in snapshot.items()
        }
    effective = apply_environment_overrides_to_snapshot(snapshot)
    overlay_runtime_settings(effective, time_period=request_period)
    if request_period is not None:
        apply_time_period_override(effective, request_period)
    with (
        patch(
            "local_deep_research.web_search_engines.search_engine_factory.retriever_registry",
            SimpleNamespace(get=lambda *a, **k: None),
        ),
        patch(
            "local_deep_research.web_search_engines.search_engine_factory.get_safe_module_class",
            return_value=CapturingEngine,
        ),
        patch(
            "local_deep_research.security.egress.policy.evaluate_engine",
            return_value=Decision(True, "local_review"),
        ),
    ):
        engine = get_search(
            "serpapi", llm_instance=Mock(), settings_snapshot=effective
        )
    assert engine is not None
    assert engine.time_period == expected


@pytest.mark.parametrize(
    ("key", "bad_value"),
    [
        (key, bad_value)
        for key in (
            "search.time_period",
            "search.engine.web.serpapi.default_params.time_period",
        )
        for bad_value in (
            "typo-private-marker",
            "",
            None,
            1,
            True,
            ["w"],
            {"period": "w"},
        )
        # A null global is "not set" (see the null-global tests below), not
        # a malformed selection: a stored null reaches the resolver as a
        # bare None once get_settings_snapshot() unwraps it.
        if not (key == "search.time_period" and bad_value is None)
    ],
)
def test_invalid_selected_period_fails_before_engine_construction(
    monkeypatch, key, bad_value
):
    from local_deep_research.web_search_engines.search_engine_factory import (
        create_search_engine,
    )

    monkeypatch.delenv("LDR_SEARCH_TIME_PERIOD", raising=False)
    monkeypatch.delenv(
        "LDR_SEARCH_ENGINE_WEB_SERPAPI_DEFAULT_PARAMS_TIME_PERIOD",
        raising=False,
    )
    snapshot = {
        "search.time_period": "d",
        "search.engine.web.serpapi.default_params.time_period": "inherit",
        "search.engine.library.enabled": False,
    }
    snapshot[key] = bad_value
    with (
        patch(
            "local_deep_research.web_search_engines.search_engine_factory.retriever_registry",
            SimpleNamespace(get=lambda *a, **k: None),
        ),
        patch(
            "local_deep_research.web_search_engines.search_engine_factory.get_safe_module_class",
            return_value=CapturingEngine,
        ) as constructor,
        patch(
            "local_deep_research.security.egress.policy.evaluate_engine",
            return_value=Decision(True, "local_review"),
        ),
        pytest.raises(ValueError) as error,
    ):
        create_search_engine("serpapi", settings_snapshot=snapshot)
    assert str(error.value) == f"{key} must be one of d, w, m, y, all"
    assert error.value.__context__ is None
    constructor.assert_not_called()


@pytest.mark.parametrize(
    "key",
    [
        "search.time_period",
        "search.engine.web.serpapi.default_params.time_period",
    ],
)
def test_invalid_operator_period_does_not_reuse_saved_choice(monkeypatch, key):
    from local_deep_research.web_search_engines.search_engine_factory import (
        create_search_engine,
    )

    monkeypatch.delenv("LDR_SEARCH_TIME_PERIOD", raising=False)
    monkeypatch.delenv(
        "LDR_SEARCH_ENGINE_WEB_SERPAPI_DEFAULT_PARAMS_TIME_PERIOD",
        raising=False,
    )
    monkeypatch.setenv(
        "LDR_" + key.replace(".", "_").upper(), "typo-private-marker"
    )
    snapshot = {
        "search.time_period": "d",
        "search.engine.web.serpapi.default_params.time_period": "inherit",
        "search.engine.library.enabled": False,
    }
    with (
        patch(
            "local_deep_research.web_search_engines.search_engine_factory.retriever_registry",
            SimpleNamespace(get=lambda *a, **k: None),
        ),
        patch(
            "local_deep_research.web_search_engines.search_engine_factory.get_safe_module_class",
            return_value=CapturingEngine,
        ) as constructor,
        patch(
            "local_deep_research.security.egress.policy.evaluate_engine",
            return_value=Decision(True, "local_review"),
        ),
        pytest.raises(ValueError) as error,
    ):
        create_search_engine("serpapi", settings_snapshot=snapshot)
    variable, choices = {
        "search.time_period": ("LDR_SEARCH_TIME_PERIOD", "d, w, m, y, all"),
        "search.engine.web.serpapi.default_params.time_period": (
            "LDR_SEARCH_ENGINE_WEB_SERPAPI_DEFAULT_PARAMS_TIME_PERIOD",
            "inherit, d, w, m, y, all",
        ),
    }[key]
    # The error names the environment variable it came from, not the key.
    assert str(error.value) == (
        f"Environment variable {variable} must be one of {choices}; "
        "case and surrounding whitespace matter"
    )
    assert "typo-private-marker" not in str(error.value)
    constructor.assert_not_called()


@pytest.mark.parametrize(
    "variable",
    [
        "LDR_SEARCH_TIME_PERIOD",
        "LDR_SEARCH_ENGINE_WEB_SERPAPI_DEFAULT_PARAMS_TIME_PERIOD",
    ],
)
@pytest.mark.parametrize(
    "value",
    ["y\r", "Y", " y", "y ", "\ty", "yy", "ALL", "Inherit", "past-year"],
)
def test_environment_pin_with_wrong_case_or_whitespace_names_the_variable(
    monkeypatch, variable, value
):
    """A value copied from a Windows-edited .env ("y" plus a carriage return)
    or typed in capitals is rejected. The error says which variable to fix
    and that case and surrounding whitespace matter, without echoing it."""
    from local_deep_research.web_search_engines.search_engine_factory import (
        create_search_engine,
    )

    monkeypatch.delenv("LDR_SEARCH_TIME_PERIOD", raising=False)
    monkeypatch.delenv(
        "LDR_SEARCH_ENGINE_WEB_SERPAPI_DEFAULT_PARAMS_TIME_PERIOD",
        raising=False,
    )
    monkeypatch.setenv(variable, value)
    snapshot = {
        "search.time_period": "d",
        "search.engine.web.serpapi.default_params.time_period": "inherit",
        "search.engine.library.enabled": False,
    }
    with (
        patch(
            "local_deep_research.web_search_engines.search_engine_factory.retriever_registry",
            SimpleNamespace(get=lambda *a, **k: None),
        ),
        patch(
            "local_deep_research.web_search_engines.search_engine_factory.get_safe_module_class",
            return_value=CapturingEngine,
        ),
        patch(
            "local_deep_research.security.egress.policy.evaluate_engine",
            return_value=Decision(True, "local_review"),
        ),
        pytest.raises(ValueError) as error,
    ):
        create_search_engine("serpapi", settings_snapshot=snapshot)
    message = str(error.value)
    assert message.startswith(f"Environment variable {variable} must be one of")
    assert message.endswith("case and surrounding whitespace matter")
    # The raw value is never echoed, control characters included.
    assert repr(value) not in message
    assert "\r" not in message
    assert "\t" not in message


def test_invalid_global_environment_pin_names_the_variable_before_any_mutation(
    monkeypatch,
):
    import copy

    monkeypatch.setenv("LDR_SEARCH_TIME_PERIOD", "y\r")
    snapshot = {
        "search.time_period": {"value": "d", "ui_element": "select"},
        "search.engine.web.serpapi.default_params.time_period": {
            "value": "m",
            "ui_element": "select",
        },
    }
    before = copy.deepcopy(snapshot)
    with pytest.raises(ValueError) as error:
        apply_time_period_override(snapshot, "w")
    assert str(error.value) == (
        "Environment variable LDR_SEARCH_TIME_PERIOD must be one of "
        "d, w, m, y, all; case and surrounding whitespace matter"
    )
    assert snapshot == before


@pytest.mark.parametrize(
    "bad_value", ["typo-private-marker", "inherit", "", None, 1, True, ["w"]]
)
def test_invalid_request_does_not_mutate_snapshot(monkeypatch, bad_value):
    import copy

    monkeypatch.delenv("LDR_SEARCH_TIME_PERIOD", raising=False)
    snapshot = {
        "search.time_period": {"value": "d", "ui_element": "select"},
        "search.engine.web.serpapi.default_params.time_period": {
            "value": "m",
            "ui_element": "select",
        },
    }
    before = copy.deepcopy(snapshot)
    with pytest.raises(ValueError, match="time_period must be one of"):
        apply_time_period_override(snapshot, bad_value)
    assert snapshot == before


@pytest.mark.parametrize("engine", ["brave", "serpapi", "tavily"])
@pytest.mark.parametrize(
    ("pinned", "expected"),
    [
        pytest.param("w", "w", id="environment-pin-wins"),
        pytest.param(None, "d", id="control-no-pin-follows-global"),
    ],
)
def test_environment_pinned_per_engine_period_outranks_global(
    monkeypatch, engine, pinned, expected
):
    """The changelog says an environment-pinned per-engine period is a
    deliberate override. Before the inherit semantics the global setting
    shadowed it for SerpAPI and Tavily, so it now takes effect for them."""
    from local_deep_research.web_search_engines.search_engine_factory import (
        create_search_engine,
    )

    env_name = (
        f"LDR_SEARCH_ENGINE_WEB_{engine.upper()}_DEFAULT_PARAMS_TIME_PERIOD"
    )
    monkeypatch.delenv("LDR_SEARCH_TIME_PERIOD", raising=False)
    monkeypatch.delenv(env_name, raising=False)
    if pinned is not None:
        monkeypatch.setenv(env_name, pinned)
    snapshot = {
        "search.time_period": "d",
        f"search.engine.web.{engine}.default_params.time_period": "inherit",
        "search.engine.library.enabled": False,
    }
    with (
        patch(
            "local_deep_research.web_search_engines.search_engine_factory.retriever_registry",
            SimpleNamespace(get=lambda *a, **k: None),
        ),
        patch(
            "local_deep_research.web_search_engines.search_engine_factory.get_safe_module_class",
            return_value=CapturingEngine,
        ),
        patch(
            "local_deep_research.security.egress.policy.evaluate_engine",
            return_value=Decision(True, "local_review"),
        ),
    ):
        built = create_search_engine(engine, settings_snapshot=snapshot)
    assert built is not None
    assert built.time_period == expected


@pytest.mark.parametrize("locked", [False, True])
def test_request_period_replaces_saved_choice_whether_or_not_settings_locked(
    monkeypatch, locked
):
    """The research start snapshot is taken with SettingsManager.get_all_settings,
    which marks every database-backed entry non-editable while app.lock_settings
    is on. That marker is not an environment pin, so a per-request period must
    replace an explicit per-engine choice on a locked installation too."""
    from sqlalchemy import create_engine as create_db_engine
    from sqlalchemy.orm import Session

    from local_deep_research.database.models.settings import (
        Setting,
        SettingType,
    )
    from local_deep_research.settings.manager import SettingsManager
    from local_deep_research.web_search_engines.search_engine_factory import (
        create_search_engine,
    )

    for name in (
        "LDR_SEARCH_TIME_PERIOD",
        "LDR_APP_LOCK_SETTINGS",
        "LDR_SEARCH_ENGINE_WEB_SERPAPI_DEFAULT_PARAMS_TIME_PERIOD",
    ):
        monkeypatch.delenv(name, raising=False)
    serpapi_key = "search.engine.web.serpapi.default_params.time_period"
    options = [
        {"label": code, "value": code}
        for code in ("inherit", "d", "w", "m", "y", "all")
    ]
    rows = [
        ("app.lock_settings", locked, SettingType.APP, "checkbox", None),
        (
            "search.time_period",
            "all",
            SettingType.SEARCH,
            "select",
            options[1:],
        ),
        (serpapi_key, "y", SettingType.SEARCH, "select", options),
    ]
    database = create_db_engine("sqlite://")
    Setting.__table__.create(database)
    try:
        with Session(database) as session:
            for key, value, kind, ui_element, choices in rows:
                session.add(
                    Setting(
                        key=key,
                        value=value,
                        type=kind,
                        name=key,
                        ui_element=ui_element,
                        options=choices,
                        visible=True,
                        editable=True,
                    )
                )
            session.commit()
            snapshot = SettingsManager(
                session, seed_defaults=False
            ).get_all_settings(
                # Research uses effective metadata. Stored-value exports
                # deliberately leave editable unchanged, even when locked.
                bypass_cache=True,
                include_environment_overrides=True,
            )
    finally:
        database.dispose()
    snapshot["search.engine.library.enabled"] = False
    snapshot["search.engine.web.serpapi.api_key"] = {"value": "sk-" + "x" * 20}
    # The premise: the saved choice is marked non-editable exactly when locked.
    assert snapshot[serpapi_key]["editable"] is (not locked)

    apply_time_period_override(snapshot, "w")

    assert snapshot[serpapi_key]["value"] == "inherit"
    assert snapshot["search.time_period"]["value"] == "w"
    with (
        patch(
            "local_deep_research.web_search_engines.search_engine_factory.retriever_registry",
            SimpleNamespace(get=lambda *a, **k: None),
        ),
        patch(
            "local_deep_research.web_search_engines.search_engine_factory.get_safe_module_class",
            return_value=CapturingEngine,
        ),
        patch(
            "local_deep_research.security.egress.policy.evaluate_engine",
            return_value=Decision(True, "local_review"),
        ),
    ):
        built = create_search_engine("serpapi", settings_snapshot=snapshot)
    assert built is not None
    assert built.time_period == "w"


@pytest.mark.parametrize("engine", ["brave", "serpapi", "tavily"])
def test_wrapped_null_global_period_falls_back_to_all(monkeypatch, engine):
    """A stored global row with no value (what a database-backed snapshot
    holds for a cleared setting) resolves to the documented "all" default,
    as it did before inherit semantics, instead of failing every search."""
    from local_deep_research.web_search_engines.search_engine_factory import (
        create_search_engine,
    )

    monkeypatch.delenv("LDR_SEARCH_TIME_PERIOD", raising=False)
    monkeypatch.delenv(
        f"LDR_SEARCH_ENGINE_WEB_{engine.upper()}_DEFAULT_PARAMS_TIME_PERIOD",
        raising=False,
    )
    snapshot = {
        "search.time_period": {"value": None, "ui_element": "select"},
        f"search.engine.web.{engine}.default_params.time_period": "inherit",
        "search.engine.library.enabled": False,
    }
    with (
        patch(
            "local_deep_research.web_search_engines.search_engine_factory.retriever_registry",
            SimpleNamespace(get=lambda *a, **k: None),
        ),
        patch(
            "local_deep_research.web_search_engines.search_engine_factory.get_safe_module_class",
            return_value=CapturingEngine,
        ),
        patch(
            "local_deep_research.security.egress.policy.evaluate_engine",
            return_value=Decision(True, "local_review"),
        ),
    ):
        built = create_search_engine(engine, settings_snapshot=snapshot)
    assert built is not None
    assert built.time_period == "all"


@pytest.mark.parametrize("engine", ["brave", "serpapi", "tavily", "wikinews"])
@pytest.mark.parametrize("stored", ["json-null", "sql-null"])
def test_stored_null_global_period_in_an_unwrapped_snapshot_means_all(
    monkeypatch, engine, stored
):
    """SettingsManager.get_settings_snapshot() (REST /api/v1, scheduled news
    runs, news/api) unwraps every entry, so a stored null search.time_period
    arrives as a bare None. As on main, that means "not set" and every engine
    that takes the shared period searches unfiltered instead of raising."""
    from sqlalchemy import create_engine as create_db_engine
    from sqlalchemy import null
    from sqlalchemy.orm import Session

    from local_deep_research.database.models.settings import (
        Setting,
        SettingType,
    )
    from local_deep_research.settings.manager import SettingsManager
    from local_deep_research.web_search_engines.search_engine_factory import (
        create_search_engine,
    )

    monkeypatch.delenv("LDR_SEARCH_TIME_PERIOD", raising=False)
    monkeypatch.delenv(
        f"LDR_SEARCH_ENGINE_WEB_{engine.upper()}_DEFAULT_PARAMS_TIME_PERIOD",
        raising=False,
    )
    database = create_db_engine("sqlite://")
    Setting.__table__.create(database)
    try:
        with Session(database) as session:
            session.add(
                Setting(
                    key="search.time_period",
                    value=None if stored == "json-null" else null(),
                    type=SettingType.SEARCH,
                    name="Time Period",
                    ui_element="select",
                    options=[
                        {"label": code, "value": code}
                        for code in ("d", "w", "m", "y", "all")
                    ],
                    visible=True,
                    editable=True,
                )
            )
            session.commit()
            snapshot = SettingsManager(
                session, seed_defaults=False
            ).get_settings_snapshot()
    finally:
        database.dispose()
    # The premise: the stored null really is a bare None in this shape.
    assert snapshot["search.time_period"] is None
    snapshot["search.engine.library.enabled"] = False
    snapshot[f"search.engine.web.{engine}.api_key"] = "sk-" + "x" * 20

    with (
        patch(
            "local_deep_research.web_search_engines.search_engine_factory.retriever_registry",
            SimpleNamespace(get=lambda *a, **k: None),
        ),
        patch(
            "local_deep_research.web_search_engines.search_engine_factory.get_safe_module_class",
            return_value=CapturingEngine,
        ),
        patch(
            "local_deep_research.security.egress.policy.evaluate_engine",
            return_value=Decision(True, "local_review"),
        ),
    ):
        built = create_search_engine(engine, settings_snapshot=snapshot)
    assert built is not None
    assert built.time_period == "all"


def _resolved_period(monkeypatch, engine="serpapi", **levels):
    """Period an engine receives with the given levels set to a value.

    Levels, highest first: explicit (kwarg), engine_env, global_env, request,
    engine_saved, global_saved. The snapshot has the shape dispatch gives it.
    """
    engine_env = (
        f"LDR_SEARCH_ENGINE_WEB_{engine.upper()}_DEFAULT_PARAMS_TIME_PERIOD"
    )
    per_engine_key = f"search.engine.web.{engine}.default_params.time_period"
    for name in ("LDR_SEARCH_TIME_PERIOD", engine_env):
        monkeypatch.delenv(name, raising=False)
    if "engine_env" in levels:
        monkeypatch.setenv(engine_env, levels["engine_env"])
    if "global_env" in levels:
        monkeypatch.setenv("LDR_SEARCH_TIME_PERIOD", levels["global_env"])
    snapshot = {
        "search.engine.library.enabled": False,
        per_engine_key: {
            "value": levels.get("engine_saved", "inherit"),
            "ui_element": "select",
            "editable": True,
        },
    }
    if "global_saved" in levels:
        snapshot["search.time_period"] = {
            "value": levels["global_saved"],
            "ui_element": "select",
            "editable": True,
        }
    if "request" in levels:
        apply_time_period_override(snapshot, levels["request"])
    kwargs = {"time_period": levels["explicit"]} if "explicit" in levels else {}
    from local_deep_research.web_search_engines.search_engine_factory import (
        create_search_engine,
    )

    with (
        patch(
            "local_deep_research.web_search_engines.search_engine_factory.retriever_registry",
            SimpleNamespace(get=lambda *a, **k: None),
        ),
        patch(
            "local_deep_research.web_search_engines.search_engine_factory.get_safe_module_class",
            return_value=CapturingEngine,
        ),
        patch(
            "local_deep_research.security.egress.policy.evaluate_engine",
            return_value=Decision(True, "local_review"),
        ),
    ):
        built = create_search_engine(
            engine, settings_snapshot=snapshot, **kwargs
        )
    assert built is not None
    return built.time_period


@pytest.mark.parametrize("engine", ["brave", "serpapi", "tavily"])
@pytest.mark.parametrize(
    ("higher", "lower"),
    [
        pytest.param("explicit", "engine_env", id="code-over-engine-pin"),
        pytest.param("explicit", "global_env", id="code-over-global-pin"),
        pytest.param(
            "engine_env", "global_env", id="engine-pin-over-global-pin"
        ),
        pytest.param("engine_env", "engine_saved", id="engine-pin-over-saved"),
        pytest.param("global_env", "request", id="global-pin-over-request"),
        pytest.param("global_env", "engine_saved", id="global-pin-over-saved"),
        pytest.param("request", "engine_saved", id="request-over-saved"),
        pytest.param("request", "global_saved", id="request-over-saved-global"),
        pytest.param(
            "engine_saved", "global_saved", id="saved-engine-over-global"
        ),
    ],
)
def test_time_period_precedence_is_one_order_on_every_path(
    monkeypatch, engine, higher, lower
):
    """explicit > engine env > LDR_SEARCH_TIME_PERIOD > request > saved
    per-engine choice > saved global setting, for every pair that meets."""
    assert (
        _resolved_period(monkeypatch, engine, **{higher: "d", lower: "w"})
        == "d"
    )
    # Control: the lower level alone is honoured, so the pair above really
    # tests which of the two wins.
    assert _resolved_period(monkeypatch, engine, **{lower: "w"}) == "w"


def test_time_period_defaults_to_all_without_any_level(monkeypatch):
    assert _resolved_period(monkeypatch) == "all"


@pytest.mark.parametrize(
    ("levels", "expected"),
    [
        pytest.param({}, "d", id="follows-saved-global"),
        pytest.param({"global_env": "m"}, "m", id="follows-global-pin"),
    ],
)
def test_engine_environment_inherit_hides_the_saved_per_engine_choice(
    monkeypatch, levels, expected
):
    """LDR_SEARCH_ENGINE_WEB_<ENGINE>_DEFAULT_PARAMS_TIME_PERIOD=inherit is an
    operator pin to the global level: a choice saved for the engine in
    Settings does not apply."""
    assert (
        _resolved_period(
            monkeypatch,
            engine_env="inherit",
            engine_saved="w",
            global_saved="d",
            **levels,
        )
        == expected
    )