use rch_common::SelfHealingConfig;
use std::sync::atomic::{AtomicBool, Ordering};
static NO_SELF_HEALING: AtomicBool = AtomicBool::new(false);
static NO_HOOK_AUTO_START: AtomicBool = AtomicBool::new(false);
pub fn set_no_self_healing(value: bool) {
NO_SELF_HEALING.store(value, Ordering::Release);
}
pub fn set_no_hook_auto_start(value: bool) {
NO_HOOK_AUTO_START.store(value, Ordering::Release);
}
pub fn no_self_healing() -> bool {
NO_SELF_HEALING.load(Ordering::Acquire)
}
pub fn no_hook_auto_start() -> bool {
NO_HOOK_AUTO_START.load(Ordering::Acquire)
}
pub fn apply_to(config: &mut SelfHealingConfig) {
if no_self_healing() {
config.hook_starts_daemon = false;
config.daemon_installs_hooks = false;
}
if no_hook_auto_start() {
config.hook_starts_daemon = false;
}
}
pub fn active_cli_overrides() -> Vec<&'static str> {
let mut out = Vec::new();
if no_self_healing() {
out.push("--no-self-healing");
}
if no_hook_auto_start() {
out.push("--no-hook-auto-start");
}
out
}
#[cfg(test)]
mod tests {
use super::*;
use rch_common::SelfHealingConfig;
fn reset() {
NO_SELF_HEALING.store(false, Ordering::Release);
NO_HOOK_AUTO_START.store(false, Ordering::Release);
}
#[test]
fn test_apply_to_no_self_healing_disables_both() {
let _guard = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
reset();
set_no_self_healing(true);
let mut config = SelfHealingConfig::default();
assert!(config.hook_starts_daemon);
assert!(config.daemon_installs_hooks);
apply_to(&mut config);
assert!(!config.hook_starts_daemon);
assert!(!config.daemon_installs_hooks);
reset();
}
#[test]
fn test_apply_to_no_hook_auto_start_only_disables_hook_starter() {
let _guard = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
reset();
set_no_hook_auto_start(true);
let mut config = SelfHealingConfig::default();
apply_to(&mut config);
assert!(!config.hook_starts_daemon);
assert!(
config.daemon_installs_hooks,
"daemon-side healing should remain enabled"
);
reset();
}
static TEST_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
#[test]
fn test_active_cli_overrides_lists_set_flags() {
let _guard = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
reset();
assert!(active_cli_overrides().is_empty());
set_no_self_healing(true);
let active = active_cli_overrides();
assert!(active.contains(&"--no-self-healing"));
reset();
}
#[test]
fn test_active_cli_overrides_combined() {
let _guard = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
reset();
set_no_self_healing(true);
set_no_hook_auto_start(true);
let active = active_cli_overrides();
assert_eq!(active.len(), 2);
reset();
}
#[test]
fn test_default_state_is_false() {
let _guard = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
reset();
assert!(!no_self_healing());
assert!(!no_hook_auto_start());
}
}