"""Auth-factory contract: OAuth tokens need the oauth beta header; an API key
(explicit billing choice) wins; double-credential rejection is avoided."""
from __future__ import annotations

import recoil.core.anthropic_client as ac


def test_oauth_token_gets_beta_header(monkeypatch):
    monkeypatch.delenv("ANTHROPIC_API_KEY", raising=False)
    monkeypatch.setenv("ANTHROPIC_AUTH_TOKEN", "sk-ant-oat01-test")
    client = ac.anthropic_client()
    assert client.auth_token == "sk-ant-oat01-test"
    assert client.default_headers.get("anthropic-beta") == "oauth-2025-04-20"


def test_falls_back_to_claude_code_token(monkeypatch):
    monkeypatch.delenv("ANTHROPIC_API_KEY", raising=False)
    monkeypatch.delenv("ANTHROPIC_AUTH_TOKEN", raising=False)
    monkeypatch.setenv("CLAUDE_CODE_OAUTH_TOKEN", "sk-ant-oat01-cc")
    client = ac.anthropic_client()
    assert client.auth_token == "sk-ant-oat01-cc"


def test_api_key_wins_and_drops_auth_token(monkeypatch):
    monkeypatch.setenv("ANTHROPIC_API_KEY", "sk-ant-api-test")
    monkeypatch.setenv("ANTHROPIC_AUTH_TOKEN", "sk-ant-oat01-test")
    client = ac.anthropic_client()
    assert client.api_key == "sk-ant-api-test"
    assert client.auth_token is None
