Refactor tool-speechtotext: extract sttlib shared library and add tests
Extract duplicated code (Whisper loading, audio recording, transcription, VAD processing) into reusable sttlib/ package. Rewrite all 3 scripts as thin wrappers. Add 24 unit tests with mocked hardware. Fix GPU fallback bug in assistant.py and args.system assignment bug.
This commit is contained in:
37
python/tool-speechtotext/tests/test_whisper_loader.py
Normal file
37
python/tool-speechtotext/tests/test_whisper_loader.py
Normal file
@@ -0,0 +1,37 @@
|
||||
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
|
||||
Reference in New Issue
Block a user