testsAndMisc/linux_configuration/tests/test_transcribe_wav.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

59 lines
1.8 KiB
Python

"""Tests for the stdlib-only sine WAV generator.
transcribe.sh uses it to produce a self-test tone without pulling in numpy or
any audio library, so this exercises the real file it writes.
"""
from __future__ import annotations
import logging
from typing import TYPE_CHECKING
import wave
import _transcribe_probes as helpers
if TYPE_CHECKING:
from pathlib import Path
import pytest
# --------------------------------------------------------------------------- #
# generate_sine_wav
# --------------------------------------------------------------------------- #
def test_generate_sine_wav_writes_a_readable_mono_16k_wav(tmp_path: Path) -> None:
"""The generated file is a real WAV with the documented parameters."""
out = tmp_path / "tone.wav"
assert helpers.generate_sine_wav(str(out)) is True
with wave.open(str(out)) as wf:
assert wf.getnchannels() == 1
assert wf.getsampwidth() == 2
assert wf.getframerate() == 16000
assert wf.getnframes() == 16000 * 3
def test_generate_sine_wav_honours_its_parameters(tmp_path: Path) -> None:
"""Duration and sample rate change the frame count accordingly."""
out = tmp_path / "tone.wav"
helpers.generate_sine_wav(str(out), duration=1, sample_rate=8000)
with wave.open(str(out)) as wf:
assert wf.getframerate() == 8000
assert wf.getnframes() == 8000
def test_generate_sine_wav_reports_failure_on_an_unwritable_path(
tmp_path: Path, caplog: pytest.LogCaptureFixture
) -> None:
"""An OSError is logged and reported as False rather than raised."""
unwritable = tmp_path / "missing-dir" / "tone.wav"
with caplog.at_level(logging.ERROR):
result = helpers.generate_sine_wav(str(unwritable))
assert result is False
assert "Failed to generate WAV" in caplog.text