diff --git a/tests/test_transcription.py b/tests/test_transcription.py index 22a2e6d..cac99c6 100644 --- a/tests/test_transcription.py +++ b/tests/test_transcription.py @@ -31,3 +31,33 @@ def decode(command, **_kwargs): transcription.audio_transcript("audio.m4a") model.assert_not_called() assert all(not Path(path).parent.exists() for path in paths) + +def _decode_wav(frames: int): + def decode(command, **_kwargs): + with wave.Wave_write(command[-1]) as output: + output.setnchannels(1) + output.setsampwidth(2) + output.setframerate(16000) + output.writeframes(bytes(frames * 2)) + + return decode + + +def test_audio_exactly_at_the_duration_limit_is_accepted(mocker): + mocker.patch.object(transcription, "MAX_AUDIO_SECONDS", 2) + mocker.patch("subprocess.run", side_effect=_decode_wav(16000 * 2)) + model = mocker.patch.object(transcription, "load_model") + model.return_value.transcribe.return_value = {"text": "hello"} + + assert transcription.audio_transcript("audio.m4a") == "hello" + model.assert_called_once() + + +def test_audio_one_frame_over_the_duration_limit_is_rejected(mocker): + mocker.patch.object(transcription, "MAX_AUDIO_SECONDS", 2) + mocker.patch("subprocess.run", side_effect=_decode_wav(16000 * 2 + 1)) + model = mocker.patch.object(transcription, "load_model") + + with pytest.raises(transcription.InvalidAudio, match="exceeds"): + transcription.audio_transcript("audio.m4a") + model.assert_not_called()