from unittest.mock import patch, MagicMock
from sttlib.whisper_loader import load_whisper_model


@patch("sttlib.whisper_loader.WhisperModel")
def test_gpu_success(mock_cls):
    mock_model = MagicMock()
    mock_cls.return_value = mock_model

    result = load_whisper_model("base")

    assert result is mock_model
    mock_cls.assert_called_once_with("base", device="cuda", compute_type="float16")


@patch("sttlib.whisper_loader.WhisperModel")
def test_gpu_fails_cpu_fallback(mock_cls):
    mock_model = MagicMock()
    mock_cls.side_effect = [RuntimeError("no CUDA"), mock_model]

    result = load_whisper_model("base")

    assert result is mock_model
    assert mock_cls.call_count == 2
    _, kwargs = mock_cls.call_args
    assert kwargs == {"device": "cpu", "compute_type": "int8"}


@patch("sttlib.whisper_loader.WhisperModel")
def test_both_fail_propagates(mock_cls):
    mock_cls.side_effect = RuntimeError("no device")

    try:
        load_whisper_model("base")
        assert False, "Should have raised"
    except RuntimeError:
        pass
