from __future__ import annotations

import asyncio

from beaver.tools.builtins import web


class _FakeResponse:
    headers = {"content-type": "text/html"}
    status_code = 200
    text = '<a class="result__a" href="https://example.com">Example</a>'
    url = "https://example.com"

    def raise_for_status(self) -> None:
        return None


class _FakeAsyncClient:
    calls: list[dict[str, object]] = []

    def __init__(self, **kwargs: object) -> None:
        self.calls.append(kwargs)

    async def __aenter__(self) -> "_FakeAsyncClient":
        return self

    async def __aexit__(self, *args: object) -> None:
        return None

    async def get(self, *args: object, **kwargs: object) -> _FakeResponse:
        return _FakeResponse()


def test_web_tools_use_environment_proxy_settings(monkeypatch) -> None:
    _FakeAsyncClient.calls = []
    monkeypatch.setattr(web.httpx, "AsyncClient", _FakeAsyncClient)

    async def _run() -> None:
        await web.WebFetchTool().execute(url="https://example.com")
        await web.WebSearchTool().execute(query="example")

    asyncio.run(_run())

    assert [call.get("trust_env") for call in _FakeAsyncClient.calls] == [True, True]
