"""`build_adapter` provider→适配器分派（C1扩，T5.4 follow-up #2）。

只断「按 provider 名选对适配器类 + provider 字段透传」，不联网（客户端构造无网络 IO，
api_key 仅持有不校验）。Anthropic/Gemini 分支懒 import 真 SDK（已 `uv sync`）。
"""

from __future__ import annotations

from ww_llm_gateway import build_adapter
from ww_llm_gateway.adapters.anthropic import AnthropicAdapter
from ww_llm_gateway.adapters.gemini import GeminiAdapter
from ww_llm_gateway.adapters.openai_compat import OpenAICompatAdapter

_FAKE_KEY = "sk-test-not-a-real-key"


def test_dispatches_anthropic_provider_to_anthropic_adapter() -> None:
    adapter = build_adapter("anthropic", api_key=_FAKE_KEY)

    assert isinstance(adapter, AnthropicAdapter)
    assert adapter.provider == "anthropic"


def test_dispatches_gemini_provider_to_gemini_adapter() -> None:
    adapter = build_adapter("gemini", api_key=_FAKE_KEY)

    assert isinstance(adapter, GeminiAdapter)
    assert adapter.provider == "gemini"


def test_dispatches_google_alias_to_gemini_adapter() -> None:
    adapter = build_adapter("google", api_key=_FAKE_KEY)

    assert isinstance(adapter, GeminiAdapter)
    assert adapter.provider == "google"


def test_dispatches_deepseek_to_openai_compat_adapter() -> None:
    adapter = build_adapter("deepseek", api_key=_FAKE_KEY, base_url="https://api.deepseek.com")

    assert isinstance(adapter, OpenAICompatAdapter)
    assert adapter.provider == "deepseek"


def test_dispatches_unknown_provider_to_openai_compat_adapter() -> None:
    # kimi/qwen/glm/openai 等一律走 OpenAI 兼容默认分支。
    for provider in ("openai", "kimi", "qwen", "glm"):
        adapter = build_adapter(provider, api_key=_FAKE_KEY)

        assert isinstance(adapter, OpenAICompatAdapter)
        assert adapter.provider == provider


def test_anthropic_adapter_capabilities_reflect_native_structured_output() -> None:
    adapter = build_adapter("anthropic", api_key=_FAKE_KEY)
    caps = adapter.capabilities()

    assert caps.structured_output is True
    assert caps.prefix_cache is True
