herolib_rust/rhai/tests/rhai/01_basic_functionality.rhai
Mahmoud-Emad 8012a66250
Some checks are pending
Rhai Tests / Run Rhai Tests (push) Waiting to run
feat: Add Rhai scripting support
- Add new `sal-rhai` crate for Rhai scripting integration
- Integrate Rhai with existing SAL modules
- Improve error handling for Rhai scripts and SAL functions
- Add comprehensive unit and integration tests for `sal-rhai`
2025-06-23 16:23:51 +03:00

157 lines
4.4 KiB
Plaintext

// SAL Rhai Integration - Basic Functionality Tests
// Tests core functionality of all SAL modules through Rhai
print("🧪 SAL Rhai Integration - Basic Functionality Tests");
print("==================================================");
let total_tests = 0;
let passed_tests = 0;
// Helper function to run a test
fn run_test(test_name, test_fn) {
total_tests += 1;
print(`\nTest ${total_tests}: ${test_name}`);
try {
let result = test_fn.call();
if result {
print(" ✓ PASSED");
passed_tests += 1;
} else {
print(" ✗ FAILED - Test returned false");
}
} catch (error) {
print(` ✗ FAILED - Error: ${error}`);
}
}
// Test 1: OS Module - File Operations
run_test("OS Module - File Existence Check", || {
// Test with a file that should exist
let exists = exist("Cargo.toml");
exists == true
});
// Test 2: OS Module - Directory Operations
run_test("OS Module - Directory Creation and Deletion", || {
let test_dir = "/tmp/sal_rhai_test_dir";
// Create directory
let create_result = mkdir(test_dir);
let dir_exists = exist(test_dir);
// Clean up
if dir_exists {
delete(test_dir);
}
create_result.contains("Successfully") && dir_exists
});
// Test 3: Process Module - Command Existence
run_test("Process Module - Command Detection", || {
// Test with a command that should exist on most systems
let echo_path = which("echo");
echo_path != ()
});
// Test 4: Process Module - Command Execution
run_test("Process Module - Command Execution", || {
let result = run_command("echo 'Hello SAL'");
result.success && result.stdout.contains("Hello SAL")
});
// Test 5: Text Module - Text Processing
run_test("Text Module - Text Dedenting", || {
let indented = " Hello\n World";
let dedented = dedent(indented);
dedented == "Hello\nWorld"
});
// Test 6: Text Module - Text Prefixing
run_test("Text Module - Text Prefixing", || {
let text = "Line 1\nLine 2";
let prefixed = prefix(text, ">> ");
prefixed.contains(">> Line 1") && prefixed.contains(">> Line 2")
});
// Test 7: Text Module - Name Fixing
run_test("Text Module - Name Sanitization", || {
let unsafe_name = "My File [Draft].txt";
let safe_name = name_fix(unsafe_name);
!safe_name.contains("[") && !safe_name.contains("]")
});
// Test 8: Net Module - TCP Connectivity
run_test("Net Module - TCP Check (Closed Port)", || {
// Test with a port that should be closed
let result = tcp_check("127.0.0.1", 65534);
result == false // Should return false for closed port
});
// Test 9: Core Module - Exec Function
run_test("Core Module - Exec with String", || {
let result = exec("21 * 2");
result == 42
});
// Test 10: Core Module - Exec with Variables
run_test("Core Module - Exec with Variables", || {
let result = exec("let x = 10; let y = 5; x + y");
result == 15
});
// Test 11: Utility Functions
run_test("Utility Functions - is_def_fn", || {
let result = is_def_fn("test_function");
result == true // Should always return true in current implementation
});
// Test 12: Cross-Module Integration
run_test("Cross-Module Integration - Text and Process", || {
// Use text module to process content, then verify with process
let content = dedent(" echo 'test'");
let trimmed = content.trim();
// Execute the processed command
let result = run_command(trimmed);
result.success && result.stdout.contains("test")
});
// Test 13: Error Handling
run_test("Error Handling - Non-existent File", || {
try {
let size = file_size("definitely_nonexistent_file_xyz123.txt");
false // Should not reach here
} catch (error) {
// Should catch the error
true
}
});
// Test 14: Process Listing
run_test("Process Module - Process Listing", || {
let processes = process_list("");
processes.len() > 0
});
// Test 15: File Finding
run_test("OS Module - File Finding", || {
// Find Cargo.toml files
let files = find_files(".", "Cargo.toml");
files.len() > 0
});
// Print summary
print("\n==================================================");
print(`Test Summary: ${passed_tests}/${total_tests} tests passed`);
if passed_tests == total_tests {
print("🎉 All tests passed!");
} else {
print(`⚠️ ${total_tests - passed_tests} test(s) failed`);
}
// Return success status
passed_tests == total_tests