sal/mycelium/tests/rhai/01_mycelium_basic.rhai
Mahmoud-Emad 4a8d3bfd24
Some checks are pending
Rhai Tests / Run Rhai Tests (push) Waiting to run
feat: Add mycelium package to workspace
- 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.
2025-06-19 12:11:55 +03:00

243 lines
7.7 KiB
Plaintext

// 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.");