import asyncio
from types import SimpleNamespace

from beaver.engine.loop import AgentProfile
from beaver.engine.providers.anthropic import AnthropicProvider
from beaver.engine.providers.litellm import LiteLLMProvider


def test_agent_profile_uses_provider_output_default() -> None:
    assert AgentProfile().max_tokens is None


def test_litellm_omits_max_tokens_when_unset(monkeypatch) -> None:
    captured_kwargs: dict = {}

    async def fake_acompletion(**kwargs):
        captured_kwargs.update(kwargs)
        return SimpleNamespace(
            choices=[
                SimpleNamespace(
                    message=SimpleNamespace(content="ok", tool_calls=[]),
                    finish_reason="stop",
                )
            ],
            usage=None,
        )

    monkeypatch.setattr("beaver.engine.providers.litellm.acompletion", fake_acompletion)

    async def run_case():
        provider = LiteLLMProvider(default_model="openai/gpt-test")
        return await provider.chat(messages=[{"role": "user", "content": "hi"}], max_tokens=None)

    response = asyncio.run(run_case())

    assert response.content == "ok"
    assert "max_tokens" not in captured_kwargs


def test_anthropic_uses_model_output_ceiling_when_unset(monkeypatch) -> None:
    captured_kwargs: dict = {}

    class FakeMessages:
        async def create(self, **kwargs):
            captured_kwargs.update(kwargs)
            return SimpleNamespace(
                content=[SimpleNamespace(type="text", text="ok")],
                usage=None,
                stop_reason="stop",
            )

    class FakeClient:
        messages = FakeMessages()

    monkeypatch.setattr(AnthropicProvider, "_client_or_raise", lambda self: FakeClient())

    async def run_case():
        provider = AnthropicProvider(default_model="claude-sonnet-4-5")
        return await provider.chat(messages=[{"role": "user", "content": "hi"}], max_tokens=None)

    response = asyncio.run(run_case())

    assert response.content == "ok"
    assert captured_kwargs["max_tokens"] == 64_000
