use std::sync::{Mutex, MutexGuard, OnceLock};

/// Serializes tests that mutate process env vars like `AIMGR_CONFIG_HOME` / `AIMGR_STATE_HOME`.
/// `cargo test` runs tests in parallel by default, which otherwise causes flaky interference.
pub fn env_lock() -> MutexGuard<'static, ()> {
    static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
    LOCK.get_or_init(|| Mutex::new(()))
        .lock()
        .unwrap_or_else(|e| e.into_inner())
}
