Files
local-transcriber/tests/test_utils.py
T
ddadminandClaude Opus 4.6 dfc5f46bf3 feat(transcriber): реализована обёртка над faster-whisper (шаг 3)
- Зачем:
  - необходим модуль транскрипции с CUDA fallback для основного flow приложения.
- Что:
  - добавлена зависимость faster-whisper>=1.2.1 в pyproject.toml.
  - реализована функция transcribe() с fallback CUDA→CPU на всех этапах (загрузка модели, вызов transcribe, итерация сегментов).
  - исправлен IndexError в get_gpu_name() при пустом stdout nvidia-smi.
  - добавлено 6 тестов в test_transcriber.py и 1 тест в test_utils.py (16 тестов зелёные).
- Проверка:
  - uv run pytest -v (16 passed).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-17 22:30:40 +03:00

85 lines
2.4 KiB
Python

import subprocess
from pathlib import Path
from unittest.mock import patch
import pytest
from local_transcriber.utils import (
build_output_path,
detect_device,
get_gpu_name,
validate_input_file,
)
def test_validate_input_file_not_found(tmp_path):
missing = tmp_path / "no_such_file.mp3"
with pytest.raises(FileNotFoundError):
validate_input_file(missing)
def test_validate_input_file_empty(tmp_path):
empty = tmp_path / "empty.mp3"
empty.touch()
with pytest.raises(ValueError, match="пустой"):
validate_input_file(empty)
def test_validate_input_file_unknown_ext(tmp_path):
f = tmp_path / "notes.txt"
f.write_text("hello")
with pytest.warns(UserWarning, match="не входит в список"):
result = validate_input_file(f)
assert result == f.resolve()
def test_validate_input_file_ok(tmp_path):
f = tmp_path / "audio.mp3"
f.write_bytes(b"\x00" * 16)
result = validate_input_file(f)
assert result == f.resolve()
def test_build_output_path_default():
inp = Path("/some/dir/meeting-2026-03-17.mp4")
result = build_output_path(inp)
assert result == Path("/some/dir/meeting-2026-03-17-transcript.md")
def test_build_output_path_custom():
inp = Path("/some/dir/audio.mp3")
custom = Path("/out/result.md")
result = build_output_path(inp, output=custom)
assert result == custom
def test_detect_device_explicit():
assert detect_device("cpu") == "cpu"
assert detect_device("cuda") == "cuda"
def test_get_gpu_name_no_nvidia_smi():
with patch("shutil.which", return_value=None):
with patch("subprocess.run", side_effect=FileNotFoundError):
result = get_gpu_name()
assert result is None
def test_get_gpu_name_empty_stdout():
"""nvidia-smi returns 0 but stdout is empty — should return None, not crash."""
mock_result = subprocess.CompletedProcess(
args=[], returncode=0, stdout="", stderr=""
)
with patch("subprocess.run", return_value=mock_result):
result = get_gpu_name()
assert result is None
def test_get_gpu_name_success():
mock_result = subprocess.CompletedProcess(
args=[], returncode=0, stdout="NVIDIA GeForce RTX 3060\n", stderr=""
)
with patch("subprocess.run", return_value=mock_result):
result = get_gpu_name()
assert result == "NVIDIA GeForce RTX 3060"