"""gateway.trust_env — one config key controls aiohttp proxy-env honoring at every adapter site (#48820)."""
import re
from pathlib import Path

import pytest

from gateway.platforms import base as gw_base

REPO = Path(__file__).resolve().parents[2]
_ADAPTER_FILES = sorted(
    list((REPO / "gateway" / "platforms").rglob("*.py"))
    + list((REPO / "plugins" / "platforms").rglob("*.py"))
)


def _write_config(tmp_path, monkeypatch, body: str) -> None:
    # load_config caches on (path, mtime) — a fresh tmp HERMES_HOME per test is a fresh cache key.
    monkeypatch.setenv("HERMES_HOME", str(tmp_path))
    (tmp_path / "config.yaml").write_text(body)


@pytest.mark.parametrize(
    "yaml_body, expected",
    [("gateway:\n  trust_env: false\n", False), ("gateway:\n  trust_env: true\n", True), ("{}\n", True)],
)
def test_gateway_trust_env_reads_config(tmp_path, monkeypatch, yaml_body, expected):
    """gateway.trust_env in config.yaml drives the shared helper; absent → True (default)."""
    _write_config(tmp_path, monkeypatch, yaml_body)
    assert gw_base.gateway_trust_env() is expected
    # The generic-proxy discovery path is gated by the same knob; explicit per-platform vars are not.
    monkeypatch.setenv("HTTPS_PROXY", "http://127.0.0.1:7890")
    monkeypatch.delenv("NO_PROXY", raising=False)
    monkeypatch.delenv("no_proxy", raising=False)
    assert (gw_base.resolve_proxy_url() is not None) is expected
    monkeypatch.setenv("X_PLATFORM_PROXY", "http://127.0.0.1:1080")
    assert gw_base.resolve_proxy_url("X_PLATFORM_PROXY") == "http://127.0.0.1:1080"


class TestResolveProxyUrlMultiplexScope:
    """A secondary multiplex profile's own TELEGRAM_PROXY/DISCORD_PROXY/etc. must gate its
    adapter, not the default profile's YAML-to-env bridge output sitting in the shared
    process's os.environ (the #72348 class, applied to this shared chokepoint used by
    Telegram, Discord, Mattermost, Matrix, SMS, and Slack)."""

    def test_scoped_profile_uses_its_own_value(self, monkeypatch):
        from agent.secret_scope import reset_secret_scope, set_multiplex_active, set_secret_scope

        monkeypatch.setenv("DISCORD_PROXY", "http://default-profile-proxy:8080")
        monkeypatch.delenv("NO_PROXY", raising=False)
        monkeypatch.delenv("no_proxy", raising=False)

        set_multiplex_active(True)
        token = set_secret_scope({"DISCORD_PROXY": "http://secondary-profile-proxy:9090"})
        try:
            assert gw_base.resolve_proxy_url("DISCORD_PROXY") == "http://secondary-profile-proxy:9090"
        finally:
            reset_secret_scope(token)
            set_multiplex_active(False)

    def test_scoped_profile_without_own_value_does_not_borrow_default(self, monkeypatch):
        from agent.secret_scope import reset_secret_scope, set_multiplex_active, set_secret_scope

        monkeypatch.setenv("DISCORD_PROXY", "http://default-profile-proxy:8080")
        monkeypatch.delenv("NO_PROXY", raising=False)
        monkeypatch.delenv("no_proxy", raising=False)

        set_multiplex_active(True)
        token = set_secret_scope({})
        try:
            assert gw_base.resolve_proxy_url("DISCORD_PROXY") is None
        finally:
            reset_secret_scope(token)
            set_multiplex_active(False)

    def test_unscoped_default_profile_still_reads_env(self, monkeypatch):
        """Control: outside multiplex (or the default profile), the legacy env read is
        unchanged."""
        monkeypatch.setenv("DISCORD_PROXY", "http://default-profile-proxy:8080")
        monkeypatch.delenv("NO_PROXY", raising=False)
        monkeypatch.delenv("no_proxy", raising=False)
        assert gw_base.resolve_proxy_url("DISCORD_PROXY") == "http://default-profile-proxy:8080"


def test_no_bare_trust_env_literal_in_adapters():
    """Every aiohttp session in gateway/ + plugins/platforms/ must go through gateway_trust_env()."""
    bare = re.compile(r"trust_env\s*=\s*(True|False)\b")
    offenders = []
    for path in _ADAPTER_FILES:
        for lineno, line in enumerate(path.read_text(encoding="utf-8").splitlines(), 1):
            if bare.search(line) and "httpx" not in line:
                offenders.append(f"{path.relative_to(REPO)}:{lineno}: {line.strip()}")
    assert not offenders, "hard-coded aiohttp trust_env literal(s); use gateway_trust_env():\n" + "\n".join(offenders)
