"""Tests for llmhosts_gpu — runs against REAL compiled Rust extension.

Skipped in CI when the Rust extension is not built (maturin develop required).
"""

from __future__ import annotations

import pytest

from llmhosts.inference.gpu import LLMHOSTS_GPU_AVAILABLE

requires_gpu_ext = pytest.mark.skipif(
    not LLMHOSTS_GPU_AVAILABLE,
    reason="Rust extension _llmhosts_gpu not built (run: cd llmhosts_gpu && maturin develop)",
)


@pytest.mark.unit
class TestLLMHostsGpuReal:
    @requires_gpu_ext
    def test_extension_is_available(self) -> None:
        assert LLMHOSTS_GPU_AVAILABLE is True

    def test_detect_returns_valid_topology(self) -> None:
        from llmhosts.inference.gpu import detect_gpus

        result = detect_gpus()
        assert "gpus" in result
        assert "total_vram_mb" in result
        assert "recommended_strategy" in result
        assert isinstance(result["gpus"], list)

    def test_detect_strategy_is_valid_enum(self) -> None:
        from llmhosts.inference.gpu import detect_gpus

        result = detect_gpus()
        valid = {"CpuOnly", "SingleGpu", "TensorParallel", "PhaseSplit"}
        assert result["recommended_strategy"] in valid


@pytest.mark.unit
class TestLLMHostsGpuGracefulDegradation:
    def test_fallback_when_extension_removed(self, monkeypatch: pytest.MonkeyPatch) -> None:
        import llmhosts.inference.gpu as gpu_mod

        monkeypatch.setattr(gpu_mod, "_detect_gpus_native", None)
        result = gpu_mod.detect_gpus()
        assert result["gpus"] == []
        assert result["recommended_strategy"] == "CpuOnly"
