use std::fs; use std::sync::{Arc, RwLock}; use rhai::Engine; use rhai_system::{System, hot_reload_callback}; #[test] fn test_hot_reload_callback() { // Create temporary script files let temp_dir = tempfile::tempdir().unwrap(); let script_path = temp_dir.path().join("test_script.rhai"); // Write initial script content let initial_script = r#" fn greet(name) { "Hello, " + name + "! This is the original script." } fn add(a, b) { a + b } "#; fs::write(&script_path, initial_script).unwrap(); // Create engine and compile initial script let engine = Engine::new(); let ast = engine.compile_file(script_path.clone()).unwrap(); let shared_ast = Arc::new(RwLock::new(ast)); // Create a system with the initial script let script_paths = vec![script_path.clone()]; let system = System::new(engine, shared_ast, script_paths); // Test initial script functionality let result: String = system.call_fn("greet", ("User",)).unwrap(); assert_eq!(result, "Hello, User! This is the original script."); let add_result: i32 = system.call_fn("add", (40, 2)).unwrap(); assert_eq!(add_result, 42); // Write modified script content with new functions let modified_script = r#" fn greet(name) { "Hello, " + name + "! This is the MODIFIED script!" } fn add(a, b) { a + b } fn multiply(a, b) { a * b } "#; fs::write(&script_path, modified_script).unwrap(); // Call the hot reload callback hot_reload_callback(&system, script_path.to_str().unwrap()).unwrap(); // Test that the script was updated let result: String = system.call_fn("greet", ("User",)).unwrap(); assert_eq!(result, "Hello, User! This is the MODIFIED script!"); // Test that the new function is available let multiply_result: i32 = system.call_fn("multiply", (6, 7)).unwrap(); assert_eq!(multiply_result, 42); } #[test] fn test_hot_reload_callback_with_syntax_error() { // Create temporary script files let temp_dir = tempfile::tempdir().unwrap(); let script_path = temp_dir.path().join("test_script.rhai"); // Write initial script content let initial_script = r#" fn greet(name) { "Hello, " + name + "! This is the original script." } fn add(a, b) { a + b } "#; fs::write(&script_path, initial_script).unwrap(); // Create engine and compile initial script let engine = Engine::new(); let ast = engine.compile_file(script_path.clone()).unwrap(); let shared_ast = Arc::new(RwLock::new(ast)); // Create a system with the initial script let script_paths = vec![script_path.clone()]; let system = System::new(engine, shared_ast, script_paths); // Test initial script functionality let result: String = system.call_fn("greet", ("User",)).unwrap(); assert_eq!(result, "Hello, User! This is the original script."); // Write modified script content with syntax error let modified_script = r#" fn greet(name) { "Hello, " + name + "! This is the MODIFIED script!" } fn add(a, b) { a + b } fn syntax_error() { // Missing closing brace if (true) { "This will cause a syntax error" } "#; fs::write(&script_path, modified_script).unwrap(); // Call the hot reload callback - it should return an error let result = hot_reload_callback(&system, script_path.to_str().unwrap()); assert!(result.is_err()); // Test that the original script functionality is still available let result: String = system.call_fn("greet", ("User",)).unwrap(); assert_eq!(result, "Hello, User! This is the original script."); }