spec_ai_config/
test_utils.rs

1use std::sync::{Mutex, OnceLock};
2
3/// Global test utilities
4///
5/// Provides a process-wide mutex to serialize tests that mutate process-wide
6/// state (like environment variables). Use this to avoid flaky tests when
7/// `cargo test` runs tests in parallel.
8pub fn env_lock() -> &'static Mutex<()> {
9    static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
10    LOCK.get_or_init(|| Mutex::new(()))
11}
12
13pub fn create_test_db() -> crate::persistence::Persistence {
14    use tempfile::tempdir;
15    let dir = tempdir().unwrap();
16    let db_path = dir.path().join("test.duckdb");
17    // Leak the temp dir to keep it alive for the test duration
18    std::mem::forget(dir);
19    crate::persistence::Persistence::new(&db_path).unwrap()
20}