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.

30.0 KB
Raw Blame
"""Follow-up regression tests for ADAPTIVE scope resolution and
``context_from_snapshot`` cross-field coupling (PR #4300 egress policy).

These cover gaps not already exercised by
``tests/security/test_egress_policy.py``:

* ADAPTIVE resolution of *registered retriever* primaries (local => PRIVATE_ONLY,
  public => PUBLIC_ONLY) via the retriever registry path inside
  ``_resolve_adaptive_scope``.
* Stray removed meta-engine names (auto / meta / parallel /
  parallel_scientific) being unclassifiable and resolving to BOTH under
  ADAPTIVE, and leaving STRICT intact (no ValueError) under STRICT.
* The PRIVATE_ONLY -> require_local_llm/require_local_embeddings *coupling* on a
  direct (non-adaptive) PRIVATE_ONLY scope, and the deliberate absence of that
  coupling under STRICT.
* Non-dict snapshot ValueError contract.
* ``allow_dns=False`` skipping ``_resolve_with_timeout`` and falling back to the
  engine's static classification (vs. the DNS-driven result with allow_dns=True).
"""

from __future__ import annotations

from contextlib import contextmanager
from unittest.mock import MagicMock, patch

import pytest
from loguru import logger

from local_deep_research.security.egress.policy import (
    EgressScope,
    PolicyDeniedError,
    _resolve_adaptive_scope,
    context_from_snapshot,
)
from local_deep_research.web_search_engines.retriever_registry import (
    retriever_registry,
)


@contextmanager
def _capture_policy_warnings():
    """Capture WARNING-level loguru records emitted by the package.

    The package disables its own loguru namespace in ``__init__`` (so audit
    warnings don't spam host apps); enable it here and add a sink so the
    fail-open warning actually reaches us. Restore both in ``finally``.
    """
    records: list[dict] = []
    logger.enable("local_deep_research")
    sink_id = logger.add(lambda m: records.append(m.record), level="WARNING")
    try:
        yield records
    finally:
        logger.remove(sink_id)
        logger.disable("local_deep_research")


# Distinctive phrase carried by the ADAPTIVE unclassifiable-primary fail-open
# warning in ``_resolve_adaptive_scope`` — pinned so the tests below key off
# the exact security signal, not incidental log lines.
_FAILOPEN_PHRASE = "could not be classified"


def _adaptive_snapshot(tool: str, **extra) -> dict:
    """Nested-value snapshot selecting ADAPTIVE scope with the given tool."""
    snap = {
        "policy.egress_scope": {"value": "adaptive"},
        "search.tool": {"value": tool},
    }
    snap.update(extra)
    return snap


# ---------------------------------------------------------------------------
# ADAPTIVE: concrete engine classification (allow + deny pairs)
# ---------------------------------------------------------------------------


def test_resolve_adaptive_concrete_public_engine_is_public_only():
    """_resolve_adaptive_scope: a concrete public engine -> PUBLIC_ONLY."""
    scope = _resolve_adaptive_scope(
        "arxiv",
        {},
        username=None,
        local_hostnames=(),
    )
    assert scope == EgressScope.PUBLIC_ONLY


def test_resolve_adaptive_concrete_private_engine_is_private_only():
    """_resolve_adaptive_scope: a concrete local engine -> PRIVATE_ONLY."""
    scope = _resolve_adaptive_scope(
        "paperless",
        {},
        username=None,
        local_hostnames=(),
    )
    assert scope == EgressScope.PRIVATE_ONLY


# ---------------------------------------------------------------------------
# ADAPTIVE: stray removed meta-engine names -> BOTH (unclassifiable fallback)
# ---------------------------------------------------------------------------


@pytest.mark.parametrize(
    "picker", ["auto", "meta", "parallel", "parallel_scientific"]
)
def test_resolve_adaptive_stray_meta_names_resolve_to_both(picker):
    # The meta engines were removed; a stray name left in the DB is simply
    # unclassifiable and falls through to BOTH (no special-case branch).
    scope = _resolve_adaptive_scope(
        picker,
        {},
        username=None,
        local_hostnames=(),
    )
    assert scope == EgressScope.BOTH


def test_resolve_adaptive_empty_primary_is_both():
    """A missing/empty primary cannot be classified -> permissive BOTH."""
    scope = _resolve_adaptive_scope(
        "",
        {},
        username=None,
        local_hostnames=(),
    )
    assert scope == EgressScope.BOTH


@pytest.mark.parametrize("picker", ["meta", "parallel", "parallel_scientific"])
def test_context_adaptive_stray_meta_names_resolve_to_both(picker):
    """Through the public entrypoint: ADAPTIVE + each stray removed meta name
    -> BOTH and leaves inference requirements untouched (no PRIVATE_ONLY
    coupling)."""
    ctx = context_from_snapshot(
        _adaptive_snapshot(picker), primary_engine=picker
    )
    assert ctx.scope == EgressScope.BOTH
    assert ctx.require_local_llm is False
    assert ctx.require_local_embeddings is False


# ---------------------------------------------------------------------------
# ADAPTIVE: registered retriever primaries (registry path)
# ---------------------------------------------------------------------------


def test_resolve_adaptive_local_retriever_is_private_only():
    """A registered LOCAL retriever (not in ENGINE_REGISTRY) resolves via the
    retriever registry to PRIVATE_ONLY."""
    name = "_adaptive_followup_local_kb"
    retriever_registry.register(name, MagicMock(), is_local=True)
    try:
        scope = _resolve_adaptive_scope(
            name,
            {},
            username=None,
            local_hostnames=(),
        )
        assert scope == EgressScope.PRIVATE_ONLY
    finally:
        retriever_registry.unregister(name)


def test_resolve_adaptive_public_retriever_is_public_only():
    """A registered PUBLIC retriever resolves to PUBLIC_ONLY."""
    name = "_adaptive_followup_public_idx"
    retriever_registry.register(name, MagicMock(), is_local=False)
    try:
        scope = _resolve_adaptive_scope(
            name,
            {},
            username=None,
            local_hostnames=(),
        )
        assert scope == EgressScope.PUBLIC_ONLY
    finally:
        retriever_registry.unregister(name)


def test_context_adaptive_local_retriever_forces_local_inference():
    """End-to-end: ADAPTIVE + a registered local retriever primary resolves to
    PRIVATE_ONLY AND inherits the require_local_* coupling."""
    name = "_adaptive_followup_local_kb2"
    retriever_registry.register(name, MagicMock(), is_local=True)
    try:
        ctx = context_from_snapshot(
            _adaptive_snapshot(name), primary_engine=name
        )
        assert ctx.scope == EgressScope.PRIVATE_ONLY
        assert ctx.require_local_llm is True
        assert ctx.require_local_embeddings is True
    finally:
        retriever_registry.unregister(name)


def test_context_adaptive_public_retriever_does_not_force_local():
    """ADAPTIVE + a registered public retriever -> PUBLIC_ONLY, which does NOT
    force local inference (public scope is orthogonal to local-inference)."""
    name = "_adaptive_followup_public_idx2"
    retriever_registry.register(name, MagicMock(), is_local=False)
    try:
        ctx = context_from_snapshot(
            _adaptive_snapshot(name), primary_engine=name
        )
        assert ctx.scope == EgressScope.PUBLIC_ONLY
        assert ctx.require_local_llm is False
        assert ctx.require_local_embeddings is False
    finally:
        retriever_registry.unregister(name)


def test_resolve_adaptive_unknown_primary_falls_back_to_both():
    """A name that is neither a static engine nor a registered retriever is
    unclassifiable -> BOTH (permissive fallback, never a hard fail), and no
    require_local coupling is applied."""
    name = "_adaptive_followup_not_registered_anywhere"
    # Ensure it is genuinely absent from the retriever registry.
    assert retriever_registry.get_metadata(name) is None
    ctx = context_from_snapshot(_adaptive_snapshot(name), primary_engine=name)
    assert ctx.scope == EgressScope.BOTH
    assert ctx.require_local_llm is False
    assert ctx.require_local_embeddings is False


# ---------------------------------------------------------------------------
# ADAPTIVE: the unclassifiable-primary fail-open must be DETECTABLE (a WARNING
# names the primary + whether a username was present) so a missed ``username=``
# threading surfaces in logs instead of resolving silently to permissive BOTH.
# ---------------------------------------------------------------------------


def test_resolve_adaptive_unclassifiable_primary_emits_failopen_warning():
    """An unclassifiable primary (not a static engine, collection, or a
    retriever visible for this user) resolves to the permissive BOTH AND emits
    a policy-audit WARNING naming the primary and reporting username presence.
    """
    name = "_adaptive_followup_failopen_unknown_primary"
    assert retriever_registry.get_metadata(name) is None
    with _capture_policy_warnings() as records:
        scope = _resolve_adaptive_scope(
            name, {}, username=None, local_hostnames=()
        )
    assert scope == EgressScope.BOTH
    failopen = [
        r
        for r in records
        if r["extra"].get("policy_audit") and _FAILOPEN_PHRASE in r["message"]
    ]
    assert failopen, (
        "expected a policy-audit fail-open WARNING for the unclassifiable "
        f"primary; got {[r['message'] for r in records]}"
    )
    msg = failopen[0]["message"]
    # Diagnostic context: the primary name and the username-present flag must
    # both ride on the message so a triager can spot a missed username thread.
    assert name in msg
    assert "username_present=False" in msg


def test_resolve_adaptive_unclassifiable_primary_reports_username_present():
    """When a username IS threaded but the primary is still unclassifiable, the
    warning reports ``username_present=True`` (so the diagnosis distinguishes a
    missed-thread from a genuinely-unregistered primary)."""
    name = "_adaptive_followup_failopen_with_username"
    assert retriever_registry.get_metadata(name, username="alice") is None
    with _capture_policy_warnings() as records:
        scope = _resolve_adaptive_scope(
            name, {}, username="alice", local_hostnames=()
        )
    assert scope == EgressScope.BOTH
    failopen = [
        r
        for r in records
        if r["extra"].get("policy_audit") and _FAILOPEN_PHRASE in r["message"]
    ]
    assert failopen
    assert "username_present=True" in failopen[0]["message"]


def test_resolve_adaptive_classified_public_primary_does_not_warn():
    """A normally-classified PUBLIC primary resolves to PUBLIC_ONLY and must NOT
    emit the unclassifiable fail-open warning (a public engine never lands in
    the None/None branch)."""
    with _capture_policy_warnings() as records:
        scope = _resolve_adaptive_scope(
            "arxiv", {}, username=None, local_hostnames=()
        )
    assert scope == EgressScope.PUBLIC_ONLY
    assert not [r for r in records if _FAILOPEN_PHRASE in r["message"]]


def test_resolve_adaptive_classified_private_retriever_does_not_warn():
    """A registered LOCAL retriever primary resolves to PRIVATE_ONLY via the
    registry path and must NOT emit the fail-open warning (it IS classified)."""
    name = "_adaptive_followup_classified_local_kb"
    retriever_registry.register(name, MagicMock(), is_local=True)
    try:
        with _capture_policy_warnings() as records:
            scope = _resolve_adaptive_scope(
                name, {}, username=None, local_hostnames=()
            )
        assert scope == EgressScope.PRIVATE_ONLY
        assert not [r for r in records if _FAILOPEN_PHRASE in r["message"]]
    finally:
        retriever_registry.unregister(name)


# ---------------------------------------------------------------------------
# The fail-open warning must be DEDUPED (log-spam guard): context_from_snapshot
# can run in multiple setup paths for the same research run, so an
# unclassifiable primary would otherwise emit repeated warnings. It must
# fire at most once per unique
# (primary, user) pair in the process, and must scrub a
# log-injection payload embedded in the (user-controlled) primary name.
# ---------------------------------------------------------------------------


def test_resolve_adaptive_unclassifiable_primary_warning_deduped_on_repeat():
    """Calling _resolve_adaptive_scope repeatedly with the SAME unclassifiable
    primary + username combo must emit the fail-open WARNING only ONCE, not
    once per call — this is the log-spam fix for the per-URL fetch gate."""
    name = "_adaptive_followup_dedup_repeat_primary"
    assert retriever_registry.get_metadata(name) is None
    with _capture_policy_warnings() as records:
        for _ in range(5):
            scope = _resolve_adaptive_scope(
                name, {}, username=None, local_hostnames=()
            )
            assert scope == EgressScope.BOTH
    failopen = [
        r
        for r in records
        if r["extra"].get("policy_audit") and _FAILOPEN_PHRASE in r["message"]
    ]
    assert len(failopen) == 1, (
        f"expected exactly one deduped fail-open WARNING across 5 identical "
        f"calls, got {len(failopen)}: {[r['message'] for r in failopen]}"
    )


def test_resolve_adaptive_unclassifiable_primary_warning_not_deduped_across_distinct_keys():
    """A different primary — or the same primary with a different
    username-presence — is a NEW dedup key and must still warn."""
    name_a = "_adaptive_followup_dedup_distinct_a"
    name_b = "_adaptive_followup_dedup_distinct_b"
    assert retriever_registry.get_metadata(name_a) is None
    assert retriever_registry.get_metadata(name_b, username="carol") is None
    with _capture_policy_warnings() as records:
        _resolve_adaptive_scope(name_a, {}, username=None, local_hostnames=())
        # Same name, but WITH a username -> distinct key -> warns again.
        _resolve_adaptive_scope(
            name_a, {}, username="carol", local_hostnames=()
        )
        # A different primary name -> distinct key -> warns again.
        _resolve_adaptive_scope(
            name_b, {}, username="carol", local_hostnames=()
        )
    failopen = [
        r
        for r in records
        if r["extra"].get("policy_audit") and _FAILOPEN_PHRASE in r["message"]
    ]
    assert len(failopen) == 3


def test_resolve_adaptive_primary_engine_newline_scrubbed_in_warning():
    """A ``primary_engine`` value containing a newline (log-injection payload
    forging a fake subsequent log line) must be scrubbed before it reaches
    the emitted log record — the raw newline must not appear in the message."""
    injected = (
        "_adaptive_followup_injected_primary\n"
        "FAKE LOG LINE: policy_audit=True level=CRITICAL forged-entry"
    )
    assert retriever_registry.get_metadata(injected) is None
    with _capture_policy_warnings() as records:
        scope = _resolve_adaptive_scope(
            injected, {}, username=None, local_hostnames=()
        )
    assert scope == EgressScope.BOTH
    failopen = [
        r
        for r in records
        if r["extra"].get("policy_audit") and _FAILOPEN_PHRASE in r["message"]
    ]
    assert failopen, "expected the fail-open warning to fire for this key"
    msg = failopen[0]["message"]
    # The raw newline must be gone (control chars stripped by the sanitizer).
    assert "\n" not in msg
    # The forged "log line" text must not appear verbatim either — stripping
    # the newline alone already breaks the injection, but assert the intent
    # explicitly: the sanitized primary is still identifiable in the message.
    assert "_adaptive_followup_injected_primary" in msg


@contextmanager
def _fresh_failopen_state():
    """Swap the module's per-user fail-open dedup state for empty structures.

    Restores the originals on exit so tests don't leak dedup state into each
    other (the structure is a process-global).
    """
    import threading as _threading
    from collections import OrderedDict as _OrderedDict

    from local_deep_research.security.egress import policy as policy_module

    with (
        patch.object(
            policy_module, "_ADAPTIVE_FAILOPEN_WARNED", _OrderedDict()
        ),
        patch.object(
            policy_module, "_ADAPTIVE_FAILOPEN_LOCK", _threading.Lock()
        ),
    ):
        yield policy_module


def test_should_warn_adaptive_failopen_per_user_cap_is_bounded():
    """Each user's bucket is capped: once full, new primaries for THAT user are
    neither recorded nor warned (memory-safe, anti-log-amplification), since
    ``primary_engine`` is user-controlled. The cap is PER USER, so it never
    touches another user's audit budget."""
    with _fresh_failopen_state() as policy_module:
        with patch.object(
            policy_module, "_ADAPTIVE_FAILOPEN_WARN_CAP_PER_USER", 2
        ):
            assert (
                policy_module._should_warn_adaptive_failopen("k1", "alice")
                is True
            )
            assert (
                policy_module._should_warn_adaptive_failopen("k2", "alice")
                is True
            )
            # alice's bucket (cap 2) is full: a brand-new primary is refused
            # and NOT recorded.
            assert (
                policy_module._should_warn_adaptive_failopen("k3", "alice")
                is False
            )
            alice_key = policy_module._adaptive_failopen_user_key("alice")
            assert len(policy_module._ADAPTIVE_FAILOPEN_WARNED[alice_key]) == 2
            assert (
                "k3" not in policy_module._ADAPTIVE_FAILOPEN_WARNED[alice_key]
            )
            # A repeat of an already-recorded pair still reports False
            # (already warned), distinct from the cap-exhausted case.
            assert (
                policy_module._should_warn_adaptive_failopen("k1", "alice")
                is False
            )
            # A DIFFERENT user is unaffected by alice exhausting her bucket:
            # bob still warns on the very same primary names.
            assert (
                policy_module._should_warn_adaptive_failopen("k1", "bob")
                is True
            )
            assert (
                policy_module._should_warn_adaptive_failopen("k3", "bob")
                is True
            )


def test_should_warn_adaptive_failopen_one_user_cannot_suppress_another():
    """The core audit-control fix: user B's genuine fail-open on a primary
    name ALREADY logged for user A must still warn (identity is in the key),
    and B flooding their own bucket to the cap must NOT silence A."""
    with _fresh_failopen_state() as policy_module:
        with patch.object(
            policy_module, "_ADAPTIVE_FAILOPEN_WARN_CAP_PER_USER", 4
        ):
            shared_primary = "some_private_kb"
            # User A logs the fail-open for the shared primary name.
            assert (
                policy_module._should_warn_adaptive_failopen(
                    shared_primary, "alice"
                )
                is True
            )
            # User B floods their OWN bucket full with junk primaries (the
            # old global-set attack). This must not consume A's budget.
            for i in range(10):
                policy_module._should_warn_adaptive_failopen(f"junk-{i}", "bob")
            # B's genuine fail-open on the SAME primary A already logged still
            # warns -- it is a distinct (user, primary) pair. Under the old
            # presence-only key this was silently deduped.
            assert (
                policy_module._should_warn_adaptive_failopen(
                    shared_primary, "bob"
                )
                is False  # bob's bucket already full of junk -> suppressed
            )
            # ...but a THIRD user, carol, whose bucket is untouched, still
            # warns on the shared primary: no cross-user suppression.
            assert (
                policy_module._should_warn_adaptive_failopen(
                    shared_primary, "carol"
                )
                is True
            )
            # And A can still warn on a brand-new primary of their own -- B's
            # flood never touched A's bucket.
            assert (
                policy_module._should_warn_adaptive_failopen(
                    "another_primary", "alice"
                )
                is True
            )


def test_should_warn_adaptive_failopen_users_dimension_is_bounded_lru():
    """The set of tracked users is a bounded LRU: beyond the users cap the
    least-recently-seen user's whole bucket is evicted, keeping total memory
    bounded. Eviction can only ever re-enable a warning, never suppress one."""
    with _fresh_failopen_state() as policy_module:
        with patch.object(policy_module, "_ADAPTIVE_FAILOPEN_MAX_USERS", 2):
            assert (
                policy_module._should_warn_adaptive_failopen("p", "u1") is True
            )
            assert (
                policy_module._should_warn_adaptive_failopen("p", "u2") is True
            )
            # Third distinct user -> evicts the least-recently-seen (u1).
            assert (
                policy_module._should_warn_adaptive_failopen("p", "u3") is True
            )
            assert len(policy_module._ADAPTIVE_FAILOPEN_WARNED) == 2
            u1_key = policy_module._adaptive_failopen_user_key("u1")
            assert u1_key not in policy_module._ADAPTIVE_FAILOPEN_WARNED
            # u1 was evicted, so u1's fail-open is now re-warnable (never
            # suppressed by eviction).
            assert (
                policy_module._should_warn_adaptive_failopen("p", "u1") is True
            )


def test_should_warn_adaptive_failopen_none_username_is_own_bucket():
    """An un-threaded run (username=None) is its own bucket, distinct from any
    authenticated user's -- a present-vs-absent username stays a distinct key,
    preserving the original dedup granularity."""
    with _fresh_failopen_state() as policy_module:
        assert policy_module._should_warn_adaptive_failopen("p", None) is True
        # Same primary WITH a username -> distinct bucket -> warns again.
        assert policy_module._should_warn_adaptive_failopen("p", "dave") is True
        # Repeat of the None-bucket pair is deduped.
        assert policy_module._should_warn_adaptive_failopen("p", None) is False


def test_should_warn_adaptive_failopen_is_thread_safe():
    """Concurrent calls with the same (user, primary) pair must record it
    exactly once: only one of many racing threads "wins" and returns True."""
    import threading as _threading

    with _fresh_failopen_state() as policy_module:

        def _hammer(results):
            for _ in range(200):
                results.append(
                    policy_module._should_warn_adaptive_failopen(
                        "_adaptive_followup_thread_key", "threaduser"
                    )
                )

        all_results: list = []
        threads = [
            _threading.Thread(target=_hammer, args=(all_results,))
            for _ in range(8)
        ]
        for t in threads:
            t.start()
        for t in threads:
            t.join()
        # Exactly one of the 1600 concurrent calls should have "won" and
        # returned True (the rest see the pair already recorded).
        assert all_results.count(True) == 1


# ---------------------------------------------------------------------------
# PRIVATE_ONLY coupling vs STRICT non-coupling
# ---------------------------------------------------------------------------


def test_direct_private_only_forces_local_llm_and_embeddings():
    """A directly-selected PRIVATE_ONLY scope (not via ADAPTIVE) forces both
    require_local_llm and require_local_embeddings even when the user left
    those flags at their permissive default. This is the core coupling that
    prevents silent exfiltration through cloud inference."""
    ctx = context_from_snapshot(
        {
            "policy.egress_scope": {"value": "private_only"},
            "llm.require_local_endpoint": {"value": False},
            "embeddings.require_local": {"value": False},
        },
        primary_engine="arxiv",
    )
    assert ctx.scope == EgressScope.PRIVATE_ONLY
    assert ctx.require_local_llm is True
    assert ctx.require_local_embeddings is True


def test_strict_does_not_force_local_inference():
    """STRICT restricts the search-engine set but is deliberately orthogonal to
    where inference runs: it must NOT force require_local_*."""
    ctx = context_from_snapshot(
        {
            "policy.egress_scope": {"value": "strict"},
            "llm.require_local_endpoint": {"value": False},
            "embeddings.require_local": {"value": False},
        },
        primary_engine="arxiv",
    )
    assert ctx.scope == EgressScope.STRICT
    assert ctx.require_local_llm is False
    assert ctx.require_local_embeddings is False


def test_strict_preserves_explicit_local_inference_flags():
    """STRICT does not force the flags, but it must preserve a user who DID
    opt in (no silent reset)."""
    ctx = context_from_snapshot(
        {
            "policy.egress_scope": {"value": "strict"},
            "llm.require_local_endpoint": {"value": True},
            "embeddings.require_local": {"value": True},
        },
        primary_engine="arxiv",
    )
    assert ctx.scope == EgressScope.STRICT
    assert ctx.require_local_llm is True
    assert ctx.require_local_embeddings is True


# ---------------------------------------------------------------------------
# STRICT + stray removed meta name -> STRICT context (no ValueError); the
# stray name only ever matches itself under the STRICT identity check, and as
# an unknown engine it is denied (engine_unknown) anyway.
# ---------------------------------------------------------------------------


@pytest.mark.parametrize(
    "picker", ["auto", "meta", "parallel", "parallel_scientific"]
)
def test_strict_with_stray_meta_name_builds_strict_context(picker):
    ctx = context_from_snapshot(
        {"policy.egress_scope": {"value": "strict"}},
        primary_engine=picker,
    )
    assert ctx.scope == EgressScope.STRICT


def test_strict_with_concrete_primary_does_not_raise():
    """Mirror: STRICT + a concrete primary is the supported, allowed combo."""
    ctx = context_from_snapshot(
        {"policy.egress_scope": {"value": "strict"}},
        primary_engine="arxiv",
    )
    assert ctx.scope == EgressScope.STRICT


# ---------------------------------------------------------------------------
# Snapshot validity contract
# ---------------------------------------------------------------------------


@pytest.mark.parametrize(
    "bad_snapshot",
    [
        ["policy.egress_scope", "both"],  # list
        "policy.egress_scope=both",  # str
        42,  # int
        None,  # None (the explicit "required" branch)
    ],
)
def test_non_dict_snapshot_raises_value_error(bad_snapshot):
    with pytest.raises(ValueError):
        context_from_snapshot(bad_snapshot, primary_engine="arxiv")


def test_unknown_scope_string_raises_policy_denied_with_target():
    """An unrecognised scope string is fail-closed as PolicyDeniedError whose
    target carries the offending value (so the operator can see what was
    rejected) rather than silently degrading to BOTH."""
    with pytest.raises(PolicyDeniedError) as excinfo:
        context_from_snapshot(
            {"policy.egress_scope": {"value": "PUBLIC"}},  # not a valid member
            primary_engine="arxiv",
        )
    assert excinfo.value.decision.reason == "unknown_egress_scope"
    assert excinfo.value.target == "PUBLIC"


# ---------------------------------------------------------------------------
# allow_dns=False: skip _resolve_with_timeout, fall back to static flags
# ---------------------------------------------------------------------------


def test_adaptive_allow_dns_false_skips_dns_and_uses_static_flags():
    """With allow_dns=False the URL-configurable primary's hostname is NOT
    DNS-resolved: _resolve_with_timeout must not be called, and resolution
    falls back to the engine's STATIC classification (searxng is_public=True
    -> PUBLIC_ONLY)."""
    snap = _adaptive_snapshot(
        "searxng",
        **{
            "search.engine.web.searxng.default_params.instance_url": (
                "http://searx.internal.lab:8080"
            )
        },
    )
    with patch(
        "local_deep_research.security.egress.policy._resolve_with_timeout"
    ) as mock_resolve:
        ctx = context_from_snapshot(
            snap, primary_engine="searxng", allow_dns=False
        )
        mock_resolve.assert_not_called()
    # Static fallback: searxng's declared is_public=True -> PUBLIC_ONLY.
    assert ctx.scope == EgressScope.PUBLIC_ONLY


def test_adaptive_allow_dns_true_uses_dns_resolution():
    """Contrast: with allow_dns=True the fail-up URL override DOES DNS-
    resolve (via _resolve_with_timeout) — for a LOCAL-nature engine. A
    paperless primary whose configured api_url resolves to a PUBLIC host is
    reclassified public, so ADAPTIVE resolves PUBLIC_ONLY instead of
    PRIVATE_ONLY. Proves allow_dns toggles the DNS path, not a no-op.

    NB: searxng no longer exercises DNS here — engine nature comes from
    static class flags and the URL override is fail-up only (it never
    relaxes a public engine to private), so a localhost searxng stays
    PUBLIC_ONLY with or without DNS."""
    snap = _adaptive_snapshot(
        "paperless",
        **{
            "search.engine.web.paperless.default_params.api_url": (
                "http://paperless.example.org:8930"
            )
        },
    )
    # NB: a real public IP — the RFC 5737 documentation ranges
    # (192.0.2.x / 198.51.100.x / 203.0.113.x) classify as PRIVATE under
    # Python's ipaddress.is_private and would defeat the fail-up here.
    public_addrinfo = [(None, None, None, None, ("93.184.216.34", 0))]
    with patch(
        "local_deep_research.security.egress.policy._resolve_with_timeout",
        return_value=public_addrinfo,
    ) as mock_resolve:
        ctx = context_from_snapshot(
            snap, primary_engine="paperless", allow_dns=True
        )
        mock_resolve.assert_called()
    assert ctx.scope == EgressScope.PUBLIC_ONLY
    # A public-resolving primary does NOT force the local-inference coupling.
    assert ctx.require_local_llm is False
    assert ctx.require_local_embeddings is False