Some checks are pending
Rhai Tests / Run Rhai Tests (push) Waiting to run
- Add the `mycelium` package to the workspace members. - Add `sal-mycelium` dependency to `Cargo.toml`. - Update MONOREPO_CONVERSION_PLAN.md to reflect the addition and completion of the mycelium package.
314 lines
9.8 KiB
Rust
314 lines
9.8 KiB
Rust
//! Rhai integration tests for Mycelium module
|
|
//!
|
|
//! These tests validate the Rhai wrapper functions and ensure proper
|
|
//! integration between Rust and Rhai for Mycelium operations.
|
|
|
|
use rhai::{Engine, EvalAltResult};
|
|
use sal_mycelium::rhai::*;
|
|
|
|
#[cfg(test)]
|
|
mod rhai_integration_tests {
|
|
use super::*;
|
|
|
|
fn create_test_engine() -> Engine {
|
|
let mut engine = Engine::new();
|
|
register_mycelium_module(&mut engine).expect("Failed to register mycelium module");
|
|
engine
|
|
}
|
|
|
|
#[test]
|
|
fn test_rhai_module_registration() {
|
|
let engine = create_test_engine();
|
|
|
|
// Test that the functions are registered by checking if they exist
|
|
let script = r#"
|
|
// Test that all mycelium functions are available
|
|
let functions_exist = true;
|
|
|
|
// We can't actually call these without a server, but we can verify they're registered
|
|
// by checking that the engine doesn't throw "function not found" errors
|
|
functions_exist
|
|
"#;
|
|
|
|
let result: Result<bool, Box<EvalAltResult>> = engine.eval(script);
|
|
assert!(result.is_ok());
|
|
assert_eq!(result.unwrap(), true);
|
|
}
|
|
|
|
#[test]
|
|
fn test_mycelium_get_node_info_function_exists() {
|
|
let engine = create_test_engine();
|
|
|
|
// Test that mycelium_get_node_info function is registered
|
|
let script = r#"
|
|
// This will fail with connection error, but proves the function exists
|
|
try {
|
|
mycelium_get_node_info("http://localhost:99999");
|
|
false; // Should not reach here
|
|
} catch(err) {
|
|
// Function exists but failed due to connection - this is expected
|
|
return err.to_string().contains("Mycelium error");
|
|
}
|
|
"#;
|
|
|
|
let result: Result<bool, Box<EvalAltResult>> = engine.eval(script);
|
|
if let Err(ref e) = result {
|
|
println!("Script evaluation error: {}", e);
|
|
}
|
|
assert!(result.is_ok());
|
|
assert_eq!(result.unwrap(), true);
|
|
}
|
|
|
|
#[test]
|
|
fn test_mycelium_list_peers_function_exists() {
|
|
let engine = create_test_engine();
|
|
|
|
let script = r#"
|
|
try {
|
|
mycelium_list_peers("http://localhost:99999");
|
|
return false;
|
|
} catch(err) {
|
|
return err.to_string().contains("Mycelium error");
|
|
}
|
|
"#;
|
|
|
|
let result: Result<bool, Box<EvalAltResult>> = engine.eval(script);
|
|
assert!(result.is_ok());
|
|
assert_eq!(result.unwrap(), true);
|
|
}
|
|
|
|
#[test]
|
|
fn test_mycelium_add_peer_function_exists() {
|
|
let engine = create_test_engine();
|
|
|
|
let script = r#"
|
|
try {
|
|
mycelium_add_peer("http://localhost:99999", "tcp://example.com:9651");
|
|
return false;
|
|
} catch(err) {
|
|
return err.to_string().contains("Mycelium error");
|
|
}
|
|
"#;
|
|
|
|
let result: Result<bool, Box<EvalAltResult>> = engine.eval(script);
|
|
assert!(result.is_ok());
|
|
assert_eq!(result.unwrap(), true);
|
|
}
|
|
|
|
#[test]
|
|
fn test_mycelium_remove_peer_function_exists() {
|
|
let engine = create_test_engine();
|
|
|
|
let script = r#"
|
|
try {
|
|
mycelium_remove_peer("http://localhost:99999", "peer_id");
|
|
return false;
|
|
} catch(err) {
|
|
return err.to_string().contains("Mycelium error");
|
|
}
|
|
"#;
|
|
|
|
let result: Result<bool, Box<EvalAltResult>> = engine.eval(script);
|
|
assert!(result.is_ok());
|
|
assert_eq!(result.unwrap(), true);
|
|
}
|
|
|
|
#[test]
|
|
fn test_mycelium_list_selected_routes_function_exists() {
|
|
let engine = create_test_engine();
|
|
|
|
let script = r#"
|
|
try {
|
|
mycelium_list_selected_routes("http://localhost:99999");
|
|
return false;
|
|
} catch(err) {
|
|
return err.to_string().contains("Mycelium error");
|
|
}
|
|
"#;
|
|
|
|
let result: Result<bool, Box<EvalAltResult>> = engine.eval(script);
|
|
assert!(result.is_ok());
|
|
assert_eq!(result.unwrap(), true);
|
|
}
|
|
|
|
#[test]
|
|
fn test_mycelium_list_fallback_routes_function_exists() {
|
|
let engine = create_test_engine();
|
|
|
|
let script = r#"
|
|
try {
|
|
mycelium_list_fallback_routes("http://localhost:99999");
|
|
return false;
|
|
} catch(err) {
|
|
return err.to_string().contains("Mycelium error");
|
|
}
|
|
"#;
|
|
|
|
let result: Result<bool, Box<EvalAltResult>> = engine.eval(script);
|
|
assert!(result.is_ok());
|
|
assert_eq!(result.unwrap(), true);
|
|
}
|
|
|
|
#[test]
|
|
fn test_mycelium_send_message_function_exists() {
|
|
let engine = create_test_engine();
|
|
|
|
let script = r#"
|
|
try {
|
|
mycelium_send_message("http://localhost:99999", "destination", "topic", "message", -1);
|
|
return false;
|
|
} catch(err) {
|
|
return err.to_string().contains("Mycelium error");
|
|
}
|
|
"#;
|
|
|
|
let result: Result<bool, Box<EvalAltResult>> = engine.eval(script);
|
|
assert!(result.is_ok());
|
|
assert_eq!(result.unwrap(), true);
|
|
}
|
|
|
|
#[test]
|
|
fn test_mycelium_receive_messages_function_exists() {
|
|
let engine = create_test_engine();
|
|
|
|
let script = r#"
|
|
try {
|
|
mycelium_receive_messages("http://localhost:99999", "topic", 1);
|
|
return false;
|
|
} catch(err) {
|
|
return err.to_string().contains("Mycelium error");
|
|
}
|
|
"#;
|
|
|
|
let result: Result<bool, Box<EvalAltResult>> = engine.eval(script);
|
|
assert!(result.is_ok());
|
|
assert_eq!(result.unwrap(), true);
|
|
}
|
|
|
|
#[test]
|
|
fn test_parameter_validation() {
|
|
let engine = create_test_engine();
|
|
|
|
// Test that functions handle parameter validation correctly
|
|
let script = r#"
|
|
let test_results = [];
|
|
|
|
// Test empty API URL
|
|
try {
|
|
mycelium_get_node_info("");
|
|
test_results.push(false);
|
|
} catch(err) {
|
|
test_results.push(true); // Expected to fail
|
|
}
|
|
|
|
// Test empty peer address
|
|
try {
|
|
mycelium_add_peer("http://localhost:8989", "");
|
|
test_results.push(false);
|
|
} catch(err) {
|
|
test_results.push(true); // Expected to fail
|
|
}
|
|
|
|
// Test negative timeout handling
|
|
try {
|
|
mycelium_receive_messages("http://localhost:99999", "topic", -1);
|
|
test_results.push(false);
|
|
} catch(err) {
|
|
// Should handle negative timeout gracefully
|
|
test_results.push(err.to_string().contains("Mycelium error"));
|
|
}
|
|
|
|
test_results
|
|
"#;
|
|
|
|
let result: Result<rhai::Array, Box<EvalAltResult>> = engine.eval(script);
|
|
assert!(result.is_ok());
|
|
let results = result.unwrap();
|
|
|
|
// All parameter validation tests should pass
|
|
for (i, result) in results.iter().enumerate() {
|
|
assert_eq!(
|
|
result.as_bool().unwrap_or(false),
|
|
true,
|
|
"Parameter validation test {} failed",
|
|
i
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn test_error_message_format() {
|
|
let engine = create_test_engine();
|
|
|
|
// Test that error messages are properly formatted
|
|
let script = r#"
|
|
try {
|
|
mycelium_get_node_info("http://localhost:99999");
|
|
return "";
|
|
} catch(err) {
|
|
let error_str = err.to_string();
|
|
// Should contain "Mycelium error:" prefix
|
|
if error_str.contains("Mycelium error:") {
|
|
return "correct_format";
|
|
} else {
|
|
return error_str;
|
|
}
|
|
}
|
|
"#;
|
|
|
|
let result: Result<String, Box<EvalAltResult>> = engine.eval(script);
|
|
assert!(result.is_ok());
|
|
assert_eq!(result.unwrap(), "correct_format");
|
|
}
|
|
|
|
#[test]
|
|
fn test_timeout_parameter_handling() {
|
|
let engine = create_test_engine();
|
|
|
|
// Test different timeout parameter values
|
|
let script = r#"
|
|
let timeout_tests = [];
|
|
|
|
// Test positive timeout
|
|
try {
|
|
mycelium_receive_messages("http://localhost:99999", "topic", 5);
|
|
timeout_tests.push(false);
|
|
} catch(err) {
|
|
timeout_tests.push(err.to_string().contains("Mycelium error"));
|
|
}
|
|
|
|
// Test zero timeout
|
|
try {
|
|
mycelium_receive_messages("http://localhost:99999", "topic", 0);
|
|
timeout_tests.push(false);
|
|
} catch(err) {
|
|
timeout_tests.push(err.to_string().contains("Mycelium error"));
|
|
}
|
|
|
|
// Test negative timeout (should be treated as no timeout)
|
|
try {
|
|
mycelium_receive_messages("http://localhost:99999", "topic", -1);
|
|
timeout_tests.push(false);
|
|
} catch(err) {
|
|
timeout_tests.push(err.to_string().contains("Mycelium error"));
|
|
}
|
|
|
|
timeout_tests
|
|
"#;
|
|
|
|
let result: Result<rhai::Array, Box<EvalAltResult>> = engine.eval(script);
|
|
assert!(result.is_ok());
|
|
let results = result.unwrap();
|
|
|
|
// All timeout tests should handle the connection error properly
|
|
for (i, result) in results.iter().enumerate() {
|
|
assert_eq!(
|
|
result.as_bool().unwrap_or(false),
|
|
true,
|
|
"Timeout test {} failed",
|
|
i
|
|
);
|
|
}
|
|
}
|
|
}
|