Some checks are pending
Rhai Tests / Run Rhai Tests (push) Waiting to run
- Add `sal-process` package for cross-platform process management. - Update workspace members in `Cargo.toml`. - Mark process package as complete in MONOREPO_CONVERSION_PLAN.md - Remove license information from `mycelium` and `os` READMEs.
154 lines
5.8 KiB
Plaintext
154 lines
5.8 KiB
Plaintext
// Test script for process management functionality
|
|
|
|
print("=== Process Management Tests ===");
|
|
|
|
// Test 1: which function with existing command
|
|
print("\n--- Test 1: Which Function (Existing Command) ---");
|
|
let echo_path = which("echo");
|
|
if echo_path != () {
|
|
assert_true(echo_path.len() > 0, "Echo path should not be empty");
|
|
print(`✓ which("echo") found at: ${echo_path}`);
|
|
} else {
|
|
// Try platform-specific commands
|
|
let cmd_path = which("cmd");
|
|
let sh_path = which("sh");
|
|
assert_true(cmd_path != () || sh_path != (), "Should find either cmd or sh");
|
|
print("✓ which() function works with platform-specific commands");
|
|
}
|
|
|
|
// Test 2: which function with nonexistent command
|
|
print("\n--- Test 2: Which Function (Nonexistent Command) ---");
|
|
let nonexistent = which("nonexistent_command_12345");
|
|
assert_true(nonexistent == (), "Nonexistent command should return ()");
|
|
print("✓ which() correctly handles nonexistent commands");
|
|
|
|
// Test 3: process_list function
|
|
print("\n--- Test 3: Process List Function ---");
|
|
let all_processes = process_list("");
|
|
assert_true(all_processes.len() > 0, "Should find at least one running process");
|
|
print(`✓ process_list("") found ${all_processes.len()} processes`);
|
|
|
|
// Test 4: process info properties
|
|
print("\n--- Test 4: Process Info Properties ---");
|
|
if all_processes.len() > 0 {
|
|
let first_process = all_processes[0];
|
|
assert_true(first_process.pid > 0, "Process PID should be positive");
|
|
assert_true(first_process.name.len() > 0, "Process name should not be empty");
|
|
assert_true(first_process.memory >= 0.0, "Process memory should be non-negative");
|
|
assert_true(first_process.cpu >= 0.0, "Process CPU should be non-negative");
|
|
print(`✓ Process properties: PID=${first_process.pid}, Name=${first_process.name}`);
|
|
}
|
|
|
|
// Test 5: process_list with pattern
|
|
print("\n--- Test 5: Process List with Pattern ---");
|
|
if all_processes.len() > 0 {
|
|
let test_process = all_processes[0];
|
|
let filtered_processes = process_list(test_process.name);
|
|
assert_true(filtered_processes.len() >= 1, "Should find at least the test process");
|
|
|
|
// Verify all filtered processes contain the pattern
|
|
for process in filtered_processes {
|
|
assert_true(process.name.contains(test_process.name), "Filtered process should contain pattern");
|
|
}
|
|
print(`✓ process_list("${test_process.name}") found ${filtered_processes.len()} matching processes`);
|
|
}
|
|
|
|
// Test 6: process_list with nonexistent pattern
|
|
print("\n--- Test 6: Process List with Nonexistent Pattern ---");
|
|
let empty_list = process_list("nonexistent_process_12345");
|
|
assert_true(empty_list.len() == 0, "Should find no processes with nonexistent pattern");
|
|
print("✓ process_list() correctly handles nonexistent patterns");
|
|
|
|
// Test 7: kill function with nonexistent process
|
|
print("\n--- Test 7: Kill Function (Nonexistent Process) ---");
|
|
let kill_result = kill("nonexistent_process_12345");
|
|
assert_true(
|
|
kill_result.contains("No matching processes") || kill_result.contains("Successfully killed"),
|
|
"Kill should handle nonexistent processes gracefully"
|
|
);
|
|
print(`✓ kill("nonexistent_process_12345") result: ${kill_result}`);
|
|
|
|
// Test 8: Common system commands detection
|
|
print("\n--- Test 8: Common System Commands Detection ---");
|
|
let common_commands = ["echo", "ls", "cat", "grep", "awk", "sed"];
|
|
let windows_commands = ["cmd", "powershell", "notepad", "tasklist"];
|
|
|
|
let found_commands = [];
|
|
for cmd in common_commands {
|
|
let path = which(cmd);
|
|
if path != () {
|
|
found_commands.push(cmd);
|
|
}
|
|
}
|
|
|
|
for cmd in windows_commands {
|
|
let path = which(cmd);
|
|
if path != () {
|
|
found_commands.push(cmd);
|
|
}
|
|
}
|
|
|
|
assert_true(found_commands.len() > 0, "Should find at least one common command");
|
|
print(`✓ Found common commands: ${found_commands}`);
|
|
|
|
// Test 9: Process filtering accuracy
|
|
print("\n--- Test 9: Process Filtering Accuracy ---");
|
|
if all_processes.len() > 0 {
|
|
let test_process = all_processes[0];
|
|
let filtered = process_list(test_process.name);
|
|
|
|
// All filtered processes should contain the pattern
|
|
let all_match = true;
|
|
for process in filtered {
|
|
if !process.name.contains(test_process.name) {
|
|
all_match = false;
|
|
break;
|
|
}
|
|
}
|
|
assert_true(all_match, "All filtered processes should contain the search pattern");
|
|
print("✓ Process filtering is accurate");
|
|
}
|
|
|
|
// Test 10: Process management performance
|
|
print("\n--- Test 10: Process Management Performance ---");
|
|
let start_time = timestamp();
|
|
let perf_processes = process_list("");
|
|
let end_time = timestamp();
|
|
let duration = end_time - start_time;
|
|
|
|
assert_true(duration < 5000, "Process listing should complete within 5 seconds");
|
|
assert_true(perf_processes.len() > 0, "Performance test should still return processes");
|
|
print(`✓ process_list() completed in ${duration}ms`);
|
|
|
|
// Test 11: which command performance
|
|
print("\n--- Test 11: Which Command Performance ---");
|
|
let which_start = timestamp();
|
|
let which_result = which("echo");
|
|
let which_end = timestamp();
|
|
let which_duration = which_end - which_start;
|
|
|
|
assert_true(which_duration < 1000, "which() should complete within 1 second");
|
|
print(`✓ which("echo") completed in ${which_duration}ms`);
|
|
|
|
// Test 12: Cross-platform process operations
|
|
print("\n--- Test 12: Cross-Platform Process Operations ---");
|
|
let platform_specific_found = false;
|
|
|
|
// Try Windows-specific
|
|
let cmd_found = which("cmd");
|
|
if cmd_found != () {
|
|
platform_specific_found = true;
|
|
print("✓ Windows platform detected (cmd found)");
|
|
}
|
|
|
|
// Try Unix-specific
|
|
let sh_found = which("sh");
|
|
if sh_found != () {
|
|
platform_specific_found = true;
|
|
print("✓ Unix-like platform detected (sh found)");
|
|
}
|
|
|
|
assert_true(platform_specific_found, "Should detect platform-specific commands");
|
|
|
|
print("\n=== All Process Management Tests Passed! ===");
|