from __future__ import annotations

import httpx
import pytest

from terminaluse.core.client_wrapper import AsyncClientWrapper, SyncClientWrapper


@pytest.mark.parametrize(
    ("token", "agent_api_key", "present_headers", "absent_headers"),
    [
        ("token-123", "", {"Authorization"}, {"x-agent-api-key"}),
        ("", "agent-123", {"x-agent-api-key"}, {"Authorization"}),
        ("", "", set(), {"Authorization", "x-agent-api-key"}),
    ],
)
def test_sync_wrapper_omits_absent_auth_headers(
    token: str,
    agent_api_key: str,
    present_headers: set[str],
    absent_headers: set[str],
) -> None:
    with httpx.Client() as httpx_client:
        wrapper = SyncClientWrapper(
            base_url="https://api.terminaluse.test",
            token=token,
            agent_api_key=agent_api_key,
            httpx_client=httpx_client,
        )
        headers = wrapper.get_headers()

    assert headers["User-Agent"].startswith("terminaluse/")
    for header in present_headers:
        assert header in headers
    for header in absent_headers:
        assert header not in headers


@pytest.mark.asyncio
@pytest.mark.parametrize(
    ("async_token", "agent_api_key", "present_headers", "absent_headers"),
    [
        ("token-123", "", {"Authorization"}, {"x-agent-api-key"}),
        ("", "agent-123", {"x-agent-api-key"}, {"Authorization"}),
        ("", "", set(), {"Authorization", "x-agent-api-key"}),
    ],
)
async def test_async_wrapper_omits_absent_auth_headers(
    async_token: str,
    agent_api_key: str,
    present_headers: set[str],
    absent_headers: set[str],
) -> None:
    async with httpx.AsyncClient() as httpx_client:
        wrapper = AsyncClientWrapper(
            base_url="https://api.terminaluse.test",
            token="",
            async_token=lambda: _return_async_token(async_token),
            agent_api_key=agent_api_key,
            httpx_client=httpx_client,
        )
        headers = await wrapper.async_get_headers()

    assert headers["User-Agent"].startswith("terminaluse/")
    for header in present_headers:
        assert header in headers
    for header in absent_headers:
        assert header not in headers


async def _return_async_token(token: str) -> str:
    return token
