sal-net 0.1.0

SAL Network - Network connectivity utilities for TCP, HTTP, and SSH
Documentation
use rhai::{Engine, EvalAltResult};
use sal_net::rhai::register_net_module;
use std::fs;

#[test]
fn test_rhai_script_tcp_operations() {
    let mut engine = Engine::new();
    register_net_module(&mut engine).expect("Failed to register net module");

    let script_content = fs::read_to_string("tests/rhai/01_tcp_operations.rhai")
        .expect("Failed to read TCP operations script");

    let result: Result<bool, Box<EvalAltResult>> = engine.eval(&script_content);

    match result {
        Ok(success) => {
            if !success {
                println!("Some TCP operation tests failed, but script executed successfully");
            }
            // Script should execute without errors, regardless of individual test results
        }
        Err(e) => panic!("TCP operations script failed to execute: {}", e),
    }
}

#[test]
fn test_rhai_script_http_operations() {
    let mut engine = Engine::new();
    register_net_module(&mut engine).expect("Failed to register net module");

    let script_content = fs::read_to_string("tests/rhai/02_http_operations.rhai")
        .expect("Failed to read HTTP operations script");

    let result: Result<bool, Box<EvalAltResult>> = engine.eval(&script_content);

    match result {
        Ok(success) => {
            if !success {
                println!("Some HTTP operation tests failed, but script executed successfully");
            }
            // Script should execute without errors
        }
        Err(e) => panic!("HTTP operations script failed to execute: {}", e),
    }
}

#[test]
fn test_rhai_script_ssh_operations() {
    let mut engine = Engine::new();
    register_net_module(&mut engine).expect("Failed to register net module");

    let script_content = fs::read_to_string("tests/rhai/03_ssh_operations.rhai")
        .expect("Failed to read SSH operations script");

    let result: Result<bool, Box<EvalAltResult>> = engine.eval(&script_content);

    match result {
        Ok(success) => {
            if !success {
                println!("Some SSH operation tests failed, but script executed successfully");
            }
            // Script should execute without errors
        }
        Err(e) => panic!("SSH operations script failed to execute: {}", e),
    }
}

#[test]
fn test_rhai_script_run_all_tests() {
    let mut engine = Engine::new();
    register_net_module(&mut engine).expect("Failed to register net module");

    let script_content = fs::read_to_string("tests/rhai/run_all_tests.rhai")
        .expect("Failed to read run all tests script");

    let result: Result<bool, Box<EvalAltResult>> = engine.eval(&script_content);

    match result {
        Ok(success) => {
            if !success {
                println!("Some tests in the comprehensive suite failed, but script executed successfully");
            }
            // Script should execute without errors
        }
        Err(e) => panic!("Run all tests script failed to execute: {}", e),
    }
}

#[test]
fn test_rhai_tcp_functions_directly() {
    let mut engine = Engine::new();
    register_net_module(&mut engine).expect("Failed to register net module");

    // Test tcp_check function directly
    let tcp_check_script = r#"
        let result = tcp_check("127.0.0.1", 65534);
        result == true || result == false
    "#;

    let result: Result<bool, Box<EvalAltResult>> = engine.eval(tcp_check_script);
    assert!(result.is_ok());
    assert!(result.unwrap()); // Should return a boolean value

    // Test tcp_ping function directly
    let tcp_ping_script = r#"
        let result = tcp_ping("localhost");
        result == true || result == false
    "#;

    let result: Result<bool, Box<EvalAltResult>> = engine.eval(tcp_ping_script);
    assert!(result.is_ok());
    assert!(result.unwrap()); // Should return a boolean value
}

#[test]
fn test_rhai_network_function_error_handling() {
    let mut engine = Engine::new();
    register_net_module(&mut engine).expect("Failed to register net module");

    // Test that functions handle invalid inputs gracefully
    let error_handling_script = r#"
        // Test with empty host
        let empty_host = tcp_check("", 80);
        
        // Test with invalid host
        let invalid_host = tcp_check("invalid.host.12345", 80);
        
        // Test with negative port
        let negative_port = tcp_check("localhost", -1);
        
        // All should return false without throwing errors
        !empty_host && !invalid_host && !negative_port
    "#;

    let result: Result<bool, Box<EvalAltResult>> = engine.eval(error_handling_script);
    assert!(result.is_ok());
    assert!(result.unwrap()); // All error cases should return false
}

#[test]
fn test_rhai_network_function_consistency() {
    let mut engine = Engine::new();
    register_net_module(&mut engine).expect("Failed to register net module");

    // Test that functions return consistent results
    let consistency_script = r#"
        // Same operation should return same result
        let result1 = tcp_check("127.0.0.1", 65534);
        let result2 = tcp_check("127.0.0.1", 65534);
        
        // Ping consistency
        let ping1 = tcp_ping("localhost");
        let ping2 = tcp_ping("localhost");
        
        result1 == result2 && ping1 == ping2
    "#;

    let result: Result<bool, Box<EvalAltResult>> = engine.eval(consistency_script);
    assert!(result.is_ok());
    assert!(result.unwrap()); // Results should be consistent
}

#[test]
fn test_rhai_network_comprehensive_functionality() {
    let mut engine = Engine::new();
    register_net_module(&mut engine).expect("Failed to register net module");

    // Comprehensive test of all network functions
    let comprehensive_script = r#"
        // Test TCP functions
        let tcp_result = tcp_check("127.0.0.1", 65534);
        let ping_result = tcp_ping("localhost");

        // Test HTTP functions
        let http_result = http_check("https://httpbin.org/status/200");
        let status_result = http_status("not-a-url");

        // Test SSH functions
        let ssh_result = ssh_execute("invalid", "user", "test");
        let ssh_ping_result = ssh_ping("invalid", "user");

        // All functions should work without throwing errors
        (tcp_result == true || tcp_result == false) &&
        (ping_result == true || ping_result == false) &&
        (http_result == true || http_result == false) &&
        (status_result >= -1) &&
        (ssh_result != 0 || ssh_result == 0) &&
        (ssh_ping_result == true || ssh_ping_result == false)
    "#;

    let result: Result<bool, Box<EvalAltResult>> = engine.eval(comprehensive_script);
    assert!(result.is_ok());
    assert!(result.unwrap()); // All functions should work correctly
}

#[test]
fn test_rhai_script_real_world_scenarios() {
    let mut engine = Engine::new();
    register_net_module(&mut engine).expect("Failed to register net module");

    let script_content = fs::read_to_string("tests/rhai/04_real_world_scenarios.rhai")
        .expect("Failed to read real-world scenarios script");

    let result: Result<bool, Box<EvalAltResult>> = engine.eval(&script_content);

    match result {
        Ok(success) => {
            if !success {
                println!("Some real-world scenarios failed, but script executed successfully");
            }
            // Script should execute without errors
        }
        Err(e) => panic!("Real-world scenarios script failed to execute: {}", e),
    }
}