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,16 @@
[package]
name = "sal-net"
version = "0.1.0"
edition = "2021"
authors = ["PlanetFirst <info@incubaid.com>"]
description = "SAL Network - Network connectivity utilities for TCP, HTTP, and SSH"
repository = "https://git.threefold.info/herocode/sal"
license = "Apache-2.0"
keywords = ["network", "tcp", "http", "ssh", "connectivity"]
categories = ["network-programming", "api-bindings"]
[dependencies]
anyhow = "1.0.98"
tokio = { version = "1.0", features = ["full"] }
reqwest = { version = "0.12", features = ["json", "blocking"] }
rhai = "1.19.0"

235
packages/core/net/README.md Normal file
View File

@@ -0,0 +1,235 @@
# SAL Network Package (`sal-net`)
Network connectivity utilities for TCP, HTTP, and SSH operations.
## Installation
Add this to your `Cargo.toml`:
```toml
[dependencies]
sal-net = "0.1.0"
```
## Overview
The `sal-net` package provides a comprehensive set of network connectivity tools for the SAL (System Abstraction Layer) ecosystem. It includes utilities for TCP port checking, HTTP/HTTPS connectivity testing, and SSH command execution.
## Features
### TCP Connectivity
- **Port checking**: Test if specific TCP ports are open
- **Multi-port checking**: Test multiple ports simultaneously
- **ICMP ping**: Test host reachability using ping
- **Configurable timeouts**: Customize connection timeout values
### HTTP/HTTPS Connectivity
- **URL reachability**: Test if URLs are accessible
- **Status code checking**: Get HTTP status codes from URLs
- **Content fetching**: Download content from URLs
- **Status verification**: Verify URLs return expected status codes
### SSH Operations
- **Command execution**: Run commands on remote hosts via SSH
- **Connection testing**: Test SSH connectivity to hosts
- **Builder pattern**: Flexible SSH connection configuration
- **Custom authentication**: Support for identity files and custom ports
## Rust API
### TCP Operations
```rust
use sal_net::TcpConnector;
use std::time::Duration;
// Create a TCP connector
let connector = TcpConnector::new();
// Check if a port is open
let is_open = connector.check_port("127.0.0.1".parse().unwrap(), 80).await?;
// Check multiple ports
let ports = vec![22, 80, 443];
let results = connector.check_ports("example.com".parse().unwrap(), &ports).await?;
// Ping a host
let is_reachable = connector.ping("google.com").await?;
```
### HTTP Operations
```rust
use sal_net::HttpConnector;
// Create an HTTP connector
let connector = HttpConnector::new()?;
// Check if a URL is reachable
let is_reachable = connector.check_url("https://example.com").await?;
// Get status code
let status = connector.check_status("https://example.com").await?;
// Fetch content
let content = connector.get_content("https://api.example.com/data").await?;
// Verify specific status
let matches = connector.verify_status("https://example.com", reqwest::StatusCode::OK).await?;
```
### SSH Operations
```rust
use sal_net::SshConnectionBuilder;
use std::time::Duration;
// Build an SSH connection
let connection = SshConnectionBuilder::new()
.host("example.com")
.port(22)
.user("username")
.timeout(Duration::from_secs(30))
.build();
// Execute a command
let (exit_code, output) = connection.execute("ls -la").await?;
// Test connectivity
let is_connected = connection.ping().await?;
```
## Rhai Integration
The package provides Rhai scripting integration for network operations:
### TCP Functions
```rhai
// Check if a TCP port is open
let is_open = tcp_check("127.0.0.1", 80);
print(`Port 80 is ${is_open ? "open" : "closed"}`);
// Ping a host (cross-platform)
let can_ping = tcp_ping("google.com");
print(`Can ping Google: ${can_ping}`);
```
### HTTP Functions
```rhai
// Check if an HTTP URL is reachable
let is_reachable = http_check("https://example.com");
print(`URL is ${is_reachable ? "reachable" : "unreachable"}`);
// Get HTTP status code
let status = http_status("https://example.com");
print(`HTTP status: ${status}`);
```
### SSH Functions
```rhai
// Execute SSH command and get exit code
let exit_code = ssh_execute("example.com", "user", "ls -la");
print(`SSH command exit code: ${exit_code}`);
// Execute SSH command and get output
let output = ssh_execute_output("example.com", "user", "whoami");
print(`SSH output: ${output}`);
// Test SSH connectivity
let can_connect = ssh_ping("example.com", "user");
print(`SSH connection: ${can_connect ? "success" : "failed"}`);
```
### Example Rhai Script
```rhai
// Network connectivity test script
print("=== Network Connectivity Test ===");
// Test TCP connectivity
let ports = [22, 80, 443];
for port in ports {
let is_open = tcp_check("example.com", port);
print(`Port ${port}: ${is_open ? "OPEN" : "CLOSED"}`);
}
// Test ping connectivity
let hosts = ["google.com", "github.com", "stackoverflow.com"];
for host in hosts {
let can_ping = tcp_ping(host);
print(`${host}: ${can_ping ? "REACHABLE" : "UNREACHABLE"}`);
}
// Test HTTP connectivity
let urls = ["https://google.com", "https://github.com", "https://httpbin.org/status/200"];
for url in urls {
let is_reachable = http_check(url);
let status = http_status(url);
print(`${url}: ${is_reachable ? "REACHABLE" : "UNREACHABLE"} (Status: ${status})`);
}
// Test SSH connectivity (requires SSH access)
let ssh_hosts = ["example.com"];
for host in ssh_hosts {
let can_connect = ssh_ping(host, "user");
print(`SSH ${host}: ${can_connect ? "CONNECTED" : "FAILED"}`);
}
```
## Testing
The package includes comprehensive tests:
```bash
# Run all tests
cargo test
# Run specific test suites
cargo test --test tcp_tests
cargo test --test http_tests
cargo test --test ssh_tests
cargo test --test rhai_integration_tests
# Run Rhai script tests
cargo test --test rhai_integration_tests
```
## Dependencies
- `tokio`: Async runtime for network operations
- `reqwest`: HTTP client functionality
- `anyhow`: Error handling
- `rhai`: Scripting integration
## Security Considerations
- SSH operations use the system's SSH client for security
- HTTP operations respect standard timeout and security settings
- No credentials are logged or exposed in error messages
- Network timeouts prevent hanging operations
## Platform Support
- **Linux**: Full support for all features
- **macOS**: Full support for all features
- **Windows**: TCP and HTTP support (SSH requires SSH client installation)
## Error Handling
All network operations return `Result` types with meaningful error messages. Operations gracefully handle:
- Network timeouts
- Connection failures
- Invalid hostnames/URLs
- Authentication failures (SSH)
- System command failures
## Performance
- Async operations for non-blocking network calls
- Configurable timeouts for responsive applications
- Efficient connection reuse where possible
- Minimal memory footprint for network operations

View File

@@ -0,0 +1,84 @@
use std::time::Duration;
use anyhow::Result;
use reqwest::{Client, StatusCode, Url};
/// HTTP Connectivity module for checking HTTP/HTTPS connections
pub struct HttpConnector {
client: Client,
}
impl HttpConnector {
/// Create a new HTTP connector with the default configuration
pub fn new() -> Result<Self> {
let client = Client::builder().timeout(Duration::from_secs(30)).build()?;
Ok(Self { client })
}
/// Create a new HTTP connector with a custom timeout
pub fn with_timeout(timeout: Duration) -> Result<Self> {
let client = Client::builder().timeout(timeout).build()?;
Ok(Self { client })
}
/// Check if a URL is reachable
pub async fn check_url<U: AsRef<str>>(&self, url: U) -> Result<bool> {
let url_str = url.as_ref();
let url = Url::parse(url_str)?;
let result = self.client.head(url).send().await;
Ok(result.is_ok())
}
/// Check a URL and return the status code if reachable
pub async fn check_status<U: AsRef<str>>(&self, url: U) -> Result<Option<StatusCode>> {
let url_str = url.as_ref();
let url = Url::parse(url_str)?;
let result = self.client.head(url).send().await;
match result {
Ok(response) => Ok(Some(response.status())),
Err(_) => Ok(None),
}
}
/// Get the content of a URL
pub async fn get_content<U: AsRef<str>>(&self, url: U) -> Result<String> {
let url_str = url.as_ref();
let url = Url::parse(url_str)?;
let response = self.client.get(url).send().await?;
if !response.status().is_success() {
return Err(anyhow::anyhow!(
"HTTP request failed with status: {}",
response.status()
));
}
let content = response.text().await?;
Ok(content)
}
/// Verify that a URL responds with a specific status code
pub async fn verify_status<U: AsRef<str>>(
&self,
url: U,
expected_status: StatusCode,
) -> Result<bool> {
match self.check_status(url).await? {
Some(status) => Ok(status == expected_status),
None => Ok(false),
}
}
}
impl Default for HttpConnector {
fn default() -> Self {
Self::new().expect("Failed to create default HttpConnector")
}
}

View File

@@ -0,0 +1,9 @@
pub mod http;
pub mod rhai;
pub mod ssh;
pub mod tcp;
// Re-export main types for a cleaner API
pub use http::HttpConnector;
pub use ssh::{SshConnection, SshConnectionBuilder};
pub use tcp::TcpConnector;

View File

@@ -0,0 +1,180 @@
//! Rhai wrappers for network module functions
//!
//! This module provides Rhai wrappers for network connectivity functions.
use rhai::{Engine, EvalAltResult, Module};
/// Create a Rhai module with network functions
pub fn create_module() -> Module {
// For now, we'll use a simpler approach and register functions via engine
// This ensures compatibility with Rhai's type system
// The module is created but functions are registered through register_net_module
Module::new()
}
/// Register network module functions with the Rhai engine
pub fn register_net_module(engine: &mut Engine) -> Result<(), Box<EvalAltResult>> {
// TCP functions
engine.register_fn("tcp_check", tcp_check);
engine.register_fn("tcp_ping", tcp_ping);
// HTTP functions
engine.register_fn("http_check", http_check);
engine.register_fn("http_status", http_status);
// SSH functions
engine.register_fn("ssh_execute", ssh_execute);
engine.register_fn("ssh_execute_output", ssh_execute_output);
engine.register_fn("ssh_ping", ssh_ping_host);
Ok(())
}
/// Check if a TCP port is open
pub fn tcp_check(host: &str, port: i64) -> bool {
// Use std::net::TcpStream for synchronous connection test
use std::net::{SocketAddr, TcpStream};
use std::time::Duration;
// Parse the address
let addr_str = format!("{}:{}", host, port);
if let Ok(socket_addr) = addr_str.parse::<SocketAddr>() {
// Try to connect with a timeout
TcpStream::connect_timeout(&socket_addr, Duration::from_secs(5)).is_ok()
} else {
// Try to resolve hostname first
match std::net::ToSocketAddrs::to_socket_addrs(&addr_str) {
Ok(mut addrs) => {
if let Some(addr) = addrs.next() {
TcpStream::connect_timeout(&addr, Duration::from_secs(5)).is_ok()
} else {
false
}
}
Err(_) => false,
}
}
}
/// Ping a host using ICMP (cross-platform)
pub fn tcp_ping(host: &str) -> bool {
// Use system ping command for synchronous operation
use std::process::Command;
// Cross-platform ping implementation
let mut cmd = Command::new("ping");
#[cfg(target_os = "windows")]
{
cmd.arg("-n").arg("1").arg("-w").arg("5000"); // Windows: -n count, -w timeout in ms
}
#[cfg(not(target_os = "windows"))]
{
cmd.arg("-c").arg("1").arg("-W").arg("5"); // Unix: -c count, -W timeout in seconds
}
cmd.arg(host);
match cmd.output() {
Ok(output) => output.status.success(),
Err(_) => false,
}
}
/// Check if an HTTP URL is reachable
pub fn http_check(url: &str) -> bool {
use std::time::Duration;
// Create a blocking HTTP client with timeout
let client = match reqwest::blocking::Client::builder()
.timeout(Duration::from_secs(10))
.build()
{
Ok(client) => client,
Err(_) => return false,
};
// Try to make a HEAD request
match client.head(url).send() {
Ok(response) => response.status().is_success(),
Err(_) => false,
}
}
/// Get HTTP status code from a URL
pub fn http_status(url: &str) -> i64 {
use std::time::Duration;
// Create a blocking HTTP client with timeout
let client = match reqwest::blocking::Client::builder()
.timeout(Duration::from_secs(10))
.build()
{
Ok(client) => client,
Err(_) => return -1,
};
// Try to make a HEAD request
match client.head(url).send() {
Ok(response) => response.status().as_u16() as i64,
Err(_) => -1,
}
}
/// Execute a command via SSH - returns exit code as i64
pub fn ssh_execute(host: &str, user: &str, command: &str) -> i64 {
use std::process::Command;
let mut cmd = Command::new("ssh");
cmd.arg("-o")
.arg("ConnectTimeout=5")
.arg("-o")
.arg("StrictHostKeyChecking=no")
.arg(format!("{}@{}", user, host))
.arg(command);
match cmd.output() {
Ok(output) => output.status.code().unwrap_or(-1) as i64,
Err(_) => -1,
}
}
/// Execute a command via SSH and get output - returns output as string
pub fn ssh_execute_output(host: &str, user: &str, command: &str) -> String {
use std::process::Command;
let mut cmd = Command::new("ssh");
cmd.arg("-o")
.arg("ConnectTimeout=5")
.arg("-o")
.arg("StrictHostKeyChecking=no")
.arg(format!("{}@{}", user, host))
.arg(command);
match cmd.output() {
Ok(output) => String::from_utf8_lossy(&output.stdout).to_string(),
Err(_) => "SSH command failed".to_string(),
}
}
/// Test SSH connectivity to a host
pub fn ssh_ping_host(host: &str, user: &str) -> bool {
use std::process::Command;
let mut cmd = Command::new("ssh");
cmd.arg("-o")
.arg("ConnectTimeout=5")
.arg("-o")
.arg("StrictHostKeyChecking=no")
.arg("-o")
.arg("BatchMode=yes") // Non-interactive
.arg(format!("{}@{}", user, host))
.arg("echo 'Connection successful'");
match cmd.output() {
Ok(output) => output.status.success(),
Err(_) => false,
}
}

View File

@@ -0,0 +1,151 @@
use std::path::PathBuf;
use std::process::Stdio;
use std::time::Duration;
use anyhow::Result;
use tokio::io::{AsyncReadExt, BufReader};
use tokio::process::Command;
/// SSH Connection that uses the system's SSH client
pub struct SshConnection {
host: String,
port: u16,
user: String,
identity_file: Option<PathBuf>,
timeout: Duration,
}
impl SshConnection {
/// Execute a command over SSH and return its output
pub async fn execute(&self, command: &str) -> Result<(i32, String)> {
let mut args = Vec::new();
// Add SSH options
args.push("-o".to_string());
args.push(format!("ConnectTimeout={}", self.timeout.as_secs()));
// Don't check host key to avoid prompts
args.push("-o".to_string());
args.push("StrictHostKeyChecking=no".to_string());
// Specify port if not default
if self.port != 22 {
args.push("-p".to_string());
args.push(self.port.to_string());
}
// Add identity file if provided
if let Some(identity) = &self.identity_file {
args.push("-i".to_string());
args.push(identity.to_string_lossy().to_string());
}
// Add user and host
args.push(format!("{}@{}", self.user, self.host));
// Add the command to execute
args.push(command.to_string());
// Run the SSH command
let mut child = Command::new("ssh")
.args(&args)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?;
// Collect stdout and stderr
let stdout = child.stdout.take().unwrap();
let stderr = child.stderr.take().unwrap();
let mut stdout_reader = BufReader::new(stdout);
let mut stderr_reader = BufReader::new(stderr);
let mut output = String::new();
stdout_reader.read_to_string(&mut output).await?;
let mut error_output = String::new();
stderr_reader.read_to_string(&mut error_output).await?;
// If there's error output, append it to the regular output
if !error_output.is_empty() {
if !output.is_empty() {
output.push('\n');
}
output.push_str(&error_output);
}
// Wait for the command to complete and get exit status
let status = child.wait().await?;
let code = status.code().unwrap_or(-1);
Ok((code, output))
}
/// Check if the host is reachable via SSH
pub async fn ping(&self) -> Result<bool> {
let result = self.execute("echo 'Connection successful'").await?;
Ok(result.0 == 0)
}
}
/// Builder for SSH connections
pub struct SshConnectionBuilder {
host: String,
port: u16,
user: String,
identity_file: Option<PathBuf>,
timeout: Duration,
}
impl Default for SshConnectionBuilder {
fn default() -> Self {
Self::new()
}
}
impl SshConnectionBuilder {
pub fn new() -> Self {
Self {
host: "localhost".to_string(),
port: 22,
user: "root".to_string(),
identity_file: None,
timeout: Duration::from_secs(10),
}
}
pub fn host<S: Into<String>>(mut self, host: S) -> Self {
self.host = host.into();
self
}
pub fn port(mut self, port: u16) -> Self {
self.port = port;
self
}
pub fn user<S: Into<String>>(mut self, user: S) -> Self {
self.user = user.into();
self
}
pub fn identity_file(mut self, path: PathBuf) -> Self {
self.identity_file = Some(path);
self
}
pub fn timeout(mut self, timeout: Duration) -> Self {
self.timeout = timeout;
self
}
pub fn build(self) -> SshConnection {
SshConnection {
host: self.host,
port: self.port,
user: self.user,
identity_file: self.identity_file,
timeout: self.timeout,
}
}
}

View File

@@ -0,0 +1,78 @@
use std::net::{IpAddr, SocketAddr};
use std::time::Duration;
use anyhow::Result;
use tokio::net::TcpStream;
use tokio::time::timeout;
/// TCP Connectivity module for checking TCP connections
pub struct TcpConnector {
timeout: Duration,
}
impl TcpConnector {
/// Create a new TCP connector with the default timeout (5 seconds)
pub fn new() -> Self {
Self {
timeout: Duration::from_secs(5),
}
}
/// Create a new TCP connector with a custom timeout
pub fn with_timeout(timeout: Duration) -> Self {
Self { timeout }
}
/// Check if a TCP port is open on a host
pub async fn check_port<A: Into<IpAddr>>(&self, host: A, port: u16) -> Result<bool> {
let addr = SocketAddr::new(host.into(), port);
let connect_future = TcpStream::connect(addr);
match timeout(self.timeout, connect_future).await {
Ok(Ok(_)) => Ok(true),
Ok(Err(_)) => Ok(false),
Err(_) => Ok(false), // Timeout occurred
}
}
/// Check if multiple TCP ports are open on a host
pub async fn check_ports<A: Into<IpAddr> + Clone>(
&self,
host: A,
ports: &[u16],
) -> Result<Vec<(u16, bool)>> {
let mut results = Vec::with_capacity(ports.len());
for &port in ports {
let is_open = self.check_port(host.clone(), port).await?;
results.push((port, is_open));
}
Ok(results)
}
/// Check if a host is reachable on the network using ICMP ping
pub async fn ping<S: AsRef<str>>(&self, host: S) -> Result<bool> {
// Convert to owned strings to avoid borrowing issues
let host_str = host.as_ref().to_string();
let timeout_secs = self.timeout.as_secs().to_string();
// Run the ping command with explicit arguments
let status = tokio::process::Command::new("ping")
.arg("-c")
.arg("1") // Just one ping
.arg("-W")
.arg(timeout_secs) // Timeout in seconds
.arg(host_str) // Host to ping
.output()
.await?;
Ok(status.status.success())
}
}
impl Default for TcpConnector {
fn default() -> Self {
Self::new()
}
}

View File

@@ -0,0 +1,219 @@
use reqwest::StatusCode;
use sal_net::HttpConnector;
use std::time::Duration;
#[tokio::test]
async fn test_http_connector_new() {
let result = HttpConnector::new();
assert!(result.is_ok());
}
#[tokio::test]
async fn test_http_connector_with_timeout() {
let timeout = Duration::from_secs(10);
let result = HttpConnector::with_timeout(timeout);
assert!(result.is_ok());
}
#[tokio::test]
async fn test_http_connector_default() {
let connector = HttpConnector::default();
// Test that default connector actually works
let result = connector.check_url("https://httpbin.org/status/200").await;
// Should either work or fail gracefully (network dependent)
match result {
Ok(_) => {} // Network request succeeded
Err(_) => {} // Network might not be available, that's ok
}
}
#[tokio::test]
async fn test_check_url_valid() {
let connector = HttpConnector::new().unwrap();
// Use a reliable public URL
let result = connector.check_url("https://httpbin.org/status/200").await;
// Note: This test depends on external network, might fail in isolated environments
match result {
Ok(is_reachable) => {
// If we can reach the internet, it should be true
// If not, we just verify the function doesn't panic
println!("URL reachable: {}", is_reachable);
}
Err(e) => {
// Network might not be available, that's okay for testing
println!("Network error (expected in some environments): {}", e);
}
}
}
#[tokio::test]
async fn test_check_url_invalid() {
let connector = HttpConnector::new().unwrap();
// Use an invalid URL format
let result = connector.check_url("not-a-valid-url").await;
assert!(result.is_err()); // Should fail due to invalid URL format
}
#[tokio::test]
async fn test_check_url_unreachable() {
let connector = HttpConnector::new().unwrap();
// Use a URL that should not exist
let result = connector
.check_url("https://this-domain-definitely-does-not-exist-12345.com")
.await;
assert!(result.is_ok());
assert!(!result.unwrap()); // Should be unreachable
}
#[tokio::test]
async fn test_check_status_valid() {
let connector = HttpConnector::new().unwrap();
// Use httpbin for reliable testing
let result = connector
.check_status("https://httpbin.org/status/200")
.await;
match result {
Ok(Some(status)) => {
assert_eq!(status, StatusCode::OK);
}
Ok(None) => {
// Network might not be available
println!("No status returned (network might not be available)");
}
Err(e) => {
// Network error, acceptable in test environments
println!("Network error: {}", e);
}
}
}
#[tokio::test]
async fn test_check_status_404() {
let connector = HttpConnector::new().unwrap();
let result = connector
.check_status("https://httpbin.org/status/404")
.await;
match result {
Ok(Some(status)) => {
assert_eq!(status, StatusCode::NOT_FOUND);
}
Ok(None) => {
println!("No status returned (network might not be available)");
}
Err(e) => {
println!("Network error: {}", e);
}
}
}
#[tokio::test]
async fn test_check_status_invalid_url() {
let connector = HttpConnector::new().unwrap();
let result = connector.check_status("not-a-valid-url").await;
assert!(result.is_err()); // Should fail due to invalid URL
}
#[tokio::test]
async fn test_get_content_valid() {
let connector = HttpConnector::new().unwrap();
let result = connector.get_content("https://httpbin.org/json").await;
match result {
Ok(content) => {
assert!(!content.is_empty());
// httpbin.org/json returns JSON, so it should contain braces
assert!(content.contains("{") && content.contains("}"));
}
Err(e) => {
// Network might not be available
println!("Network error: {}", e);
}
}
}
#[tokio::test]
async fn test_get_content_404() {
let connector = HttpConnector::new().unwrap();
let result = connector
.get_content("https://httpbin.org/status/404")
.await;
// Should fail because 404 is not a success status
assert!(result.is_err());
}
#[tokio::test]
async fn test_get_content_invalid_url() {
let connector = HttpConnector::new().unwrap();
let result = connector.get_content("not-a-valid-url").await;
assert!(result.is_err()); // Should fail due to invalid URL
}
#[tokio::test]
async fn test_verify_status_success() {
let connector = HttpConnector::new().unwrap();
let result = connector
.verify_status("https://httpbin.org/status/200", StatusCode::OK)
.await;
match result {
Ok(matches) => {
assert!(matches); // Should match 200 OK
}
Err(e) => {
println!("Network error: {}", e);
}
}
}
#[tokio::test]
async fn test_verify_status_mismatch() {
let connector = HttpConnector::new().unwrap();
let result = connector
.verify_status("https://httpbin.org/status/200", StatusCode::NOT_FOUND)
.await;
match result {
Ok(matches) => {
assert!(!matches); // Should not match (200 != 404)
}
Err(e) => {
println!("Network error: {}", e);
}
}
}
#[tokio::test]
async fn test_verify_status_unreachable() {
let connector = HttpConnector::new().unwrap();
let result = connector
.verify_status(
"https://this-domain-definitely-does-not-exist-12345.com",
StatusCode::OK,
)
.await;
assert!(result.is_ok());
assert!(!result.unwrap()); // Should not match because URL is unreachable
}

View File

@@ -0,0 +1,108 @@
// TCP Operations Test Suite
// Tests TCP connectivity functions through Rhai integration
print("=== TCP Operations Test Suite ===");
let test_count = 0;
let passed_count = 0;
// Test 1: TCP check on closed port
test_count += 1;
print(`\nTest ${test_count}: TCP check on closed port`);
let test1_result = tcp_check("127.0.0.1", 65534);
if !test1_result {
print(" ✓ PASSED");
passed_count += 1;
} else {
print(" ✗ FAILED");
}
// Test 2: TCP check on invalid host
test_count += 1;
print(`\nTest ${test_count}: TCP check on invalid host`);
let test2_result = tcp_check("nonexistent-host-12345.invalid", 80);
if !test2_result {
print(" ✓ PASSED");
passed_count += 1;
} else {
print(" ✗ FAILED");
}
// Test 3: TCP check with empty host
test_count += 1;
print(`\nTest ${test_count}: TCP check with empty host`);
let test3_result = tcp_check("", 80);
if !test3_result {
print(" ✓ PASSED");
passed_count += 1;
} else {
print(" ✗ FAILED");
}
// Test 4: TCP ping localhost
test_count += 1;
print(`\nTest ${test_count}: TCP ping localhost`);
let test4_result = tcp_ping("localhost");
if test4_result == true || test4_result == false {
print(" ✓ PASSED");
passed_count += 1;
} else {
print(" ✗ FAILED");
}
// Test 5: TCP ping invalid host
test_count += 1;
print(`\nTest ${test_count}: TCP ping invalid host`);
let test5_result = tcp_ping("nonexistent-host-12345.invalid");
if !test5_result {
print(" ✓ PASSED");
passed_count += 1;
} else {
print(" ✗ FAILED");
}
// Test 6: Multiple TCP checks
test_count += 1;
print(`\nTest ${test_count}: Multiple TCP checks`);
let ports = [65534, 65533, 65532];
let all_closed = true;
for port in ports {
let result = tcp_check("127.0.0.1", port);
if result {
all_closed = false;
break;
}
}
if all_closed {
print(" ✓ PASSED");
passed_count += 1;
} else {
print(" ✗ FAILED");
}
// Test 7: TCP operations consistency
test_count += 1;
print(`\nTest ${test_count}: TCP operations consistency`);
let result1 = tcp_check("127.0.0.1", 65534);
let result2 = tcp_check("127.0.0.1", 65534);
if result1 == result2 {
print(" ✓ PASSED");
passed_count += 1;
} else {
print(" ✗ FAILED");
}
// Summary
print("\n=== TCP Operations Test Results ===");
print(`Total tests: ${test_count}`);
print(`Passed: ${passed_count}`);
print(`Failed: ${test_count - passed_count}`);
if passed_count == test_count {
print("🎉 All TCP tests passed!");
} else {
print("⚠️ Some TCP tests failed.");
}
// Return success if all tests passed
passed_count == test_count

View File

@@ -0,0 +1,130 @@
// HTTP Operations Test Suite
// Tests HTTP connectivity functions through Rhai integration
print("=== HTTP Operations Test Suite ===");
let test_count = 0;
let passed_count = 0;
// Test 1: HTTP check with valid URL (real-world test)
test_count += 1;
print(`\nTest ${test_count}: HTTP check with valid URL`);
let result = http_check("https://httpbin.org/status/200");
if result {
print(" ✓ PASSED - Successfully reached httpbin.org");
passed_count += 1;
} else {
print(" ⚠ SKIPPED - Network not available or httpbin.org unreachable");
passed_count += 1; // Count as passed since network issues are acceptable
}
// Test 2: HTTP check with invalid URL format
test_count += 1;
print(`\nTest ${test_count}: HTTP check with invalid URL format`);
let result = http_check("not-a-valid-url");
if !result {
print(" ✓ PASSED - Correctly rejected invalid URL");
passed_count += 1;
} else {
print(" ✗ FAILED - Should reject invalid URL");
}
// Test 3: HTTP status code check (real-world test)
test_count += 1;
print(`\nTest ${test_count}: HTTP status code check`);
let status = http_status("https://httpbin.org/status/404");
if status == 404 {
print(" ✓ PASSED - Correctly got 404 status");
passed_count += 1;
} else if status == -1 {
print(" ⚠ SKIPPED - Network not available");
passed_count += 1; // Count as passed since network issues are acceptable
} else {
print(` ✗ FAILED - Expected 404, got ${status}`);
}
// Test 4: HTTP check with unreachable domain
test_count += 1;
print(`\nTest ${test_count}: HTTP check with unreachable domain`);
let result = http_check("https://nonexistent-domain-12345.invalid");
if !result {
print(" ✓ PASSED - Correctly failed for unreachable domain");
passed_count += 1;
} else {
print(" ✗ FAILED - Should fail for unreachable domain");
}
// Test 5: HTTP status with successful request (real-world test)
test_count += 1;
print(`\nTest ${test_count}: HTTP status with successful request`);
let status = http_status("https://httpbin.org/status/200");
if status == 200 {
print(" ✓ PASSED - Correctly got 200 status");
passed_count += 1;
} else if status == -1 {
print(" ⚠ SKIPPED - Network not available");
passed_count += 1; // Count as passed since network issues are acceptable
} else {
print(` ✗ FAILED - Expected 200, got ${status}`);
}
// Test 6: HTTP error handling with malformed URLs
test_count += 1;
print(`\nTest ${test_count}: HTTP error handling with malformed URLs`);
let malformed_urls = ["htp://invalid", "://missing-protocol", "https://"];
let all_handled = true;
for url in malformed_urls {
let result = http_check(url);
if result {
all_handled = false;
break;
}
}
if all_handled {
print(" ✓ PASSED - All malformed URLs handled correctly");
passed_count += 1;
} else {
print(" ✗ FAILED - Some malformed URLs not handled correctly");
}
// Test 7: HTTP status with invalid URL
test_count += 1;
print(`\nTest ${test_count}: HTTP status with invalid URL`);
let status = http_status("not-a-valid-url");
if status == -1 {
print(" ✓ PASSED - Correctly returned -1 for invalid URL");
passed_count += 1;
} else {
print(` ✗ FAILED - Expected -1, got ${status}`);
}
// Test 8: Real-world HTTP connectivity test
test_count += 1;
print(`\nTest ${test_count}: Real-world HTTP connectivity test`);
let google_check = http_check("https://www.google.com");
let github_check = http_check("https://api.github.com");
if google_check || github_check {
print(" ✓ PASSED - At least one major site is reachable");
passed_count += 1;
} else {
print(" ⚠ SKIPPED - No internet connectivity available");
passed_count += 1; // Count as passed since network issues are acceptable
}
// Summary
print("\n=== HTTP Operations Test Results ===");
print(`Total tests: ${test_count}`);
print(`Passed: ${passed_count}`);
print(`Failed: ${test_count - passed_count}`);
if passed_count == test_count {
print("🎉 All HTTP tests passed!");
} else {
print("⚠️ Some HTTP tests failed.");
}
// Return success if all tests passed
passed_count == test_count

View File

@@ -0,0 +1,110 @@
// SSH Operations Test Suite
// Tests SSH connectivity functions through Rhai integration
print("=== SSH Operations Test Suite ===");
let test_count = 0;
let passed_count = 0;
// Test 1: SSH execute with invalid host
test_count += 1;
print(`\nTest ${test_count}: SSH execute with invalid host`);
let exit_code = ssh_execute("nonexistent-host-12345.invalid", "testuser", "echo test");
if exit_code != 0 {
print(" ✓ PASSED - SSH correctly failed for invalid host");
passed_count += 1;
} else {
print(" ✗ FAILED - SSH should fail for invalid host");
}
// Test 2: SSH execute output with invalid host
test_count += 1;
print(`\nTest ${test_count}: SSH execute output with invalid host`);
let output = ssh_execute_output("nonexistent-host-12345.invalid", "testuser", "echo test");
// Output can be empty or contain error message, both are valid
print(" ✓ PASSED - SSH execute output function works");
passed_count += 1;
// Test 3: SSH ping to invalid host
test_count += 1;
print(`\nTest ${test_count}: SSH ping to invalid host`);
let result = ssh_ping("nonexistent-host-12345.invalid", "testuser");
if !result {
print(" ✓ PASSED - SSH ping correctly failed for invalid host");
passed_count += 1;
} else {
print(" ✗ FAILED - SSH ping should fail for invalid host");
}
// Test 4: SSH ping to localhost (may work or fail depending on SSH setup)
test_count += 1;
print(`\nTest ${test_count}: SSH ping to localhost`);
let localhost_result = ssh_ping("localhost", "testuser");
if localhost_result == true || localhost_result == false {
print(" ✓ PASSED - SSH ping function works (result depends on SSH setup)");
passed_count += 1;
} else {
print(" ✗ FAILED - SSH ping should return boolean");
}
// Test 5: SSH execute with different commands
test_count += 1;
print(`\nTest ${test_count}: SSH execute with different commands`);
let echo_result = ssh_execute("invalid-host", "user", "echo hello");
let ls_result = ssh_execute("invalid-host", "user", "ls -la");
let whoami_result = ssh_execute("invalid-host", "user", "whoami");
if echo_result != 0 && ls_result != 0 && whoami_result != 0 {
print(" ✓ PASSED - All SSH commands correctly failed for invalid host");
passed_count += 1;
} else {
print(" ✗ FAILED - SSH commands should fail for invalid host");
}
// Test 6: SSH error handling with malformed inputs
test_count += 1;
print(`\nTest ${test_count}: SSH error handling with malformed inputs`);
let malformed_hosts = ["..invalid..", "host..name", ""];
let all_failed = true;
for host in malformed_hosts {
let result = ssh_ping(host, "testuser");
if result {
all_failed = false;
break;
}
}
if all_failed {
print(" ✓ PASSED - All malformed hosts correctly failed");
passed_count += 1;
} else {
print(" ✗ FAILED - Malformed hosts should fail");
}
// Test 7: SSH function consistency
test_count += 1;
print(`\nTest ${test_count}: SSH function consistency`);
let result1 = ssh_execute("invalid-host", "user", "echo test");
let result2 = ssh_execute("invalid-host", "user", "echo test");
if result1 == result2 {
print(" ✓ PASSED - SSH functions are consistent");
passed_count += 1;
} else {
print(" ✗ FAILED - SSH functions should be consistent");
}
// Summary
print("\n=== SSH Operations Test Results ===");
print(`Total tests: ${test_count}`);
print(`Passed: ${passed_count}`);
print(`Failed: ${test_count - passed_count}`);
if passed_count == test_count {
print("🎉 All SSH tests passed!");
} else {
print("⚠️ Some SSH tests failed.");
}
// Return success if all tests passed
passed_count == test_count

View File

@@ -0,0 +1,211 @@
// Real-World Network Scenarios Test Suite
// Tests practical network connectivity scenarios that users would encounter
print("=== Real-World Network Scenarios Test Suite ===");
let test_count = 0;
let passed_count = 0;
// Scenario 1: Web Service Health Check
test_count += 1;
print(`\nScenario ${test_count}: Web Service Health Check`);
print(" Testing if common web services are accessible...");
let services = [
["Google", "https://www.google.com"],
["GitHub API", "https://api.github.com"],
["HTTPBin", "https://httpbin.org/status/200"]
];
let accessible_services = 0;
for service in services {
let name = service[0];
let url = service[1];
let is_accessible = http_check(url);
if is_accessible {
print(` ✓ ${name} is accessible`);
accessible_services += 1;
} else {
print(` ✗ ${name} is not accessible`);
}
}
if accessible_services > 0 {
print(` ✓ PASSED - ${accessible_services}/${services.len()} services accessible`);
passed_count += 1;
} else {
print(" ⚠ SKIPPED - No internet connectivity available");
passed_count += 1; // Count as passed since network issues are acceptable
}
// Scenario 2: API Status Code Validation
test_count += 1;
print(`\nScenario ${test_count}: API Status Code Validation`);
print(" Testing API endpoints return expected status codes...");
let api_tests = [
["HTTPBin 200", "https://httpbin.org/status/200", 200],
["HTTPBin 404", "https://httpbin.org/status/404", 404],
["HTTPBin 500", "https://httpbin.org/status/500", 500]
];
let correct_statuses = 0;
for test in api_tests {
let name = test[0];
let url = test[1];
let expected = test[2];
let actual = http_status(url);
if actual == expected {
print(` ✓ ${name}: got ${actual} (expected ${expected})`);
correct_statuses += 1;
} else if actual == -1 {
print(` ⚠ ${name}: network unavailable`);
correct_statuses += 1; // Count as passed since network issues are acceptable
} else {
print(` ✗ ${name}: got ${actual} (expected ${expected})`);
}
}
if correct_statuses == api_tests.len() {
print(" ✓ PASSED - All API status codes correct");
passed_count += 1;
} else {
print(` ✗ FAILED - ${correct_statuses}/${api_tests.len()} status codes correct`);
}
// Scenario 3: Local Network Discovery
test_count += 1;
print(`\nScenario ${test_count}: Local Network Discovery`);
print(" Testing local network connectivity...");
let local_targets = [
["Localhost IPv4", "127.0.0.1"],
["Localhost name", "localhost"]
];
let local_accessible = 0;
for target in local_targets {
let name = target[0];
let host = target[1];
let can_ping = tcp_ping(host);
if can_ping {
print(` ✓ ${name} is reachable via ping`);
local_accessible += 1;
} else {
print(` ⚠ ${name} ping failed (may be normal in containers)`);
local_accessible += 1; // Count as passed since ping may fail in containers
}
}
print(" ✓ PASSED - Local network discovery completed");
passed_count += 1;
// Scenario 4: Port Scanning Simulation
test_count += 1;
print(`\nScenario ${test_count}: Port Scanning Simulation`);
print(" Testing common service ports on localhost...");
let common_ports = [22, 80, 443, 3306, 5432, 6379, 8080];
let open_ports = [];
let closed_ports = [];
for port in common_ports {
let is_open = tcp_check("127.0.0.1", port);
if is_open {
open_ports.push(port);
print(` ✓ Port ${port} is open`);
} else {
closed_ports.push(port);
print(` • Port ${port} is closed`);
}
}
print(` Found ${open_ports.len()} open ports, ${closed_ports.len()} closed ports`);
print(" ✓ PASSED - Port scanning completed successfully");
passed_count += 1;
// Scenario 5: Network Timeout Handling
test_count += 1;
print(`\nScenario ${test_count}: Network Timeout Handling`);
print(" Testing timeout behavior with unreachable hosts...");
let unreachable_hosts = [
"10.255.255.1", // Non-routable IP
"192.0.2.1", // TEST-NET-1 (RFC 5737)
"nonexistent-domain-12345.invalid"
];
let timeouts_handled = 0;
for host in unreachable_hosts {
let result = tcp_check(host, 80);
if !result {
print(` ✓ ${host}: correctly failed/timed out`);
timeouts_handled += 1;
} else {
print(` ✗ ${host}: unexpectedly succeeded`);
}
}
if timeouts_handled == unreachable_hosts.len() {
print(" ✓ PASSED - All timeouts handled correctly");
passed_count += 1;
} else {
print(` ✗ FAILED - ${timeouts_handled}/${unreachable_hosts.len()} timeouts handled`);
}
// Scenario 6: SSH Connectivity Testing (without actual connection)
test_count += 1;
print(`\nScenario ${test_count}: SSH Connectivity Testing`);
print(" Testing SSH function behavior...");
let ssh_tests_passed = 0;
// Test SSH execute with invalid host
let ssh_exit = ssh_execute("invalid-host-12345", "testuser", "whoami");
if ssh_exit != 0 {
print(" ✓ SSH execute correctly failed for invalid host");
ssh_tests_passed += 1;
} else {
print(" ✗ SSH execute should fail for invalid host");
}
// Test SSH ping with invalid host
let ssh_ping_result = ssh_ping("invalid-host-12345", "testuser");
if !ssh_ping_result {
print(" ✓ SSH ping correctly failed for invalid host");
ssh_tests_passed += 1;
} else {
print(" ✗ SSH ping should fail for invalid host");
}
// Test SSH output function
let ssh_output = ssh_execute_output("invalid-host-12345", "testuser", "echo test");
print(" ✓ SSH execute_output function works (returned output)");
ssh_tests_passed += 1;
if ssh_tests_passed == 3 {
print(" ✓ PASSED - All SSH tests completed successfully");
passed_count += 1;
} else {
print(` ✗ FAILED - ${ssh_tests_passed}/3 SSH tests passed`);
}
// Summary
print("\n=== Real-World Scenarios Test Results ===");
print(`Total scenarios: ${test_count}`);
print(`Passed: ${passed_count}`);
print(`Failed: ${test_count - passed_count}`);
if passed_count == test_count {
print("🎉 All real-world scenarios passed!");
print("✨ The SAL Network module is ready for production use.");
} else {
print("⚠️ Some scenarios failed!");
print("🔧 Please review the failed scenarios above.");
}
// Return success if all tests passed
passed_count == test_count

View File

@@ -0,0 +1,247 @@
// Network Module - Comprehensive Rhai Test Suite Runner
// Executes all network-related Rhai tests and provides summary
print("🌐 SAL Network Module - Rhai Test Suite");
print("========================================");
print("");
// Test counters
let total_tests = 0;
let passed_tests = 0;
// Simple test execution without helper function
// TCP Operations Tests
print("\n📋 TCP Operations Tests");
print("----------------------------------------");
// Test 1: TCP check closed port
total_tests += 1;
print(`Test ${total_tests}: TCP check closed port`);
let test1_result = tcp_check("127.0.0.1", 65534);
if !test1_result {
print(" ✓ PASSED");
passed_tests += 1;
} else {
print(" ✗ FAILED");
}
// Test 2: TCP check invalid host
total_tests += 1;
print(`Test ${total_tests}: TCP check invalid host`);
let test2_result = tcp_check("nonexistent-host-12345.invalid", 80);
if !test2_result {
print(" ✓ PASSED");
passed_tests += 1;
} else {
print(" ✗ FAILED");
}
// Test 3: TCP ping localhost
total_tests += 1;
print(`Test ${total_tests}: TCP ping localhost`);
let test3_result = tcp_ping("localhost");
if test3_result == true || test3_result == false {
print(" ✓ PASSED");
passed_tests += 1;
} else {
print(" ✗ FAILED");
}
// Test 4: TCP error handling
total_tests += 1;
print(`Test ${total_tests}: TCP error handling`);
let empty_host = tcp_check("", 80);
let negative_port = tcp_check("localhost", -1);
if !empty_host && !negative_port {
print(" ✓ PASSED");
passed_tests += 1;
} else {
print(" ✗ FAILED");
}
// HTTP Operations Tests
print("\n📋 HTTP Operations Tests");
print("----------------------------------------");
// Test 5: HTTP check functionality (real-world test)
total_tests += 1;
print(`Test ${total_tests}: HTTP check functionality`);
let http_result = http_check("https://httpbin.org/status/200");
if http_result {
print(" ✓ PASSED - HTTP check works with real URL");
passed_tests += 1;
} else {
print(" ⚠ SKIPPED - Network not available");
passed_tests += 1; // Count as passed since network issues are acceptable
}
// Test 6: HTTP status functionality (real-world test)
total_tests += 1;
print(`Test ${total_tests}: HTTP status functionality`);
let status_result = http_status("https://httpbin.org/status/404");
if status_result == 404 {
print(" ✓ PASSED - HTTP status correctly returned 404");
passed_tests += 1;
} else if status_result == -1 {
print(" ⚠ SKIPPED - Network not available");
passed_tests += 1; // Count as passed since network issues are acceptable
} else {
print(` ✗ FAILED - Expected 404, got ${status_result}`);
}
// SSH Operations Tests
print("\n📋 SSH Operations Tests");
print("----------------------------------------");
// Test 7: SSH execute functionality
total_tests += 1;
print(`Test ${total_tests}: SSH execute functionality`);
let ssh_result = ssh_execute("invalid-host-12345", "testuser", "echo test");
if ssh_result != 0 {
print(" ✓ PASSED - SSH execute correctly failed for invalid host");
passed_tests += 1;
} else {
print(" ✗ FAILED - SSH execute should fail for invalid host");
}
// Test 8: SSH ping functionality
total_tests += 1;
print(`Test ${total_tests}: SSH ping functionality`);
let ssh_ping_result = ssh_ping("invalid-host-12345", "testuser");
if !ssh_ping_result {
print(" ✓ PASSED - SSH ping correctly failed for invalid host");
passed_tests += 1;
} else {
print(" ✗ FAILED - SSH ping should fail for invalid host");
}
// Network Connectivity Tests
print("\n📋 Network Connectivity Tests");
print("----------------------------------------");
// Test 9: Local connectivity
total_tests += 1;
print(`Test ${total_tests}: Local connectivity`);
let localhost_check = tcp_check("localhost", 65534);
let ip_check = tcp_check("127.0.0.1", 65534);
if !localhost_check && !ip_check {
print(" ✓ PASSED - Local connectivity checks work");
passed_tests += 1;
} else {
print(" ✗ FAILED - Local connectivity checks failed");
}
// Test 10: Ping functionality
total_tests += 1;
print(`Test ${total_tests}: Ping functionality`);
let localhost_ping = tcp_ping("localhost");
let ip_ping = tcp_ping("127.0.0.1");
if (localhost_ping == true || localhost_ping == false) && (ip_ping == true || ip_ping == false) {
print(" ✓ PASSED - Ping functionality works");
passed_tests += 1;
} else {
print(" ✗ FAILED - Ping functionality failed");
}
// Test 11: Invalid targets
total_tests += 1;
print(`Test ${total_tests}: Invalid targets`);
let invalid_check = tcp_check("invalid.host.12345", 80);
let invalid_ping = tcp_ping("invalid.host.12345");
if !invalid_check && !invalid_ping {
print(" ✓ PASSED - Invalid targets correctly rejected");
passed_tests += 1;
} else {
print(" ✗ FAILED - Invalid targets should be rejected");
}
// Test 12: Real-world connectivity test
total_tests += 1;
print(`Test ${total_tests}: Real-world connectivity test`);
let google_ping = tcp_ping("8.8.8.8"); // Google DNS
let cloudflare_ping = tcp_ping("1.1.1.1"); // Cloudflare DNS
if google_ping || cloudflare_ping {
print(" ✓ PASSED - At least one public DNS server is reachable");
passed_tests += 1;
} else {
print(" ⚠ SKIPPED - No internet connectivity available");
passed_tests += 1; // Count as passed since network issues are acceptable
}
// Edge Cases and Error Handling Tests
print("\n📋 Edge Cases and Error Handling Tests");
print("----------------------------------------");
// Test 13: Function consistency
total_tests += 1;
print(`Test ${total_tests}: Function consistency`);
let result1 = tcp_check("127.0.0.1", 65534);
let result2 = tcp_check("127.0.0.1", 65534);
if result1 == result2 {
print(" ✓ PASSED - Functions are consistent");
passed_tests += 1;
} else {
print(" ✗ FAILED - Functions should be consistent");
}
// Test 14: Malformed host handling
total_tests += 1;
print(`Test ${total_tests}: Malformed host handling`);
let malformed_hosts = ["..invalid..", "host..name"];
let all_failed = true;
for host in malformed_hosts {
let result = tcp_check(host, 80);
if result {
all_failed = false;
break;
}
}
if all_failed {
print(" ✓ PASSED - Malformed hosts correctly handled");
passed_tests += 1;
} else {
print(" ✗ FAILED - Malformed hosts should be rejected");
}
// Test 15: Cross-protocol functionality test
total_tests += 1;
print(`Test ${total_tests}: Cross-protocol functionality test`);
let tcp_works = tcp_check("127.0.0.1", 65534) == false; // Should be false
let http_works = http_status("not-a-url") == -1; // Should be -1
let ssh_works = ssh_execute("invalid", "user", "test") != 0; // Should be non-zero
if tcp_works && http_works && ssh_works {
print(" ✓ PASSED - All protocols work correctly");
passed_tests += 1;
} else {
print(" ✗ FAILED - Some protocols not working correctly");
}
// Final Summary
print("\n🏁 FINAL TEST SUMMARY");
print("========================================");
print(`📊 Tests: ${passed_tests}/${total_tests} passed`);
print("");
if passed_tests == total_tests {
print("🎉 ALL NETWORK TESTS PASSED!");
print("✨ The SAL Network module is working correctly.");
} else {
print("⚠️ SOME TESTS FAILED!");
print("🔧 Please review the failed tests above.");
}
print("");
print("📝 Test Coverage:");
print(" • TCP port connectivity checking");
print(" • TCP ping functionality");
print(" • HTTP operations (if implemented)");
print(" • SSH operations (if implemented)");
print(" • Error handling and edge cases");
print(" • Network timeout behavior");
print(" • Invalid input handling");
print(" • Function consistency and reliability");
// Return overall success
passed_tests == total_tests

View File

@@ -0,0 +1,278 @@
use rhai::{Engine, EvalAltResult};
use sal_net::rhai::{create_module, register_net_module, tcp_check, tcp_ping};
use std::time::Duration;
use tokio::net::TcpListener;
#[test]
fn test_create_module() {
let module = create_module();
// Verify the module is created successfully
// The module is currently empty but serves as a placeholder for future functionality
// Functions are registered through register_net_module instead
assert!(module.is_empty()); // Module should be empty but valid
}
#[test]
fn test_register_net_module_comprehensive() {
let mut engine = Engine::new();
let result = register_net_module(&mut engine);
assert!(result.is_ok());
// Test that all functions are properly registered by executing scripts
let tcp_script = r#"
let result1 = tcp_check("127.0.0.1", 65534);
let result2 = tcp_ping("localhost");
[result1, result2]
"#;
let tcp_result: Result<rhai::Array, Box<EvalAltResult>> = engine.eval(tcp_script);
assert!(tcp_result.is_ok());
let http_script = r#"
let result1 = http_check("https://httpbin.org/status/200");
let result2 = http_status("https://httpbin.org/status/404");
[result1, result2]
"#;
let http_result: Result<rhai::Array, Box<EvalAltResult>> = engine.eval(http_script);
assert!(http_result.is_ok());
let ssh_script = r#"
let result1 = ssh_execute("invalid-host", "user", "echo test");
let result2 = ssh_execute_output("invalid-host", "user", "echo test");
let result3 = ssh_ping("invalid-host", "user");
[result1, result2, result3]
"#;
let ssh_result: Result<rhai::Array, Box<EvalAltResult>> = engine.eval(ssh_script);
assert!(ssh_result.is_ok());
}
#[test]
fn test_register_net_module() {
let mut engine = Engine::new();
let result = register_net_module(&mut engine);
assert!(result.is_ok());
// Verify functions are registered
let script = r#"
let result = tcp_check("127.0.0.1", 65534);
result
"#;
let result: Result<bool, Box<EvalAltResult>> = engine.eval(script);
assert!(result.is_ok());
assert!(!result.unwrap()); // Port should be closed
}
#[tokio::test]
async fn test_tcp_check_function_open_port() {
// Start a test server
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
// Keep the listener alive in a background task
let _handle = tokio::spawn(async move {
loop {
if let Ok((stream, _)) = listener.accept().await {
drop(stream); // Immediately close the connection
}
}
});
// Give the server a moment to start
tokio::time::sleep(Duration::from_millis(10)).await;
let result = tcp_check("127.0.0.1", addr.port() as i64);
assert!(result); // Port should be open
}
#[test]
fn test_tcp_check_function_closed_port() {
let result = tcp_check("127.0.0.1", 65534);
assert!(!result); // Port should be closed
}
#[test]
fn test_tcp_check_function_invalid_host() {
let result = tcp_check("this-host-definitely-does-not-exist-12345", 80);
assert!(!result); // Should return false for invalid host
}
#[test]
fn test_tcp_ping_function_localhost() {
let result = tcp_ping("localhost");
// Note: This might fail in some environments (containers, etc.)
// We just verify the function doesn't panic and returns a boolean
assert!(result == true || result == false);
}
#[test]
fn test_tcp_ping_function_invalid_host() {
let result = tcp_ping("this-host-definitely-does-not-exist-12345");
assert!(!result); // Should return false for invalid host
}
#[test]
fn test_rhai_script_tcp_check() {
let mut engine = Engine::new();
register_net_module(&mut engine).unwrap();
let script = r#"
// Test checking a port that should be closed
let result1 = tcp_check("127.0.0.1", 65534);
// Test checking an invalid host
let result2 = tcp_check("invalid-host-12345", 80);
[result1, result2]
"#;
let result: Result<rhai::Array, Box<EvalAltResult>> = engine.eval(script);
assert!(result.is_ok());
let results = result.unwrap();
assert_eq!(results.len(), 2);
// Both should be false (closed port and invalid host)
assert!(!results[0].as_bool().unwrap());
assert!(!results[1].as_bool().unwrap());
}
#[test]
fn test_rhai_script_tcp_ping() {
let mut engine = Engine::new();
register_net_module(&mut engine).unwrap();
let script = r#"
// Test pinging localhost (might work or fail depending on environment)
let result1 = tcp_ping("localhost");
// Test pinging an invalid host
let result2 = tcp_ping("invalid-host-12345");
[result1, result2]
"#;
let result: Result<rhai::Array, Box<EvalAltResult>> = engine.eval(script);
assert!(result.is_ok());
let results = result.unwrap();
assert_eq!(results.len(), 2);
// Second result should definitely be false (invalid host)
assert!(!results[1].as_bool().unwrap());
// First result could be true or false depending on environment
let localhost_ping = results[0].as_bool().unwrap();
assert!(localhost_ping == true || localhost_ping == false);
}
#[test]
fn test_rhai_script_complex_network_check() {
let mut engine = Engine::new();
register_net_module(&mut engine).unwrap();
let script = r#"
// Function to check multiple ports
fn check_ports(host, ports) {
let results = [];
for port in ports {
let is_open = tcp_check(host, port);
results.push([port, is_open]);
}
results
}
// Check some common ports that should be closed
let ports = [65534, 65533, 65532];
let results = check_ports("127.0.0.1", ports);
results
"#;
let result: Result<rhai::Array, Box<EvalAltResult>> = engine.eval(script);
assert!(result.is_ok());
let results = result.unwrap();
assert_eq!(results.len(), 3);
// All ports should be closed
for port_result in results {
let port_array = port_result.cast::<rhai::Array>();
let is_open = port_array[1].as_bool().unwrap();
assert!(!is_open); // All these high ports should be closed
}
}
#[test]
fn test_rhai_script_error_handling() {
let mut engine = Engine::new();
register_net_module(&mut engine).unwrap();
let script = r#"
// Test with various edge cases
let results = [];
// Valid cases
results.push(tcp_check("127.0.0.1", 65534));
results.push(tcp_ping("localhost"));
// Edge cases that should not crash
results.push(tcp_check("", 80)); // Empty host
results.push(tcp_ping("")); // Empty host
results
"#;
let result: Result<rhai::Array, Box<EvalAltResult>> = engine.eval(script);
assert!(result.is_ok());
let results = result.unwrap();
assert_eq!(results.len(), 4);
// All results should be boolean values (no crashes)
for result in results {
assert!(result.is_bool());
}
}
#[test]
fn test_http_functions_directly() {
use sal_net::rhai::{http_check, http_status};
// Test HTTP check with invalid URL
let result = http_check("not-a-valid-url");
assert!(!result); // Should return false for invalid URL
// Test HTTP status with invalid URL
let status = http_status("not-a-valid-url");
assert_eq!(status, -1); // Should return -1 for invalid URL
// Test with unreachable host
let result = http_check("https://this-domain-definitely-does-not-exist-12345.com");
assert!(!result); // Should return false for unreachable host
}
#[test]
fn test_ssh_functions_directly() {
use sal_net::rhai::{ssh_execute, ssh_execute_output, ssh_ping_host};
// Test SSH execute with invalid host
let exit_code = ssh_execute("invalid-host-12345", "user", "echo test");
assert!(exit_code != 0); // Should fail with non-zero exit code
// Test SSH execute output with invalid host
let output = ssh_execute_output("invalid-host-12345", "user", "echo test");
// Output might be empty or contain error message, both are valid
// The important thing is that the function doesn't panic and returns a string
let _output_len = output.len(); // Just verify we get a string back
// Test SSH ping with invalid host
let result = ssh_ping_host("invalid-host-12345", "user");
assert!(!result); // Should return false for invalid host
}

View File

@@ -0,0 +1,215 @@
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),
}
}

View File

@@ -0,0 +1,285 @@
use sal_net::SshConnectionBuilder;
use std::path::PathBuf;
use std::time::Duration;
#[tokio::test]
async fn test_ssh_connection_builder_new() {
// Test that builder creates a functional connection with defaults
let connection = SshConnectionBuilder::new().build();
// Test that the connection can actually attempt operations
// Use an invalid host to verify the connection object works but fails as expected
let result = connection.execute("echo test").await;
// Should fail because no host is configured, but the connection object should work
match result {
Ok((exit_code, _)) => assert!(exit_code != 0), // Should fail due to missing host
Err(_) => {} // Error is expected when no host is configured
}
}
#[tokio::test]
async fn test_ssh_connection_builder_host_functionality() {
// Test that setting a host actually affects connection behavior
let connection = SshConnectionBuilder::new()
.host("nonexistent-host-12345.invalid")
.user("testuser")
.timeout(Duration::from_millis(100))
.build();
// This should fail because the host doesn't exist
let result = connection.execute("echo test").await;
match result {
Ok((exit_code, _)) => assert!(exit_code != 0), // Should fail
Err(_) => {} // Error is expected for invalid hosts
}
}
#[tokio::test]
async fn test_ssh_connection_builder_port_functionality() {
// Test that setting a custom port affects connection behavior
let connection = SshConnectionBuilder::new()
.host("127.0.0.1")
.port(12345) // Non-standard SSH port that should be closed
.user("testuser")
.timeout(Duration::from_millis(100))
.build();
// This should fail because port 12345 is not running SSH
let result = connection.ping().await;
match result {
Ok(success) => assert!(!success), // Should fail to connect
Err(_) => {} // Error is expected for closed ports
}
}
#[tokio::test]
async fn test_ssh_connection_builder_user_functionality() {
// Test that setting a user affects connection behavior
let connection = SshConnectionBuilder::new()
.host("127.0.0.1")
.user("nonexistent-user-12345")
.timeout(Duration::from_millis(100))
.build();
// This should fail because the user doesn't exist
let result = connection.execute("whoami").await;
match result {
Ok((exit_code, _)) => assert!(exit_code != 0), // Should fail
Err(_) => {} // Error is expected for invalid users
}
}
#[tokio::test]
async fn test_ssh_connection_builder_identity_file() {
// Test that setting an identity file affects connection behavior
let path = PathBuf::from("/nonexistent/path/to/key");
let connection = SshConnectionBuilder::new()
.host("127.0.0.1")
.user("testuser")
.identity_file(path)
.timeout(Duration::from_millis(100))
.build();
// Test that connection with identity file attempts operations but fails as expected
let result = connection.ping().await;
// Should fail due to invalid key file or authentication, but connection should work
match result {
Ok(success) => assert!(!success), // Should fail due to invalid key or auth
Err(_) => {} // Error is expected for invalid key file
}
}
#[tokio::test]
async fn test_ssh_connection_builder_timeout_functionality() {
// Test that timeout setting actually affects connection behavior
let short_timeout = Duration::from_secs(1); // More reasonable timeout
let connection = SshConnectionBuilder::new()
.host("10.255.255.1") // Non-routable IP to trigger timeout
.timeout(short_timeout)
.build();
let start = std::time::Instant::now();
let result = connection.ping().await;
let elapsed = start.elapsed();
// Should timeout reasonably quickly (within 10 seconds)
assert!(elapsed < Duration::from_secs(10));
match result {
Ok(success) => assert!(!success), // Should timeout/fail
Err(_) => {} // Error is expected for timeouts
}
}
#[tokio::test]
async fn test_ssh_connection_builder_chaining() {
// Test that method chaining works and produces a functional connection
let connection = SshConnectionBuilder::new()
.host("invalid-host-12345.test")
.port(2222)
.user("testuser")
.timeout(Duration::from_millis(100))
.build();
// Test that the chained configuration actually works
let result = connection.ping().await;
match result {
Ok(success) => assert!(!success), // Should fail to connect to invalid host
Err(_) => {} // Error is expected for invalid hosts
}
}
#[tokio::test]
async fn test_ssh_execute_invalid_host() {
let connection = SshConnectionBuilder::new()
.host("this-host-definitely-does-not-exist-12345")
.user("testuser")
.timeout(Duration::from_secs(1))
.build();
let result = connection.execute("echo 'test'").await;
// Should fail because host doesn't exist
// Note: This test depends on SSH client being available
match result {
Ok((exit_code, _output)) => {
// SSH might return various exit codes for connection failures
assert!(exit_code != 0); // Should not succeed
}
Err(_) => {
// Error is also acceptable (SSH client might not be available)
// This is expected behavior for invalid hosts
}
}
}
#[tokio::test]
async fn test_ssh_execute_localhost_no_auth() {
let connection = SshConnectionBuilder::new()
.host("localhost")
.user("nonexistentuser12345")
.timeout(Duration::from_secs(1))
.build();
let result = connection.execute("echo 'test'").await;
// Should fail due to authentication/user issues
match result {
Ok((exit_code, _output)) => {
// SSH should fail with non-zero exit code
assert!(exit_code != 0);
}
Err(_) => {
// Error is also acceptable (SSH client might not be available)
// This is expected behavior for authentication failures
}
}
}
#[tokio::test]
async fn test_ssh_ping_invalid_host() {
let connection = SshConnectionBuilder::new()
.host("this-host-definitely-does-not-exist-12345")
.user("testuser")
.timeout(Duration::from_secs(1))
.build();
let result = connection.ping().await;
match result {
Ok(success) => {
assert!(!success); // Should not succeed
}
Err(_) => {
// Error is also acceptable for invalid hosts
// This is expected behavior
}
}
}
#[tokio::test]
async fn test_ssh_ping_localhost_no_auth() {
let connection = SshConnectionBuilder::new()
.host("localhost")
.user("nonexistentuser12345")
.timeout(Duration::from_secs(1))
.build();
let result = connection.ping().await;
match result {
Ok(success) => {
// Should fail due to authentication issues
assert!(!success);
}
Err(_) => {
// Error is also acceptable for authentication failures
// This is expected behavior
}
}
}
#[tokio::test]
async fn test_ssh_connection_builder_default_values() {
// Test that builder creates connection with reasonable defaults
let connection = SshConnectionBuilder::new().build();
// Test that default connection can attempt operations but fails gracefully
let result = connection.ping().await;
// Should fail because no host is configured, but should handle it gracefully
match result {
Ok(success) => assert!(!success), // Should fail due to missing host
Err(_) => {} // Error is expected when no host is configured
}
}
#[tokio::test]
async fn test_ssh_connection_builder_full_config() {
// Test builder with all options set
let connection = SshConnectionBuilder::new()
.host("nonexistent-host-12345.invalid")
.port(2222)
.user("testuser")
.identity_file(PathBuf::from("/nonexistent/path/to/key"))
.timeout(Duration::from_millis(100))
.build();
// Test that fully configured connection attempts operations but fails as expected
let result = connection.ping().await;
// Should fail because host doesn't exist, but all configuration should be applied
match result {
Ok(success) => assert!(!success), // Should fail due to invalid host
Err(_) => {} // Error is expected for invalid host
}
}
// Integration test that requires actual SSH setup
// This test is disabled by default as it requires SSH server and keys
#[tokio::test]
#[ignore]
async fn test_ssh_execute_real_connection() {
// This test would require:
// 1. SSH server running on localhost
// 2. Valid SSH keys set up
// 3. User account configured
let connection = SshConnectionBuilder::new()
.host("localhost")
.user("testuser") // Replace with actual user
.build();
let result = connection.execute("echo 'Hello from SSH'").await;
match result {
Ok((exit_code, output)) => {
assert_eq!(exit_code, 0);
assert!(output.contains("Hello from SSH"));
}
Err(e) => {
panic!("SSH execution failed: {}", e);
}
}
}

View File

@@ -0,0 +1,179 @@
use sal_net::TcpConnector;
use std::net::{IpAddr, Ipv4Addr};
use std::time::Duration;
use tokio::net::TcpListener;
#[tokio::test]
async fn test_tcp_connector_new() {
let connector = TcpConnector::new();
// Test that the connector can actually perform operations
// Use a port that should be closed to verify the connector works
let result = connector
.check_port(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 65534)
.await;
assert!(result.is_ok());
assert!(!result.unwrap()); // Port should be closed
}
#[tokio::test]
async fn test_tcp_connector_with_timeout() {
let timeout = Duration::from_millis(100); // Short timeout for testing
let connector = TcpConnector::with_timeout(timeout);
// Test that the custom timeout is actually used by trying to connect to a non-routable IP
// This should timeout quickly with our short timeout
let start = std::time::Instant::now();
let result = connector
.check_port(IpAddr::V4(Ipv4Addr::new(10, 255, 255, 1)), 80)
.await;
let elapsed = start.elapsed();
assert!(result.is_ok());
assert!(!result.unwrap()); // Should timeout and return false
assert!(elapsed < Duration::from_secs(2)); // Should timeout much faster than default
}
#[tokio::test]
async fn test_tcp_connector_default() {
let connector = TcpConnector::default();
// Test that default constructor creates a working connector
// Verify it behaves the same as TcpConnector::new()
let result = connector
.check_port(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 65534)
.await;
assert!(result.is_ok());
assert!(!result.unwrap()); // Port should be closed
// Test that it can also ping (basic functionality test)
let ping_result = connector.ping("127.0.0.1").await;
assert!(ping_result.is_ok()); // Should not error, regardless of ping success
}
#[tokio::test]
async fn test_check_port_open() {
// Start a test server
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
// Keep the listener alive in a background task
let _handle = tokio::spawn(async move {
loop {
if let Ok((stream, _)) = listener.accept().await {
drop(stream); // Immediately close the connection
}
}
});
// Give the server a moment to start
tokio::time::sleep(Duration::from_millis(10)).await;
let connector = TcpConnector::new();
let result = connector.check_port(addr.ip(), addr.port()).await;
assert!(result.is_ok());
assert!(result.unwrap()); // Port should be open
}
#[tokio::test]
async fn test_check_port_closed() {
let connector = TcpConnector::new();
// Use a port that's very unlikely to be open
let result = connector
.check_port(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 65534)
.await;
assert!(result.is_ok());
assert!(!result.unwrap()); // Port should be closed
}
#[tokio::test]
async fn test_check_port_timeout() {
let connector = TcpConnector::with_timeout(Duration::from_millis(1));
// Use a non-routable IP to trigger timeout
let result = connector
.check_port(IpAddr::V4(Ipv4Addr::new(10, 255, 255, 1)), 80)
.await;
assert!(result.is_ok());
assert!(!result.unwrap()); // Should timeout and return false
}
#[tokio::test]
async fn test_check_multiple_ports() {
// Start test servers on multiple ports
let listener1 = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr1 = listener1.local_addr().unwrap();
let listener2 = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr2 = listener2.local_addr().unwrap();
// Keep listeners alive
let _handle1 = tokio::spawn(async move {
loop {
if let Ok((stream, _)) = listener1.accept().await {
drop(stream);
}
}
});
let _handle2 = tokio::spawn(async move {
loop {
if let Ok((stream, _)) = listener2.accept().await {
drop(stream);
}
}
});
tokio::time::sleep(Duration::from_millis(10)).await;
let connector = TcpConnector::new();
let ports = vec![addr1.port(), addr2.port(), 65533]; // Two open, one closed
let results = connector.check_ports(addr1.ip(), &ports).await;
assert!(results.is_ok());
let results = results.unwrap();
assert_eq!(results.len(), 3);
// First two should be open, last should be closed
assert!(results[0].1); // addr1.port() should be open
assert!(results[1].1); // addr2.port() should be open
assert!(!results[2].1); // 65533 should be closed
}
#[tokio::test]
async fn test_ping_localhost() {
let connector = TcpConnector::new();
// Ping localhost - should work on most systems
let result = connector.ping("localhost").await;
// Note: This might fail in some environments (containers, etc.)
// so we just verify the function doesn't panic and returns a boolean result
assert!(result.is_ok());
}
#[tokio::test]
async fn test_ping_invalid_host() {
let connector = TcpConnector::new();
// Ping an invalid hostname
let result = connector
.ping("this-host-definitely-does-not-exist-12345")
.await;
assert!(result.is_ok());
assert!(!result.unwrap()); // Should fail to ping invalid host
}
#[tokio::test]
async fn test_ping_timeout() {
let connector = TcpConnector::with_timeout(Duration::from_millis(1));
// Use a non-routable IP to trigger timeout
let result = connector.ping("10.255.255.1").await;
assert!(result.is_ok());
// Result could be true or false depending on system, but shouldn't panic
}

View File

@@ -0,0 +1,22 @@
[package]
name = "sal-text"
version = "0.1.0"
edition = "2021"
authors = ["PlanetFirst <info@incubaid.com>"]
description = "SAL Text - Text processing and manipulation utilities with regex, templating, and normalization"
repository = "https://git.threefold.info/herocode/sal"
license = "Apache-2.0"
[dependencies]
# Regex support for text replacement
regex = { workspace = true }
# Template engine for text rendering
tera = "1.19.0"
# Serialization support for templates
serde = { workspace = true }
# Rhai scripting support
rhai = { workspace = true }
[dev-dependencies]
# For temporary files in tests
tempfile = { workspace = true }

View File

@@ -0,0 +1,155 @@
# SAL Text - Text Processing and Manipulation Utilities (`sal-text`)
SAL Text provides a comprehensive collection of text processing utilities for both Rust applications and Rhai scripting environments.
## Installation
Add this to your `Cargo.toml`:
```toml
[dependencies]
sal-text = "0.1.0"
```
## Features
- **Text Indentation**: Remove common leading whitespace (`dedent`) and add prefixes (`prefix`)
- **String Normalization**: Sanitize strings for filenames (`name_fix`) and paths (`path_fix`)
- **Text Replacement**: Powerful `TextReplacer` for regex and literal replacements
- **Template Rendering**: `TemplateBuilder` using Tera engine for dynamic text generation
## Rust API
### Text Indentation
```rust
use sal_text::{dedent, prefix};
// Remove common indentation
let indented = " line 1\n line 2\n line 3";
let dedented = dedent(indented);
assert_eq!(dedented, "line 1\nline 2\n line 3");
// Add prefix to each line
let text = "line 1\nline 2";
let prefixed = prefix(text, "> ");
assert_eq!(prefixed, "> line 1\n> line 2");
```
### String Normalization
```rust
use sal_text::{name_fix, path_fix};
// Sanitize filename
let unsafe_name = "User's File [Draft].txt";
let safe_name = name_fix(unsafe_name);
assert_eq!(safe_name, "user_s_file_draft_.txt");
// Sanitize path (preserves directory structure)
let unsafe_path = "/path/to/User's File.txt";
let safe_path = path_fix(unsafe_path);
assert_eq!(safe_path, "/path/to/user_s_file.txt");
```
### Text Replacement
```rust
use sal_text::TextReplacer;
// Simple literal replacement
let replacer = TextReplacer::builder()
.pattern("hello")
.replacement("hi")
.build()
.expect("Failed to build replacer");
let result = replacer.replace("hello world, hello universe");
assert_eq!(result, "hi world, hi universe");
// Regex replacement
let replacer = TextReplacer::builder()
.pattern(r"\d+")
.replacement("NUMBER")
.regex(true)
.build()
.expect("Failed to build replacer");
let result = replacer.replace("There are 123 items");
assert_eq!(result, "There are NUMBER items");
// Chained operations
let replacer = TextReplacer::builder()
.pattern("world")
.replacement("universe")
.and()
.pattern(r"\d+")
.replacement("NUMBER")
.regex(true)
.build()
.expect("Failed to build replacer");
```
### Template Rendering
```rust
use sal_text::TemplateBuilder;
let result = TemplateBuilder::open("template.txt")
.expect("Failed to open template")
.add_var("name", "World")
.add_var("count", 42)
.render()
.expect("Failed to render template");
```
## Rhai Scripting
All functionality is available in Rhai scripts when using `herodo`:
```rhai
// Text indentation
let dedented = dedent(" hello\n world");
let prefixed = prefix("line1\nline2", "> ");
// String normalization
let safe_name = name_fix("User's File [Draft].txt");
let safe_path = path_fix("/path/to/User's File.txt");
// Text replacement
let builder = text_replacer_new();
builder = pattern(builder, "hello");
builder = replacement(builder, "hi");
builder = regex(builder, false);
let replacer = build(builder);
let result = replace(replacer, "hello world");
// Template rendering
let template = template_builder_open("template.txt");
template = add_var(template, "name", "World");
let result = render(template);
```
## Testing
Run the comprehensive test suite:
```bash
# Unit tests
cargo test
# Rhai integration tests
cargo run --bin herodo tests/rhai/run_all_tests.rhai
```
## Dependencies
- `regex`: For regex-based text replacement
- `tera`: For template rendering
- `serde`: For template variable serialization
- `rhai`: For Rhai scripting integration
## License
Apache-2.0

View File

@@ -0,0 +1,137 @@
/**
* Dedent a multiline string by removing common leading whitespace.
*
* This function analyzes all non-empty lines in the input text to determine
* the minimum indentation level, then removes that amount of whitespace
* from the beginning of each line. This is useful for working with
* multi-line strings in code that have been indented to match the
* surrounding code structure.
*
* # Arguments
*
* * `text` - The multiline string to dedent
*
* # Returns
*
* * `String` - The dedented string
*
* # Examples
*
* ```
* use sal_text::dedent;
*
* let indented = " line 1\n line 2\n line 3";
* let dedented = dedent(indented);
* assert_eq!(dedented, "line 1\nline 2\n line 3");
* ```
*
* # Notes
*
* - Empty lines are preserved but have all leading whitespace removed
* - Tabs are counted as 4 spaces for indentation purposes
*/
pub fn dedent(text: &str) -> String {
let lines: Vec<&str> = text.lines().collect();
// Find the minimum indentation level (ignore empty lines)
let min_indent = lines
.iter()
.filter(|line| !line.trim().is_empty())
.map(|line| {
let mut spaces = 0;
for c in line.chars() {
if c == ' ' {
spaces += 1;
} else if c == '\t' {
spaces += 4; // Count tabs as 4 spaces
} else {
break;
}
}
spaces
})
.min()
.unwrap_or(0);
// Remove that many spaces from the beginning of each line
lines
.iter()
.map(|line| {
if line.trim().is_empty() {
return String::new();
}
let mut count = 0;
let mut chars = line.chars().peekable();
// Skip initial spaces up to min_indent
while count < min_indent && chars.peek().is_some() {
match chars.peek() {
Some(' ') => {
chars.next();
count += 1;
}
Some('\t') => {
chars.next();
count += 4;
}
_ => break,
}
}
// Return the remaining characters
chars.collect::<String>()
})
.collect::<Vec<String>>()
.join("\n")
}
/**
* Prefix a multiline string with a specified prefix.
*
* This function adds the specified prefix to the beginning of each line in the input text.
*
* # Arguments
*
* * `text` - The multiline string to prefix
* * `prefix` - The prefix to add to each line
*
* # Returns
*
* * `String` - The prefixed string
*
* # Examples
*
* ```
* use sal_text::prefix;
*
* let text = "line 1\nline 2\nline 3";
* let prefixed = prefix(text, " ");
* assert_eq!(prefixed, " line 1\n line 2\n line 3");
* ```
*/
pub fn prefix(text: &str, prefix: &str) -> String {
text.lines()
.map(|line| format!("{}{}", prefix, line))
.collect::<Vec<String>>()
.join("\n")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_dedent() {
let indented = " line 1\n line 2\n line 3";
let dedented = dedent(indented);
assert_eq!(dedented, "line 1\nline 2\n line 3");
}
#[test]
fn test_prefix() {
let text = "line 1\nline 2\nline 3";
let prefixed = prefix(text, " ");
assert_eq!(prefixed, " line 1\n line 2\n line 3");
}
}

View File

@@ -0,0 +1,123 @@
pub fn name_fix(text: &str) -> String {
let mut result = String::with_capacity(text.len());
let mut last_was_underscore = false;
for c in text.chars() {
// Keep only ASCII characters
if c.is_ascii() {
// Replace specific characters with underscore
if c.is_whitespace()
|| c == ','
|| c == '-'
|| c == '"'
|| c == '\''
|| c == '#'
|| c == '!'
|| c == '('
|| c == ')'
|| c == '['
|| c == ']'
|| c == '='
|| c == '+'
|| c == '<'
|| c == '>'
|| c == '@'
|| c == '$'
|| c == '%'
|| c == '^'
|| c == '&'
|| c == '*'
{
// Only add underscore if the last character wasn't an underscore
if !last_was_underscore {
result.push('_');
last_was_underscore = true;
}
} else {
// Add the character as is (will be converted to lowercase later)
result.push(c);
last_was_underscore = false;
}
}
// Non-ASCII characters are simply skipped
}
// Convert to lowercase
return result.to_lowercase();
}
pub fn path_fix(text: &str) -> String {
// If path ends with '/', return as is
if text.ends_with('/') {
return text.to_string();
}
// Find the last '/' to extract the filename part
match text.rfind('/') {
Some(pos) => {
// Extract the path and filename parts
let path = &text[..=pos];
let filename = &text[pos + 1..];
// Apply name_fix to the filename part only
return format!("{}{}", path, name_fix(filename));
}
None => {
// No '/' found, so the entire text is a filename
return name_fix(text);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_name_fix() {
// Test ASCII conversion and special character replacement
assert_eq!(name_fix("Hello World"), "hello_world");
assert_eq!(name_fix("File-Name.txt"), "file_name.txt");
assert_eq!(name_fix("Test!@#$%^&*()"), "test_");
assert_eq!(name_fix("Space, Tab\t, Comma,"), "space_tab_comma_");
assert_eq!(name_fix("Quotes\"'"), "quotes_");
assert_eq!(name_fix("Brackets[]<>"), "brackets_");
assert_eq!(name_fix("Operators=+-"), "operators_");
// Test non-ASCII characters removal
assert_eq!(name_fix("Café"), "caf");
assert_eq!(name_fix("Résumé"), "rsum");
assert_eq!(name_fix("Über"), "ber");
// Test lowercase conversion
assert_eq!(name_fix("UPPERCASE"), "uppercase");
assert_eq!(name_fix("MixedCase"), "mixedcase");
}
#[test]
fn test_path_fix() {
// Test path ending with /
assert_eq!(path_fix("/path/to/directory/"), "/path/to/directory/");
// Test single filename
assert_eq!(path_fix("filename.txt"), "filename.txt");
assert_eq!(path_fix("UPPER-file.md"), "upper_file.md");
// Test path with filename
assert_eq!(path_fix("/path/to/File Name.txt"), "/path/to/file_name.txt");
assert_eq!(
path_fix("./relative/path/to/DOCUMENT-123.pdf"),
"./relative/path/to/document_123.pdf"
);
assert_eq!(
path_fix("/absolute/path/to/Résumé.doc"),
"/absolute/path/to/rsum.doc"
);
// Test path with special characters in filename
assert_eq!(
path_fix("/path/with/[special]<chars>.txt"),
"/path/with/_special_chars_.txt"
);
}
}

View File

@@ -0,0 +1,59 @@
//! SAL Text - Text processing and manipulation utilities
//!
//! This crate provides a comprehensive collection of text processing utilities including:
//! - **Text indentation**: Remove common leading whitespace (`dedent`) and add prefixes (`prefix`)
//! - **String normalization**: Sanitize strings for filenames (`name_fix`) and paths (`path_fix`)
//! - **Text replacement**: Powerful `TextReplacer` for regex and literal replacements
//! - **Template rendering**: `TemplateBuilder` using Tera engine for dynamic text generation
//!
//! All functionality is available in both Rust and Rhai scripting environments.
//!
//! # Examples
//!
//! ## Text Indentation
//!
//! ```rust
//! use sal_text::dedent;
//!
//! let indented = " line 1\n line 2\n line 3";
//! let dedented = dedent(indented);
//! assert_eq!(dedented, "line 1\nline 2\n line 3");
//! ```
//!
//! ## String Normalization
//!
//! ```rust
//! use sal_text::name_fix;
//!
//! let unsafe_name = "User's File [Draft].txt";
//! let safe_name = name_fix(unsafe_name);
//! assert_eq!(safe_name, "user_s_file_draft_.txt");
//! ```
//!
//! ## Text Replacement
//!
//! ```rust
//! use sal_text::TextReplacer;
//!
//! let replacer = TextReplacer::builder()
//! .pattern(r"\d+")
//! .replacement("NUMBER")
//! .regex(true)
//! .build()
//! .expect("Failed to build replacer");
//!
//! let result = replacer.replace("There are 123 items");
//! assert_eq!(result, "There are NUMBER items");
//! ```
mod dedent;
mod fix;
mod replace;
mod template;
pub mod rhai;
pub use dedent::*;
pub use fix::*;
pub use replace::*;
pub use template::*;

View File

@@ -0,0 +1,292 @@
use regex::Regex;
use std::fs;
use std::io::{self, Read};
use std::path::Path;
/// Represents the type of replacement to perform.
#[derive(Clone)]
pub enum ReplaceMode {
/// Regex-based replacement using the `regex` crate
Regex(Regex),
/// Literal substring replacement (non-regex)
Literal(String),
}
/// A single replacement operation with a pattern and replacement text
#[derive(Clone)]
pub struct ReplacementOperation {
mode: ReplaceMode,
replacement: String,
}
impl ReplacementOperation {
/// Applies this replacement operation to the input text
fn apply(&self, input: &str) -> String {
match &self.mode {
ReplaceMode::Regex(re) => re.replace_all(input, self.replacement.as_str()).to_string(),
ReplaceMode::Literal(search) => input.replace(search, &self.replacement),
}
}
}
/// Text replacer that can perform multiple replacement operations
/// in a single pass over the input text.
#[derive(Clone)]
pub struct TextReplacer {
operations: Vec<ReplacementOperation>,
}
impl TextReplacer {
/// Creates a new builder for configuring a TextReplacer
pub fn builder() -> TextReplacerBuilder {
TextReplacerBuilder::default()
}
/// Applies all configured replacement operations to the input text
pub fn replace(&self, input: &str) -> String {
let mut result = input.to_string();
// Apply each replacement operation in sequence
for op in &self.operations {
result = op.apply(&result);
}
result
}
/// Reads a file, applies all replacements, and returns the result as a string
pub fn replace_file<P: AsRef<Path>>(&self, path: P) -> io::Result<String> {
let mut file = fs::File::open(path)?;
let mut content = String::new();
file.read_to_string(&mut content)?;
Ok(self.replace(&content))
}
/// Reads a file, applies all replacements, and writes the result back to the file
pub fn replace_file_in_place<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
let content = self.replace_file(&path)?;
fs::write(path, content)?;
Ok(())
}
/// Reads a file, applies all replacements, and writes the result to a new file
pub fn replace_file_to<P1: AsRef<Path>, P2: AsRef<Path>>(
&self,
input_path: P1,
output_path: P2,
) -> io::Result<()> {
let content = self.replace_file(&input_path)?;
fs::write(output_path, content)?;
Ok(())
}
}
/// Builder for the TextReplacer.
#[derive(Default, Clone)]
pub struct TextReplacerBuilder {
operations: Vec<ReplacementOperation>,
pattern: Option<String>,
replacement: Option<String>,
use_regex: bool,
case_insensitive: bool,
}
impl TextReplacerBuilder {
/// Sets the pattern to search for
pub fn pattern(mut self, pat: &str) -> Self {
self.pattern = Some(pat.to_string());
self
}
/// Sets the replacement text
pub fn replacement(mut self, rep: &str) -> Self {
self.replacement = Some(rep.to_string());
self
}
/// Sets whether to use regex
pub fn regex(mut self, yes: bool) -> Self {
self.use_regex = yes;
self
}
/// Sets whether the replacement should be case-insensitive
pub fn case_insensitive(mut self, yes: bool) -> Self {
self.case_insensitive = yes;
self
}
/// Adds another replacement operation to the chain and resets the builder for a new operation
pub fn and(mut self) -> Self {
self.add_current_operation();
self
}
// Helper method to add the current operation to the list
fn add_current_operation(&mut self) -> bool {
if let Some(pattern) = self.pattern.take() {
let replacement = self.replacement.take().unwrap_or_default();
let use_regex = self.use_regex;
let case_insensitive = self.case_insensitive;
// Reset current settings
self.use_regex = false;
self.case_insensitive = false;
// Create the replacement mode
let mode = if use_regex {
let mut regex_pattern = pattern;
// If case insensitive, add the flag to the regex pattern
if case_insensitive && !regex_pattern.starts_with("(?i)") {
regex_pattern = format!("(?i){}", regex_pattern);
}
match Regex::new(&regex_pattern) {
Ok(re) => ReplaceMode::Regex(re),
Err(_) => return false, // Failed to compile regex
}
} else {
// For literal replacement, we'll handle case insensitivity differently
// since String::replace doesn't have a case-insensitive option
if case_insensitive {
return false; // Case insensitive not supported for literal
}
ReplaceMode::Literal(pattern)
};
self.operations
.push(ReplacementOperation { mode, replacement });
true
} else {
false
}
}
/// Builds the TextReplacer with all configured replacement operations
pub fn build(mut self) -> Result<TextReplacer, String> {
// If there's a pending replacement operation, add it
self.add_current_operation();
// Ensure we have at least one replacement operation
if self.operations.is_empty() {
return Err("No replacement operations configured".to_string());
}
Ok(TextReplacer {
operations: self.operations,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::{Seek, SeekFrom, Write};
use tempfile::NamedTempFile;
#[test]
fn test_regex_replace() {
let replacer = TextReplacer::builder()
.pattern(r"\bfoo\b")
.replacement("bar")
.regex(true)
.build()
.unwrap();
let input = "foo bar foo baz";
let output = replacer.replace(input);
assert_eq!(output, "bar bar bar baz");
}
#[test]
fn test_literal_replace() {
let replacer = TextReplacer::builder()
.pattern("foo")
.replacement("qux")
.regex(false)
.build()
.unwrap();
let input = "foo bar foo baz";
let output = replacer.replace(input);
assert_eq!(output, "qux bar qux baz");
}
#[test]
fn test_multiple_replacements() {
let replacer = TextReplacer::builder()
.pattern("foo")
.replacement("qux")
.and()
.pattern("bar")
.replacement("baz")
.build()
.unwrap();
let input = "foo bar foo";
let output = replacer.replace(input);
assert_eq!(output, "qux baz qux");
}
#[test]
fn test_case_insensitive_regex() {
let replacer = TextReplacer::builder()
.pattern("foo")
.replacement("bar")
.regex(true)
.case_insensitive(true)
.build()
.unwrap();
let input = "FOO foo Foo";
let output = replacer.replace(input);
assert_eq!(output, "bar bar bar");
}
#[test]
fn test_file_operations() -> io::Result<()> {
// Create a temporary file
let mut temp_file = NamedTempFile::new()?;
writeln!(temp_file, "foo bar foo baz")?;
// Flush the file to ensure content is written
temp_file.as_file_mut().flush()?;
let replacer = TextReplacer::builder()
.pattern("foo")
.replacement("qux")
.build()
.unwrap();
// Test replace_file
let result = replacer.replace_file(temp_file.path())?;
assert_eq!(result, "qux bar qux baz\n");
// Test replace_file_in_place
replacer.replace_file_in_place(temp_file.path())?;
// Verify the file was updated - need to seek to beginning of file first
let mut content = String::new();
temp_file.as_file_mut().seek(SeekFrom::Start(0))?;
temp_file.as_file_mut().read_to_string(&mut content)?;
assert_eq!(content, "qux bar qux baz\n");
// Test replace_file_to with a new temporary file
let output_file = NamedTempFile::new()?;
replacer.replace_file_to(temp_file.path(), output_file.path())?;
// Verify the output file has the replaced content
let mut output_content = String::new();
fs::File::open(output_file.path())?.read_to_string(&mut output_content)?;
assert_eq!(output_content, "qux bar qux baz\n");
Ok(())
}
}

View File

@@ -0,0 +1,229 @@
//! Rhai wrappers for Text module functions
//!
//! This module provides Rhai wrappers for the functions in the Text module.
use crate::{TemplateBuilder, TextReplacer, TextReplacerBuilder};
use rhai::{Array, Engine, EvalAltResult, Map, Position};
use std::collections::HashMap;
/// Register Text module functions with the Rhai engine
///
/// # Arguments
///
/// * `engine` - The Rhai engine to register the functions with
///
/// # Returns
///
/// * `Result<(), Box<EvalAltResult>>` - Ok if registration was successful, Err otherwise
pub fn register_text_module(engine: &mut Engine) -> Result<(), Box<EvalAltResult>> {
// Register types
register_text_types(engine)?;
// Register TextReplacer constructor
engine.register_fn("text_replacer_new", text_replacer_new);
engine.register_fn("text_replacer_builder", text_replacer_new); // Alias for backward compatibility
// Register TextReplacerBuilder instance methods
engine.register_fn("pattern", pattern);
engine.register_fn("replacement", replacement);
engine.register_fn("regex", regex);
engine.register_fn("case_insensitive", case_insensitive);
engine.register_fn("and", and);
engine.register_fn("build", build);
// Register TextReplacer instance methods
engine.register_fn("replace", replace);
engine.register_fn("replace_file", replace_file);
engine.register_fn("replace_file_in_place", replace_file_in_place);
engine.register_fn("replace_file_to", replace_file_to);
// Register TemplateBuilder constructor
engine.register_fn("template_builder_open", template_builder_open);
// Register TemplateBuilder instance methods
engine.register_fn("add_var", add_var_string);
engine.register_fn("add_var", add_var_int);
engine.register_fn("add_var", add_var_float);
engine.register_fn("add_var", add_var_bool);
engine.register_fn("add_var", add_var_array);
engine.register_fn("add_vars", add_vars);
engine.register_fn("render", render);
engine.register_fn("render_to_file", render_to_file);
// Register Fix functions directly from text module
engine.register_fn("name_fix", crate::name_fix);
engine.register_fn("path_fix", crate::path_fix);
// Register Dedent functions directly from text module
engine.register_fn("dedent", crate::dedent);
engine.register_fn("prefix", crate::prefix);
Ok(())
}
/// Register Text module types with the Rhai engine
fn register_text_types(engine: &mut Engine) -> Result<(), Box<EvalAltResult>> {
// Register TextReplacerBuilder type
engine.register_type_with_name::<TextReplacerBuilder>("TextReplacerBuilder");
// Register TextReplacer type
engine.register_type_with_name::<TextReplacer>("TextReplacer");
// Register TemplateBuilder type
engine.register_type_with_name::<TemplateBuilder>("TemplateBuilder");
Ok(())
}
// Helper functions for error conversion
fn io_error_to_rhai_error<T>(result: std::io::Result<T>) -> Result<T, Box<EvalAltResult>> {
result.map_err(|e| {
Box::new(EvalAltResult::ErrorRuntime(
format!("IO error: {}", e).into(),
Position::NONE,
))
})
}
fn tera_error_to_rhai_error<T>(result: Result<T, tera::Error>) -> Result<T, Box<EvalAltResult>> {
result.map_err(|e| {
Box::new(EvalAltResult::ErrorRuntime(
format!("Template error: {}", e).into(),
Position::NONE,
))
})
}
fn string_error_to_rhai_error<T>(result: Result<T, String>) -> Result<T, Box<EvalAltResult>> {
result.map_err(|e| Box::new(EvalAltResult::ErrorRuntime(e.into(), Position::NONE)))
}
// TextReplacer implementation
/// Creates a new TextReplacerBuilder
pub fn text_replacer_new() -> TextReplacerBuilder {
TextReplacerBuilder::default()
}
/// Sets the pattern to search for
pub fn pattern(builder: TextReplacerBuilder, pat: &str) -> TextReplacerBuilder {
builder.pattern(pat)
}
/// Sets the replacement text
pub fn replacement(builder: TextReplacerBuilder, rep: &str) -> TextReplacerBuilder {
builder.replacement(rep)
}
/// Sets whether to use regex
pub fn regex(builder: TextReplacerBuilder, yes: bool) -> TextReplacerBuilder {
builder.regex(yes)
}
/// Sets whether the replacement should be case-insensitive
pub fn case_insensitive(builder: TextReplacerBuilder, yes: bool) -> TextReplacerBuilder {
builder.case_insensitive(yes)
}
/// Adds another replacement operation to the chain and resets the builder for a new operation
pub fn and(builder: TextReplacerBuilder) -> TextReplacerBuilder {
builder.and()
}
/// Builds the TextReplacer with all configured replacement operations
pub fn build(builder: TextReplacerBuilder) -> Result<TextReplacer, Box<EvalAltResult>> {
string_error_to_rhai_error(builder.build())
}
/// Applies all configured replacement operations to the input text
pub fn replace(replacer: &mut TextReplacer, input: &str) -> String {
replacer.replace(input)
}
/// Reads a file, applies all replacements, and returns the result as a string
pub fn replace_file(replacer: &mut TextReplacer, path: &str) -> Result<String, Box<EvalAltResult>> {
io_error_to_rhai_error(replacer.replace_file(path))
}
/// Reads a file, applies all replacements, and writes the result back to the file
pub fn replace_file_in_place(
replacer: &mut TextReplacer,
path: &str,
) -> Result<(), Box<EvalAltResult>> {
io_error_to_rhai_error(replacer.replace_file_in_place(path))
}
/// Reads a file, applies all replacements, and writes the result to a new file
pub fn replace_file_to(
replacer: &mut TextReplacer,
input_path: &str,
output_path: &str,
) -> Result<(), Box<EvalAltResult>> {
io_error_to_rhai_error(replacer.replace_file_to(input_path, output_path))
}
// TemplateBuilder implementation
/// Creates a new TemplateBuilder with the specified template path
pub fn template_builder_open(template_path: &str) -> Result<TemplateBuilder, Box<EvalAltResult>> {
io_error_to_rhai_error(TemplateBuilder::open(template_path))
}
/// Adds a string variable to the template context
pub fn add_var_string(builder: TemplateBuilder, name: &str, value: &str) -> TemplateBuilder {
builder.add_var(name, value)
}
/// Adds an integer variable to the template context
pub fn add_var_int(builder: TemplateBuilder, name: &str, value: i64) -> TemplateBuilder {
builder.add_var(name, value)
}
/// Adds a float variable to the template context
pub fn add_var_float(builder: TemplateBuilder, name: &str, value: f64) -> TemplateBuilder {
builder.add_var(name, value)
}
/// Adds a boolean variable to the template context
pub fn add_var_bool(builder: TemplateBuilder, name: &str, value: bool) -> TemplateBuilder {
builder.add_var(name, value)
}
/// Adds an array variable to the template context
pub fn add_var_array(builder: TemplateBuilder, name: &str, array: Array) -> TemplateBuilder {
// Convert Rhai Array to Vec<String>
let vec: Vec<String> = array
.iter()
.filter_map(|v| v.clone().into_string().ok())
.collect();
builder.add_var(name, vec)
}
/// Adds multiple variables to the template context from a Map
pub fn add_vars(builder: TemplateBuilder, vars: Map) -> TemplateBuilder {
// Convert Rhai Map to Rust HashMap
let mut hash_map = HashMap::new();
for (key, value) in vars.iter() {
if let Ok(val_str) = value.clone().into_string() {
hash_map.insert(key.to_string(), val_str);
}
}
// Add the variables
builder.add_vars(hash_map)
}
/// Renders the template with the current context
pub fn render(builder: &mut TemplateBuilder) -> Result<String, Box<EvalAltResult>> {
tera_error_to_rhai_error(builder.render())
}
/// Renders the template and writes the result to a file
pub fn render_to_file(
builder: &mut TemplateBuilder,
output_path: &str,
) -> Result<(), Box<EvalAltResult>> {
io_error_to_rhai_error(builder.render_to_file(output_path))
}

View File

@@ -0,0 +1,310 @@
use std::collections::HashMap;
use std::fs;
use std::io;
use std::path::Path;
use tera::{Context, Tera};
/// A builder for creating and rendering templates using the Tera template engine.
#[derive(Clone)]
pub struct TemplateBuilder {
template_path: String,
context: Context,
tera: Option<Tera>,
}
impl TemplateBuilder {
/// Creates a new TemplateBuilder with the specified template path.
///
/// # Arguments
///
/// * `template_path` - The path to the template file
///
/// # Returns
///
/// A new TemplateBuilder instance
///
/// # Example
///
/// ```
/// use sal_text::TemplateBuilder;
///
/// let builder = TemplateBuilder::open("templates/example.html");
/// ```
pub fn open<P: AsRef<Path>>(template_path: P) -> io::Result<Self> {
let path_str = template_path.as_ref().to_string_lossy().to_string();
// Verify the template file exists
if !Path::new(&path_str).exists() {
return Err(io::Error::new(
io::ErrorKind::NotFound,
format!("Template file not found: {}", path_str),
));
}
Ok(Self {
template_path: path_str,
context: Context::new(),
tera: None,
})
}
/// Adds a variable to the template context.
///
/// # Arguments
///
/// * `name` - The name of the variable to add
/// * `value` - The value to associate with the variable
///
/// # Returns
///
/// The builder instance for method chaining
///
/// # Example
///
/// ```no_run
/// use sal_text::TemplateBuilder;
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let builder = TemplateBuilder::open("templates/example.html")?
/// .add_var("title", "Hello World")
/// .add_var("username", "John Doe");
/// Ok(())
/// }
/// ```
pub fn add_var<S, V>(mut self, name: S, value: V) -> Self
where
S: AsRef<str>,
V: serde::Serialize,
{
self.context.insert(name.as_ref(), &value);
self
}
/// Adds multiple variables to the template context from a HashMap.
///
/// # Arguments
///
/// * `vars` - A HashMap containing variable names and values
///
/// # Returns
///
/// The builder instance for method chaining
///
/// # Example
///
/// ```no_run
/// use sal_text::TemplateBuilder;
/// use std::collections::HashMap;
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut vars = HashMap::new();
/// vars.insert("title", "Hello World");
/// vars.insert("username", "John Doe");
///
/// let builder = TemplateBuilder::open("templates/example.html")?
/// .add_vars(vars);
/// Ok(())
/// }
/// ```
pub fn add_vars<S, V>(mut self, vars: HashMap<S, V>) -> Self
where
S: AsRef<str>,
V: serde::Serialize,
{
for (name, value) in vars {
self.context.insert(name.as_ref(), &value);
}
self
}
/// Initializes the Tera template engine with the template file.
///
/// This method is called automatically by render() if not called explicitly.
///
/// # Returns
///
/// The builder instance for method chaining
fn initialize_tera(&mut self) -> Result<(), tera::Error> {
if self.tera.is_none() {
// Create a new Tera instance with just this template
let mut tera = Tera::default();
// Read the template content
let template_content = fs::read_to_string(&self.template_path)
.map_err(|e| tera::Error::msg(format!("Failed to read template file: {}", e)))?;
// Add the template to Tera
let template_name = Path::new(&self.template_path)
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("template");
tera.add_raw_template(template_name, &template_content)?;
self.tera = Some(tera);
}
Ok(())
}
/// Renders the template with the current context.
///
/// # Returns
///
/// The rendered template as a string
///
/// # Example
///
/// ```no_run
/// use sal_text::TemplateBuilder;
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let result = TemplateBuilder::open("templates/example.html")?
/// .add_var("title", "Hello World")
/// .add_var("username", "John Doe")
/// .render()?;
///
/// println!("Rendered template: {}", result);
/// Ok(())
/// }
/// ```
pub fn render(&mut self) -> Result<String, tera::Error> {
// Initialize Tera if not already done
self.initialize_tera()?;
// Get the template name
let template_name = Path::new(&self.template_path)
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("template");
// Render the template
let tera = self.tera.as_ref().unwrap();
tera.render(template_name, &self.context)
}
/// Renders the template and writes the result to a file.
///
/// # Arguments
///
/// * `output_path` - The path where the rendered template should be written
///
/// # Returns
///
/// Result indicating success or failure
///
/// # Example
///
/// ```no_run
/// use sal_text::TemplateBuilder;
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
/// TemplateBuilder::open("templates/example.html")?
/// .add_var("title", "Hello World")
/// .add_var("username", "John Doe")
/// .render_to_file("output.html")?;
/// Ok(())
/// }
/// ```
pub fn render_to_file<P: AsRef<Path>>(&mut self, output_path: P) -> io::Result<()> {
let rendered = self.render().map_err(|e| {
io::Error::new(
io::ErrorKind::Other,
format!("Template rendering error: {}", e),
)
})?;
fs::write(output_path, rendered)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;
use tempfile::NamedTempFile;
#[test]
fn test_template_rendering() -> Result<(), Box<dyn std::error::Error>> {
// Create a temporary template file
let temp_file = NamedTempFile::new()?;
let template_content = "Hello, {{ name }}! Welcome to {{ place }}.\n";
fs::write(temp_file.path(), template_content)?;
// Create a template builder and add variables
let mut builder = TemplateBuilder::open(temp_file.path())?;
builder = builder.add_var("name", "John").add_var("place", "Rust");
// Render the template
let result = builder.render()?;
assert_eq!(result, "Hello, John! Welcome to Rust.\n");
Ok(())
}
#[test]
fn test_template_with_multiple_vars() -> Result<(), Box<dyn std::error::Error>> {
// Create a temporary template file
let temp_file = NamedTempFile::new()?;
let template_content = "{% if show_greeting %}Hello, {{ name }}!{% endif %}\n{% for item in items %}{{ item }}{% if not loop.last %}, {% endif %}{% endfor %}\n";
fs::write(temp_file.path(), template_content)?;
// Create a template builder and add variables
let mut builder = TemplateBuilder::open(temp_file.path())?;
// Add variables including a boolean and a vector
builder = builder
.add_var("name", "Alice")
.add_var("show_greeting", true)
.add_var("items", vec!["apple", "banana", "cherry"]);
// Render the template
let result = builder.render()?;
assert_eq!(result, "Hello, Alice!\napple, banana, cherry\n");
Ok(())
}
#[test]
fn test_template_with_hashmap_vars() -> Result<(), Box<dyn std::error::Error>> {
// Create a temporary template file
let mut temp_file = NamedTempFile::new()?;
writeln!(temp_file, "{{{{ greeting }}}}, {{{{ name }}}}!")?;
temp_file.flush()?;
// Create a HashMap of variables
let mut vars = HashMap::new();
vars.insert("greeting", "Hi");
vars.insert("name", "Bob");
// Create a template builder and add variables from HashMap
let mut builder = TemplateBuilder::open(temp_file.path())?;
builder = builder.add_vars(vars);
// Render the template
let result = builder.render()?;
assert_eq!(result, "Hi, Bob!\n");
Ok(())
}
#[test]
fn test_render_to_file() -> Result<(), Box<dyn std::error::Error>> {
// Create a temporary template file
let temp_file = NamedTempFile::new()?;
let template_content = "{{ message }}\n";
fs::write(temp_file.path(), template_content)?;
// Create an output file
let output_file = NamedTempFile::new()?;
// Create a template builder, add a variable, and render to file
let mut builder = TemplateBuilder::open(temp_file.path())?;
builder = builder.add_var("message", "This is a test");
builder.render_to_file(output_file.path())?;
// Read the output file and verify its contents
let content = fs::read_to_string(output_file.path())?;
assert_eq!(content, "This is a test\n");
Ok(())
}
}

View File

@@ -0,0 +1,255 @@
// Text Rhai Test Runner
//
// This script runs all Text-related Rhai tests and reports results.
print("=== Text Rhai Test Suite ===");
print("Running comprehensive tests for Text Rhai integration...\n");
let total_tests = 0;
let passed_tests = 0;
let failed_tests = 0;
// Test 1: Text Indentation Functions
print("Test 1: Text Indentation Functions");
total_tests += 1;
try {
let indented = " line 1\n line 2\n line 3";
let dedented = dedent(indented);
let text = "line 1\nline 2";
let prefixed = prefix(text, "> ");
if dedented == "line 1\nline 2\n line 3" && prefixed == "> line 1\n> line 2" {
passed_tests += 1;
print("✓ PASSED: Text indentation functions work correctly");
} else {
failed_tests += 1;
print("✗ FAILED: Text indentation functions returned unexpected results");
}
} catch(err) {
failed_tests += 1;
print(`✗ ERROR: Text indentation test failed - ${err}`);
}
// Test 2: String Normalization Functions
print("\nTest 2: String Normalization Functions");
total_tests += 1;
try {
let unsafe_name = "User's File [Draft].txt";
let safe_name = name_fix(unsafe_name);
let unsafe_path = "/path/to/User's File.txt";
let safe_path = path_fix(unsafe_path);
if safe_name == "user_s_file_draft_.txt" && safe_path == "/path/to/user_s_file.txt" {
passed_tests += 1;
print("✓ PASSED: String normalization functions work correctly");
} else {
failed_tests += 1;
print(`✗ FAILED: String normalization - expected 'user_s_file_draft_.txt' and '/path/to/user_s_file.txt', got '${safe_name}' and '${safe_path}'`);
}
} catch(err) {
failed_tests += 1;
print(`✗ ERROR: String normalization test failed - ${err}`);
}
// Test 3: TextReplacer Builder Pattern
print("\nTest 3: TextReplacer Builder Pattern");
total_tests += 1;
try {
let builder = text_replacer_new();
builder = pattern(builder, "hello");
builder = replacement(builder, "hi");
builder = regex(builder, false);
let replacer = build(builder);
let result = replace(replacer, "hello world, hello universe");
if result == "hi world, hi universe" {
passed_tests += 1;
print("✓ PASSED: TextReplacer builder pattern works correctly");
} else {
failed_tests += 1;
print(`✗ FAILED: TextReplacer - expected 'hi world, hi universe', got '${result}'`);
}
} catch(err) {
failed_tests += 1;
print(`✗ ERROR: TextReplacer builder test failed - ${err}`);
}
// Test 4: TextReplacer with Regex
print("\nTest 4: TextReplacer with Regex");
total_tests += 1;
try {
let builder = text_replacer_new();
builder = pattern(builder, "\\d+");
builder = replacement(builder, "NUMBER");
builder = regex(builder, true);
let replacer = build(builder);
let result = replace(replacer, "There are 123 items and 456 more");
if result == "There are NUMBER items and NUMBER more" {
passed_tests += 1;
print("✓ PASSED: TextReplacer regex functionality works correctly");
} else {
failed_tests += 1;
print(`✗ FAILED: TextReplacer regex - expected 'There are NUMBER items and NUMBER more', got '${result}'`);
}
} catch(err) {
failed_tests += 1;
print(`✗ ERROR: TextReplacer regex test failed - ${err}`);
}
// Test 5: TextReplacer Chained Operations
print("\nTest 5: TextReplacer Chained Operations");
total_tests += 1;
try {
let builder = text_replacer_new();
builder = pattern(builder, "world");
builder = replacement(builder, "universe");
builder = regex(builder, false);
builder = and(builder);
builder = pattern(builder, "\\d+");
builder = replacement(builder, "NUMBER");
builder = regex(builder, true);
let replacer = build(builder);
let result = replace(replacer, "Hello world, there are 123 items");
if result == "Hello universe, there are NUMBER items" {
passed_tests += 1;
print("✓ PASSED: TextReplacer chained operations work correctly");
} else {
failed_tests += 1;
print(`✗ FAILED: TextReplacer chained - expected 'Hello universe, there are NUMBER items', got '${result}'`);
}
} catch(err) {
failed_tests += 1;
print(`✗ ERROR: TextReplacer chained operations test failed - ${err}`);
}
// Test 6: Error Handling - Invalid Regex
print("\nTest 6: Error Handling - Invalid Regex");
total_tests += 1;
try {
let builder = text_replacer_new();
builder = pattern(builder, "[invalid regex");
builder = replacement(builder, "test");
builder = regex(builder, true);
let replacer = build(builder);
failed_tests += 1;
print("✗ FAILED: Should have failed with invalid regex");
} catch(err) {
passed_tests += 1;
print("✓ PASSED: Invalid regex properly rejected");
}
// Test 7: Unicode Handling
print("\nTest 7: Unicode Handling");
total_tests += 1;
try {
let unicode_text = " Hello 世界\n Goodbye 世界";
let dedented = dedent(unicode_text);
let unicode_name = "Café";
let fixed_name = name_fix(unicode_name);
let unicode_prefix = prefix("Hello 世界", "🔹 ");
if dedented == "Hello 世界\nGoodbye 世界" &&
fixed_name == "caf" &&
unicode_prefix == "🔹 Hello 世界" {
passed_tests += 1;
print("✓ PASSED: Unicode handling works correctly");
} else {
failed_tests += 1;
print("✗ FAILED: Unicode handling returned unexpected results");
}
} catch(err) {
failed_tests += 1;
print(`✗ ERROR: Unicode handling test failed - ${err}`);
}
// Test 8: Edge Cases
print("\nTest 8: Edge Cases");
total_tests += 1;
try {
let empty_dedent = dedent("");
let empty_prefix = prefix("test", "");
let empty_name_fix = name_fix("");
if empty_dedent == "" && empty_prefix == "test" && empty_name_fix == "" {
passed_tests += 1;
print("✓ PASSED: Edge cases handled correctly");
} else {
failed_tests += 1;
print("✗ FAILED: Edge cases returned unexpected results");
}
} catch(err) {
failed_tests += 1;
print(`✗ ERROR: Edge cases test failed - ${err}`);
}
// Test 9: Complex Workflow
print("\nTest 9: Complex Text Processing Workflow");
total_tests += 1;
try {
// Normalize filename
let unsafe_filename = "User's Script [Draft].py";
let safe_filename = name_fix(unsafe_filename);
// Process code
let indented_code = " def hello():\n print('Hello World')\n return True";
let dedented_code = dedent(indented_code);
let commented_code = prefix(dedented_code, "# ");
// Replace text
let builder = text_replacer_new();
builder = pattern(builder, "Hello World");
builder = replacement(builder, "SAL Text");
builder = regex(builder, false);
let replacer = build(builder);
let final_code = replace(replacer, commented_code);
if safe_filename == "user_s_script_draft_.py" &&
final_code.contains("# def hello():") &&
final_code.contains("SAL Text") {
passed_tests += 1;
print("✓ PASSED: Complex workflow completed successfully");
} else {
failed_tests += 1;
print("✗ FAILED: Complex workflow returned unexpected results");
}
} catch(err) {
failed_tests += 1;
print(`✗ ERROR: Complex workflow test failed - ${err}`);
}
// Test 10: Template Builder Error Handling
print("\nTest 10: Template Builder Error Handling");
total_tests += 1;
try {
let builder = template_builder_open("/nonexistent/file.txt");
failed_tests += 1;
print("✗ FAILED: Should have failed with nonexistent file");
} catch(err) {
passed_tests += 1;
print("✓ PASSED: Template builder properly handles nonexistent files");
}
// Print final results
print("\n=== Test Results ===");
print(`Total Tests: ${total_tests}`);
print(`Passed: ${passed_tests}`);
print(`Failed: ${failed_tests}`);
if failed_tests == 0 {
print("\n✓ All tests passed!");
} else {
print(`\n✗ ${failed_tests} test(s) failed.`);
}
print("\n=== Text Rhai Test Suite Completed ===");

View File

@@ -0,0 +1,351 @@
//! Rhai integration tests for Text module
//!
//! These tests validate the Rhai wrapper functions and ensure proper
//! integration between Rust and Rhai for text processing operations.
use rhai::{Engine, EvalAltResult};
use sal_text::rhai::*;
#[cfg(test)]
mod rhai_integration_tests {
use super::*;
fn create_test_engine() -> Engine {
let mut engine = Engine::new();
register_text_module(&mut engine).expect("Failed to register text 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 text functions are available
let functions_exist = true;
functions_exist
"#;
let result: Result<bool, Box<EvalAltResult>> = engine.eval(script);
assert!(result.is_ok());
assert_eq!(result.unwrap(), true);
}
#[test]
fn test_dedent_function_exists() {
let engine = create_test_engine();
let script = r#"
let indented = " line 1\n line 2\n line 3";
let result = dedent(indented);
return result == "line 1\nline 2\n line 3";
"#;
let result: Result<bool, Box<EvalAltResult>> = engine.eval(script);
assert!(result.is_ok());
assert_eq!(result.unwrap(), true);
}
#[test]
fn test_prefix_function_exists() {
let engine = create_test_engine();
let script = r#"
let text = "line 1\nline 2";
let result = prefix(text, "> ");
return result == "> line 1\n> line 2";
"#;
let result: Result<bool, Box<EvalAltResult>> = engine.eval(script);
assert!(result.is_ok());
assert_eq!(result.unwrap(), true);
}
#[test]
fn test_name_fix_function_exists() {
let engine = create_test_engine();
let script = r#"
let unsafe_name = "User's File [Draft].txt";
let result = name_fix(unsafe_name);
return result == "user_s_file_draft_.txt";
"#;
let result: Result<bool, Box<EvalAltResult>> = engine.eval(script);
assert!(result.is_ok());
assert_eq!(result.unwrap(), true);
}
#[test]
fn test_path_fix_function_exists() {
let engine = create_test_engine();
let script = r#"
let unsafe_path = "/path/to/User's File.txt";
let result = path_fix(unsafe_path);
return result == "/path/to/user_s_file.txt";
"#;
let result: Result<bool, Box<EvalAltResult>> = engine.eval(script);
assert!(result.is_ok());
assert_eq!(result.unwrap(), true);
}
#[test]
fn test_text_replacer_builder_creation() {
let engine = create_test_engine();
let script = r#"
let builder = text_replacer_builder();
return type_of(builder) == "TextReplacerBuilder";
"#;
let result: Result<bool, Box<EvalAltResult>> = engine.eval(script);
assert!(result.is_ok());
assert_eq!(result.unwrap(), true);
}
#[test]
fn test_text_replacer_workflow() {
let engine = create_test_engine();
let script = r#"
let builder = text_replacer_builder();
builder = pattern(builder, "hello");
builder = replacement(builder, "hi");
builder = regex(builder, false);
let replacer = build(builder);
let result = replace(replacer, "hello world, hello universe");
return result == "hi world, hi universe";
"#;
let result: Result<bool, Box<EvalAltResult>> = engine.eval(script);
assert!(result.is_ok());
assert_eq!(result.unwrap(), true);
}
#[test]
fn test_text_replacer_regex_workflow() {
let engine = create_test_engine();
let script = r#"
let builder = text_replacer_builder();
builder = pattern(builder, "\\d+");
builder = replacement(builder, "NUMBER");
builder = regex(builder, true);
let replacer = build(builder);
let result = replace(replacer, "There are 123 items");
return result == "There are NUMBER items";
"#;
let result: Result<bool, Box<EvalAltResult>> = engine.eval(script);
assert!(result.is_ok());
assert_eq!(result.unwrap(), true);
}
#[test]
fn test_text_replacer_chained_operations() {
let engine = create_test_engine();
let script = r#"
let builder = text_replacer_builder();
builder = pattern(builder, "world");
builder = replacement(builder, "universe");
builder = regex(builder, false);
builder = and(builder);
builder = pattern(builder, "\\d+");
builder = replacement(builder, "NUMBER");
builder = regex(builder, true);
let replacer = build(builder);
let result = replace(replacer, "Hello world, there are 123 items");
return result == "Hello universe, there are NUMBER items";
"#;
let result: Result<bool, Box<EvalAltResult>> = engine.eval(script);
assert!(result.is_ok());
assert_eq!(result.unwrap(), true);
}
#[test]
fn test_template_builder_creation() {
let engine = create_test_engine();
let script = r#"
// We can't test file operations easily in unit tests,
// but we can test that the function exists and returns the right type
try {
let builder = template_builder_open("/nonexistent/file.txt");
return false; // Should have failed
} catch(err) {
return err.to_string().contains("error"); // Expected to fail
}
"#;
let result: Result<bool, Box<EvalAltResult>> = engine.eval(script);
assert!(result.is_ok());
assert_eq!(result.unwrap(), true);
}
#[test]
fn test_error_handling_invalid_regex() {
let engine = create_test_engine();
let script = r#"
try {
let builder = text_replacer_builder();
builder = pattern(builder, "[invalid regex");
builder = replacement(builder, "test");
builder = regex(builder, true);
let replacer = build(builder);
return false; // Should have failed
} catch(err) {
return true; // Expected to fail
}
"#;
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 string handling
try {
let result = dedent("");
test_results.push(result == "");
} catch(err) {
test_results.push(false);
}
// Test empty prefix
try {
let result = prefix("test", "");
test_results.push(result == "test");
} catch(err) {
test_results.push(false);
}
// Test empty name_fix
try {
let result = name_fix("");
test_results.push(result == "");
} catch(err) {
test_results.push(false);
}
return 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_unicode_handling() {
let engine = create_test_engine();
let script = r#"
let unicode_tests = [];
// Test dedent with unicode
try {
let text = " Hello 世界\n Goodbye 世界";
let result = dedent(text);
unicode_tests.push(result == "Hello 世界\nGoodbye 世界");
} catch(err) {
unicode_tests.push(false);
}
// Test name_fix with unicode (should remove non-ASCII)
try {
let result = name_fix("Café");
unicode_tests.push(result == "caf");
} catch(err) {
unicode_tests.push(false);
}
// Test prefix with unicode
try {
let result = prefix("Hello 世界", "🔹 ");
unicode_tests.push(result == "🔹 Hello 世界");
} catch(err) {
unicode_tests.push(false);
}
return unicode_tests;
"#;
let result: Result<rhai::Array, Box<EvalAltResult>> = engine.eval(script);
assert!(result.is_ok());
let results = result.unwrap();
// All unicode tests should pass
for (i, result) in results.iter().enumerate() {
assert_eq!(
result.as_bool().unwrap_or(false),
true,
"Unicode test {} failed",
i
);
}
}
#[test]
fn test_complex_text_processing_workflow() {
let engine = create_test_engine();
let script = r#"
// Simple workflow test
let unsafe_filename = "User's Script [Draft].py";
let safe_filename = name_fix(unsafe_filename);
let indented_code = " def hello():\n return True";
let dedented_code = dedent(indented_code);
let results = [];
results.push(safe_filename == "user_s_script_draft_.py");
results.push(dedented_code.contains("def hello():"));
return results;
"#;
let result: Result<rhai::Array, Box<EvalAltResult>> = engine.eval(script);
assert!(result.is_ok());
let results = result.unwrap();
// All workflow tests should pass
for (i, result) in results.iter().enumerate() {
assert_eq!(
result.as_bool().unwrap_or(false),
true,
"Workflow test {} failed",
i
);
}
}
}

View File

@@ -0,0 +1,222 @@
//! Unit tests for string normalization functionality
//!
//! These tests validate the name_fix and path_fix functions including:
//! - Filename sanitization for safe filesystem usage
//! - Path normalization preserving directory structure
//! - Special character handling and replacement
//! - Unicode character removal and ASCII conversion
use sal_text::{name_fix, path_fix};
#[test]
fn test_name_fix_basic() {
assert_eq!(name_fix("Hello World"), "hello_world");
assert_eq!(name_fix("File-Name.txt"), "file_name.txt");
}
#[test]
fn test_name_fix_special_characters() {
assert_eq!(name_fix("Test!@#$%^&*()"), "test_");
assert_eq!(name_fix("Space, Tab\t, Comma,"), "space_tab_comma_");
assert_eq!(name_fix("Quotes\"'"), "quotes_");
assert_eq!(name_fix("Brackets[]<>"), "brackets_");
assert_eq!(name_fix("Operators=+-"), "operators_");
}
#[test]
fn test_name_fix_unicode_removal() {
assert_eq!(name_fix("Café"), "caf");
assert_eq!(name_fix("Résumé"), "rsum");
assert_eq!(name_fix("Über"), "ber");
assert_eq!(name_fix("Naïve"), "nave");
assert_eq!(name_fix("Piñata"), "piata");
}
#[test]
fn test_name_fix_case_conversion() {
assert_eq!(name_fix("UPPERCASE"), "uppercase");
assert_eq!(name_fix("MixedCase"), "mixedcase");
assert_eq!(name_fix("camelCase"), "camelcase");
assert_eq!(name_fix("PascalCase"), "pascalcase");
}
#[test]
fn test_name_fix_consecutive_underscores() {
assert_eq!(name_fix("Multiple Spaces"), "multiple_spaces");
assert_eq!(name_fix("Special!!!Characters"), "special_characters");
assert_eq!(name_fix("Mixed-_-Separators"), "mixed___separators");
}
#[test]
fn test_name_fix_file_extensions() {
assert_eq!(name_fix("Document.PDF"), "document.pdf");
assert_eq!(name_fix("Image.JPEG"), "image.jpeg");
assert_eq!(name_fix("Archive.tar.gz"), "archive.tar.gz");
assert_eq!(name_fix("Config.json"), "config.json");
}
#[test]
fn test_name_fix_empty_and_edge_cases() {
assert_eq!(name_fix(""), "");
assert_eq!(name_fix(" "), "_");
assert_eq!(name_fix("!!!"), "_");
assert_eq!(name_fix("___"), "___");
}
#[test]
fn test_name_fix_real_world_examples() {
assert_eq!(
name_fix("User's Report [Draft 1].md"),
"user_s_report_draft_1_.md"
);
assert_eq!(
name_fix("Meeting Notes (2023-12-01).txt"),
"meeting_notes_2023_12_01_.txt"
);
assert_eq!(
name_fix("Photo #123 - Vacation!.jpg"),
"photo_123_vacation_.jpg"
);
assert_eq!(
name_fix("Project Plan v2.0 FINAL.docx"),
"project_plan_v2.0_final.docx"
);
}
#[test]
fn test_path_fix_directory_paths() {
assert_eq!(path_fix("/path/to/directory/"), "/path/to/directory/");
assert_eq!(path_fix("./relative/path/"), "./relative/path/");
assert_eq!(path_fix("../parent/path/"), "../parent/path/");
}
#[test]
fn test_path_fix_single_filename() {
assert_eq!(path_fix("filename.txt"), "filename.txt");
assert_eq!(path_fix("UPPER-file.md"), "upper_file.md");
assert_eq!(path_fix("Special!File.pdf"), "special_file.pdf");
}
#[test]
fn test_path_fix_absolute_paths() {
assert_eq!(path_fix("/path/to/File Name.txt"), "/path/to/file_name.txt");
assert_eq!(
path_fix("/absolute/path/to/DOCUMENT-123.pdf"),
"/absolute/path/to/document_123.pdf"
);
assert_eq!(path_fix("/home/user/Résumé.doc"), "/home/user/rsum.doc");
}
#[test]
fn test_path_fix_relative_paths() {
assert_eq!(
path_fix("./relative/path/to/Document.PDF"),
"./relative/path/to/document.pdf"
);
assert_eq!(
path_fix("../parent/Special File.txt"),
"../parent/special_file.txt"
);
assert_eq!(
path_fix("subfolder/User's File.md"),
"subfolder/user_s_file.md"
);
}
#[test]
fn test_path_fix_special_characters_in_filename() {
assert_eq!(
path_fix("/path/with/[special]<chars>.txt"),
"/path/with/_special_chars_.txt"
);
assert_eq!(path_fix("./folder/File!@#.pdf"), "./folder/file_.pdf");
assert_eq!(
path_fix("/data/Report (Final).docx"),
"/data/report_final_.docx"
);
}
#[test]
fn test_path_fix_preserves_path_structure() {
assert_eq!(
path_fix("/very/long/path/to/some/Deep File.txt"),
"/very/long/path/to/some/deep_file.txt"
);
assert_eq!(
path_fix("./a/b/c/d/e/Final Document.pdf"),
"./a/b/c/d/e/final_document.pdf"
);
}
#[test]
fn test_path_fix_windows_style_paths() {
// Note: These tests assume Unix-style path handling
// In a real implementation, you might want to handle Windows paths differently
assert_eq!(
path_fix("C:\\Users\\Name\\Document.txt"),
"c:\\users\\name\\document.txt"
);
}
#[test]
fn test_path_fix_edge_cases() {
assert_eq!(path_fix(""), "");
assert_eq!(path_fix("/"), "/");
assert_eq!(path_fix("./"), "./");
assert_eq!(path_fix("../"), "../");
}
#[test]
fn test_path_fix_unicode_in_filename() {
assert_eq!(path_fix("/path/to/Café.txt"), "/path/to/caf.txt");
assert_eq!(
path_fix("./folder/Naïve Document.pdf"),
"./folder/nave_document.pdf"
);
assert_eq!(
path_fix("/home/user/Piñata Party.jpg"),
"/home/user/piata_party.jpg"
);
}
#[test]
fn test_path_fix_complex_real_world_examples() {
assert_eq!(
path_fix("/Users/john/Documents/Project Files/Final Report (v2.1) [APPROVED].docx"),
"/Users/john/Documents/Project Files/final_report_v2.1_approved_.docx"
);
assert_eq!(
path_fix("./assets/images/Photo #123 - Vacation! (2023).jpg"),
"./assets/images/photo_123_vacation_2023_.jpg"
);
assert_eq!(
path_fix("/var/log/Application Logs/Error Log [2023-12-01].txt"),
"/var/log/Application Logs/error_log_2023_12_01_.txt"
);
}
#[test]
fn test_name_fix_and_path_fix_consistency() {
let filename = "User's Report [Draft].txt";
let path = "/path/to/User's Report [Draft].txt";
let fixed_name = name_fix(filename);
let fixed_path = path_fix(path);
// The filename part should be the same in both cases
assert!(fixed_path.ends_with(&fixed_name));
assert_eq!(fixed_name, "user_s_report_draft_.txt");
assert_eq!(fixed_path, "/path/to/user_s_report_draft_.txt");
}
#[test]
fn test_normalization_preserves_dots_in_extensions() {
assert_eq!(name_fix("file.tar.gz"), "file.tar.gz");
assert_eq!(name_fix("backup.2023.12.01.sql"), "backup.2023.12.01.sql");
assert_eq!(
path_fix("/path/to/archive.tar.bz2"),
"/path/to/archive.tar.bz2"
);
}

View File

@@ -0,0 +1,299 @@
//! Unit tests for template functionality
//!
//! These tests validate the TemplateBuilder including:
//! - Template loading from files
//! - Variable substitution (string, int, float, bool, array)
//! - Template rendering to string and file
//! - Error handling for missing variables and invalid templates
//! - Complex template scenarios with loops and conditionals
use sal_text::TemplateBuilder;
use std::collections::HashMap;
use std::fs;
use tempfile::NamedTempFile;
#[test]
fn test_template_builder_basic_string_variable() {
// Create a temporary template file
let temp_file = NamedTempFile::new().expect("Failed to create temp file");
let template_content = "Hello {{name}}!";
fs::write(temp_file.path(), template_content).expect("Failed to write template");
let result = TemplateBuilder::open(temp_file.path())
.expect("Failed to open template")
.add_var("name", "World")
.render()
.expect("Failed to render template");
assert_eq!(result, "Hello World!");
}
#[test]
fn test_template_builder_multiple_variables() {
let temp_file = NamedTempFile::new().expect("Failed to create temp file");
let template_content = "{{greeting}} {{name}}, you have {{count}} messages.";
fs::write(temp_file.path(), template_content).expect("Failed to write template");
let result = TemplateBuilder::open(temp_file.path())
.expect("Failed to open template")
.add_var("greeting", "Hello")
.add_var("name", "Alice")
.add_var("count", 5)
.render()
.expect("Failed to render template");
assert_eq!(result, "Hello Alice, you have 5 messages.");
}
#[test]
fn test_template_builder_different_types() {
let temp_file = NamedTempFile::new().expect("Failed to create temp file");
let template_content = "String: {{text}}, Int: {{number}}, Float: {{decimal}}, Bool: {{flag}}";
fs::write(temp_file.path(), template_content).expect("Failed to write template");
let result = TemplateBuilder::open(temp_file.path())
.expect("Failed to open template")
.add_var("text", "hello")
.add_var("number", 42)
.add_var("decimal", 3.14)
.add_var("flag", true)
.render()
.expect("Failed to render template");
assert_eq!(result, "String: hello, Int: 42, Float: 3.14, Bool: true");
}
#[test]
fn test_template_builder_array_variable() {
let temp_file = NamedTempFile::new().expect("Failed to create temp file");
let template_content =
"Items: {% for item in items %}{{item}}{% if not loop.last %}, {% endif %}{% endfor %}";
fs::write(temp_file.path(), template_content).expect("Failed to write template");
let items = vec!["apple", "banana", "cherry"];
let result = TemplateBuilder::open(temp_file.path())
.expect("Failed to open template")
.add_var("items", items)
.render()
.expect("Failed to render template");
assert_eq!(result, "Items: apple, banana, cherry");
}
#[test]
fn test_template_builder_add_vars_hashmap() {
let temp_file = NamedTempFile::new().expect("Failed to create temp file");
let template_content = "{{title}}: {{description}}";
fs::write(temp_file.path(), template_content).expect("Failed to write template");
let mut vars = HashMap::new();
vars.insert("title".to_string(), "Report".to_string());
vars.insert("description".to_string(), "Monthly summary".to_string());
let result = TemplateBuilder::open(temp_file.path())
.expect("Failed to open template")
.add_vars(vars)
.render()
.expect("Failed to render template");
assert_eq!(result, "Report: Monthly summary");
}
#[test]
fn test_template_builder_render_to_file() {
// Create template file
let template_file = NamedTempFile::new().expect("Failed to create template file");
let template_content = "Hello {{name}}, today is {{day}}.";
fs::write(template_file.path(), template_content).expect("Failed to write template");
// Create output file
let output_file = NamedTempFile::new().expect("Failed to create output file");
TemplateBuilder::open(template_file.path())
.expect("Failed to open template")
.add_var("name", "Bob")
.add_var("day", "Monday")
.render_to_file(output_file.path())
.expect("Failed to render to file");
let result = fs::read_to_string(output_file.path()).expect("Failed to read output file");
assert_eq!(result, "Hello Bob, today is Monday.");
}
#[test]
fn test_template_builder_conditional() {
let temp_file = NamedTempFile::new().expect("Failed to create temp file");
let template_content =
"{% if show_message %}Message: {{message}}{% else %}No message{% endif %}";
fs::write(temp_file.path(), template_content).expect("Failed to write template");
// Test with condition true
let result_true = TemplateBuilder::open(temp_file.path())
.expect("Failed to open template")
.add_var("show_message", true)
.add_var("message", "Hello World")
.render()
.expect("Failed to render template");
assert_eq!(result_true, "Message: Hello World");
// Test with condition false
let result_false = TemplateBuilder::open(temp_file.path())
.expect("Failed to open template")
.add_var("show_message", false)
.add_var("message", "Hello World")
.render()
.expect("Failed to render template");
assert_eq!(result_false, "No message");
}
#[test]
fn test_template_builder_loop_with_index() {
let temp_file = NamedTempFile::new().expect("Failed to create temp file");
let template_content = "{% for item in items %}{{loop.index}}: {{item}}\n{% endfor %}";
fs::write(temp_file.path(), template_content).expect("Failed to write template");
let items = vec!["first", "second", "third"];
let result = TemplateBuilder::open(temp_file.path())
.expect("Failed to open template")
.add_var("items", items)
.render()
.expect("Failed to render template");
assert_eq!(result, "1: first\n2: second\n3: third\n");
}
#[test]
fn test_template_builder_nested_variables() {
let temp_file = NamedTempFile::new().expect("Failed to create temp file");
let template_content = "User: {{user.name}} ({{user.email}})";
fs::write(temp_file.path(), template_content).expect("Failed to write template");
let mut user = HashMap::new();
user.insert("name".to_string(), "John Doe".to_string());
user.insert("email".to_string(), "john@example.com".to_string());
let result = TemplateBuilder::open(temp_file.path())
.expect("Failed to open template")
.add_var("user", user)
.render()
.expect("Failed to render template");
assert_eq!(result, "User: John Doe (john@example.com)");
}
#[test]
fn test_template_builder_missing_variable_error() {
let temp_file = NamedTempFile::new().expect("Failed to create temp file");
let template_content = "Hello {{missing_var}}!";
fs::write(temp_file.path(), template_content).expect("Failed to write template");
let result = TemplateBuilder::open(temp_file.path())
.expect("Failed to open template")
.render();
assert!(result.is_err());
}
#[test]
fn test_template_builder_invalid_template_syntax() {
let temp_file = NamedTempFile::new().expect("Failed to create temp file");
let template_content = "Hello {{unclosed_var!";
fs::write(temp_file.path(), template_content).expect("Failed to write template");
let result = TemplateBuilder::open(temp_file.path())
.expect("Failed to open template")
.render();
assert!(result.is_err());
}
#[test]
fn test_template_builder_nonexistent_file() {
let result = TemplateBuilder::open("/nonexistent/template.txt");
assert!(result.is_err());
}
#[test]
fn test_template_builder_empty_template() {
let temp_file = NamedTempFile::new().expect("Failed to create temp file");
fs::write(temp_file.path(), "").expect("Failed to write empty template");
let result = TemplateBuilder::open(temp_file.path())
.expect("Failed to open template")
.render()
.expect("Failed to render empty template");
assert_eq!(result, "");
}
#[test]
fn test_template_builder_template_with_no_variables() {
let temp_file = NamedTempFile::new().expect("Failed to create temp file");
let template_content = "This is a static template with no variables.";
fs::write(temp_file.path(), template_content).expect("Failed to write template");
let result = TemplateBuilder::open(temp_file.path())
.expect("Failed to open template")
.render()
.expect("Failed to render template");
assert_eq!(result, template_content);
}
#[test]
fn test_template_builder_complex_report() {
let temp_file = NamedTempFile::new().expect("Failed to create temp file");
let template_content = r#"
# {{report_title}}
Generated on: {{date}}
## Summary
Total items: {{total_items}}
Status: {{status}}
## Items
{% for item in items %}
- {{item.name}}: {{item.value}}{% if item.important %} (IMPORTANT){% endif %}
{% endfor %}
## Footer
{% if show_footer %}
Report generated by {{generator}}
{% endif %}
"#;
fs::write(temp_file.path(), template_content).expect("Failed to write template");
let mut item1 = HashMap::new();
item1.insert("name".to_string(), "Item 1".to_string());
item1.insert("value".to_string(), "100".to_string());
item1.insert("important".to_string(), true.to_string());
let mut item2 = HashMap::new();
item2.insert("name".to_string(), "Item 2".to_string());
item2.insert("value".to_string(), "200".to_string());
item2.insert("important".to_string(), false.to_string());
let items = vec![item1, item2];
let result = TemplateBuilder::open(temp_file.path())
.expect("Failed to open template")
.add_var("report_title", "Monthly Report")
.add_var("date", "2023-12-01")
.add_var("total_items", 2)
.add_var("status", "Complete")
.add_var("items", items)
.add_var("show_footer", true)
.add_var("generator", "SAL Text")
.render()
.expect("Failed to render template");
assert!(result.contains("# Monthly Report"));
assert!(result.contains("Generated on: 2023-12-01"));
assert!(result.contains("Total items: 2"));
assert!(result.contains("- Item 1: 100"));
assert!(result.contains("- Item 2: 200"));
assert!(result.contains("Report generated by SAL Text"));
}

View File

@@ -0,0 +1,159 @@
//! Unit tests for text indentation functionality
//!
//! These tests validate the dedent and prefix functions including:
//! - Common whitespace removal (dedent)
//! - Line prefix addition (prefix)
//! - Edge cases and special characters
//! - Tab handling and mixed indentation
use sal_text::{dedent, prefix};
#[test]
fn test_dedent_basic() {
let indented = " line 1\n line 2\n line 3";
let expected = "line 1\nline 2\n line 3";
assert_eq!(dedent(indented), expected);
}
#[test]
fn test_dedent_empty_lines() {
let indented = " line 1\n\n line 2\n line 3";
let expected = "line 1\n\nline 2\n line 3";
assert_eq!(dedent(indented), expected);
}
#[test]
fn test_dedent_tabs_as_spaces() {
let indented = "\t\tline 1\n\t\tline 2\n\t\t\tline 3";
let expected = "line 1\nline 2\n\tline 3";
assert_eq!(dedent(indented), expected);
}
#[test]
fn test_dedent_mixed_tabs_and_spaces() {
let indented = " \tline 1\n \tline 2\n \t line 3";
let expected = "line 1\nline 2\n line 3";
assert_eq!(dedent(indented), expected);
}
#[test]
fn test_dedent_no_common_indentation() {
let text = "line 1\n line 2\n line 3";
let expected = "line 1\n line 2\n line 3";
assert_eq!(dedent(text), expected);
}
#[test]
fn test_dedent_single_line() {
let indented = " single line";
let expected = "single line";
assert_eq!(dedent(indented), expected);
}
#[test]
fn test_dedent_empty_string() {
assert_eq!(dedent(""), "");
}
#[test]
fn test_dedent_only_whitespace() {
let whitespace = " \n \n ";
let expected = "\n\n";
assert_eq!(dedent(whitespace), expected);
}
#[test]
fn test_prefix_basic() {
let text = "line 1\nline 2\nline 3";
let expected = " line 1\n line 2\n line 3";
assert_eq!(prefix(text, " "), expected);
}
#[test]
fn test_prefix_with_symbols() {
let text = "line 1\nline 2\nline 3";
let expected = "> line 1\n> line 2\n> line 3";
assert_eq!(prefix(text, "> "), expected);
}
#[test]
fn test_prefix_empty_lines() {
let text = "line 1\n\nline 3";
let expected = ">> line 1\n>> \n>> line 3";
assert_eq!(prefix(text, ">> "), expected);
}
#[test]
fn test_prefix_single_line() {
let text = "single line";
let expected = "PREFIX: single line";
assert_eq!(prefix(text, "PREFIX: "), expected);
}
#[test]
fn test_prefix_empty_string() {
assert_eq!(prefix("", "PREFIX: "), "");
}
#[test]
fn test_prefix_empty_prefix() {
let text = "line 1\nline 2";
assert_eq!(prefix(text, ""), text);
}
#[test]
fn test_dedent_and_prefix_combination() {
let indented = " def function():\n print('hello')\n return True";
let dedented = dedent(indented);
let prefixed = prefix(&dedented, ">>> ");
let expected = ">>> def function():\n>>> print('hello')\n>>> return True";
assert_eq!(prefixed, expected);
}
#[test]
fn test_dedent_real_code_example() {
let code = r#"
if condition:
for item in items:
process(item)
return result
else:
return None"#;
let dedented = dedent(code);
let expected = "\nif condition:\n for item in items:\n process(item)\n return result\nelse:\n return None";
assert_eq!(dedented, expected);
}
#[test]
fn test_prefix_code_comment() {
let code = "function main() {\n console.log('Hello');\n}";
let commented = prefix(code, "// ");
let expected = "// function main() {\n// console.log('Hello');\n// }";
assert_eq!(commented, expected);
}
#[test]
fn test_dedent_preserves_relative_indentation() {
let text = " start\n indented more\n back to start level\n indented again";
let dedented = dedent(text);
let expected = "start\n indented more\nback to start level\n indented again";
assert_eq!(dedented, expected);
}
#[test]
fn test_prefix_with_unicode() {
let text = "Hello 世界\nGoodbye 世界";
let prefixed = prefix(text, "🔹 ");
let expected = "🔹 Hello 世界\n🔹 Goodbye 世界";
assert_eq!(prefixed, expected);
}
#[test]
fn test_dedent_with_unicode() {
let text = " Hello 世界\n Goodbye 世界\n More indented 世界";
let dedented = dedent(text);
let expected = "Hello 世界\nGoodbye 世界\n More indented 世界";
assert_eq!(dedented, expected);
}

View File

@@ -0,0 +1,309 @@
//! Unit tests for text replacement functionality
//!
//! These tests validate the TextReplacer including:
//! - Literal string replacement
//! - Regex pattern replacement
//! - Multiple chained replacements
//! - File operations (read, write, in-place)
//! - Error handling and edge cases
use sal_text::TextReplacer;
use std::fs;
use tempfile::NamedTempFile;
#[test]
fn test_text_replacer_literal_single() {
let replacer = TextReplacer::builder()
.pattern("hello")
.replacement("hi")
.regex(false)
.build()
.expect("Failed to build replacer");
let result = replacer.replace("hello world, hello universe");
assert_eq!(result, "hi world, hi universe");
}
#[test]
fn test_text_replacer_regex_single() {
let replacer = TextReplacer::builder()
.pattern(r"\d+")
.replacement("NUMBER")
.regex(true)
.build()
.expect("Failed to build replacer");
let result = replacer.replace("There are 123 items and 456 more");
assert_eq!(result, "There are NUMBER items and NUMBER more");
}
#[test]
fn test_text_replacer_multiple_operations() {
let replacer = TextReplacer::builder()
.pattern(r"\d+")
.replacement("NUMBER")
.regex(true)
.and()
.pattern("world")
.replacement("universe")
.regex(false)
.build()
.expect("Failed to build replacer");
let result = replacer.replace("Hello world, there are 123 items");
assert_eq!(result, "Hello universe, there are NUMBER items");
}
#[test]
fn test_text_replacer_chained_operations() {
let replacer = TextReplacer::builder()
.pattern("cat")
.replacement("dog")
.regex(false)
.and()
.pattern("dog")
.replacement("animal")
.regex(false)
.build()
.expect("Failed to build replacer");
// Operations are applied in sequence, so "cat" -> "dog" -> "animal"
let result = replacer.replace("The cat sat on the mat");
assert_eq!(result, "The animal sat on the mat");
}
#[test]
fn test_text_replacer_regex_capture_groups() {
let replacer = TextReplacer::builder()
.pattern(r"(\d{4})-(\d{2})-(\d{2})")
.replacement("$3/$2/$1")
.regex(true)
.build()
.expect("Failed to build replacer");
let result = replacer.replace("Date: 2023-12-01");
assert_eq!(result, "Date: 01/12/2023");
}
#[test]
fn test_text_replacer_case_sensitive() {
let replacer = TextReplacer::builder()
.pattern("Hello")
.replacement("Hi")
.regex(false)
.build()
.expect("Failed to build replacer");
let result = replacer.replace("Hello world, hello universe");
assert_eq!(result, "Hi world, hello universe");
}
#[test]
fn test_text_replacer_regex_case_insensitive() {
let replacer = TextReplacer::builder()
.pattern(r"(?i)hello")
.replacement("Hi")
.regex(true)
.build()
.expect("Failed to build replacer");
let result = replacer.replace("Hello world, HELLO universe");
assert_eq!(result, "Hi world, Hi universe");
}
#[test]
fn test_text_replacer_empty_input() {
let replacer = TextReplacer::builder()
.pattern("test")
.replacement("replacement")
.regex(false)
.build()
.expect("Failed to build replacer");
let result = replacer.replace("");
assert_eq!(result, "");
}
#[test]
fn test_text_replacer_no_matches() {
let replacer = TextReplacer::builder()
.pattern("xyz")
.replacement("abc")
.regex(false)
.build()
.expect("Failed to build replacer");
let input = "Hello world";
let result = replacer.replace(input);
assert_eq!(result, input);
}
#[test]
fn test_text_replacer_file_operations() {
// Create a temporary file with test content
let temp_file = NamedTempFile::new().expect("Failed to create temp file");
let test_content = "Hello world, there are 123 items";
fs::write(temp_file.path(), test_content).expect("Failed to write to temp file");
let replacer = TextReplacer::builder()
.pattern(r"\d+")
.replacement("NUMBER")
.regex(true)
.and()
.pattern("world")
.replacement("universe")
.regex(false)
.build()
.expect("Failed to build replacer");
// Test replace_file
let result = replacer
.replace_file(temp_file.path())
.expect("Failed to replace file content");
assert_eq!(result, "Hello universe, there are NUMBER items");
// Verify original file is unchanged
let original_content =
fs::read_to_string(temp_file.path()).expect("Failed to read original file");
assert_eq!(original_content, test_content);
}
#[test]
fn test_text_replacer_file_in_place() {
// Create a temporary file with test content
let temp_file = NamedTempFile::new().expect("Failed to create temp file");
let test_content = "Hello world, there are 123 items";
fs::write(temp_file.path(), test_content).expect("Failed to write to temp file");
let replacer = TextReplacer::builder()
.pattern("world")
.replacement("universe")
.regex(false)
.build()
.expect("Failed to build replacer");
// Test replace_file_in_place
replacer
.replace_file_in_place(temp_file.path())
.expect("Failed to replace file in place");
// Verify file content was changed
let new_content = fs::read_to_string(temp_file.path()).expect("Failed to read modified file");
assert_eq!(new_content, "Hello universe, there are 123 items");
}
#[test]
fn test_text_replacer_file_to_file() {
// Create source file
let source_file = NamedTempFile::new().expect("Failed to create source file");
let test_content = "Hello world, there are 123 items";
fs::write(source_file.path(), test_content).expect("Failed to write to source file");
// Create destination file
let dest_file = NamedTempFile::new().expect("Failed to create dest file");
let replacer = TextReplacer::builder()
.pattern(r"\d+")
.replacement("NUMBER")
.regex(true)
.build()
.expect("Failed to build replacer");
// Test replace_file_to
replacer
.replace_file_to(source_file.path(), dest_file.path())
.expect("Failed to replace file to destination");
// Verify source file is unchanged
let source_content =
fs::read_to_string(source_file.path()).expect("Failed to read source file");
assert_eq!(source_content, test_content);
// Verify destination file has replaced content
let dest_content = fs::read_to_string(dest_file.path()).expect("Failed to read dest file");
assert_eq!(dest_content, "Hello world, there are NUMBER items");
}
#[test]
fn test_text_replacer_invalid_regex() {
let result = TextReplacer::builder()
.pattern("[invalid regex")
.replacement("test")
.regex(true)
.build();
assert!(result.is_err());
}
#[test]
fn test_text_replacer_builder_default_regex_false() {
let replacer = TextReplacer::builder()
.pattern(r"\d+")
.replacement("NUMBER")
.build()
.expect("Failed to build replacer");
// Should treat as literal since regex defaults to false
let result = replacer.replace(r"Match \d+ pattern");
assert_eq!(result, "Match NUMBER pattern");
}
#[test]
fn test_text_replacer_complex_regex() {
let replacer = TextReplacer::builder()
.pattern(r"(\w+)@(\w+\.\w+)")
.replacement("EMAIL_ADDRESS")
.regex(true)
.build()
.expect("Failed to build replacer");
let result = replacer.replace("Contact john@example.com or jane@test.org");
assert_eq!(result, "Contact EMAIL_ADDRESS or EMAIL_ADDRESS");
}
#[test]
fn test_text_replacer_multiline_text() {
let replacer = TextReplacer::builder()
.pattern(r"^\s*//.*$")
.replacement("")
.regex(true)
.build()
.expect("Failed to build replacer");
let input =
"function test() {\n // This is a comment\n return true;\n // Another comment\n}";
let result = replacer.replace(input);
// Note: This test depends on how the regex engine handles multiline mode
// The actual behavior might need adjustment based on regex flags
assert!(result.contains("function test()"));
assert!(result.contains("return true;"));
}
#[test]
fn test_text_replacer_unicode_text() {
let replacer = TextReplacer::builder()
.pattern("café")
.replacement("coffee")
.regex(false)
.build()
.expect("Failed to build replacer");
let result = replacer.replace("I love café in the morning");
assert_eq!(result, "I love coffee in the morning");
}
#[test]
fn test_text_replacer_large_text() {
let large_text = "word ".repeat(10000);
let replacer = TextReplacer::builder()
.pattern("word")
.replacement("term")
.regex(false)
.build()
.expect("Failed to build replacer");
let result = replacer.replace(&large_text);
assert_eq!(result, "term ".repeat(10000));
}