use serde::{Deserialize, Serialize};
pub const MAX_AGENT_PRUNE_DAYS: u32 = 36_500;
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq)]
#[serde(default)]
pub struct ServerSettings {
#[serde(skip_serializing_if = "Option::is_none")]
pub agent_prune_days: Option<u32>,
}
impl ServerSettings {
pub fn defaults() -> Self {
Self {
agent_prune_days: None,
}
}
pub fn effective_agent_prune_days(&self) -> u32 {
self.agent_prune_days
.or(Self::defaults().agent_prune_days)
.unwrap_or(0)
.min(MAX_AGENT_PRUNE_DAYS)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_is_unset() {
assert_eq!(ServerSettings::default().agent_prune_days, None);
}
#[test]
fn unset_resolves_to_disabled() {
assert_eq!(ServerSettings::default().effective_agent_prune_days(), 0);
}
#[test]
fn stored_value_wins_over_default() {
let s = ServerSettings {
agent_prune_days: Some(30),
};
assert_eq!(s.effective_agent_prune_days(), 30);
}
#[test]
fn effective_clamps_to_max() {
let s = ServerSettings {
agent_prune_days: Some(u32::MAX),
};
assert_eq!(s.effective_agent_prune_days(), MAX_AGENT_PRUNE_DAYS);
}
#[test]
fn round_trips_through_json() {
let s = ServerSettings {
agent_prune_days: Some(30),
};
let json = serde_json::to_string(&s).unwrap();
assert_eq!(json, r#"{"agent_prune_days":30}"#);
let back: ServerSettings = serde_json::from_str(&json).unwrap();
assert_eq!(back, s);
}
#[test]
fn unset_serialises_to_empty_object() {
let s = ServerSettings::default();
let json = serde_json::to_string(&s).unwrap();
assert_eq!(json, "{}");
let back: ServerSettings = serde_json::from_str(&json).unwrap();
assert_eq!(back, s);
}
#[test]
fn explicit_null_decodes_to_unset() {
let s: ServerSettings = serde_json::from_str(r#"{"agent_prune_days":null}"#).unwrap();
assert_eq!(s.agent_prune_days, None);
}
#[test]
fn empty_object_decodes_to_default() {
let s: ServerSettings = serde_json::from_str("{}").unwrap();
assert_eq!(s, ServerSettings::default());
}
#[test]
fn accepts_unknown_fields_for_forward_compat() {
let json = r#"{"agent_prune_days":7,"some_future_knob":true}"#;
let s: ServerSettings = serde_json::from_str(json).unwrap();
assert_eq!(s.agent_prune_days, Some(7));
}
}