"""Tests for llmhosts.tier -- tier detection and capabilities."""

from __future__ import annotations

from unittest.mock import patch

from llmhosts.tier import detect_tier, get_capabilities


class TestDetectTier:
    def test_core_tier_no_extras(self) -> None:
        with patch("importlib.util.find_spec", return_value=None):
            assert detect_tier() == "core"

    def test_smart_tier_onnx_only(self) -> None:
        def fake_find_spec(name: str):
            if name == "onnxruntime":
                return True  # Not None = found
            return None

        with patch("importlib.util.find_spec", side_effect=fake_find_spec):
            assert detect_tier() == "smart"

    def test_full_tier_torch(self) -> None:
        def fake_find_spec(name: str):
            return True  # Everything found

        with patch("importlib.util.find_spec", side_effect=fake_find_spec):
            assert detect_tier() == "full"


class TestGetCapabilities:
    def test_core_capabilities(self) -> None:
        caps = get_capabilities("core")
        assert caps["proxy"] is True
        assert caps["openai_compat"] is True
        assert caps["anthropic_compat"] is True
        assert caps["rule_based_router"] is True
        assert caps["exact_hash_cache"] is True
        assert caps["tui_dashboard"] is True
        assert caps["web_dashboard"] is True
        assert caps["byok_management"] is True
        assert caps["cost_tracking"] is True
        assert caps["audit_logging"] is True
        assert caps["health_monitoring"] is True
        assert caps["fallback_chain"] is True
        assert caps["latency_tracking"] is True
        # Smart/full features OFF
        assert caps["knn_router"] is False
        assert caps["modernbert_router"] is False
        assert caps["qwen_router"] is False
        assert caps["faiss_gpu"] is False

    def test_smart_capabilities(self) -> None:
        caps = get_capabilities("smart")
        assert caps["knn_router"] is True
        assert caps["modernbert_router"] is True
        assert caps["namespace_cache"] is True
        assert caps["vcache_semantic"] is True
        assert caps["embedding_pipeline"] is True
        # Full features still OFF
        assert caps["qwen_router"] is False
        assert caps["faiss_gpu"] is False
        assert caps["vllm_integration"] is False

    def test_full_capabilities(self) -> None:
        caps = get_capabilities("full")
        assert caps["knn_router"] is True
        assert caps["qwen_router"] is True
        assert caps["faiss_gpu"] is True
        assert caps["vllm_integration"] is True

    def test_auto_detect_when_none(self) -> None:
        with patch("llmhosts.tier.detect_tier", return_value="core"):
            caps = get_capabilities(None)
            assert caps["knn_router"] is False

    def test_all_capabilities_have_bool_values(self) -> None:
        for tier in ("core", "smart", "full"):
            caps = get_capabilities(tier)
            for key, value in caps.items():
                assert isinstance(value, bool), f"{key} is not bool in {tier}"
