feat: Add Whisper transcription client

Introduces a new module for interacting with the Whisper ASR service.
This includes
an async function to POST audio files and retrieve transcripts. Error
handling
for network and HTTP status codes is also implemented.

New tests are added to cover successful transcription, HTTP error
responses,
and request timeouts.
This commit is contained in:
2026-04-13 16:40:20 +02:00
parent 73692b0a02
commit 84f8df2bf4
3 changed files with 147 additions and 0 deletions
+73
View File
@@ -1,6 +1,10 @@
use std::path::PathBuf;
use std::time::Duration;
use doctate_server::transcribe::ffmpeg::remux_faststart;
use doctate_server::transcribe::whisper::{transcribe, WhisperError};
use wiremock::matchers::{method, path, query_param};
use wiremock::{Mock, MockServer, ResponseTemplate};
fn fixture(name: &str) -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
@@ -43,3 +47,72 @@ async fn ffmpeg_remux_fails_on_missing_input() {
let result = remux_faststart(&input).await;
assert!(result.is_err(), "expected error for missing input");
}
#[tokio::test]
async fn whisper_client_posts_multipart_and_returns_text() {
let server = MockServer::start().await;
let expected = "Hallo Welt, das ist ein Test.";
Mock::given(method("POST"))
.and(path("/asr"))
.and(query_param("output", "txt"))
.and(query_param("encode", "false"))
.respond_with(ResponseTemplate::new(200).set_body_string(expected))
.expect(1)
.mount(&server)
.await;
let client = reqwest::Client::new();
let text = transcribe(&client, &server.uri(), &fixture("sample.m4a"), Duration::from_secs(10))
.await
.expect("transcribe failed");
assert_eq!(text, expected);
}
#[tokio::test]
async fn whisper_client_returns_status_error_on_500() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/asr"))
.respond_with(ResponseTemplate::new(500).set_body_string("boom"))
.mount(&server)
.await;
let client = reqwest::Client::new();
let err = transcribe(&client, &server.uri(), &fixture("sample.m4a"), Duration::from_secs(10))
.await
.expect_err("expected error");
match err {
WhisperError::Status { status, body } => {
assert_eq!(status, 500);
assert_eq!(body, "boom");
}
other => panic!("unexpected error variant: {other:?}"),
}
}
#[tokio::test]
async fn whisper_client_times_out() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/asr"))
.respond_with(ResponseTemplate::new(200).set_delay(Duration::from_secs(2)))
.mount(&server)
.await;
let client = reqwest::Client::new();
let err = transcribe(
&client,
&server.uri(),
&fixture("sample.m4a"),
Duration::from_millis(100),
)
.await
.expect_err("expected timeout");
assert!(matches!(err, WhisperError::Http(_)), "expected Http error, got {err:?}");
}