"""Test Anthropic OAuth provider."""
import pytest
from unittest.mock import AsyncMock, patch, MagicMock
from nanobot.providers.anthropic_oauth import AnthropicOAuthProvider
from nanobot.providers.base import LLMResponse


@pytest.fixture
def provider():
    """Create provider with test OAuth token."""
    return AnthropicOAuthProvider(
        oauth_token="sk-ant-oat01-test-token",
        default_model="claude-opus-4-5"
    )


def test_provider_init(provider):
    """Provider should initialize with OAuth token."""
    assert provider.oauth_token == "sk-ant-oat01-test-token"
    assert provider.default_model == "claude-opus-4-5"


def test_provider_uses_bearer_auth(provider):
    """Provider should use Bearer auth, not x-api-key."""
    headers = provider._get_headers()
    assert "Authorization" in headers
    assert headers["Authorization"].startswith("Bearer ")
    assert "x-api-key" not in headers


# test_chat_prepends_system_prompt removed - feature no longer exists
# System prompt handling is done by the agent loop, not the provider


def test_parse_response_text(provider):
    """Should parse text response correctly."""
    response = {
        "content": [{"type": "text", "text": "Hello world"}],
        "stop_reason": "end_turn",
        "usage": {"input_tokens": 10, "output_tokens": 5},
    }
    result = provider._parse_response(response)
    assert result.content == "Hello world"
    assert result.finish_reason == "end_turn"
    assert result.usage["prompt_tokens"] == 10


def test_parse_response_tool_calls(provider):
    """Should parse tool call response correctly."""
    response = {
        "content": [
            {"type": "tool_use", "id": "call_1", "name": "read_file", "input": {"path": "/tmp/test"}}
        ],
        "stop_reason": "tool_use",
        "usage": {"input_tokens": 10, "output_tokens": 5},
    }
    result = provider._parse_response(response)
    assert len(result.tool_calls) == 1
    assert result.tool_calls[0].name == "read_file"
    assert result.tool_calls[0].arguments == {"path": "/tmp/test"}


def test_convert_tools_to_anthropic(provider):
    """Should convert OpenAI-format tools to Anthropic format."""
    openai_tools = [
        {
            "type": "function",
            "function": {
                "name": "read_file",
                "description": "Read a file",
                "parameters": {"type": "object", "properties": {"path": {"type": "string"}}}
            }
        }
    ]
    anthropic_tools = provider._convert_tools_to_anthropic(openai_tools)
    assert len(anthropic_tools) == 1
    assert anthropic_tools[0]["name"] == "read_file"
    assert "input_schema" in anthropic_tools[0]
