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:
local
2026-02-08 00:40:31 +00:00
parent 848681087e
commit 104da381fb
15 changed files with 480 additions and 195 deletions

View 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