Some checks are pending
Rhai Tests / Run Rhai Tests (push) Waiting to run
- Add a new `sal-os` package containing OS interaction utilities. - Update workspace members to include the new package. - Add README and basic usage examples for the new package.
365 lines
11 KiB
Rust
365 lines
11 KiB
Rust
use rhai::Engine;
|
|
use sal_os::rhai::register_os_module;
|
|
use tempfile::TempDir;
|
|
|
|
fn create_test_engine() -> Engine {
|
|
let mut engine = Engine::new();
|
|
register_os_module(&mut engine).expect("Failed to register OS module");
|
|
engine
|
|
}
|
|
|
|
#[test]
|
|
fn test_rhai_module_registration() {
|
|
// Test that the OS module can be registered without errors
|
|
let _engine = create_test_engine();
|
|
|
|
// If we get here without panicking, the module was registered successfully
|
|
// We can't easily test function registration without calling the functions
|
|
}
|
|
|
|
#[test]
|
|
fn test_rhai_file_operations() {
|
|
let engine = create_test_engine();
|
|
let temp_dir = TempDir::new().unwrap();
|
|
let temp_path = temp_dir.path().to_str().unwrap();
|
|
|
|
// Test file operations through Rhai
|
|
let script = format!(
|
|
r#"
|
|
let test_dir = "{}/test_rhai";
|
|
let test_file = test_dir + "/test.txt";
|
|
let content = "Hello from Rhai!";
|
|
|
|
// Create directory
|
|
mkdir(test_dir);
|
|
|
|
// Check if directory exists
|
|
let dir_exists = exist(test_dir);
|
|
|
|
// Write file
|
|
file_write(test_file, content);
|
|
|
|
// Check if file exists
|
|
let file_exists = exist(test_file);
|
|
|
|
// Read file
|
|
let read_content = file_read(test_file);
|
|
|
|
// Return results
|
|
#{{"dir_exists": dir_exists, "file_exists": file_exists, "content_match": read_content == content}}
|
|
"#,
|
|
temp_path
|
|
);
|
|
|
|
let result: rhai::Map = engine.eval(&script).expect("Script execution failed");
|
|
|
|
assert_eq!(result["dir_exists"].as_bool().unwrap(), true);
|
|
assert_eq!(result["file_exists"].as_bool().unwrap(), true);
|
|
assert_eq!(result["content_match"].as_bool().unwrap(), true);
|
|
}
|
|
|
|
#[test]
|
|
fn test_rhai_file_size() {
|
|
let engine = create_test_engine();
|
|
let temp_dir = TempDir::new().unwrap();
|
|
let temp_path = temp_dir.path().to_str().unwrap();
|
|
|
|
let script = format!(
|
|
r#"
|
|
let test_file = "{}/size_test.txt";
|
|
let content = "12345"; // 5 bytes
|
|
|
|
file_write(test_file, content);
|
|
let size = file_size(test_file);
|
|
|
|
size
|
|
"#,
|
|
temp_path
|
|
);
|
|
|
|
let result: i64 = engine.eval(&script).expect("Script execution failed");
|
|
assert_eq!(result, 5);
|
|
}
|
|
|
|
#[test]
|
|
fn test_rhai_file_append() {
|
|
let engine = create_test_engine();
|
|
let temp_dir = TempDir::new().unwrap();
|
|
let temp_path = temp_dir.path().to_str().unwrap();
|
|
|
|
let script = format!(
|
|
r#"
|
|
let test_file = "{}/append_test.txt";
|
|
|
|
file_write(test_file, "Line 1\n");
|
|
file_write_append(test_file, "Line 2\n");
|
|
|
|
let content = file_read(test_file);
|
|
content
|
|
"#,
|
|
temp_path
|
|
);
|
|
|
|
let result: String = engine.eval(&script).expect("Script execution failed");
|
|
assert_eq!(result, "Line 1\nLine 2\n");
|
|
}
|
|
|
|
#[test]
|
|
fn test_rhai_copy_and_move() {
|
|
let engine = create_test_engine();
|
|
let temp_dir = TempDir::new().unwrap();
|
|
let temp_path = temp_dir.path().to_str().unwrap();
|
|
|
|
let script = format!(
|
|
r#"
|
|
let source = "{}/source.txt";
|
|
let copy_dest = "{}/copy.txt";
|
|
let move_dest = "{}/moved.txt";
|
|
let content = "Test content";
|
|
|
|
// Create source file
|
|
file_write(source, content);
|
|
|
|
// Copy file
|
|
copy(source, copy_dest);
|
|
|
|
// Move the copy
|
|
mv(copy_dest, move_dest);
|
|
|
|
// Check results
|
|
let source_exists = exist(source);
|
|
let copy_exists = exist(copy_dest);
|
|
let move_exists = exist(move_dest);
|
|
let move_content = file_read(move_dest);
|
|
|
|
#{{"source_exists": source_exists, "copy_exists": copy_exists, "move_exists": move_exists, "content_match": move_content == content}}
|
|
"#,
|
|
temp_path, temp_path, temp_path
|
|
);
|
|
|
|
let result: rhai::Map = engine.eval(&script).expect("Script execution failed");
|
|
|
|
assert_eq!(result["source_exists"].as_bool().unwrap(), true);
|
|
assert_eq!(result["copy_exists"].as_bool().unwrap(), false); // Should be moved
|
|
assert_eq!(result["move_exists"].as_bool().unwrap(), true);
|
|
assert_eq!(result["content_match"].as_bool().unwrap(), true);
|
|
}
|
|
|
|
#[test]
|
|
fn test_rhai_delete() {
|
|
let engine = create_test_engine();
|
|
let temp_dir = TempDir::new().unwrap();
|
|
let temp_path = temp_dir.path().to_str().unwrap();
|
|
|
|
let script = format!(
|
|
r#"
|
|
let test_file = "{}/delete_test.txt";
|
|
|
|
// Create file
|
|
file_write(test_file, "content");
|
|
let exists_before = exist(test_file);
|
|
|
|
// Delete file
|
|
delete(test_file);
|
|
let exists_after = exist(test_file);
|
|
|
|
#{{"before": exists_before, "after": exists_after}}
|
|
"#,
|
|
temp_path
|
|
);
|
|
|
|
let result: rhai::Map = engine.eval(&script).expect("Script execution failed");
|
|
|
|
assert_eq!(result["before"].as_bool().unwrap(), true);
|
|
assert_eq!(result["after"].as_bool().unwrap(), false);
|
|
}
|
|
|
|
#[test]
|
|
fn test_rhai_find_files() {
|
|
let engine = create_test_engine();
|
|
let temp_dir = TempDir::new().unwrap();
|
|
let temp_path = temp_dir.path().to_str().unwrap();
|
|
|
|
let script = format!(
|
|
r#"
|
|
let test_dir = "{}/find_test";
|
|
mkdir(test_dir);
|
|
|
|
// Create test files
|
|
file_write(test_dir + "/file1.txt", "content1");
|
|
file_write(test_dir + "/file2.txt", "content2");
|
|
file_write(test_dir + "/other.log", "log content");
|
|
|
|
// Find .txt files
|
|
let txt_files = find_files(test_dir, "*.txt");
|
|
let all_files = find_files(test_dir, "*");
|
|
|
|
#{{"txt_count": txt_files.len(), "all_count": all_files.len()}}
|
|
"#,
|
|
temp_path
|
|
);
|
|
|
|
let result: rhai::Map = engine.eval(&script).expect("Script execution failed");
|
|
|
|
assert_eq!(result["txt_count"].as_int().unwrap(), 2);
|
|
assert!(result["all_count"].as_int().unwrap() >= 3);
|
|
}
|
|
|
|
#[test]
|
|
fn test_rhai_which_command() {
|
|
let engine = create_test_engine();
|
|
|
|
let script = r#"
|
|
let ls_path = which("ls");
|
|
let nonexistent = which("nonexistentcommand12345");
|
|
|
|
#{"ls_found": ls_path.len() > 0, "nonexistent_found": nonexistent.len() > 0}
|
|
"#;
|
|
|
|
let result: rhai::Map = engine.eval(script).expect("Script execution failed");
|
|
|
|
assert_eq!(result["ls_found"].as_bool().unwrap(), true);
|
|
assert_eq!(result["nonexistent_found"].as_bool().unwrap(), false);
|
|
}
|
|
|
|
#[test]
|
|
fn test_rhai_error_handling() {
|
|
let engine = create_test_engine();
|
|
|
|
// Test that errors are properly propagated to Rhai
|
|
// Instead of try-catch, just test that the function call fails
|
|
let script = r#"file_read("/nonexistent/path/file.txt")"#;
|
|
|
|
let result = engine.eval::<String>(script);
|
|
assert!(
|
|
result.is_err(),
|
|
"Expected error when reading non-existent file"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn test_rhai_package_functions() {
|
|
let engine = create_test_engine();
|
|
|
|
// Test that package functions are registered by calling them
|
|
|
|
let script = r#"
|
|
let platform = package_platform();
|
|
let debug_result = package_set_debug(true);
|
|
|
|
#{"platform": platform, "debug": debug_result}
|
|
"#;
|
|
|
|
let result: rhai::Map = engine.eval(script).expect("Script execution failed");
|
|
|
|
// Platform should be a non-empty string
|
|
let platform: String = result["platform"].clone().try_cast().unwrap();
|
|
assert!(!platform.is_empty());
|
|
|
|
// Debug setting should return true
|
|
assert_eq!(result["debug"].as_bool().unwrap(), true);
|
|
}
|
|
|
|
#[test]
|
|
fn test_rhai_download_functions() {
|
|
let engine = create_test_engine();
|
|
|
|
// Test that download functions are registered by calling them
|
|
|
|
let temp_dir = TempDir::new().unwrap();
|
|
let temp_path = temp_dir.path().to_str().unwrap();
|
|
|
|
let script = format!(
|
|
r#"
|
|
let test_file = "{}/test_script.sh";
|
|
|
|
// Create a test script
|
|
file_write(test_file, "echo 'test'");
|
|
|
|
// Make it executable
|
|
try {{
|
|
let result = chmod_exec(test_file);
|
|
result.len() >= 0 // chmod_exec returns a string, so check if it's valid
|
|
}} catch {{
|
|
false
|
|
}}
|
|
"#,
|
|
temp_path
|
|
);
|
|
|
|
let result: bool = engine.eval(&script).expect("Script execution failed");
|
|
assert!(result);
|
|
}
|
|
|
|
#[test]
|
|
fn test_rhai_array_returns() {
|
|
let engine = create_test_engine();
|
|
let temp_dir = TempDir::new().unwrap();
|
|
let temp_path = temp_dir.path().to_str().unwrap();
|
|
|
|
let script = format!(
|
|
r#"
|
|
let test_dir = "{}/array_test";
|
|
mkdir(test_dir);
|
|
|
|
// Create some files
|
|
file_write(test_dir + "/file1.txt", "content");
|
|
file_write(test_dir + "/file2.txt", "content");
|
|
|
|
// Test that find_files returns an array
|
|
let files = find_files(test_dir, "*.txt");
|
|
|
|
// Test array operations
|
|
let count = files.len();
|
|
let first_file = if count > 0 {{ files[0] }} else {{ "" }};
|
|
|
|
#{{"count": count, "has_files": count > 0, "first_file_exists": first_file.len() > 0}}
|
|
"#,
|
|
temp_path
|
|
);
|
|
|
|
let result: rhai::Map = engine.eval(&script).expect("Script execution failed");
|
|
|
|
assert_eq!(result["count"].as_int().unwrap(), 2);
|
|
assert_eq!(result["has_files"].as_bool().unwrap(), true);
|
|
assert_eq!(result["first_file_exists"].as_bool().unwrap(), true);
|
|
}
|
|
|
|
#[test]
|
|
fn test_rhai_platform_functions() {
|
|
let engine = create_test_engine();
|
|
|
|
let script = r#"
|
|
let is_osx = platform_is_osx();
|
|
let is_linux = platform_is_linux();
|
|
let is_arm = platform_is_arm();
|
|
let is_x86 = platform_is_x86();
|
|
|
|
// Test that platform detection is consistent
|
|
let platform_consistent = !(is_osx && is_linux);
|
|
let arch_consistent = !(is_arm && is_x86);
|
|
|
|
#{"osx": is_osx, "linux": is_linux, "arm": is_arm, "x86": is_x86, "platform_consistent": platform_consistent, "arch_consistent": arch_consistent}
|
|
"#;
|
|
|
|
let result: rhai::Map = engine.eval(script).expect("Script execution failed");
|
|
|
|
// Verify platform detection consistency
|
|
assert_eq!(result["platform_consistent"].as_bool().unwrap(), true);
|
|
assert_eq!(result["arch_consistent"].as_bool().unwrap(), true);
|
|
|
|
// At least one platform should be detected
|
|
let osx = result["osx"].as_bool().unwrap();
|
|
let linux = result["linux"].as_bool().unwrap();
|
|
|
|
// At least one architecture should be detected
|
|
let arm = result["arm"].as_bool().unwrap();
|
|
let x86 = result["x86"].as_bool().unwrap();
|
|
|
|
// Print current platform for debugging
|
|
println!(
|
|
"Platform detection: OSX={}, Linux={}, ARM={}, x86={}",
|
|
osx, linux, arm, x86
|
|
);
|
|
}
|