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:
@@ -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
|
||||
/// splits a call, e.g. when the headset is switched.
|
||||
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
|
||||
/// OpenAPI spec also lists `"standard"`, not offered here). An older
|
||||
@@ -113,6 +115,13 @@ impl Settings {
|
||||
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()) {
|
||||
return Err(format!(
|
||||
"Transkriptionsmodell: nur {} möglich",
|
||||
@@ -152,8 +161,9 @@ impl Settings {
|
||||
/// Reads the settings file. A missing or unreadable file yields the
|
||||
/// defaults; fields missing from the file take their default value. A
|
||||
/// `deepscript_model` outside [`DEEPSCRIPT_MODELS`] (e.g. an older
|
||||
/// file's `"standard"`) falls back to the default instead of failing;
|
||||
/// `vocabulary` is normalized (see [`normalize_vocabulary`]).
|
||||
/// file's `"standard"`) falls back to the default instead of failing, and
|
||||
/// so does a `retention_days` outside [`RETENTION_DAYS`]; `vocabulary`
|
||||
/// is normalized (see [`normalize_vocabulary`]).
|
||||
pub fn load(path: &Path) -> Settings {
|
||||
let mut settings: Settings = fs::read_to_string(path)
|
||||
.ok()
|
||||
@@ -162,6 +172,10 @@ impl Settings {
|
||||
if !DEEPSCRIPT_MODELS.contains(&settings.deepscript_model.as_str()) {
|
||||
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
|
||||
}
|
||||
@@ -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]
|
||||
fn validate_rejects_a_model_outside_the_offered_two() {
|
||||
for model in ["standard", "nonsense", ""] {
|
||||
|
||||
Reference in New Issue
Block a user