"""Subprocess adapter tests with synthetic silence and fake CLI output.""" from __future__ import annotations import json import subprocess import wave from decimal import Decimal from pathlib import Path from unittest.mock import patch import pytest from voice_dictation.core.local_transcriber import LocalTranscriber from voice_dictation.infrastructure.errors import TranscriptionError @pytest.fixture def local_setup(tmp_path): model = tmp_path / "model.bin" model.write_bytes(b"fake") executable = tmp_path / "whisper-cli" executable.touch() audio = tmp_path / "silence.wav" with wave.open(str(audio), "wb") as stream: stream.setnchannels(1) stream.setsampwidth(2) stream.setframerate(16000) stream.writeframes(b"\x00\x00" * 16000) return LocalTranscriber(model, executable), audio def test_pcm_wav_transcription_is_local(local_setup): transcriber, audio = local_setup calls = [] def run(command, **kwargs): calls.append(command) output = Path(command[command.index("-of") + 1]).with_suffix(".json") output.write_text( json.dumps( { "transcription": [{"text": " Привет "}, {"text": " мир "}], "result": {"language": "ru"}, } ) ) return subprocess.CompletedProcess(command, 0) with patch("voice_dictation.core.local_transcriber.subprocess.run", side_effect=run): result = transcriber.transcribe(audio, prompt="terms", model="whisper-base") assert result.text == "Привет мир" assert result.cost_usd == Decimal("0") assert result.duration_sec == 1.0 assert result.language == "ru" assert result.model == "whisper-base" assert result.prompt_used == "terms" assert len(calls) == 1 assert "--prompt" in calls[0] assert calls[0][calls[0].index("-l") + 1] == "auto" assert not Path(calls[0][calls[0].index("-of") + 1]).parent.exists() def test_missing_model_no_process(tmp_path): with patch("voice_dictation.core.local_transcriber.subprocess.run") as run: with pytest.raises(TranscriptionError, match="не установлена"): LocalTranscriber(tmp_path / "absent").transcribe(tmp_path / "audio") run.assert_not_called() def test_timeout_no_fallback(local_setup): transcriber, audio = local_setup with ( patch( "voice_dictation.core.local_transcriber.subprocess.run", side_effect=subprocess.TimeoutExpired("whisper-cli", 120), ), pytest.raises(TranscriptionError, match="время ожидания"), ): transcriber.transcribe(audio) def test_failure_does_not_expose_stderr(local_setup): transcriber, audio = local_setup with ( patch( "voice_dictation.core.local_transcriber.subprocess.run", return_value=subprocess.CompletedProcess([], 1, stderr=b"PRIVATE TRANSCRIPT"), ), pytest.raises(TranscriptionError, match="кодом 1") as exc, ): transcriber.transcribe(audio) assert "PRIVATE" not in str(exc.value) def test_invalid_json(local_setup): transcriber, audio = local_setup def run(command, **kwargs): Path(command[command.index("-of") + 1]).with_suffix(".json").write_text("[]") return subprocess.CompletedProcess(command, 0) with ( patch("voice_dictation.core.local_transcriber.subprocess.run", side_effect=run), pytest.raises(TranscriptionError, match="результат"), ): transcriber.transcribe(audio) def test_opus_converted_before_inference(local_setup, tmp_path): transcriber, source = local_setup audio = tmp_path / "audio.ogg" audio.write_bytes(b"fake ogg") calls = [] def run(command, **kwargs): calls.append(command) if "-of" in command: Path(command[command.index("-of") + 1]).with_suffix(".json").write_text( '{"transcription": [{"text": "ok"}]}' ) else: Path(command[-1]).write_bytes(source.read_bytes()) return subprocess.CompletedProcess(command, 0) with ( patch("voice_dictation.core.local_transcriber.subprocess.run", side_effect=run), patch( "voice_dictation.core.local_transcriber.discover_executable", return_value=Path("/fake/ffmpeg"), ), ): assert transcriber.transcribe(audio).text == "ok" assert len(calls) == 2 assert "pcm_s16le" in calls[0]