testsAndMisc/linux_configuration/tests/test_transcribe_model_ops.py
Krzysztof kuhy Rudnicki bda24b6305
Some checks are pending
Pre-commit checks / pre-commit (push) Waiting to run
Python tests / test (push) Waiting to run
Shell tests / shell-tests (push) Waiting to run
Shell tests / arch-tests (push) Waiting to run
transcribe_helpers: split 289 -> 177 + 142
Split along the natural seam now that the file is fully covered:

  _transcribe_probes.py  177  _try_import, the four dependency probes,
                              generate_sine_wav, prepare_model, test_cuda
  transcribe_helpers.py  142  the eight _handle_* functions and main()

All 18 top-level definitions verified AST-identical to HEAD, and an AST
scan confirms neither module references a name it does not bind.

transcribe.sh is untouched - it invokes tools/transcribe_helpers.py by
path, and that path still exists with the same name and executable bit.
The CLI tests' patch targets did not move either, because main() dispatches
to names imported into its own namespace.

Verified by running every subcommand and reading the exit status directly,
since those codes are the contract transcribe.sh branches on:
python-version 0, check-faster-whisper 7, check-ctranslate2 1,
check-diarization 0, deps-installed 0; generate-wav writes the same
96044-byte WAV as before the split. 41 tests pass at 100% line and branch
on both modules; 243 pass in the directory. Over-cap files 35 -> 34.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011w2Pj4Ek3ZNMQVYcFHgXDY
2026-08-17 13:33:38 +02:00

144 lines
4.6 KiB
Python

"""Tests for transcribe.sh's model download and CUDA probe.
Both reach faster_whisper through ``_try_import``, so the package itself is
never needed; the double records how the model would have been constructed.
"""
from __future__ import annotations
import logging
from typing import TYPE_CHECKING
import _transcribe_probes as helpers
if TYPE_CHECKING:
from collections.abc import Callable
from conftest import FakeWhisper
import pytest
# --------------------------------------------------------------------------- #
# prepare_model / test_cuda
# --------------------------------------------------------------------------- #
def test_prepare_model_downloads_with_cpu_int8(
fake_whisper: type[FakeWhisper],
importer: Callable[[dict[str, object]], object],
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""The model is fetched on CPU in int8, into the requested directory."""
fw = fake_whisper()
monkeypatch.setattr(helpers, "_try_import", importer({"faster_whisper": fw}))
assert helpers.prepare_model("small", "/models") is True
assert fw.calls == [
{
"name": "small",
"device": "cpu",
"compute_type": "int8",
"download_root": "/models",
}
]
def test_prepare_model_enables_progress_bars_when_hub_is_present(
fake_whisper: type[FakeWhisper],
importer: Callable[[dict[str, object]], object],
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""huggingface_hub progress bars are turned back on for the download."""
fw = fake_whisper()
hub_logging = type("L", (), {"set_verbosity_info": lambda self: None})()
hub = type("H", (), {"constants": type("C", (), {})()})()
monkeypatch.setattr(
helpers,
"_try_import",
importer(
{
"faster_whisper": fw,
"huggingface_hub.logging": hub_logging,
"huggingface_hub": hub,
}
),
)
monkeypatch.delenv("HF_HUB_DISABLE_PROGRESS_BARS", raising=False)
assert helpers.prepare_model("small", "/models") is True
assert hub.constants.HF_HUB_DISABLE_PROGRESS_BARS is False
def test_prepare_model_fails_without_faster_whisper(
fake_whisper: type[FakeWhisper],
importer: Callable[[dict[str, object]], object],
monkeypatch: pytest.MonkeyPatch,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Without the package there is nothing to download."""
monkeypatch.setattr(helpers, "_try_import", importer({}))
with caplog.at_level(logging.ERROR):
result = helpers.prepare_model("small", "/models")
assert result is False
assert "not installed" in caplog.text
def test_prepare_model_reports_a_download_failure(
fake_whisper: type[FakeWhisper],
importer: Callable[[dict[str, object]], object],
monkeypatch: pytest.MonkeyPatch,
caplog: pytest.LogCaptureFixture,
) -> None:
"""A network or disk error is logged and reported, not raised."""
failing = fake_whisper(error=OSError("no space left"))
monkeypatch.setattr(helpers, "_try_import", importer({"faster_whisper": failing}))
with caplog.at_level(logging.ERROR):
result = helpers.prepare_model("small", "/models")
assert result is False
assert "Failed to prepare model" in caplog.text
def test_test_cuda_initialises_a_tiny_model_on_the_gpu(
fake_whisper: type[FakeWhisper],
importer: Callable[[dict[str, object]], object],
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""The CUDA probe loads the tiny model in float16 on the GPU."""
fw = fake_whisper()
monkeypatch.setattr(helpers, "_try_import", importer({"faster_whisper": fw}))
assert helpers.test_cuda() is True
assert fw.calls == [{"name": "tiny", "device": "cuda", "compute_type": "float16"}]
def test_test_cuda_fails_without_faster_whisper(
fake_whisper: type[FakeWhisper],
importer: Callable[[dict[str, object]], object],
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""No package means no CUDA test."""
monkeypatch.setattr(helpers, "_try_import", importer({}))
assert helpers.test_cuda() is False
def test_test_cuda_reports_an_unusable_gpu(
fake_whisper: type[FakeWhisper],
importer: Callable[[dict[str, object]], object],
monkeypatch: pytest.MonkeyPatch,
caplog: pytest.LogCaptureFixture,
) -> None:
"""A machine without a working CUDA stack reports False, not a crash."""
no_cuda = fake_whisper(error=RuntimeError("CUDA driver not found"))
monkeypatch.setattr(helpers, "_try_import", importer({"faster_whisper": no_cuda}))
with caplog.at_level(logging.ERROR):
result = helpers.test_cuda()
assert result is False
assert "CUDA test failed" in caplog.text