agentmux_srv_core/backend/
always_available.rs1use std::collections::HashMap;
11
12pub const SETTING_ALWAYS_AVAILABLE: &str = "agents:always_available";
14
15pub fn enabled(extra: &HashMap<String, serde_json::Value>) -> bool {
17 match extra.get(SETTING_ALWAYS_AVAILABLE) {
18 Some(serde_json::Value::Bool(on)) => *on,
19 Some(serde_json::Value::String(s)) => !matches!(s.trim().to_ascii_lowercase().as_str(), "false" | "off" | "no" | "0"),
21 Some(serde_json::Value::Number(n)) => n.as_f64() != Some(0.0),
22 _ => true,
23 }
24}
25
26#[cfg(test)]
27mod tests {
28 use super::*;
29
30 #[test]
31 fn it_is_on_unless_the_setting_is_false() {
32 let mut extra = HashMap::new();
33 assert!(enabled(&extra), "the default");
34 extra.insert(SETTING_ALWAYS_AVAILABLE.to_string(), serde_json::json!("maybe"));
35 assert!(enabled(&extra), "a value that says nothing about off leaves the default");
36 for off in [serde_json::json!("no"), serde_json::json!("False"), serde_json::json!(" off "), serde_json::json!("0"), serde_json::json!(0)] {
37 extra.insert(SETTING_ALWAYS_AVAILABLE.to_string(), off.clone());
38 assert!(!enabled(&extra), "{off} is off");
39 }
40 extra.insert(SETTING_ALWAYS_AVAILABLE.to_string(), serde_json::json!(false));
41 assert!(!enabled(&extra));
42 extra.insert(SETTING_ALWAYS_AVAILABLE.to_string(), serde_json::json!(true));
43 assert!(enabled(&extra));
44 }
45}