"""Tests for the MCP direct-search egress audit net.

Direct MCP searches call ``engine.run()`` without going through
``AdvancedSearchSystem`` (which arms the audit-hook net itself), so
``_execute_search`` arms the net via ``_egress_audit_net``.
"""

import pytest

from local_deep_research.security.egress.audit_hook import (
    active_egress_context,
    get_active_context,
)
from local_deep_research.security.egress.policy import (
    EgressScope,
    context_from_snapshot,
)

try:
    import mcp  # noqa: F401

    MCP_AVAILABLE = True
except ImportError:
    MCP_AVAILABLE = False

pytestmark = pytest.mark.skipif(
    not MCP_AVAILABLE, reason="MCP package not installed"
)

if MCP_AVAILABLE:
    from local_deep_research.mcp.server import _egress_audit_net


def test_arms_context_for_private_only():
    settings = {
        "policy.egress_scope": {"value": "private_only"},
        "search.tool": {"value": "library"},
        "_username": "tester",
    }
    assert get_active_context() is None
    with _egress_audit_net(settings):
        ctx = get_active_context()
        assert ctx is not None
        assert ctx.scope == EgressScope.PRIVATE_ONLY
        assert ctx.username == "tester"
    assert get_active_context() is None


def test_empty_settings_is_noop():
    with _egress_audit_net({}):
        assert get_active_context() is None


def test_already_armed_context_is_kept():
    """When a caller already armed a context (web research worker), the
    MCP net must not replace it."""
    outer = context_from_snapshot(
        {"policy.egress_scope": {"value": "both"}}, "searxng"
    )
    with active_egress_context(outer):
        with _egress_audit_net(
            {
                "policy.egress_scope": {"value": "private_only"},
                "search.tool": {"value": "library"},
            }
        ):
            assert get_active_context() is outer


def test_unevaluable_policy_is_noop():
    """A corrupted scope must not break MCP search — the net simply stays
    unarmed (the factory PEP remains the primary enforcement)."""
    settings = {"policy.egress_scope": {"value": "garbage_scope"}}
    with _egress_audit_net(settings):
        assert get_active_context() is None
