"""providers.py 注册表测试(M1 设计 §7;register_provider 为纯函数,无可变全局)。"""

import pytest

from polygateway.providers import (
    DEFAULT_PROFILES,
    ProviderProfile,
    get_provider,
    register_provider,
)


class TestDefaultProfiles:
    def test_qwen_profile(self):
        p = get_provider("qwen")
        assert p.thinking_on == {"enable_thinking": True}
        assert p.thinking_off == {"enable_thinking": False}
        assert p.strip_think_tags is True
        assert p.supports_native_schema is False

    def test_deepseek_profile(self):
        p = get_provider("deepseek")
        assert p.thinking_on == {"thinking": {"type": "enabled"}}
        assert p.thinking_off == {"thinking": {"type": "disabled"}}
        assert p.strip_think_tags is False

    def test_openai_slots_are_unknown_not_empty(self):
        """issue #5: 该段名实践中被复用为任意兼容厂商的兜底(下游把 kimi 挂在此),

        故不能下发任何厂商方言参数。None = 形态未知 → 配了 enable_thinking 即报错,
        而不是空字典那种"注入了个寂寞"的静默失效。
        """
        p = get_provider("openai")
        assert p.thinking_on is None and p.thinking_off is None
        assert p.strip_think_tags is False

    def test_minimax_profile_uses_reasoning_effort(self):
        """2026-08-02 实测: reasoning_effort 才是 MiniMax 认的开关。"""
        p = get_provider("minimax")
        assert p.thinking_off == {"reasoning_effort": "none"}
        assert p.thinking_on == {"reasoning_effort": "medium"}
        assert p.strip_think_tags is False

    def test_unknown_provider_fails_loudly(self):
        """消灭子串猜测: 未注册 provider 装配期即报错,不做模糊匹配。"""
        with pytest.raises(ValueError, match="glm"):
            get_provider("glm")
        with pytest.raises(ValueError):
            get_provider("qwen2")  # 子串相似也不放行


class TestPureFunctionRegistration:
    def test_register_returns_new_mapping(self):
        glm = ProviderProfile(name="glm", thinking_on={}, thinking_off={}, strip_think_tags=False)
        table = register_provider(glm)
        assert get_provider("glm", registry=table) is glm
        # 默认表未被污染(无可变全局状态铁律)
        with pytest.raises(ValueError):
            get_provider("glm")

    def test_register_on_custom_base_and_override(self):
        custom_qwen = ProviderProfile(
            name="qwen", thinking_on={"x": 1}, thinking_off={}, strip_think_tags=False
        )
        table = register_provider(custom_qwen, base=DEFAULT_PROFILES)
        assert get_provider("qwen", registry=table).thinking_on == {"x": 1}
        assert get_provider("qwen").thinking_on == {"enable_thinking": True}

    def test_default_profiles_mapping_is_read_only(self):
        with pytest.raises(TypeError):
            DEFAULT_PROFILES["hack"] = None  # type: ignore[index]
