This commit is contained in:
2025-08-05 15:33:03 +02:00
parent 7856fc0a4e
commit 0c02d0e99f
326 changed files with 334 additions and 0 deletions

View File

@@ -0,0 +1,279 @@
//! Unit tests for Mycelium client functionality
//!
//! These tests validate the core Mycelium client operations including:
//! - Node information retrieval
//! - Peer management (listing, adding, removing)
//! - Route inspection (selected and fallback routes)
//! - Message operations (sending and receiving)
//!
//! Tests are designed to work with a real Mycelium node when available,
//! but gracefully handle cases where the node is not accessible.
use sal_mycelium::*;
use std::time::Duration;
/// Test configuration for Mycelium API
const TEST_API_URL: &str = "http://localhost:8989";
const FALLBACK_API_URL: &str = "http://localhost:7777";
/// Helper function to check if a Mycelium node is available
async fn is_mycelium_available(api_url: &str) -> bool {
match get_node_info(api_url).await {
Ok(_) => true,
Err(_) => false,
}
}
/// Helper function to get an available Mycelium API URL
async fn get_available_api_url() -> Option<String> {
if is_mycelium_available(TEST_API_URL).await {
Some(TEST_API_URL.to_string())
} else if is_mycelium_available(FALLBACK_API_URL).await {
Some(FALLBACK_API_URL.to_string())
} else {
None
}
}
#[tokio::test]
async fn test_get_node_info_success() {
if let Some(api_url) = get_available_api_url().await {
let result = get_node_info(&api_url).await;
match result {
Ok(node_info) => {
// Validate that we got a JSON response with expected fields
assert!(node_info.is_object(), "Node info should be a JSON object");
// Check for common Mycelium node info fields
let obj = node_info.as_object().unwrap();
// These fields are typically present in Mycelium node info
// We check if at least one of them exists to validate the response
let has_expected_fields = obj.contains_key("nodeSubnet")
|| obj.contains_key("nodePubkey")
|| obj.contains_key("peers")
|| obj.contains_key("routes");
assert!(
has_expected_fields,
"Node info should contain expected Mycelium fields"
);
println!("✓ Node info retrieved successfully: {:?}", node_info);
}
Err(e) => {
// If we can connect but get an error, it might be a version mismatch
// or API change - log it but don't fail the test
println!("⚠ Node info request failed (API might have changed): {}", e);
}
}
} else {
println!("⚠ Skipping test_get_node_info_success: No Mycelium node available");
}
}
#[tokio::test]
async fn test_get_node_info_invalid_url() {
let invalid_url = "http://localhost:99999";
let result = get_node_info(invalid_url).await;
assert!(result.is_err(), "Should fail with invalid URL");
let error = result.unwrap_err();
assert!(
error.contains("Failed to send request") || error.contains("Request failed"),
"Error should indicate connection failure: {}",
error
);
println!("✓ Correctly handled invalid URL: {}", error);
}
#[tokio::test]
async fn test_list_peers() {
if let Some(api_url) = get_available_api_url().await {
let result = list_peers(&api_url).await;
match result {
Ok(peers) => {
// Peers should be an array (even if empty)
assert!(peers.is_array(), "Peers should be a JSON array");
println!(
"✓ Peers listed successfully: {} peers found",
peers.as_array().unwrap().len()
);
}
Err(e) => {
println!(
"⚠ List peers request failed (API might have changed): {}",
e
);
}
}
} else {
println!("⚠ Skipping test_list_peers: No Mycelium node available");
}
}
#[tokio::test]
async fn test_add_peer_validation() {
if let Some(api_url) = get_available_api_url().await {
// Test with an invalid peer address format
let invalid_peer = "invalid-peer-address";
let result = add_peer(&api_url, invalid_peer).await;
// This should either succeed (if the node accepts it) or fail with a validation error
match result {
Ok(response) => {
println!("✓ Add peer response: {:?}", response);
}
Err(e) => {
// Expected for invalid peer addresses
println!("✓ Correctly rejected invalid peer address: {}", e);
}
}
} else {
println!("⚠ Skipping test_add_peer_validation: No Mycelium node available");
}
}
#[tokio::test]
async fn test_list_selected_routes() {
if let Some(api_url) = get_available_api_url().await {
let result = list_selected_routes(&api_url).await;
match result {
Ok(routes) => {
// Routes should be an array or object
assert!(
routes.is_array() || routes.is_object(),
"Routes should be a JSON array or object"
);
println!("✓ Selected routes retrieved successfully");
}
Err(e) => {
println!("⚠ List selected routes request failed: {}", e);
}
}
} else {
println!("⚠ Skipping test_list_selected_routes: No Mycelium node available");
}
}
#[tokio::test]
async fn test_list_fallback_routes() {
if let Some(api_url) = get_available_api_url().await {
let result = list_fallback_routes(&api_url).await;
match result {
Ok(routes) => {
// Routes should be an array or object
assert!(
routes.is_array() || routes.is_object(),
"Routes should be a JSON array or object"
);
println!("✓ Fallback routes retrieved successfully");
}
Err(e) => {
println!("⚠ List fallback routes request failed: {}", e);
}
}
} else {
println!("⚠ Skipping test_list_fallback_routes: No Mycelium node available");
}
}
#[tokio::test]
async fn test_send_message_validation() {
if let Some(api_url) = get_available_api_url().await {
// Test message sending with invalid destination
let invalid_destination = "invalid-destination";
let topic = "test_topic";
let message = "test message";
let deadline = Some(Duration::from_secs(1));
let result = send_message(&api_url, invalid_destination, topic, message, deadline).await;
// This should fail with invalid destination
match result {
Ok(response) => {
// Some implementations might accept any destination format
println!("✓ Send message response: {:?}", response);
}
Err(e) => {
// Expected for invalid destinations
println!("✓ Correctly rejected invalid destination: {}", e);
}
}
} else {
println!("⚠ Skipping test_send_message_validation: No Mycelium node available");
}
}
#[tokio::test]
async fn test_receive_messages_timeout() {
if let Some(api_url) = get_available_api_url().await {
let topic = "non_existent_topic";
let deadline = Some(Duration::from_secs(1)); // Short timeout
let result = receive_messages(&api_url, topic, deadline).await;
match result {
Ok(messages) => {
// Should return empty or no messages for non-existent topic
println!("✓ Receive messages completed: {:?}", messages);
}
Err(e) => {
// Timeout or no messages is acceptable
println!("✓ Receive messages handled correctly: {}", e);
}
}
} else {
println!("⚠ Skipping test_receive_messages_timeout: No Mycelium node available");
}
}
#[tokio::test]
async fn test_error_handling_malformed_url() {
let malformed_url = "not-a-url";
let result = get_node_info(malformed_url).await;
assert!(result.is_err(), "Should fail with malformed URL");
let error = result.unwrap_err();
assert!(
error.contains("Failed to send request"),
"Error should indicate request failure: {}",
error
);
println!("✓ Correctly handled malformed URL: {}", error);
}
#[tokio::test]
async fn test_base64_encoding_in_messages() {
// Test that our message functions properly handle base64 encoding
// This is a unit test that doesn't require a running Mycelium node
let topic = "test/topic";
let message = "Hello, Mycelium!";
// Test base64 encoding directly
use base64::{engine::general_purpose, Engine as _};
let encoded_topic = general_purpose::STANDARD.encode(topic);
let encoded_message = general_purpose::STANDARD.encode(message);
assert!(
!encoded_topic.is_empty(),
"Encoded topic should not be empty"
);
assert!(
!encoded_message.is_empty(),
"Encoded message should not be empty"
);
// Verify we can decode back
let decoded_topic = general_purpose::STANDARD.decode(&encoded_topic).unwrap();
let decoded_message = general_purpose::STANDARD.decode(&encoded_message).unwrap();
assert_eq!(String::from_utf8(decoded_topic).unwrap(), topic);
assert_eq!(String::from_utf8(decoded_message).unwrap(), message);
println!("✓ Base64 encoding/decoding works correctly");
}

View File

@@ -0,0 +1,242 @@
// Basic Mycelium functionality tests in Rhai
//
// This script tests the core Mycelium operations available through Rhai.
// It's designed to work with or without a running Mycelium node.
print("=== Mycelium Basic Functionality Tests ===");
// Test configuration
let test_api_url = "http://localhost:8989";
let fallback_api_url = "http://localhost:7777";
// Helper function to check if Mycelium is available
fn is_mycelium_available(api_url) {
try {
mycelium_get_node_info(api_url);
return true;
} catch(err) {
return false;
}
}
// Find an available API URL
let api_url = "";
if is_mycelium_available(test_api_url) {
api_url = test_api_url;
print(`✓ Using primary API URL: ${api_url}`);
} else if is_mycelium_available(fallback_api_url) {
api_url = fallback_api_url;
print(`✓ Using fallback API URL: ${api_url}`);
} else {
print("⚠ No Mycelium node available - testing error handling only");
api_url = "http://localhost:99999"; // Intentionally invalid for error testing
}
// Test 1: Get Node Information
print("\n--- Test 1: Get Node Information ---");
try {
let node_info = mycelium_get_node_info(api_url);
if api_url.contains("99999") {
print("✗ Expected error but got success");
assert_true(false, "Should have failed with invalid URL");
} else {
print("✓ Node info retrieved successfully");
print(` Node info type: ${type_of(node_info)}`);
// Validate response structure
if type_of(node_info) == "map" {
print("✓ Node info is a proper object");
// Check for common fields (at least one should exist)
let has_fields = node_info.contains("nodeSubnet") ||
node_info.contains("nodePubkey") ||
node_info.contains("peers") ||
node_info.contains("routes");
if has_fields {
print("✓ Node info contains expected fields");
} else {
print("⚠ Node info structure might have changed");
}
}
}
} catch(err) {
if api_url.contains("99999") {
print("✓ Correctly handled connection error");
assert_true(err.to_string().contains("Mycelium error"), "Error should be properly formatted");
} else {
print(`⚠ Unexpected error with available node: ${err}`);
}
}
// Test 2: List Peers
print("\n--- Test 2: List Peers ---");
try {
let peers = mycelium_list_peers(api_url);
if api_url.contains("99999") {
print("✗ Expected error but got success");
assert_true(false, "Should have failed with invalid URL");
} else {
print("✓ Peers listed successfully");
print(` Peers type: ${type_of(peers)}`);
if type_of(peers) == "array" {
print(`✓ Found ${peers.len()} peers`);
// If we have peers, check their structure
if peers.len() > 0 {
let first_peer = peers[0];
print(` First peer type: ${type_of(first_peer)}`);
if type_of(first_peer) == "map" {
print("✓ Peer has proper object structure");
}
}
} else {
print("⚠ Peers response is not an array");
}
}
} catch(err) {
if api_url.contains("99999") {
print("✓ Correctly handled connection error");
} else {
print(`⚠ Unexpected error listing peers: ${err}`);
}
}
// Test 3: Add Peer (with validation)
print("\n--- Test 3: Add Peer Validation ---");
try {
// Test with invalid peer address
let result = mycelium_add_peer(api_url, "invalid-peer-format");
if api_url.contains("99999") {
print("✗ Expected connection error but got success");
} else {
print("✓ Add peer completed (validation depends on node implementation)");
print(` Result type: ${type_of(result)}`);
}
} catch(err) {
if api_url.contains("99999") {
print("✓ Correctly handled connection error");
} else {
print(`✓ Peer validation error (expected): ${err}`);
}
}
// Test 4: List Selected Routes
print("\n--- Test 4: List Selected Routes ---");
try {
let routes = mycelium_list_selected_routes(api_url);
if api_url.contains("99999") {
print("✗ Expected error but got success");
} else {
print("✓ Selected routes retrieved successfully");
print(` Routes type: ${type_of(routes)}`);
if type_of(routes) == "array" {
print(`✓ Found ${routes.len()} selected routes`);
} else if type_of(routes) == "map" {
print("✓ Routes returned as object");
}
}
} catch(err) {
if api_url.contains("99999") {
print("✓ Correctly handled connection error");
} else {
print(`⚠ Error retrieving selected routes: ${err}`);
}
}
// Test 5: List Fallback Routes
print("\n--- Test 5: List Fallback Routes ---");
try {
let routes = mycelium_list_fallback_routes(api_url);
if api_url.contains("99999") {
print("✗ Expected error but got success");
} else {
print("✓ Fallback routes retrieved successfully");
print(` Routes type: ${type_of(routes)}`);
}
} catch(err) {
if api_url.contains("99999") {
print("✓ Correctly handled connection error");
} else {
print(`⚠ Error retrieving fallback routes: ${err}`);
}
}
// Test 6: Send Message (validation)
print("\n--- Test 6: Send Message Validation ---");
try {
let result = mycelium_send_message(api_url, "invalid-destination", "test_topic", "test message", -1);
if api_url.contains("99999") {
print("✗ Expected connection error but got success");
} else {
print("✓ Send message completed (validation depends on node implementation)");
print(` Result type: ${type_of(result)}`);
}
} catch(err) {
if api_url.contains("99999") {
print("✓ Correctly handled connection error");
} else {
print(`✓ Message validation error (expected): ${err}`);
}
}
// Test 7: Receive Messages (timeout test)
print("\n--- Test 7: Receive Messages Timeout ---");
try {
// Use short timeout to avoid long waits
let messages = mycelium_receive_messages(api_url, "non_existent_topic", 1);
if api_url.contains("99999") {
print("✗ Expected connection error but got success");
} else {
print("✓ Receive messages completed");
print(` Messages type: ${type_of(messages)}`);
if type_of(messages) == "array" {
print(`✓ Received ${messages.len()} messages`);
} else {
print("✓ Messages returned as object");
}
}
} catch(err) {
if api_url.contains("99999") {
print("✓ Correctly handled connection error");
} else {
print(`✓ Receive timeout handled correctly: ${err}`);
}
}
// Test 8: Parameter Validation
print("\n--- Test 8: Parameter Validation ---");
// Test empty API URL
try {
mycelium_get_node_info("");
print("✗ Should have failed with empty API URL");
} catch(err) {
print("✓ Correctly rejected empty API URL");
}
// Test negative timeout handling
try {
mycelium_receive_messages(api_url, "test_topic", -1);
if api_url.contains("99999") {
print("✗ Expected connection error");
} else {
print("✓ Negative timeout handled (treated as no timeout)");
}
} catch(err) {
print("✓ Timeout parameter handled correctly");
}
print("\n=== Mycelium Basic Tests Completed ===");
print("All core Mycelium functions are properly registered and handle errors correctly.");

View File

@@ -0,0 +1,174 @@
// Mycelium Rhai Test Runner
//
// This script runs all Mycelium-related Rhai tests and reports results.
// It includes simplified versions of the individual tests to avoid dependency issues.
print("=== Mycelium Rhai Test Suite ===");
print("Running comprehensive tests for Mycelium Rhai integration...\n");
let total_tests = 0;
let passed_tests = 0;
let failed_tests = 0;
let skipped_tests = 0;
// Test 1: Function Registration
print("Test 1: Function Registration");
total_tests += 1;
try {
// Test that all mycelium functions are registered
let invalid_url = "http://localhost:99999";
let all_functions_exist = true;
try { mycelium_get_node_info(invalid_url); } catch(err) {
if !err.to_string().contains("Mycelium error") { all_functions_exist = false; }
}
try { mycelium_list_peers(invalid_url); } catch(err) {
if !err.to_string().contains("Mycelium error") { all_functions_exist = false; }
}
try { mycelium_send_message(invalid_url, "dest", "topic", "msg", -1); } catch(err) {
if !err.to_string().contains("Mycelium error") { all_functions_exist = false; }
}
if all_functions_exist {
passed_tests += 1;
print("✓ PASSED: All mycelium functions are registered");
} else {
failed_tests += 1;
print("✗ FAILED: Some mycelium functions are missing");
}
} catch(err) {
failed_tests += 1;
print(`✗ ERROR: Function registration test failed - ${err}`);
}
// Test 2: Error Handling
print("\nTest 2: Error Handling");
total_tests += 1;
try {
mycelium_get_node_info("http://localhost:99999");
failed_tests += 1;
print("✗ FAILED: Should have failed with connection error");
} catch(err) {
if err.to_string().contains("Mycelium error") {
passed_tests += 1;
print("✓ PASSED: Error handling works correctly");
} else {
failed_tests += 1;
print(`✗ FAILED: Unexpected error format - ${err}`);
}
}
// Test 3: Parameter Validation
print("\nTest 3: Parameter Validation");
total_tests += 1;
try {
mycelium_get_node_info("");
failed_tests += 1;
print("✗ FAILED: Should have failed with empty API URL");
} catch(err) {
passed_tests += 1;
print("✓ PASSED: Parameter validation works correctly");
}
// Test 4: Timeout Parameter Handling
print("\nTest 4: Timeout Parameter Handling");
total_tests += 1;
try {
let invalid_url = "http://localhost:99999";
// Test negative timeout (should be treated as no timeout)
try {
mycelium_receive_messages(invalid_url, "topic", -1);
failed_tests += 1;
print("✗ FAILED: Should have failed with connection error");
} catch(err) {
if err.to_string().contains("Mycelium error") {
passed_tests += 1;
print("✓ PASSED: Timeout parameter handling works correctly");
} else {
failed_tests += 1;
print(`✗ FAILED: Unexpected error - ${err}`);
}
}
} catch(err) {
failed_tests += 1;
print(`✗ ERROR: Timeout test failed - ${err}`);
}
// Check if Mycelium is available for integration tests
let test_api_url = "http://localhost:8989";
let fallback_api_url = "http://localhost:7777";
let available_api_url = "";
try {
mycelium_get_node_info(test_api_url);
available_api_url = test_api_url;
} catch(err) {
try {
mycelium_get_node_info(fallback_api_url);
available_api_url = fallback_api_url;
} catch(err2) {
// No Mycelium node available
}
}
if available_api_url != "" {
print(`\n✓ Mycelium node available at: ${available_api_url}`);
// Test 5: Get Node Info
print("\nTest 5: Get Node Info");
total_tests += 1;
try {
let node_info = mycelium_get_node_info(available_api_url);
if type_of(node_info) == "map" {
passed_tests += 1;
print("✓ PASSED: Node info retrieved successfully");
} else {
failed_tests += 1;
print("✗ FAILED: Node info should be an object");
}
} catch(err) {
failed_tests += 1;
print(`✗ ERROR: Node info test failed - ${err}`);
}
// Test 6: List Peers
print("\nTest 6: List Peers");
total_tests += 1;
try {
let peers = mycelium_list_peers(available_api_url);
if type_of(peers) == "array" {
passed_tests += 1;
print("✓ PASSED: Peers listed successfully");
} else {
failed_tests += 1;
print("✗ FAILED: Peers should be an array");
}
} catch(err) {
failed_tests += 1;
print(`✗ ERROR: List peers test failed - ${err}`);
}
} else {
print("\n⚠ No Mycelium node available - skipping integration tests");
skipped_tests += 2; // Skip node info and list peers tests
total_tests += 2;
}
// Print final results
print("\n=== Test Results ===");
print(`Total Tests: ${total_tests}`);
print(`Passed: ${passed_tests}`);
print(`Failed: ${failed_tests}`);
print(`Skipped: ${skipped_tests}`);
if failed_tests == 0 {
print("\n✓ All tests passed!");
} else {
print(`\n✗ ${failed_tests} test(s) failed.`);
}
print("\n=== Mycelium Rhai Test Suite Completed ===");

View File

@@ -0,0 +1,313 @@
//! 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
);
}
}
}