use super::*;
use crate::{
AsrError, AudioBuffer, ErrorKind, Segment, SessionFailure, SessionOptions, SessionOutcome,
Transcript, TranscriptSnapshotSink,
};
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::{Duration, Instant};
fn wav_bytes(sample_rate: u32, secs: u32) -> Vec<u8> {
let samples = sample_rate * secs;
let data_len = samples * 2;
let mut out = Vec::with_capacity(44 + data_len as usize);
out.extend_from_slice(b"RIFF");
out.extend_from_slice(&(36 + data_len).to_le_bytes());
out.extend_from_slice(b"WAVE");
out.extend_from_slice(b"fmt ");
out.extend_from_slice(&16u32.to_le_bytes());
out.extend_from_slice(&1u16.to_le_bytes()); out.extend_from_slice(&1u16.to_le_bytes()); out.extend_from_slice(&sample_rate.to_le_bytes());
out.extend_from_slice(&(sample_rate * 2).to_le_bytes());
out.extend_from_slice(&2u16.to_le_bytes());
out.extend_from_slice(&16u16.to_le_bytes());
out.extend_from_slice(b"data");
out.extend_from_slice(&data_len.to_le_bytes());
for i in 0..samples {
let t = i as f32 / sample_rate as f32;
let v = (t * 440.0 * 2.0 * std::f32::consts::PI).sin() * 0.3;
out.extend_from_slice(&((v * i16::MAX as f32) as i16).to_le_bytes());
}
out
}
type Captured = std::sync::Arc<std::sync::Mutex<(Option<Duration>, Option<ErrorKind>)>>;
fn fixture_outcome() -> SessionOutcome {
SessionOutcome {
transcript: Transcript {
segments: vec![Segment {
id: "s0".to_string(),
index: 0,
text: "你好世界".to_string(),
start_seconds: Some(0.0),
end_seconds: Some(1.0),
}],
},
..Default::default()
}
}
struct ProgressEngine {
snapshots: Vec<String>,
outcome: Result<String, ErrorKind>,
cancelled: Arc<AtomicBool>,
}
impl Transcribe for ProgressEngine {
fn transcribe(
&self,
_buffer: &AudioBuffer,
_options: SessionOptions,
_deadline: Instant,
) -> SessionResult {
Ok(fixture_outcome())
}
fn supports_streaming(&self) -> bool {
true
}
fn transcribe_streaming(
&self,
_buffer: &AudioBuffer,
_options: SessionOptions,
_deadline: Instant,
mut progress: TranscriptSnapshotSink,
) -> SessionResult {
for snapshot in &self.snapshots {
std::thread::sleep(Duration::from_millis(30));
if !progress.emit(snapshot) {
self.cancelled.store(true, Ordering::SeqCst);
return Err(Box::new(SessionFailure {
error: AsrError::new(ErrorKind::Cancelled, "engine", "client went away"),
outcome: SessionOutcome::default(),
}));
}
}
match &self.outcome {
Ok(text) => Ok(SessionOutcome {
transcript: Transcript {
segments: vec![Segment {
id: "s0".to_string(),
index: 0,
text: text.clone(),
start_seconds: Some(0.0),
end_seconds: Some(1.0),
}],
},
..Default::default()
}),
Err(kind) => Err(Box::new(SessionFailure {
error: AsrError::new(*kind, "engine", "the mock refuses"),
outcome: SessionOutcome::default(),
})),
}
}
}
struct MockEngine {
captured: Captured,
}
impl Transcribe for MockEngine {
fn transcribe(
&self,
_buffer: &AudioBuffer,
_options: SessionOptions,
deadline: Instant,
) -> SessionResult {
let mut captured = self.captured.lock().unwrap();
captured.0 = Some(deadline.duration_since(Instant::now()));
if let Some(kind) = captured.1 {
return Err(Box::new(SessionFailure {
error: AsrError::new(kind, "engine", "the mock refuses"),
outcome: SessionOutcome::default(),
}));
}
Ok(fixture_outcome())
}
}
struct DeadlineBlindEngine {
release: Arc<AtomicBool>,
}
impl Transcribe for DeadlineBlindEngine {
fn transcribe(
&self,
_buffer: &AudioBuffer,
_options: SessionOptions,
_deadline: Instant,
) -> SessionResult {
let started = Instant::now();
while !self.release.load(Ordering::Relaxed) && started.elapsed() < Duration::from_secs(30) {
std::thread::sleep(Duration::from_millis(50));
}
Ok(fixture_outcome())
}
}
struct SingleSessionEngine {
active: Arc<std::sync::Mutex<usize>>,
}
impl Transcribe for SingleSessionEngine {
fn transcribe(
&self,
_buffer: &AudioBuffer,
_options: SessionOptions,
_deadline: Instant,
) -> SessionResult {
let mut active = self.active.lock().unwrap();
*active += 1;
if *active > 1 {
*active -= 1;
return Err(Box::new(SessionFailure {
error: AsrError::new(ErrorKind::Busy, "engine", "the mock is at its limit"),
outcome: SessionOutcome::default(),
}));
}
drop(active);
std::thread::sleep(Duration::from_millis(200));
*self.active.lock().unwrap() -= 1;
Ok(fixture_outcome())
}
}
fn mock_options() -> ServeOptions {
ServeOptions {
model_id: "test-model".to_string(),
family: "SenseVoice".to_string(),
..ServeOptions::default()
}
}
async fn spawn(engine: Arc<dyn Transcribe>, options: ServeOptions) -> String {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
tokio::spawn(async move {
axum::serve(listener, router(engine, options))
.await
.unwrap();
});
format!("http://{addr}")
}
async fn upload(base: &str, form: reqwest::multipart::Form) -> reqwest::Response {
reqwest::Client::new()
.post(format!("{base}/v1/audio/transcriptions"))
.multipart(form)
.send()
.await
.unwrap()
}
fn file_form(bytes: Vec<u8>) -> reqwest::multipart::Form {
reqwest::multipart::Form::new().part(
"file",
reqwest::multipart::Part::bytes(bytes)
.file_name("clip.wav")
.mime_str("audio/wav")
.unwrap(),
)
}
#[tokio::test]
async fn a_well_formed_upload_transcribes_to_the_openai_body() {
let captured: Captured = Captured::default();
let base = spawn(
Arc::new(MockEngine {
captured: captured.clone(),
}),
mock_options(),
)
.await;
let response = upload(&base, file_form(wav_bytes(8000, 2))).await;
assert_eq!(response.status(), 200);
let body: serde_json::Value = response.json().await.unwrap();
assert_eq!(body["text"], "你好世界");
}
#[tokio::test]
async fn a_missing_file_field_is_a_named_bad_request() {
let base = spawn(
Arc::new(MockEngine {
captured: Captured::default(),
}),
mock_options(),
)
.await;
let form = reqwest::multipart::Form::new().text("model", "whisper-1");
let response = upload(&base, form).await;
assert_eq!(response.status(), 400);
let body: serde_json::Value = response.json().await.unwrap();
assert!(body["error"]["message"]
.as_str()
.unwrap()
.contains("'file' is required"));
}
#[tokio::test]
async fn unsupported_response_formats_are_refused() {
let base = spawn(
Arc::new(MockEngine {
captured: Captured::default(),
}),
mock_options(),
)
.await;
let form = file_form(wav_bytes(8000, 1)).text("response_format", "verbose_json");
let response = upload(&base, form).await;
assert_eq!(response.status(), 400);
let body: serde_json::Value = response.json().await.unwrap();
assert!(body["error"]["message"]
.as_str()
.unwrap()
.contains("only 'json' and 'text' are supported"));
}
#[tokio::test]
async fn text_response_format_answers_plain_text() {
let base = spawn(
Arc::new(MockEngine {
captured: Captured::default(),
}),
mock_options(),
)
.await;
let form = file_form(wav_bytes(8000, 1)).text("response_format", "text");
let response = upload(&base, form).await;
assert_eq!(response.status(), 200);
let content_type = response
.headers()
.get("content-type")
.and_then(|value| value.to_str().ok())
.unwrap_or_default()
.to_string();
assert!(
content_type.starts_with("text/plain"),
"got content-type {content_type}"
);
assert_eq!(response.text().await.unwrap(), "你好世界");
}
#[tokio::test]
async fn streaming_answers_the_sse_event_protocol() {
let base = spawn(
Arc::new(ProgressEngine {
snapshots: vec!["你".to_string(), "你好".to_string()],
outcome: Ok("你好世界".to_string()),
cancelled: Arc::new(AtomicBool::new(false)),
}),
mock_options(),
)
.await;
let form = file_form(wav_bytes(8000, 1)).text("stream", "true");
let response = upload(&base, form).await;
assert_eq!(response.status(), 200);
let content_type = response
.headers()
.get("content-type")
.and_then(|value| value.to_str().ok())
.unwrap_or_default()
.to_string();
assert!(
content_type.starts_with("text/event-stream"),
"got content-type {content_type}"
);
let body = response.text().await.unwrap();
let frames = sse_frames(&body);
assert_eq!(
frames
.iter()
.map(|(event, _)| event.as_str())
.collect::<Vec<_>>(),
[
"transcript.text.delta",
"transcript.text.delta",
"transcript.text.done"
]
);
assert!(body.contains(r#""delta":"你""#), "{body}");
assert!(body.contains(r#""delta":"好""#), "{body}");
assert!(body.contains(r#""text":"你好世界""#), "{body}");
}
fn sse_frames(body: &str) -> Vec<(String, serde_json::Value)> {
let mut frames = Vec::new();
for block in body.split("\n\n") {
let mut event = String::new();
let mut data = Vec::new();
for line in block.lines() {
if let Some(name) = line.strip_prefix("event: ") {
event = name.to_string();
} else if let Some(payload) = line.strip_prefix("data: ") {
data.push(payload);
}
}
if let Ok(json) = data.join("\n").parse() {
frames.push((event, json));
}
}
frames
}
#[tokio::test]
async fn the_wire_opens_with_a_typed_delta_and_types_live_in_the_json() {
let base = spawn(
Arc::new(ProgressEngine {
snapshots: vec!["hello".to_string(), "hello world".to_string()],
outcome: Ok("hello world".to_string()),
cancelled: Arc::new(AtomicBool::new(false)),
}),
mock_options(),
)
.await;
let form = file_form(wav_bytes(8000, 1)).text("stream", "true");
let response = upload(&base, form).await;
assert_eq!(response.status(), 200);
let body = response.text().await.unwrap();
let frames = sse_frames(&body);
assert!(!frames.is_empty(), "{body}");
let (first_event, first_json) = &frames[0];
assert_eq!(first_event, "transcript.text.delta");
assert_eq!(first_json["delta"], "hello");
for (event, json) in &frames {
assert_eq!(json["type"], *event, "frame without a JSON type: {json}");
}
let (last_event, last_json) = frames.last().unwrap();
assert_eq!(last_event, "transcript.text.done");
assert_eq!(last_json["text"], "hello world");
let concatenated: String = frames[..frames.len() - 1]
.iter()
.map(|(_, json)| json["delta"].as_str().unwrap_or_default())
.collect();
assert_eq!(concatenated, "hello world");
}
#[tokio::test]
async fn the_wire_matches_the_openai_sse_fixture_byte_for_byte() {
let base = spawn(
Arc::new(ProgressEngine {
snapshots: vec!["你好".to_string()],
outcome: Ok("你好世界".to_string()),
cancelled: Arc::new(AtomicBool::new(false)),
}),
mock_options(),
)
.await;
let form = file_form(wav_bytes(8000, 1)).text("stream", "true");
let response = upload(&base, form).await;
assert_eq!(response.status(), 200);
let body = response.text().await.unwrap();
let fixture = concat!(
"event: transcript.text.delta\n",
"data: {\"delta\":\"你好\",\"type\":\"transcript.text.delta\"}\n",
"\n",
"event: transcript.text.done\n",
"data: {\"text\":\"你好世界\",\"type\":\"transcript.text.done\"}\n",
"\n",
);
assert_eq!(body, fixture);
}
#[tokio::test]
async fn a_snapshot_that_rewrites_sent_text_is_skipped_not_mangled() {
let base = spawn(
Arc::new(ProgressEngine {
snapshots: vec!["你好".to_string(), "您".to_string()],
outcome: Ok("您好".to_string()),
cancelled: Arc::new(AtomicBool::new(false)),
}),
mock_options(),
)
.await;
let form = file_form(wav_bytes(8000, 1)).text("stream", "true");
let response = upload(&base, form).await;
assert_eq!(response.status(), 200);
let body = response.text().await.unwrap();
assert!(body.contains(r#""delta":"你好""#), "{body}");
assert!(!body.contains("您好世界"), "{body}");
assert!(body.contains(r#""text":"您好""#), "{body}");
}
#[tokio::test]
async fn streaming_engine_failures_travel_as_error_events() {
let cases = [
(ErrorKind::Busy, "concurrent-session limit"),
(ErrorKind::Timeout, "transcription failed"),
(ErrorKind::Backend, "transcription failed"),
];
for (kind, expected) in cases {
let base = spawn(
Arc::new(ProgressEngine {
snapshots: vec![],
outcome: Err(kind),
cancelled: Arc::new(AtomicBool::new(false)),
}),
mock_options(),
)
.await;
let form = file_form(wav_bytes(8000, 1)).text("stream", "true");
let response = upload(&base, form).await;
assert_eq!(response.status(), 200, "kind {kind:?}");
let body = response.text().await.unwrap();
assert!(body.contains("event: error"), "kind {kind:?}: {body}");
assert!(body.contains(expected), "kind {kind:?}: {body}");
}
}
#[tokio::test]
async fn a_disconnecting_client_cancels_the_engine() {
let cancelled = Arc::new(AtomicBool::new(false));
let base = spawn(
Arc::new(ProgressEngine {
snapshots: (0..50).map(|i| "你".repeat(i + 1)).collect(),
outcome: Ok("你好世界".to_string()),
cancelled: cancelled.clone(),
}),
mock_options(),
)
.await;
let form = file_form(wav_bytes(8000, 1)).text("stream", "true");
let client = reqwest::Client::new();
let mut response = client
.post(format!("{base}/v1/audio/transcriptions"))
.multipart(form)
.send()
.await
.unwrap();
let chunk = response.chunk().await.unwrap().unwrap();
assert!(!chunk.is_empty());
drop(response);
let started = Instant::now();
while !cancelled.load(Ordering::SeqCst) {
assert!(
started.elapsed() < Duration::from_secs(10),
"the engine was never cancelled"
);
tokio::time::sleep(Duration::from_millis(20)).await;
}
}
#[tokio::test]
async fn a_non_streaming_wrapper_keeps_the_named_refusal() {
struct Wrapper {
inner: MockEngine,
}
impl Transcribe for Wrapper {
fn transcribe(
&self,
buffer: &AudioBuffer,
options: SessionOptions,
deadline: Instant,
) -> SessionResult {
self.inner.transcribe(buffer, options, deadline)
}
}
let base = spawn(
Arc::new(Wrapper {
inner: MockEngine {
captured: Captured::default(),
},
}),
mock_options(),
)
.await;
let form = file_form(wav_bytes(8000, 1)).text("stream", "true");
let response = upload(&base, form).await;
assert_eq!(response.status(), 400);
let body: serde_json::Value = response.json().await.unwrap();
assert!(
body["error"]["message"]
.as_str()
.unwrap()
.contains("no incremental endpoint"),
"{}",
body
);
}
#[tokio::test]
async fn streaming_with_text_format_is_refused() {
let base = spawn(
Arc::new(MockEngine {
captured: Captured::default(),
}),
mock_options(),
)
.await;
let form = file_form(wav_bytes(8000, 1))
.text("response_format", "text")
.text("stream", "true");
let response = upload(&base, form).await;
assert_eq!(response.status(), 400);
let body: serde_json::Value = response.json().await.unwrap();
assert!(
body["error"]["message"]
.as_str()
.unwrap()
.contains("requires response_format 'json'"),
"{}",
body
);
}
#[tokio::test]
async fn the_body_limit_answers_413() {
let mut options = mock_options();
options.max_body_bytes = 128;
let base = spawn(
Arc::new(MockEngine {
captured: Captured::default(),
}),
options,
)
.await;
let response = upload(&base, file_form(wav_bytes(8000, 1))).await;
assert_eq!(response.status(), 413);
}
#[tokio::test]
async fn engine_failures_map_onto_their_status_codes() {
for (kind, expected) in [
(ErrorKind::Busy, 503),
(ErrorKind::Timeout, 504),
(ErrorKind::InvalidInput, 400),
(ErrorKind::Backend, 500),
] {
let captured: Captured = Captured::default();
captured.lock().unwrap().1 = Some(kind);
let base = spawn(
Arc::new(MockEngine {
captured: captured.clone(),
}),
mock_options(),
)
.await;
let response = upload(&base, file_form(wav_bytes(8000, 1))).await;
assert_eq!(response.status(), expected, "kind {kind:?}");
let body: serde_json::Value = response.json().await.unwrap();
assert!(
body["error"]["message"].as_str().is_some(),
"kind {kind:?}: the error body names a message"
);
}
}
#[tokio::test]
async fn a_pinned_language_matches_or_refuses_the_request() {
let mut options = mock_options();
options.language = Some("zh".to_string());
let base = spawn(
Arc::new(MockEngine {
captured: Captured::default(),
}),
options,
)
.await;
let form = file_form(wav_bytes(8000, 1)).text("language", "zh");
assert_eq!(upload(&base, form).await.status(), 200);
let form = file_form(wav_bytes(8000, 1)).text("language", "en");
let response = upload(&base, form).await;
assert_eq!(response.status(), 400);
let body: serde_json::Value = response.json().await.unwrap();
assert!(body["error"]["message"].as_str().unwrap().contains("zh"));
let base = spawn(
Arc::new(MockEngine {
captured: Captured::default(),
}),
mock_options(),
)
.await;
let form = file_form(wav_bytes(8000, 1)).text("language", "en");
let response = upload(&base, form).await;
assert_eq!(response.status(), 400);
}
#[tokio::test]
async fn an_explicit_timeout_is_the_hard_deadline() {
let captured: Captured = Captured::default();
let mut options = mock_options();
options.timeout_secs = Some(30);
let base = spawn(
Arc::new(MockEngine {
captured: captured.clone(),
}),
options,
)
.await;
upload(&base, file_form(wav_bytes(8000, 100))).await;
let budget = captured.lock().unwrap().0.unwrap();
assert!(
budget >= Duration::from_secs(29) && budget <= Duration::from_secs(31),
"expected the explicit 30s wall, got {budget:?}"
);
}
#[tokio::test]
async fn the_default_policy_stretches_with_the_audio() {
let captured: Captured = Captured::default();
let base = spawn(
Arc::new(MockEngine {
captured: captured.clone(),
}),
mock_options(),
)
.await;
upload(&base, file_form(wav_bytes(8000, 100))).await;
let budget = captured.lock().unwrap().0.unwrap();
assert!(
budget >= Duration::from_secs(299) && budget <= Duration::from_secs(301),
"expected the audio-derived 300s budget, got {budget:?}"
);
}
#[tokio::test]
async fn health_and_models_report_the_loaded_engine() {
let base = spawn(
Arc::new(MockEngine {
captured: Captured::default(),
}),
mock_options(),
)
.await;
let client = reqwest::Client::new();
let health: serde_json::Value = client
.get(format!("{base}/health"))
.send()
.await
.unwrap()
.json()
.await
.unwrap();
assert_eq!(health["status"], "ok");
assert_eq!(health["model"], "test-model");
let models: serde_json::Value = client
.get(format!("{base}/v1/models"))
.send()
.await
.unwrap()
.json()
.await
.unwrap();
assert_eq!(models["data"][0]["id"], "test-model");
}
#[tokio::test]
async fn the_decode_budget_refuses_over_long_audio() {
let mut options = mock_options();
options.max_audio_secs = 1;
let base = spawn(
Arc::new(MockEngine {
captured: Captured::default(),
}),
options,
)
.await;
let response = upload(&base, file_form(wav_bytes(8000, 7))).await;
assert_eq!(response.status(), 400);
let body: serde_json::Value = response.json().await.unwrap();
assert!(body["error"]["message"]
.as_str()
.unwrap()
.contains("budget"));
}
#[tokio::test]
async fn a_hung_engine_cannot_hang_the_request_past_the_budget() {
let release = Arc::new(AtomicBool::new(false));
let mut options = mock_options();
options.timeout_secs = Some(1);
let base = spawn(
Arc::new(DeadlineBlindEngine {
release: release.clone(),
}),
options,
)
.await;
let response = upload(&base, file_form(wav_bytes(8000, 1))).await;
assert_eq!(response.status(), 504);
let body: serde_json::Value = response.json().await.unwrap();
assert!(body["error"]["message"]
.as_str()
.unwrap()
.contains("exceeded the request budget"));
release.store(true, Ordering::Relaxed);
}
#[tokio::test]
async fn concurrent_requests_beyond_the_engine_budget_answer_503_not_a_queue() {
let base = spawn(
Arc::new(SingleSessionEngine {
active: Arc::new(std::sync::Mutex::new(0)),
}),
mock_options(),
)
.await;
let (first, second) = tokio::join!(
upload(&base, file_form(wav_bytes(8000, 1))),
upload(&base, file_form(wav_bytes(8000, 1))),
);
let mut statuses = [first.status().as_u16(), second.status().as_u16()];
statuses.sort();
assert_eq!(
statuses,
[200, 503],
"one request transcribes, the concurrent one is refused"
);
}
#[test]
fn webm_uploads_are_refused_with_the_opus_hint() {
let mut bytes = vec![0x1A, 0x45, 0xDF, 0xA3];
bytes.extend_from_slice(&[0x42, 0x82, 0x84, b'w', b'e', b'b', b'm']);
bytes.extend_from_slice(&[0; 16]);
let error = decode_upload(&bytes, 3600).unwrap_err();
assert_eq!(error.kind, ErrorKind::InvalidInput);
assert!(error.message.contains("opus"), "{}", error.message);
}
#[test]
fn matroska_files_are_not_confused_with_webm() {
let mut bytes = vec![0x1A, 0x45, 0xDF, 0xA3];
bytes.extend_from_slice(&[0x42, 0x82, 0x84, b'm', b'a', b't', b'r']);
bytes.extend_from_slice(b"oska");
bytes.extend_from_slice(&[0; 16]);
let error = decode_upload(&bytes, 3600).unwrap_err();
assert!(!error.message.contains("cannot be decoded here"), "{error}");
}