feat #14740: transcription pipeline
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,683 @@
|
||||
//! Turns a recorded call into a stored transcript: encode both tracks to
|
||||
//! FLAC, transcribe them remotely, merge the results and store them. Also
|
||||
//! the startup routine that repairs recordings cut off by a crash.
|
||||
//!
|
||||
//! The [`Store`] is shared with the rest of the app; it locks its connection
|
||||
//! only inside each call, so no lock is ever held across an `.await`.
|
||||
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::Duration;
|
||||
|
||||
use chrono::{Local, TimeDelta};
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::audio::{encode_flac, repair_wav};
|
||||
use crate::merge::merge;
|
||||
use crate::model::{Call, CallId, CallStatus, Word};
|
||||
use crate::store::{RecoveryReport, Store, StoreError};
|
||||
use crate::transcribe::{TranscribeError, TranscribeOptions, Transcriber};
|
||||
|
||||
/// Settings a pipeline run uses.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct PipelineOptions {
|
||||
/// ISO 639-1 code, or `"auto"`.
|
||||
pub language: String,
|
||||
pub deepscript_model: String,
|
||||
}
|
||||
|
||||
/// Everything a pipeline run can fail with.
|
||||
#[derive(Debug, Error)]
|
||||
pub enum PipelineError {
|
||||
#[error("transcription failed: {0:?}")]
|
||||
Transcribe(TranscribeError),
|
||||
#[error(transparent)]
|
||||
Store(#[from] StoreError),
|
||||
#[error("cannot read audio file {}: {source}", path.display())]
|
||||
Io { path: PathBuf, source: io::Error },
|
||||
#[error("call {0} not found")]
|
||||
CallNotFound(CallId),
|
||||
}
|
||||
|
||||
impl From<TranscribeError> for PipelineError {
|
||||
fn from(e: TranscribeError) -> Self {
|
||||
PipelineError::Transcribe(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl PipelineError {
|
||||
/// German, user-facing text for this error. Never includes an API key
|
||||
/// or transcript text.
|
||||
pub fn user_message(&self) -> String {
|
||||
match self {
|
||||
PipelineError::Transcribe(e) => e.user_message(),
|
||||
PipelineError::Store(e) => format!("Interner Fehler: {e}"),
|
||||
PipelineError::Io { path, source } => {
|
||||
format!("Audiodatei nicht lesbar: {} ({source})", path.display())
|
||||
}
|
||||
PipelineError::CallNotFound(_) => "Gespräch nicht gefunden".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `track` value of the mic track's remote job.
|
||||
const MIC_TRACK: &str = "mic";
|
||||
/// `track` value of the output track's remote job.
|
||||
const OUT_TRACK: &str = "out";
|
||||
|
||||
/// Transcribes call `id` and stores the merged transcript, ending in
|
||||
/// `Done`. A call that already has a transcript (a retry after a later
|
||||
/// step failed) is not transcribed again.
|
||||
///
|
||||
/// Both tracks are encoded to FLAC in `work_dir` (removed again afterwards)
|
||||
/// and uploaded; the remote jobs are deleted once their results are in, or
|
||||
/// the run failed. Any error sets the call to `Failed` with
|
||||
/// [`PipelineError::user_message`]. The recordings themselves are never
|
||||
/// deleted here.
|
||||
pub async fn process_call(
|
||||
store: &Store,
|
||||
tr: &dyn Transcriber,
|
||||
id: CallId,
|
||||
opts: &PipelineOptions,
|
||||
work_dir: &Path,
|
||||
) -> Result<(), PipelineError> {
|
||||
let result = run(store, tr, id, opts, work_dir).await;
|
||||
if let Err(e) = &result {
|
||||
log::warn!("call {id}: processing failed: {e}");
|
||||
if let Err(se) = store.set_status(id, CallStatus::Failed, Some(&e.user_message())) {
|
||||
log::error!("call {id}: could not mark as failed: {se}");
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
async fn run(
|
||||
store: &Store,
|
||||
tr: &dyn Transcriber,
|
||||
id: CallId,
|
||||
opts: &PipelineOptions,
|
||||
work_dir: &Path,
|
||||
) -> Result<(), PipelineError> {
|
||||
let call = store.get_call(id)?.ok_or(PipelineError::CallNotFound(id))?;
|
||||
if call.transcribed_at.is_none() && store.segments(id)?.is_empty() {
|
||||
store.set_status(id, CallStatus::Processing, None)?;
|
||||
transcribe(store, tr, &call, opts, work_dir).await?;
|
||||
}
|
||||
// SUMMARY SEAM (Task 14b): summarize here.
|
||||
store.set_status(id, CallStatus::Done, None)?;
|
||||
log::info!("call {id}: done");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Encodes, uploads and transcribes both tracks, then stores the merged
|
||||
/// transcript.
|
||||
async fn transcribe(
|
||||
store: &Store,
|
||||
tr: &dyn Transcriber,
|
||||
call: &Call,
|
||||
opts: &PipelineOptions,
|
||||
work_dir: &Path,
|
||||
) -> Result<(), PipelineError> {
|
||||
let id = call.id;
|
||||
fs::create_dir_all(work_dir).map_err(|source| PipelineError::Io {
|
||||
path: work_dir.to_path_buf(),
|
||||
source,
|
||||
})?;
|
||||
let mic_flac = work_dir.join(format!("{id}-mic.flac"));
|
||||
let out_flac = work_dir.join(format!("{id}-out.flac"));
|
||||
let _flac_files = RemoveOnDrop(vec![mic_flac.clone(), out_flac.clone()]);
|
||||
|
||||
tokio::try_join!(
|
||||
encode(&call.mic_path, &mic_flac),
|
||||
encode(&call.out_path, &out_flac)
|
||||
)?;
|
||||
log::info!("call {id}: uploading both tracks");
|
||||
|
||||
let track_opts = |single_speaker| TranscribeOptions {
|
||||
language: opts.language.clone(),
|
||||
model: opts.deepscript_model.clone(),
|
||||
single_speaker,
|
||||
};
|
||||
let (mic_opts, out_opts) = (track_opts(true), track_opts(false));
|
||||
let (mic_job, out_job) = tokio::join!(
|
||||
submit(store, tr, id, MIC_TRACK, &mic_flac, &mic_opts),
|
||||
submit(store, tr, id, OUT_TRACK, &out_flac, &out_opts)
|
||||
);
|
||||
let submitted: Vec<String> = [&mic_job, &out_job]
|
||||
.into_iter()
|
||||
.filter_map(|job| job.as_ref().ok().cloned())
|
||||
.collect();
|
||||
let words = wait_both(tr, id, mic_job, out_job).await;
|
||||
// Also after a failed submit or wait: nothing stays at DeepScript.
|
||||
delete_jobs(store, tr, id, &submitted).await;
|
||||
let (mic, out) = words?;
|
||||
|
||||
let segments = merge(&mic, &out);
|
||||
store.save_segments(id, &segments)?;
|
||||
store.mark_transcribed(id, Local::now())?;
|
||||
log::info!("call {id}: transcribed, {} segments", segments.len());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Waits for both jobs at once, once both were submitted.
|
||||
async fn wait_both(
|
||||
tr: &dyn Transcriber,
|
||||
id: CallId,
|
||||
mic_job: Result<String, PipelineError>,
|
||||
out_job: Result<String, PipelineError>,
|
||||
) -> Result<(Vec<Word>, Vec<Word>), PipelineError> {
|
||||
let (mic_job, out_job) = (mic_job?, out_job?);
|
||||
log::info!("call {id}: waiting for remote jobs {mic_job} and {out_job}");
|
||||
let (mic, out) = tokio::join!(tr.wait(&mic_job), tr.wait(&out_job));
|
||||
Ok((mic?, out?))
|
||||
}
|
||||
|
||||
/// Encodes `wav` to FLAC at `flac` on the blocking pool.
|
||||
async fn encode(wav: &Path, flac: &Path) -> Result<(), PipelineError> {
|
||||
let (src, dst) = (wav.to_path_buf(), flac.to_path_buf());
|
||||
tokio::task::spawn_blocking(move || encode_flac(&src, &dst))
|
||||
.await
|
||||
.unwrap_or_else(|e| Err(io::Error::other(e)))
|
||||
.map_err(|source| PipelineError::Io {
|
||||
path: wav.to_path_buf(),
|
||||
source,
|
||||
})
|
||||
}
|
||||
|
||||
/// Uploads one track and records its remote job right away, so a crash
|
||||
/// mid-wait still leaves a record of what to delete remotely.
|
||||
async fn submit(
|
||||
store: &Store,
|
||||
tr: &dyn Transcriber,
|
||||
id: CallId,
|
||||
track: &str,
|
||||
flac: &Path,
|
||||
opts: &TranscribeOptions,
|
||||
) -> Result<String, PipelineError> {
|
||||
let remote_id = tr.submit(flac, opts).await?;
|
||||
store.record_remote_job(id, track, &remote_id)?;
|
||||
Ok(remote_id)
|
||||
}
|
||||
|
||||
/// Best-effort deletes the remote jobs recorded for call `id` (left over
|
||||
/// from a run the app did not finish), then forgets them.
|
||||
pub async fn delete_remote_jobs(store: &Store, tr: &dyn Transcriber, id: CallId) {
|
||||
delete_jobs(store, tr, id, &[]).await;
|
||||
}
|
||||
|
||||
/// Deletes every remote job recorded for `id` plus `known` (jobs submitted
|
||||
/// in this run, in case recording one failed). Forgets the recorded jobs
|
||||
/// only if all of them were deleted, so a later run tries again.
|
||||
async fn delete_jobs(store: &Store, tr: &dyn Transcriber, id: CallId, known: &[String]) {
|
||||
let mut jobs: Vec<String> = match store.remote_jobs(id) {
|
||||
Ok(jobs) => jobs.into_iter().map(|(_, remote_id)| remote_id).collect(),
|
||||
Err(e) => {
|
||||
log::error!("call {id}: could not read remote jobs: {e}");
|
||||
Vec::new()
|
||||
}
|
||||
};
|
||||
for job in known {
|
||||
if !jobs.contains(job) {
|
||||
jobs.push(job.clone());
|
||||
}
|
||||
}
|
||||
|
||||
let mut all_deleted = true;
|
||||
for job in &jobs {
|
||||
if let Err(e) = tr.delete(job).await {
|
||||
all_deleted = false;
|
||||
log::warn!(
|
||||
"call {id}: deleting remote job {job} failed: {}",
|
||||
e.user_message()
|
||||
);
|
||||
}
|
||||
}
|
||||
if all_deleted {
|
||||
if let Err(e) = store.clear_remote_jobs(id) {
|
||||
log::error!("call {id}: could not clear remote jobs: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Removes its files when dropped (missing files are fine).
|
||||
struct RemoveOnDrop(Vec<PathBuf>);
|
||||
|
||||
impl Drop for RemoveOnDrop {
|
||||
fn drop(&mut self) {
|
||||
for path in &self.0 {
|
||||
if let Err(e) = fs::remove_file(path) {
|
||||
if e.kind() != io::ErrorKind::NotFound {
|
||||
log::warn!("could not remove {}: {e}", path.display());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Reconciles call states left over from an unclean shutdown (see
|
||||
/// [`Store::startup_recovery`]) and repairs the WAV headers of recordings
|
||||
/// that were cut off. An interrupted call's `ended_at` becomes its start
|
||||
/// plus the shorter track's length (a missing or unrepairable track counts
|
||||
/// as empty).
|
||||
///
|
||||
/// The caller is responsible for deleting the remote jobs of the calls
|
||||
/// reported as `failed` (see [`delete_remote_jobs`]).
|
||||
pub fn recover_on_startup(store: &Store) -> Result<RecoveryReport, PipelineError> {
|
||||
let report = store.startup_recovery()?;
|
||||
for &id in &report.interrupted {
|
||||
let Some(call) = store.get_call(id)? else {
|
||||
continue;
|
||||
};
|
||||
let length = repaired_length(id, &call.mic_path).min(repaired_length(id, &call.out_path));
|
||||
let length = TimeDelta::from_std(length).unwrap_or(TimeDelta::zero());
|
||||
store.set_ended_at(id, call.started_at + length)?;
|
||||
}
|
||||
Ok(report)
|
||||
}
|
||||
|
||||
/// Repairs one track's header and returns its length; zero if the file is
|
||||
/// missing or cannot be repaired.
|
||||
fn repaired_length(id: CallId, path: &Path) -> Duration {
|
||||
match repair_wav(path) {
|
||||
Ok(length) => length,
|
||||
Err(e) => {
|
||||
log::warn!("call {id}: could not repair {}: {e}", path.display());
|
||||
Duration::ZERO
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::collections::HashMap;
|
||||
use std::fs::{self, OpenOptions};
|
||||
use std::io::{Seek, SeekFrom, Write};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Mutex;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use chrono::{Duration, Local, TimeZone};
|
||||
|
||||
use super::*;
|
||||
use crate::audio::TrackWriter;
|
||||
use crate::merge::label;
|
||||
use crate::model::{CallStatus, Segment, Speaker, Word};
|
||||
use crate::transcribe::TranscribeOptions;
|
||||
|
||||
/// Scripted [`Transcriber`]: a file whose name contains `-mic.` gets the
|
||||
/// job id `job-mic`, anything else `job-out`; `wait` returns the words
|
||||
/// (or error) scripted for that job.
|
||||
#[derive(Default)]
|
||||
struct FakeTranscriber {
|
||||
results: Mutex<HashMap<String, Result<Vec<Word>, TranscribeError>>>,
|
||||
submitted: Mutex<Vec<(String, TranscribeOptions)>>,
|
||||
deleted: Mutex<Vec<String>>,
|
||||
/// Job whose upload fails with `Rejected`.
|
||||
reject_upload: Option<&'static str>,
|
||||
}
|
||||
|
||||
impl FakeTranscriber {
|
||||
fn returning(mic: Vec<Word>, out: Vec<Word>) -> Self {
|
||||
let fake = FakeTranscriber::default();
|
||||
fake.script("job-mic", Ok(mic));
|
||||
fake.script("job-out", Ok(out));
|
||||
fake
|
||||
}
|
||||
|
||||
fn script(&self, job: &str, result: Result<Vec<Word>, TranscribeError>) {
|
||||
self.results.lock().unwrap().insert(job.to_string(), result);
|
||||
}
|
||||
|
||||
fn submitted(&self) -> Vec<(String, TranscribeOptions)> {
|
||||
let mut submitted = self.submitted.lock().unwrap().clone();
|
||||
submitted.sort_by(|a, b| a.0.cmp(&b.0));
|
||||
submitted
|
||||
}
|
||||
|
||||
fn deleted(&self) -> Vec<String> {
|
||||
let mut deleted = self.deleted.lock().unwrap().clone();
|
||||
deleted.sort();
|
||||
deleted
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Transcriber for FakeTranscriber {
|
||||
async fn submit(
|
||||
&self,
|
||||
audio: &Path,
|
||||
opts: &TranscribeOptions,
|
||||
) -> Result<String, TranscribeError> {
|
||||
let bytes = fs::read(audio).expect("submitted file exists");
|
||||
assert_eq!(&bytes[0..4], b"fLaC", "uploads are FLAC");
|
||||
let name = audio.file_name().unwrap().to_string_lossy();
|
||||
let job = if name.contains("-mic.") {
|
||||
"job-mic"
|
||||
} else {
|
||||
"job-out"
|
||||
};
|
||||
if self.reject_upload == Some(job) {
|
||||
return Err(TranscribeError::Rejected("unsupported-format".to_string()));
|
||||
}
|
||||
self.submitted
|
||||
.lock()
|
||||
.unwrap()
|
||||
.push((job.to_string(), opts.clone()));
|
||||
Ok(job.to_string())
|
||||
}
|
||||
|
||||
async fn wait(&self, remote_id: &str) -> Result<Vec<Word>, TranscribeError> {
|
||||
self.results
|
||||
.lock()
|
||||
.unwrap()
|
||||
.get(remote_id)
|
||||
.cloned()
|
||||
.unwrap_or_else(|| panic!("no result scripted for {remote_id}"))
|
||||
}
|
||||
|
||||
async fn delete(&self, remote_id: &str) -> Result<(), TranscribeError> {
|
||||
self.deleted.lock().unwrap().push(remote_id.to_string());
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn word(text: &str, start: f64, end: f64, speaker: Option<&str>) -> Word {
|
||||
Word {
|
||||
text: text.to_string(),
|
||||
start,
|
||||
end,
|
||||
speaker: speaker.map(str::to_string),
|
||||
}
|
||||
}
|
||||
|
||||
fn opts() -> PipelineOptions {
|
||||
PipelineOptions {
|
||||
language: "de".to_string(),
|
||||
deepscript_model: "premium".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes a mono 16 kHz WAV of `secs` seconds of a quiet tone.
|
||||
fn write_wav(path: &Path, secs: f32) {
|
||||
let mut writer = TrackWriter::create(path).unwrap();
|
||||
let samples: Vec<f32> = (0..(16_000.0 * secs) as usize)
|
||||
.map(|i| 0.1 * (i as f32 * 0.05).sin())
|
||||
.collect();
|
||||
writer.write(&samples).unwrap();
|
||||
writer.finalize().unwrap();
|
||||
}
|
||||
|
||||
struct Fixture {
|
||||
store: Store,
|
||||
dir: tempfile::TempDir,
|
||||
id: CallId,
|
||||
mic: PathBuf,
|
||||
out: PathBuf,
|
||||
}
|
||||
|
||||
impl Fixture {
|
||||
fn work_dir(&self) -> PathBuf {
|
||||
self.dir.path().join("upload")
|
||||
}
|
||||
|
||||
async fn process(&self, tr: &dyn Transcriber) -> Result<(), PipelineError> {
|
||||
process_call(&self.store, tr, self.id, &opts(), &self.work_dir()).await
|
||||
}
|
||||
|
||||
fn status(&self) -> CallStatus {
|
||||
self.store.get_call(self.id).unwrap().unwrap().status
|
||||
}
|
||||
|
||||
/// Files left in the upload directory (it may not exist at all).
|
||||
fn upload_files(&self) -> Vec<PathBuf> {
|
||||
fs::read_dir(self.work_dir())
|
||||
.map(|entries| entries.map(|e| e.unwrap().path()).collect())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
}
|
||||
|
||||
/// A call awaiting the decision, with one second of audio per track.
|
||||
fn fixture() -> Fixture {
|
||||
let store = Store::open_in_memory().unwrap();
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let mic = dir.path().join("1-mic.wav");
|
||||
let out = dir.path().join("1-out.wav");
|
||||
write_wav(&mic, 1.0);
|
||||
write_wav(&out, 1.0);
|
||||
let started = Local::now() - Duration::minutes(5);
|
||||
let id = store
|
||||
.create_call("Microsoft Teams", started, &mic, &out)
|
||||
.unwrap();
|
||||
store.finish_recording(id, Local::now(), false).unwrap();
|
||||
Fixture {
|
||||
store,
|
||||
dir,
|
||||
id,
|
||||
mic,
|
||||
out,
|
||||
}
|
||||
}
|
||||
|
||||
fn transcript(f: &Fixture) -> Vec<String> {
|
||||
let names = Default::default();
|
||||
f.store
|
||||
.segments(f.id)
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|s| format!("{}: {}", label(&s.speaker, &names), s.text))
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn pipeline_happy_path_sets_done_and_stores_segments() {
|
||||
let f = fixture();
|
||||
let fake = FakeTranscriber::returning(
|
||||
vec![word("Guten", 0.0, 0.4, None), word("Tag", 0.4, 0.8, None)],
|
||||
vec![word("Hallo", 2.0, 2.5, Some("A"))],
|
||||
);
|
||||
|
||||
f.process(&fake).await.unwrap();
|
||||
|
||||
let call = f.store.get_call(f.id).unwrap().unwrap();
|
||||
assert_eq!(call.status, CallStatus::Done);
|
||||
assert_eq!(call.error, None);
|
||||
assert!(call.transcribed_at.is_some());
|
||||
assert_eq!(
|
||||
transcript(&f),
|
||||
vec![
|
||||
"Ich: Guten Tag".to_string(),
|
||||
"Sprecher 1: Hallo".to_string()
|
||||
]
|
||||
);
|
||||
assert_eq!(f.store.remote_jobs(f.id).unwrap(), vec![]);
|
||||
assert_eq!(fake.deleted(), vec!["job-mic", "job-out"]);
|
||||
assert_eq!(f.upload_files(), Vec::<PathBuf>::new());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn pipeline_mic_submitted_as_single_speaker() {
|
||||
let f = fixture();
|
||||
let fake = FakeTranscriber::returning(vec![], vec![]);
|
||||
|
||||
f.process(&fake).await.unwrap();
|
||||
|
||||
let expected = |single_speaker| TranscribeOptions {
|
||||
language: "de".to_string(),
|
||||
model: "premium".to_string(),
|
||||
single_speaker,
|
||||
};
|
||||
assert_eq!(
|
||||
fake.submitted(),
|
||||
vec![
|
||||
("job-mic".to_string(), expected(true)),
|
||||
("job-out".to_string(), expected(false)),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn pipeline_transcription_failure_sets_failed_keeps_audio() {
|
||||
let f = fixture();
|
||||
let fake = FakeTranscriber::returning(vec![word("Hallo", 0.0, 0.5, None)], vec![]);
|
||||
fake.script("job-out", Err(TranscribeError::InsufficientBalance));
|
||||
|
||||
let err = f.process(&fake).await.unwrap_err();
|
||||
|
||||
assert_eq!(err.user_message(), "DeepScript-Guthaben aufgebraucht.");
|
||||
let call = f.store.get_call(f.id).unwrap().unwrap();
|
||||
assert_eq!(call.status, CallStatus::Failed);
|
||||
assert_eq!(call.error, Some(err.user_message()));
|
||||
assert!(call.transcribed_at.is_none());
|
||||
assert!(f.mic.exists());
|
||||
assert!(f.out.exists());
|
||||
assert_eq!(fake.deleted(), vec!["job-mic", "job-out"]);
|
||||
assert_eq!(f.store.remote_jobs(f.id).unwrap(), vec![]);
|
||||
assert_eq!(f.store.segments(f.id).unwrap(), vec![]);
|
||||
assert_eq!(f.upload_files(), Vec::<PathBuf>::new());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn pipeline_failed_upload_deletes_the_other_job() {
|
||||
let f = fixture();
|
||||
let fake = FakeTranscriber {
|
||||
reject_upload: Some("job-out"),
|
||||
..FakeTranscriber::returning(vec![], vec![])
|
||||
};
|
||||
|
||||
let err = f.process(&fake).await.unwrap_err();
|
||||
|
||||
assert_eq!(
|
||||
err.user_message(),
|
||||
"DeepScript hat die Datei abgelehnt (unsupported-format)."
|
||||
);
|
||||
assert_eq!(f.status(), CallStatus::Failed);
|
||||
assert_eq!(fake.deleted(), vec!["job-mic"]);
|
||||
assert_eq!(f.store.remote_jobs(f.id).unwrap(), vec![]);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn leftover_remote_jobs_are_deleted_and_forgotten() {
|
||||
let f = fixture();
|
||||
f.store.record_remote_job(f.id, "mic", "old-mic").unwrap();
|
||||
f.store.record_remote_job(f.id, "out", "old-out").unwrap();
|
||||
let fake = FakeTranscriber::default();
|
||||
|
||||
delete_remote_jobs(&f.store, &fake, f.id).await;
|
||||
|
||||
assert_eq!(fake.deleted(), vec!["old-mic", "old-out"]);
|
||||
assert_eq!(f.store.remote_jobs(f.id).unwrap(), vec![]);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn pipeline_unreadable_audio_sets_failed() {
|
||||
let f = fixture();
|
||||
fs::remove_file(&f.out).unwrap();
|
||||
let fake = FakeTranscriber::returning(vec![], vec![]);
|
||||
|
||||
let err = f.process(&fake).await.unwrap_err();
|
||||
|
||||
assert!(
|
||||
err.user_message().starts_with("Audiodatei nicht lesbar: "),
|
||||
"{}",
|
||||
err.user_message()
|
||||
);
|
||||
assert_eq!(f.status(), CallStatus::Failed);
|
||||
assert_eq!(fake.submitted(), vec![]);
|
||||
assert_eq!(f.upload_files(), Vec::<PathBuf>::new());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn pipeline_tolerates_empty_track() {
|
||||
let f = fixture();
|
||||
let fake = FakeTranscriber::returning(vec![word("Hallo", 0.0, 0.5, None)], vec![]);
|
||||
|
||||
f.process(&fake).await.unwrap();
|
||||
|
||||
assert_eq!(f.status(), CallStatus::Done);
|
||||
assert_eq!(transcript(&f), vec!["Ich: Hallo".to_string()]);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn pipeline_skips_transcription_when_segments_exist() {
|
||||
let f = fixture();
|
||||
let segments = vec![Segment {
|
||||
speaker: Speaker::Me,
|
||||
start: 0.0,
|
||||
end: 1.0,
|
||||
text: "Schon da".to_string(),
|
||||
}];
|
||||
f.store.save_segments(f.id, &segments).unwrap();
|
||||
f.store
|
||||
.set_status(f.id, CallStatus::Failed, Some("vorher"))
|
||||
.unwrap();
|
||||
let fake = FakeTranscriber::default();
|
||||
|
||||
f.process(&fake).await.unwrap();
|
||||
|
||||
let call = f.store.get_call(f.id).unwrap().unwrap();
|
||||
assert_eq!(call.status, CallStatus::Done);
|
||||
assert_eq!(call.error, None);
|
||||
assert_eq!(fake.submitted(), vec![]);
|
||||
assert_eq!(f.store.segments(f.id).unwrap(), segments);
|
||||
}
|
||||
|
||||
/// Zeroes the RIFF and data size fields, as left by a recording killed
|
||||
/// before its first header refresh.
|
||||
fn zero_header_sizes(path: &Path) {
|
||||
let mut file = OpenOptions::new().write(true).open(path).unwrap();
|
||||
for offset in [4, 40] {
|
||||
file.seek(SeekFrom::Start(offset)).unwrap();
|
||||
file.write_all(&0u32.to_le_bytes()).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
fn wav_duration(path: &Path) -> f64 {
|
||||
let reader = hound::WavReader::open(path).unwrap();
|
||||
reader.duration() as f64 / reader.spec().sample_rate as f64
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn startup_repairs_interrupted_recordings() {
|
||||
let store = Store::open_in_memory().unwrap();
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let mic = dir.path().join("1-mic.wav");
|
||||
let out = dir.path().join("1-out.wav");
|
||||
write_wav(&mic, 3.0);
|
||||
write_wav(&out, 2.5);
|
||||
zero_header_sizes(&mic);
|
||||
zero_header_sizes(&out);
|
||||
let started = Local.with_ymd_and_hms(2026, 11, 5, 10, 14, 0).unwrap();
|
||||
let id = store
|
||||
.create_call("Microsoft Teams", started, &mic, &out)
|
||||
.unwrap();
|
||||
|
||||
let report = recover_on_startup(&store).unwrap();
|
||||
|
||||
assert_eq!(report.interrupted, vec![id]);
|
||||
let call = store.get_call(id).unwrap().unwrap();
|
||||
assert_eq!(call.status, CallStatus::AwaitingDecision);
|
||||
assert!(call.interrupted);
|
||||
assert_eq!(wav_duration(&mic), 3.0);
|
||||
assert_eq!(wav_duration(&out), 2.5);
|
||||
assert_eq!(call.ended_at, Some(started + Duration::milliseconds(2500)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn startup_treats_a_missing_track_as_empty() {
|
||||
let store = Store::open_in_memory().unwrap();
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let mic = dir.path().join("1-mic.wav");
|
||||
write_wav(&mic, 3.0);
|
||||
let started = Local.with_ymd_and_hms(2026, 11, 5, 10, 14, 0).unwrap();
|
||||
let id = store
|
||||
.create_call("Manuell", started, &mic, &dir.path().join("1-out.wav"))
|
||||
.unwrap();
|
||||
|
||||
recover_on_startup(&store).unwrap();
|
||||
|
||||
let call = store.get_call(id).unwrap().unwrap();
|
||||
assert_eq!(call.status, CallStatus::AwaitingDecision);
|
||||
assert_eq!(call.ended_at, Some(started));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user