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.3 KB
Raw Blame
"""Contracts for tests/conftest.py fixture finalization order.

``reset_all_singletons`` calls ``discover_providers(force_refresh=True)``
(among other module globals) in its teardown. Any ``monkeypatch`` double
installed over those globals must therefore be undone BEFORE that teardown
runs. pytest finalizes fixtures in LIFO order over setup order, autouse
fixtures join each test's fixture closure in alphabetical order, and a
fixture's ``monkeypatch`` dependency is set up when the first fixture
requesting it is set up — so ``monkeypatch`` must be set up AFTER
``reset_all_singletons``.

When ``isolate_server_process_dump_policy`` took a ``monkeypatch``
dependency it inverted this order (its name sorts before
``reset_all_singletons``), and every test in
tests/web/routers/test_model_refresh_concurrency.py ended with "ERROR at
teardown: TypeError: <lambda>() got an unexpected keyword argument
'force_refresh'" (102 in CI). The tests here pin the order and the
behaviour so that regression cannot return silently.
"""

from local_deep_research.llm import providers
from local_deep_research.security import process_dump_policy


def test_monkeypatch_is_set_up_after_singleton_reset(request, monkeypatch):
    """``monkeypatch`` must come after ``reset_all_singletons`` in the closure.

    Autouse fixtures join the closure in alphabetical order (conftest
    attributes are scanned via ``dir()``) with a fixture's dependencies
    right after it, so any ``monkeypatch``-dependent autouse fixture whose
    name sorts before ``reset_all_singletons`` pulls ``monkeypatch`` first —
    the revert this test catches.
    """
    names = list(request.node.fixturenames)
    monkeypatch_position = names.index("monkeypatch")
    reset_position = names.index("reset_all_singletons")
    assert monkeypatch_position > reset_position, (
        "monkeypatch must be set up after reset_all_singletons so that "
        "monkeypatch.undo() runs before the singleton-reset teardown; "
        f"fixture closure order is {names!r}"
    )


def test_strict_discover_providers_double_is_undone_before_reset_teardown(
    monkeypatch,
):
    """A strict discover_providers double must not survive into _reset().

    The singleton-reset teardown calls ``discover_providers(force_refresh=True)``.
    With the fixture order reverted, this test errors at teardown with
    ``TypeError: <lambda>() got an unexpected keyword argument
    'force_refresh'`` — the exact CI failure this contract prevents.
    """
    monkeypatch.setattr(
        "local_deep_research.llm.providers.discover_providers",
        lambda: {},
    )
    assert providers.discover_providers() == {}


def test_process_dump_policy_stub_is_active_during_tests(
    isolate_server_process_dump_policy,
):
    """The autouse isolation stub shields test bodies from the OS policy.

    The fixture returns the real enforcer while its stub is installed, so a
    helper test can exercise the real function after replacing its OS
    boundaries without any test changing the pytest process's policy.
    """
    assert process_dump_policy.enforce_process_dump_policy() is True
    assert (
        isolate_server_process_dump_policy
        is not process_dump_policy.enforce_process_dump_policy
    )
    assert (
        isolate_server_process_dump_policy.__name__
        == "enforce_process_dump_policy"
    )