fix #14740: re-check retention eligibility and validate retention_days

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
hauke
2026-11-10 14:20:16 +01:00
co-authored by Claude Opus 5.5
parent 0b735b2e98
commit ad73e901a0
4 changed files with 395 additions and 43 deletions
+62 -2
View File
@@ -26,6 +26,8 @@ pub const START_AFTER_SECS: RangeInclusive<u64> = 1..=30;
/// Allowed mic release before a call counts as ended, in seconds. Too short /// Allowed mic release before a call counts as ended, in seconds. Too short
/// splits a call, e.g. when the headset is switched. /// splits a call, e.g. when the headset is switched.
pub const END_AFTER_SECS: RangeInclusive<u64> = 3..=120; pub const END_AFTER_SECS: RangeInclusive<u64> = 3..=120;
/// Allowed retention period, in days (the settings UI checks the same).
pub const RETENTION_DAYS: RangeInclusive<u32> = 1..=3650;
/// The only `deepscript_model` values the settings UI offers (DeepScript's /// The only `deepscript_model` values the settings UI offers (DeepScript's
/// OpenAPI spec also lists `"standard"`, not offered here). An older /// OpenAPI spec also lists `"standard"`, not offered here). An older
@@ -113,6 +115,13 @@ impl Settings {
END_AFTER_SECS.end() END_AFTER_SECS.end()
)); ));
} }
if !RETENTION_DAYS.contains(&self.retention_days) {
return Err(format!(
"Aufbewahrung: {}–{} Tage",
RETENTION_DAYS.start(),
RETENTION_DAYS.end()
));
}
if !DEEPSCRIPT_MODELS.contains(&self.deepscript_model.as_str()) { if !DEEPSCRIPT_MODELS.contains(&self.deepscript_model.as_str()) {
return Err(format!( return Err(format!(
"Transkriptionsmodell: nur {} möglich", "Transkriptionsmodell: nur {} möglich",
@@ -152,8 +161,9 @@ impl Settings {
/// Reads the settings file. A missing or unreadable file yields the /// Reads the settings file. A missing or unreadable file yields the
/// defaults; fields missing from the file take their default value. A /// defaults; fields missing from the file take their default value. A
/// `deepscript_model` outside [`DEEPSCRIPT_MODELS`] (e.g. an older /// `deepscript_model` outside [`DEEPSCRIPT_MODELS`] (e.g. an older
/// file's `"standard"`) falls back to the default instead of failing; /// file's `"standard"`) falls back to the default instead of failing, and
/// `vocabulary` is normalized (see [`normalize_vocabulary`]). /// so does a `retention_days` outside [`RETENTION_DAYS`]; `vocabulary`
/// is normalized (see [`normalize_vocabulary`]).
pub fn load(path: &Path) -> Settings { pub fn load(path: &Path) -> Settings {
let mut settings: Settings = fs::read_to_string(path) let mut settings: Settings = fs::read_to_string(path)
.ok() .ok()
@@ -162,6 +172,10 @@ impl Settings {
if !DEEPSCRIPT_MODELS.contains(&settings.deepscript_model.as_str()) { if !DEEPSCRIPT_MODELS.contains(&settings.deepscript_model.as_str()) {
settings.deepscript_model = Settings::default().deepscript_model; settings.deepscript_model = Settings::default().deepscript_model;
} }
// 0 would delete all audio on the next pass.
if !RETENTION_DAYS.contains(&settings.retention_days) {
settings.retention_days = Settings::default().retention_days;
}
settings.vocabulary = normalize_vocabulary(settings.vocabulary); settings.vocabulary = normalize_vocabulary(settings.vocabulary);
settings settings
} }
@@ -354,6 +368,52 @@ mod tests {
} }
} }
#[test]
fn validate_checks_the_retention_period() {
for days in [1, 3650] {
let s = Settings {
retention_days: days,
..Settings::default()
};
assert_eq!(s.validate(), Ok(()), "{days} days");
}
for days in [0, 3651] {
let s = Settings {
retention_days: days,
..Settings::default()
};
assert_eq!(
s.validate(),
Err("Aufbewahrung: 1–3650 Tage".to_string()),
"{days} days"
);
}
}
#[test]
fn a_retention_period_out_of_range_falls_back_to_the_default_on_load() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("settings.json");
for days in [0, 3651] {
fs::write(
&path,
format!(
r#"{{"retention_days": {days}, "mic_device": "headset", "backend_key_base_url": "https://crm.example"}}"#
),
)
.unwrap();
assert_eq!(
Settings::load(&path),
Settings {
mic_device: Some("headset".into()),
backend_key_base_url: Some("https://crm.example".into()),
..Settings::default()
},
"{days} days"
);
}
}
#[test] #[test]
fn validate_rejects_a_model_outside_the_offered_two() { fn validate_rejects_a_model_outside_the_offered_two() {
for model in ["standard", "nonsense", ""] { for model in ["standard", "nonsense", ""] {
+149 -9
View File
@@ -10,11 +10,12 @@
//! logged, never fatal. //! logged, never fatal.
use std::panic::{self, AssertUnwindSafe}; use std::panic::{self, AssertUnwindSafe};
use std::slice;
use std::thread; use std::thread;
use std::time::{Duration, SystemTime}; use std::time::{Duration, SystemTime};
use chrono::{DateTime, Local}; use chrono::{DateTime, Local};
use recorder_core::model::CallId; use recorder_core::model::{Call, CallId};
use recorder_core::retention::{plan_retention, RetentionAction}; use recorder_core::retention::{plan_retention, RetentionAction};
use tauri::{AppHandle, Manager}; use tauri::{AppHandle, Manager};
@@ -32,8 +33,10 @@ const WAKE_INTERVAL: Duration = Duration::from_secs(60);
pub struct PassReport { pub struct PassReport {
pub audio_deleted: Vec<CallId>, pub audio_deleted: Vec<CallId>,
pub calls_deleted: Vec<CallId>, pub calls_deleted: Vec<CallId>,
/// In use; tried again by the next pass. /// In use or changed since planned; tried again by the next pass.
pub skipped: Vec<CallId>, pub skipped: Vec<CallId>,
/// Those of `skipped` whose DeepScript jobs are still on record.
pub cleanup_pending: Vec<CallId>,
pub failed: Vec<CallId>, pub failed: Vec<CallId>,
} }
@@ -99,6 +102,9 @@ fn run(app: &AppHandle) {
if !report.calls_deleted.is_empty() { if !report.calls_deleted.is_empty() {
commands::prompt_queue_changed(app); commands::prompt_queue_changed(app);
} }
// As "Verwerfen" does: once their jobs are gone, a later pass can
// delete them.
processing::delete_leftover_jobs(app, report.cleanup_pending.clone());
if !report.is_empty() { if !report.is_empty() {
log::info!("retention: {}", report.summary()); log::info!("retention: {}", report.summary());
} }
@@ -107,34 +113,63 @@ fn run(app: &AppHandle) {
/// Runs one pass over all calls as of `now`, with the retention period /// Runs one pass over all calls as of `now`, with the retention period
/// currently set. /// currently set.
pub fn run_pass(state: &AppState, now: DateTime<Local>) -> PassReport { pub fn run_pass(state: &AppState, now: DateTime<Local>) -> PassReport {
let mut report = PassReport::default();
let calls = match state.store.list_calls() { let calls = match state.store.list_calls() {
Ok(calls) => calls, Ok(calls) => calls,
Err(e) => { Err(e) => {
log::error!("retention: could not list the calls: {e}"); log::error!("retention: could not list the calls: {e}");
return report; return PassReport::default();
} }
}; };
for action in plan_retention(&calls, now, state.settings().retention_days) { let retention_days = state.settings().retention_days;
let plan = plan_retention(&calls, now, retention_days);
execute(state, &plan, now, retention_days)
}
/// Carries out `plan`, made as of `now` for `retention_days`. Each call is
/// planned again from its row as it is when the action runs, while nothing
/// else can take it: a call that changed since (e.g. a "Transkribieren"
/// that failed meanwhile, so it keeps its audio) is skipped. A call deleted
/// meanwhile is ignored.
fn execute(
state: &AppState,
plan: &[RetentionAction],
now: DateTime<Local>,
retention_days: u32,
) -> PassReport {
let mut report = PassReport::default();
for &action in plan {
let still_planned =
|call: &Call| plan_retention(slice::from_ref(call), now, retention_days) == [action];
match action { match action {
RetentionAction::DeleteAudio(id) => match state.delete_audio(id) { RetentionAction::DeleteAudio(id) => match state.delete_audio(id, still_planned) {
Ok(()) => report.audio_deleted.push(id), Ok(()) => report.audio_deleted.push(id),
Err(AudioDeleteError::Busy(why)) => { Err(AudioDeleteError::Busy(why)) => {
log::info!("retention: call {id}: audio kept for now: {why}"); log::info!("retention: call {id}: audio kept for now: {why}");
report.skipped.push(id); report.skipped.push(id);
} }
Err(AudioDeleteError::NotFound) => {
log::info!("retention: call {id}: already deleted");
}
Err(AudioDeleteError::Failed(e)) => { Err(AudioDeleteError::Failed(e)) => {
log::error!("retention: call {id}: audio not deleted: {e}"); log::error!("retention: call {id}: audio not deleted: {e}");
report.failed.push(id); report.failed.push(id);
} }
}, },
// The same path, and refusals, as "Verwerfen". // The same path, and refusals, as "Verwerfen".
RetentionAction::DeleteCall(id) => match state.discard_call(id) { RetentionAction::DeleteCall(id) => match state.discard_call_if(id, still_planned) {
Ok(_) => report.calls_deleted.push(id), Ok(_) => report.calls_deleted.push(id),
Err(e @ (DiscardError::Busy(_) | DiscardError::CleanupPending)) => { Err(e @ DiscardError::Busy(_)) => {
log::info!("retention: call {id}: kept for now: {e}"); log::info!("retention: call {id}: kept for now: {e}");
report.skipped.push(id); report.skipped.push(id);
} }
Err(e @ DiscardError::CleanupPending) => {
log::info!("retention: call {id}: kept for now: {e}");
report.skipped.push(id);
report.cleanup_pending.push(id);
}
Err(DiscardError::NotFound) => {
log::info!("retention: call {id}: already deleted");
}
Err(DiscardError::Other(e)) => { Err(DiscardError::Other(e)) => {
log::error!("retention: call {id}: not deleted: {e}"); log::error!("retention: call {id}: not deleted: {e}");
report.failed.push(id); report.failed.push(id);
@@ -249,6 +284,12 @@ mod tests {
.record_remote_job(cleanup, "mic", "job-1") .record_remote_job(cleanup, "mic", "job-1")
.unwrap(); .unwrap();
let undecided_old_tracks = tracks(&f, undecided_old); let undecided_old_tracks = tracks(&f, undecided_old);
let store = &f.state.store;
store.set_speaker_name(done_old, "me", "Hauke").unwrap();
store
.set_speaker_merge(done_old, "remote:2", "remote:1")
.unwrap();
store.set_title(done_old, Some("Bestellung")).unwrap();
let report = run_pass(&f.state, now); let report = run_pass(&f.state, now);
@@ -257,17 +298,36 @@ mod tests {
audio_deleted: sorted(report.audio_deleted), audio_deleted: sorted(report.audio_deleted),
calls_deleted: report.calls_deleted, calls_deleted: report.calls_deleted,
skipped: sorted(report.skipped), skipped: sorted(report.skipped),
cleanup_pending: report.cleanup_pending,
failed: report.failed, failed: report.failed,
}, },
PassReport { PassReport {
audio_deleted: vec![done_old], audio_deleted: vec![done_old],
calls_deleted: vec![undecided_old], calls_deleted: vec![undecided_old],
skipped: sorted(vec![uploading, cleanup]), skipped: sorted(vec![uploading, cleanup]),
cleanup_pending: vec![cleanup],
failed: vec![], failed: vec![],
} }
); );
assert!(!has_audio(&f, done_old)); assert!(!has_audio(&f, done_old));
assert!(f.state.store.get_call(done_old).unwrap().is_some()); let kept = store.get_call(done_old).unwrap().unwrap();
assert_eq!(kept.title.as_deref(), Some("Bestellung"));
assert_eq!(
store
.speaker_names(done_old)
.unwrap()
.get("me")
.map(String::as_str),
Some("Hauke")
);
assert_eq!(
store
.speaker_merges(done_old)
.unwrap()
.get("remote:2")
.map(String::as_str),
Some("remote:1")
);
assert_eq!(f.state.store.get_call(undecided_old).unwrap(), None); assert_eq!(f.state.store.get_call(undecided_old).unwrap(), None);
assert!(undecided_old_tracks.iter().all(|p| !p.exists())); assert!(undecided_old_tracks.iter().all(|p| !p.exists()));
for kept in [done_new, undecided_new, failed_old, uploading, cleanup] { for kept in [done_new, undecided_new, failed_old, uploading, cleanup] {
@@ -284,6 +344,85 @@ mod tests {
assert!(!has_audio(&f, uploading)); assert!(!has_audio(&f, uploading));
} }
/// The plan for the calls as they are now, with 7 days.
fn plan(f: &Fixture, now: DateTime<Local>) -> Vec<RetentionAction> {
plan_retention(&f.state.store.list_calls().unwrap(), now, 7)
}
#[test]
fn an_undecided_call_that_changed_after_planning_is_kept() {
let f = fixture();
let now = Local::now();
let failed = call(&f, "failed", now, 8, None);
let processing = call(&f, "processing", now, 8, None);
let transcribed = call(&f, "transcribed", now, 8, None);
let plan = plan(&f, now);
assert_eq!(plan.len(), 3);
// "Transkribieren" without a key marks the call failed (and lets go
// of it) between planning and the delete.
let store = &f.state.store;
store
.set_status(failed, CallStatus::Failed, Some("Kein API-Schlüssel"))
.unwrap();
store
.set_status(processing, CallStatus::Processing, None)
.unwrap();
store
.set_status(transcribed, CallStatus::Done, None)
.unwrap();
store.mark_transcribed(transcribed, now).unwrap();
let report = execute(&f.state, &plan, now, 7);
assert_eq!(report.calls_deleted, Vec::<CallId>::new());
assert_eq!(
sorted(report.skipped),
sorted(vec![failed, processing, transcribed])
);
for id in [failed, processing, transcribed] {
assert!(has_audio(&f, id), "call {id} keeps its audio");
}
}
#[test]
fn a_transcribed_call_that_changed_after_planning_keeps_its_audio() {
let f = fixture();
let now = Local::now();
let failed = call(&f, "failed", now, 9, Some(8));
let undecided = call(&f, "undecided", now, 9, Some(8));
let plan = plan(&f, now);
assert_eq!(plan.len(), 2);
assert!(plan
.iter()
.all(|action| matches!(action, RetentionAction::DeleteAudio(_))));
let store = &f.state.store;
store
.set_status(failed, CallStatus::Failed, Some("Fehler"))
.unwrap();
store
.set_status(undecided, CallStatus::AwaitingDecision, None)
.unwrap();
let report = execute(&f.state, &plan, now, 7);
assert_eq!(report.audio_deleted, Vec::<CallId>::new());
assert_eq!(sorted(report.skipped), sorted(vec![failed, undecided]));
assert!(has_audio(&f, failed));
assert!(has_audio(&f, undecided));
}
#[test]
fn a_call_deleted_after_planning_is_ignored() {
let f = fixture();
let now = Local::now();
let done = call(&f, "done", now, 9, Some(8));
let undecided = call(&f, "undecided", now, 8, None);
let plan = plan(&f, now);
f.state.discard_call(done).unwrap();
f.state.discard_call(undecided).unwrap();
assert_eq!(execute(&f.state, &plan, now, 7), PassReport::default());
}
#[test] #[test]
fn a_call_that_fails_does_not_stop_the_pass() { fn a_call_that_fails_does_not_stop_the_pass() {
let f = fixture(); let f = fixture();
@@ -327,6 +466,7 @@ mod tests {
audio_deleted: vec![1, 2], audio_deleted: vec![1, 2],
calls_deleted: vec![3], calls_deleted: vec![3],
skipped: vec![4], skipped: vec![4],
cleanup_pending: vec![],
failed: vec![], failed: vec![],
}; };
assert_eq!( assert_eq!(
+176 -27
View File
@@ -42,6 +42,8 @@ const ALREADY_PROCESSING: &str = "Wird bereits verarbeitet";
const ALREADY_UPLOADING: &str = "Wird bereits hochgeladen"; const ALREADY_UPLOADING: &str = "Wird bereits hochgeladen";
const BEING_RECORDED: &str = "Das Gespräch wird gerade aufgenommen"; const BEING_RECORDED: &str = "Das Gespräch wird gerade aufgenommen";
const PLAYING: &str = "Das Gespräch wird gerade abgespielt"; const PLAYING: &str = "Das Gespräch wird gerade abgespielt";
const CHANGED: &str = "Das Gespräch hat sich inzwischen geändert";
const NOT_FOUND: &str = "Gespräch nicht gefunden";
/// Why [`AppState::discard_call`] refused. /// Why [`AppState::discard_call`] refused.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
@@ -49,8 +51,10 @@ pub enum DiscardError {
/// DeepScript jobs of the call are still on record; their delete has to /// DeepScript jobs of the call are still on record; their delete has to
/// succeed first, or the cascade would forget them. /// succeed first, or the cascade would forget them.
CleanupPending, CleanupPending,
/// The call is recorded, processed or uploaded right now. /// The call is recorded, processed or uploaded right now, or no longer
/// what the caller expected ([`AppState::discard_call_if`]).
Busy(&'static str), Busy(&'static str),
NotFound,
Other(String), Other(String),
} }
@@ -61,6 +65,7 @@ impl std::fmt::Display for DiscardError {
"Daten bei DeepScript werden noch gelöscht – bitte später erneut versuchen.", "Daten bei DeepScript werden noch gelöscht – bitte später erneut versuchen.",
), ),
DiscardError::Busy(msg) => f.write_str(msg), DiscardError::Busy(msg) => f.write_str(msg),
DiscardError::NotFound => f.write_str(NOT_FOUND),
DiscardError::Other(msg) => f.write_str(msg), DiscardError::Other(msg) => f.write_str(msg),
} }
} }
@@ -75,8 +80,10 @@ impl From<String> for DiscardError {
/// Why [`AppState::delete_audio`] did not delete a call's audio. /// Why [`AppState::delete_audio`] did not delete a call's audio.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum AudioDeleteError { pub enum AudioDeleteError {
/// The call is in use; the next retention pass tries again. /// The call is in use, or no longer what the caller expected; the next
/// retention pass tries again.
Busy(&'static str), Busy(&'static str),
NotFound,
Failed(String), Failed(String),
} }
@@ -830,6 +837,17 @@ impl AppState {
/// ([`AppState::claim_upload`]), or while its DeepScript jobs are still /// ([`AppState::claim_upload`]), or while its DeepScript jobs are still
/// on record ([`DiscardError::CleanupPending`]). /// on record ([`DiscardError::CleanupPending`]).
pub fn discard_call(&self, id: CallId) -> Result<usize, DiscardError> { pub fn discard_call(&self, id: CallId) -> Result<usize, DiscardError> {
self.discard_call_if(id, |_| true)
}
/// Like [`AppState::discard_call`], but only if the call, read while
/// nothing else can take it, still passes `still_wanted`; otherwise
/// nothing is deleted ([`DiscardError::Busy`]).
pub fn discard_call_if(
&self,
id: CallId,
still_wanted: impl FnOnce(&Call) -> bool,
) -> Result<usize, DiscardError> {
if lock(&self.current) if lock(&self.current)
.as_ref() .as_ref()
.is_some_and(|cur| cur.call_id == id) .is_some_and(|cur| cur.call_id == id)
@@ -851,10 +869,13 @@ impl AppState {
.store .store
.get_call(id) .get_call(id)
.map_err(|e| e.to_string())? .map_err(|e| e.to_string())?
.ok_or_else(|| "Gespräch nicht gefunden".to_string())?; .ok_or(DiscardError::NotFound)?;
if call.status == CallStatus::Processing { if call.status == CallStatus::Processing {
return Err(DiscardError::Busy(ALREADY_PROCESSING)); return Err(DiscardError::Busy(ALREADY_PROCESSING));
} }
if !still_wanted(&call) {
return Err(DiscardError::Busy(CHANGED));
}
let remote_jobs = self.store.remote_jobs(id).map_err(|e| e.to_string())?; let remote_jobs = self.store.remote_jobs(id).map_err(|e| e.to_string())?;
if !remote_jobs.is_empty() { if !remote_jobs.is_empty() {
return Err(DiscardError::CleanupPending); return Err(DiscardError::CleanupPending);
@@ -893,14 +914,21 @@ impl AppState {
Ok(self.prompts_waiting()) Ok(self.prompts_waiting())
} }
/// Deletes a call's audio for the retention job: both track files and /// Deletes a transcribed (`Done`) call's audio for the retention job:
/// its cached mix, then marks the row (`audio_deleted`). Transcript, /// both track files and its cached mix, then marks the row
/// title, speaker names and merges and the CRM upload stay. Refused /// (`audio_deleted`). Transcript, title, speaker names and merges and the
/// ([`AudioDeleteError::Busy`]) while the call is recorded, processed /// CRM upload stay. Refused ([`AudioDeleteError::Busy`]) while the call
/// (or its remote jobs cleaned up), uploaded or a player holds its mix. /// is recorded, processed (or its remote jobs cleaned up), uploaded or a
/// player holds its mix, and unless the row, read while nothing else can
/// take the call, is `Done` with its audio not yet deleted and passes
/// `still_due`.
/// Already missing files are fine; a file that cannot be deleted leaves /// Already missing files are fine; a file that cannot be deleted leaves
/// the row unmarked, so the next pass tries again. /// the row unmarked, so the next pass tries again.
pub fn delete_audio(&self, id: CallId) -> Result<(), AudioDeleteError> { pub fn delete_audio(
&self,
id: CallId,
still_due: impl FnOnce(&Call) -> bool,
) -> Result<(), AudioDeleteError> {
if lock(&self.current) if lock(&self.current)
.as_ref() .as_ref()
.is_some_and(|cur| cur.call_id == id) .is_some_and(|cur| cur.call_id == id)
@@ -922,11 +950,17 @@ impl AppState {
if leases.latest.contains_key(&id) { if leases.latest.contains_key(&id) {
return Err(AudioDeleteError::Busy(PLAYING)); return Err(AudioDeleteError::Busy(PLAYING));
} }
let call = self.find_call(id).map_err(AudioDeleteError::Failed)?; let call = self
.store
.get_call(id)
.map_err(|e| AudioDeleteError::Failed(e.to_string()))?
.ok_or(AudioDeleteError::NotFound)?;
match call.status { match call.status {
CallStatus::Recording => return Err(AudioDeleteError::Busy(BEING_RECORDED)), CallStatus::Recording => return Err(AudioDeleteError::Busy(BEING_RECORDED)),
CallStatus::Processing => return Err(AudioDeleteError::Busy(ALREADY_PROCESSING)), CallStatus::Processing => return Err(AudioDeleteError::Busy(ALREADY_PROCESSING)),
_ => {} // Failed and undecided calls keep their audio.
CallStatus::Done if !call.audio_deleted && still_due(&call) => {}
_ => return Err(AudioDeleteError::Busy(CHANGED)),
} }
// Every file is tried, so as little audio as possible stays behind. // Every file is tried, so as little audio as possible stays behind.
let mut failed = None; let mut failed = None;
@@ -2240,7 +2274,7 @@ mod tests {
let id = transcribed_call_with_audio(&f); let id = transcribed_call_with_audio(&f);
let before = f.state.store.get_call(id).unwrap().unwrap(); let before = f.state.store.get_call(id).unwrap().unwrap();
assert_eq!(f.state.delete_audio(id), Ok(())); assert_eq!(f.state.delete_audio(id, |_| true), Ok(()));
let after = f.state.store.get_call(id).unwrap().unwrap(); let after = f.state.store.get_call(id).unwrap().unwrap();
assert!(!before.mic_path.exists()); assert!(!before.mic_path.exists());
@@ -2265,7 +2299,7 @@ mod tests {
fs::remove_file(&call.mic_path).unwrap(); fs::remove_file(&call.mic_path).unwrap();
fs::remove_file(&call.out_path).unwrap(); fs::remove_file(&call.out_path).unwrap();
assert_eq!(f.state.delete_audio(id), Ok(())); assert_eq!(f.state.delete_audio(id, |_| true), Ok(()));
assert!(f.state.store.get_call(id).unwrap().unwrap().audio_deleted); assert!(f.state.store.get_call(id).unwrap().unwrap().audio_deleted);
} }
@@ -2281,12 +2315,12 @@ mod tests {
let claim = f.state.claim_upload(id).unwrap(); let claim = f.state.claim_upload(id).unwrap();
assert_eq!( assert_eq!(
f.state.delete_audio(id), f.state.delete_audio(id, |_| true),
Err(AudioDeleteError::Busy(ALREADY_UPLOADING)) Err(AudioDeleteError::Busy(ALREADY_UPLOADING))
); );
assert_audio_kept(&f, id); assert_audio_kept(&f, id);
drop(claim); drop(claim);
assert_eq!(f.state.delete_audio(id), Ok(())); assert_eq!(f.state.delete_audio(id, |_| true), Ok(()));
} }
#[test] #[test]
@@ -2296,7 +2330,7 @@ mod tests {
// A cleanup of its remote jobs reserves a call of any status. // A cleanup of its remote jobs reserves a call of any status.
assert!(f.state.reserve_processing(id)); assert!(f.state.reserve_processing(id));
assert_eq!( assert_eq!(
f.state.delete_audio(id), f.state.delete_audio(id, |_| true),
Err(AudioDeleteError::Busy(ALREADY_PROCESSING)) Err(AudioDeleteError::Busy(ALREADY_PROCESSING))
); );
assert_audio_kept(&f, id); assert_audio_kept(&f, id);
@@ -2307,7 +2341,7 @@ mod tests {
.set_status(id, CallStatus::Processing, None) .set_status(id, CallStatus::Processing, None)
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
f.state.delete_audio(id), f.state.delete_audio(id, |_| true),
Err(AudioDeleteError::Busy(ALREADY_PROCESSING)) Err(AudioDeleteError::Busy(ALREADY_PROCESSING))
); );
assert_audio_kept(&f, id); assert_audio_kept(&f, id);
@@ -2327,7 +2361,7 @@ mod tests {
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
f.state.delete_audio(id), f.state.delete_audio(id, |_| true),
Err(AudioDeleteError::Busy(BEING_RECORDED)) Err(AudioDeleteError::Busy(BEING_RECORDED))
); );
assert_audio_kept(&f, id); assert_audio_kept(&f, id);
@@ -2344,14 +2378,14 @@ mod tests {
let lease = f.state.prepare_playback(id).unwrap().lease; let lease = f.state.prepare_playback(id).unwrap().lease;
assert_eq!( assert_eq!(
f.state.delete_audio(id), f.state.delete_audio(id, |_| true),
Err(AudioDeleteError::Busy(PLAYING)) Err(AudioDeleteError::Busy(PLAYING))
); );
assert_audio_kept(&f, id); assert_audio_kept(&f, id);
assert!(playback_file(&f, id).exists()); assert!(playback_file(&f, id).exists());
f.state.release_playback(id, lease); f.state.release_playback(id, lease);
assert_eq!(f.state.delete_audio(id), Ok(())); assert_eq!(f.state.delete_audio(id, |_| true), Ok(()));
} }
#[test] #[test]
@@ -2371,7 +2405,7 @@ mod tests {
store.set_status(id, CallStatus::Done, None).unwrap(); store.set_status(id, CallStatus::Done, None).unwrap();
assert!(matches!( assert!(matches!(
f.state.delete_audio(id), f.state.delete_audio(id, |_| true),
Err(AudioDeleteError::Failed(_)) Err(AudioDeleteError::Failed(_))
)); ));
assert!(!f.state.store.get_call(id).unwrap().unwrap().audio_deleted); assert!(!f.state.store.get_call(id).unwrap().unwrap().audio_deleted);
@@ -2379,16 +2413,131 @@ mod tests {
assert!(!out.exists()); assert!(!out.exists());
fs::remove_dir(&mic).unwrap(); fs::remove_dir(&mic).unwrap();
assert_eq!(f.state.delete_audio(id), Ok(())); assert_eq!(f.state.delete_audio(id, |_| true), Ok(()));
assert!(f.state.store.get_call(id).unwrap().unwrap().audio_deleted); assert!(f.state.store.get_call(id).unwrap().unwrap().audio_deleted);
} }
#[test] #[test]
fn deleting_the_audio_of_an_unknown_call_fails() { fn an_unknown_call_is_not_found() {
let f = fixture(); let f = fixture();
assert!(matches!( assert_eq!(
f.state.delete_audio(999), f.state.delete_audio(999, |_| true),
Err(AudioDeleteError::Failed(_)) Err(AudioDeleteError::NotFound)
)); );
assert_eq!(f.state.discard_call(999), Err(DiscardError::NotFound));
assert_eq!(
DiscardError::NotFound.to_string(),
"Gespräch nicht gefunden"
);
}
/// A capture source that starts and then delivers nothing.
struct SilentSource;
impl CaptureSource for SilentSource {
fn start(
&mut self,
_on_data: recorder_core::capture::DataCallback,
) -> Result<recorder_core::capture::StreamFormat, recorder_core::capture::CaptureError>
{
Ok(recorder_core::capture::StreamFormat {
rate: 16_000,
channels: 1,
})
}
fn stop(&mut self) {}
fn failure(&self) -> Option<String> {
None
}
}
#[test]
fn audio_of_the_running_recording_is_not_deleted() {
let f = fixture();
// A row that looks deletable: only `current` says it records.
let id = transcribed_call_with_audio(&f);
let dir = f.dir.path();
let recorder = Recorder::start(
Box::new(SilentSource),
Box::new(SilentSource),
&dir.join("live-mic.wav"),
&dir.join("live-out.wav"),
)
.unwrap();
*lock(&f.state.current) = Some(CurrentRecording {
call_id: id,
app: "Microsoft Teams".into(),
started_at: at(10, 0, 0),
detected: false,
recorder,
});
assert_eq!(
f.state.delete_audio(id, |_| true),
Err(AudioDeleteError::Busy(BEING_RECORDED))
);
assert_audio_kept(&f, id);
lock(&f.state.current).take().unwrap().recorder.discard();
}
#[test]
fn only_the_audio_of_a_transcribed_call_is_deleted() {
let f = fixture();
let (undecided, _, _) = finished_call(&f, "u");
let (failed, _, _) = finished_call(&f, "f");
f.state
.store
.set_status(failed, CallStatus::Failed, Some("Fehler"))
.unwrap();
for id in [undecided, failed] {
assert_eq!(
f.state.delete_audio(id, |_| true),
Err(AudioDeleteError::Busy(CHANGED))
);
assert_audio_kept(&f, id);
}
}
#[test]
fn audio_the_caller_no_longer_wants_deleted_is_kept() {
let f = fixture();
let id = transcribed_call_with_audio(&f);
let mut seen = None;
assert_eq!(
f.state.delete_audio(id, |call| {
seen = Some(call.id);
false
}),
Err(AudioDeleteError::Busy(CHANGED))
);
assert_eq!(seen, Some(id));
assert_audio_kept(&f, id);
assert!(playback_file(&f, id).exists());
}
#[test]
fn a_call_the_caller_no_longer_wants_deleted_is_kept() {
let f = fixture();
let (id, mic, out) = finished_call(&f, "a");
f.state.queue_prompt(id);
assert_eq!(
f.state
.discard_call_if(id, |call| call.status == CallStatus::Failed),
Err(DiscardError::Busy(CHANGED))
);
assert!(mic.exists());
assert!(out.exists());
assert!(f.state.store.get_call(id).unwrap().is_some());
assert_eq!(f.state.prompts_waiting(), 1);
assert_eq!(
f.state
.discard_call_if(id, |call| call.status == CallStatus::AwaitingDecision),
Ok(0)
);
assert_eq!(f.state.store.get_call(id).unwrap(), None);
} }
} }
+8 -5
View File
@@ -266,20 +266,23 @@
settings ? delayError(settings.start_after_secs, 1, 30, 'Start nach') : '', settings ? delayError(settings.start_after_secs, 1, 30, 'Start nach') : '',
) )
let endError = $derived(settings ? delayError(settings.end_after_secs, 3, 120, 'Ende nach') : '') let endError = $derived(settings ? delayError(settings.end_after_secs, 3, 120, 'Ende nach') : '')
/** The range the backend accepts (`RETENTION_DAYS`). */
function retentionValid(days: number | null | undefined): days is number {
return typeof days === 'number' && Number.isInteger(days) && days >= 1 && days <= 3650
}
/** The retention period in the hint ("7 Tage", "1 Tag"; "N Tage" while /** The retention period in the hint ("7 Tage", "1 Tag"; "N Tage" while
* the field holds no valid value). */ * the field holds no valid value). */
let retentionShown = $derived.by(() => { let retentionShown = $derived.by(() => {
const days = settings?.retention_days const days = settings?.retention_days
if (days === undefined || !Number.isInteger(days) || days < 1) return 'N Tage' if (!retentionValid(days)) return 'N Tage'
return days === 1 ? '1 Tag' : `${days} Tage` return days === 1 ? '1 Tag' : `${days} Tage`
}) })
async function persist() { async function persist() {
if (!settings) return if (!settings) return
if (startError || endError) throw startError || endError if (startError || endError) throw startError || endError
if (!Number.isInteger(settings.retention_days) || settings.retention_days < 1) { if (!retentionValid(settings.retention_days)) throw 'Aufbewahrung: 1–3650 Tage'
throw 'Aufbewahrung: bitte eine ganze Zahl ab 1 eingeben'
}
settings.vocabulary = vocabularyText.split('\n') settings.vocabulary = vocabularyText.split('\n')
await saveSettings($state.snapshot(settings)) await saveSettings($state.snapshot(settings))
// Re-read: the backend trims/dedupes the vocabulary, so the textarea // Re-read: the backend trims/dedupes the vocabulary, so the textarea
@@ -485,7 +488,7 @@
<option value="en">Englisch</option> <option value="en">Englisch</option>
</select> </select>
<label for="retention">Aufbewahrung der Audiodateien</label> <label for="retention">Aufbewahrung der Audiodateien</label>
<span><input id="retention" type="number" min="1" bind:value={settings.retention_days} /> Tage</span> <span><input id="retention" type="number" min="1" max="3650" bind:value={settings.retention_days} /> Tage</span>
<span></span> <span></span>
<span class="muted"> <span class="muted">
Audio wird {retentionShown} nach der Transkription gelöscht, nicht transkribierte Audio wird {retentionShown} nach der Transkription gelöscht, nicht transkribierte