# retoor <retoor@molodetz.nl>

import httpx

from devplacepy import stealth
from devplacepy.curl_transport import CurlTransport
from tests.conftest import run_async


def test_factory_returns_async_client():
    client = stealth.stealth_async_client(timeout=5.0)
    assert isinstance(client, httpx.AsyncClient)
    run_async(client.aclose())


def test_curl_active_true():
    assert stealth.curl_active() is True


def test_stealth_transport_is_curl_when_active():
    transport = stealth.stealth_transport()
    assert isinstance(transport, CurlTransport)
    run_async(transport.aclose())


def test_curl_mode_forwards_headers_without_chrome_base():
    client = stealth.stealth_async_client(headers={"Authorization": "Bearer k"})
    assert client.headers.get("authorization") == "Bearer k"
    assert client.headers.get("sec-ch-ua") is None
    run_async(client.aclose())


def test_chrome_base_headers_match_chrome_149():
    headers = stealth.chrome_base_headers()
    assert "Chrome/149.0.0.0" in headers["user-agent"]
    assert headers["sec-ch-ua"] == (
        '"Google Chrome";v="149", "Chromium";v="149", "Not)A;Brand";v="24"'
    )
    assert headers["accept-encoding"] == "gzip, deflate, br, zstd"


def test_fallback_path_injects_chrome_headers(monkeypatch):
    monkeypatch.setattr(stealth, "CURL_AVAILABLE", False)
    transport = stealth.stealth_transport()
    assert isinstance(transport, httpx.AsyncHTTPTransport)
    client = stealth.stealth_async_client()
    assert client.headers.get("sec-ch-ua") is not None
    assert "Chrome/149.0.0.0" in client.headers.get("user-agent", "")
    run_async(client.aclose())
    run_async(transport.aclose())
