use crate::tests::types::Suite; use serde::Deserialize; use serde_json::Value; use std::collections::HashMap; use std::fs; use std::sync::{Arc, OnceLock, RwLock}; pub fn deserialize_some<'de, D>(deserializer: D) -> Result, D::Error> where D: serde::Deserializer<'de>, { let v = Value::deserialize(deserializer)?; Ok(Some(v)) } // Type alias for easier reading type CompiledSuite = Arc, crate::drop::Drop>>)>>; // Global cache mapping filename -> Vector of (Parsed JSON suite, Compiled Database) static CACHE: OnceLock>> = OnceLock::new(); fn get_cached_file(path: &str) -> CompiledSuite { let cache_lock = CACHE.get_or_init(|| RwLock::new(HashMap::new())); let file_data = { let read_guard = cache_lock.read().unwrap(); read_guard.get(path).cloned() }; match file_data { Some(data) => data, None => { let mut write_guard = cache_lock.write().unwrap(); // double check in case another thread compiled while we waited for lock if let Some(data) = write_guard.get(path) { data.clone() } else { let content = fs::read_to_string(path).unwrap_or_else(|_| panic!("Failed to read file: {}", path)); let suites: Vec = serde_json::from_str(&content) .unwrap_or_else(|e| panic!("Failed to parse JSON in {}: {}", path, e)); let mut compiled_suites = Vec::new(); for suite in suites { let db_result = crate::database::Database::new(&suite.database); let compiled_db = match db_result { Ok(db) => Ok(Arc::new(db)), Err(drop) => Err(drop), }; compiled_suites.push((suite, Arc::new(compiled_db))); } let new_data = Arc::new(compiled_suites); write_guard.insert(path.to_string(), new_data.clone()); new_data } } } } pub fn run_test_case(path: &str, suite_idx: usize, case_idx: usize) -> Result<(), String> { let file_data = get_cached_file(path); if suite_idx >= file_data.len() { panic!("Suite Index {} out of bounds for file {}", suite_idx, path); } let (group, db) = &file_data[suite_idx]; if case_idx >= group.tests.len() { panic!( "Case Index {} out of bounds for suite {} in file {}", case_idx, suite_idx, path ); } let test = &group.tests[case_idx]; let mut failures = Vec::::new(); // For validate/merge/query, if setup failed we must structurally fail this test let db_unwrapped = if test.action.as_str() != "compile" && test.action.as_str() != "database_compile" { match &**db { Ok(valid_db) => Some(valid_db.clone()), Err(drop) => { let error_messages: Vec = drop .errors .iter() .map(|e| format!("Error {} at path {}: {}", e.code, e.details.path, e.message)) .collect(); failures.push(format!( "[{}] Cannot run '{}' test '{}': System Setup Compilation structurally failed:\n{}", group.description, test.action, test.description, error_messages.join("\n") )); None } } } else { None }; if !failures.is_empty() { return Err(failures.join("\n")); } // 4. Run Tests match test.action.as_str() { "compile" | "database_compile" => { let result = test.run_compile(db); if let Err(e) = result { println!("TEST COMPILE ERROR FOR '{}': {}", test.description, e); failures.push(format!( "[{}] Compile Test '{}' failed. Error: {}", group.description, test.description, e )); } } "validate" => { let result = test.run_validate(db_unwrapped.unwrap()); if let Err(e) = result { println!("TEST VALIDATE ERROR FOR '{}': {}", test.description, e); failures.push(format!( "[{}] Validate Test '{}' failed. Error: {}", group.description, test.description, e )); } } "merge" => { let result = test.run_merge(db_unwrapped.unwrap()); if let Err(e) = result { println!("TEST MERGE ERROR FOR '{}': {}", test.description, e); failures.push(format!( "[{}] Merge Test '{}' failed. Error: {}", group.description, test.description, e )); } } "query" => { let result = test.run_query(db_unwrapped.unwrap()); if let Err(e) = result { println!("TEST QUERY ERROR FOR '{}': {}", test.description, e); failures.push(format!( "[{}] Query Test '{}' failed. Error: {}", group.description, test.description, e )); } } _ => { failures.push(format!( "[{}] Unknown action '{}' for test '{}'", group.description, test.action, test.description )); } } if !failures.is_empty() { return Err(failures.join("\n")); } Ok(()) }