cargo-hammerwork 1.5.2

A comprehensive cargo subcommand for managing Hammerwork job queues with webhook management, event streaming, database operations, and advanced monitoring capabilities
Documentation
use std::env;
use std::fs;
use tempfile::TempDir;

#[test]
fn test_config_file_creation() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = temp_dir.path().join("config.toml");

    // Test that we can create a basic config file structure
    let config_content = r#"
database_url = "postgres://localhost/test"
default_queue = "test_queue"
default_limit = 100
log_level = "info"
connection_pool_size = 5
"#;

    fs::write(&config_path, config_content).unwrap();
    let read_content = fs::read_to_string(&config_path).unwrap();
    assert!(read_content.contains("database_url"));
    assert!(read_content.contains("postgres://localhost/test"));
}

#[test]
fn test_env_var_precedence() {
    // Test that environment variables take precedence
    unsafe {
        env::set_var("DATABASE_URL", "postgres://env_override/db");
    }

    // In a real implementation, we'd load config and verify env vars override file values
    let env_url = env::var("DATABASE_URL").unwrap();
    assert_eq!(env_url, "postgres://env_override/db");

    unsafe {
        env::remove_var("DATABASE_URL");
    }
}

#[test]
fn test_validation_functions() {
    // Test URL validation
    assert!(is_valid_database_url("postgres://localhost/db"));
    assert!(is_valid_database_url("mysql://localhost/db"));
    assert!(!is_valid_database_url("invalid_url"));
    assert!(!is_valid_database_url(""));

    // Test queue name validation
    assert!(is_valid_queue_name("valid_queue"));
    assert!(is_valid_queue_name("queue-with-dashes"));
    assert!(!is_valid_queue_name("queue with spaces"));
    assert!(!is_valid_queue_name(""));
    assert!(!is_valid_queue_name("queue/with/slashes"));
}

#[test]
fn test_log_level_validation() {
    assert!(is_valid_log_level("error"));
    assert!(is_valid_log_level("warn"));
    assert!(is_valid_log_level("info"));
    assert!(is_valid_log_level("debug"));
    assert!(is_valid_log_level("trace"));
    assert!(!is_valid_log_level("invalid"));
    assert!(!is_valid_log_level(""));
}

// Helper functions that mirror the validation logic
fn is_valid_database_url(url: &str) -> bool {
    !url.is_empty() && (url.starts_with("postgres://") || url.starts_with("mysql://"))
}

fn is_valid_queue_name(name: &str) -> bool {
    !name.is_empty()
        && !name.contains(' ')
        && !name.contains('/')
        && name
            .chars()
            .all(|c| c.is_alphanumeric() || c == '_' || c == '-')
}

fn is_valid_log_level(level: &str) -> bool {
    matches!(level, "error" | "warn" | "info" | "debug" | "trace")
}

#[cfg(test)]
mod cli_tests {

    #[test]
    fn test_cli_structure() {
        // Test that the CLI structure is valid
        // This would use clap's testing capabilities in a real implementation
        let expected_commands = vec!["migration", "config"];

        for command in expected_commands {
            assert!(!command.is_empty());
            assert!(command.chars().all(|c| c.is_ascii_alphanumeric()));
        }
    }

    #[test]
    fn test_migration_subcommands() {
        let migration_commands = vec!["run", "status"];

        for command in migration_commands {
            assert!(!command.is_empty());
            assert!(command.chars().all(|c| c.is_ascii_alphanumeric()));
        }
    }

    #[test]
    fn test_config_subcommands() {
        let config_commands = vec!["show", "set", "get", "reset", "path"];

        for command in config_commands {
            assert!(!command.is_empty());
            assert!(command.chars().all(|c| c.is_ascii_alphanumeric()));
        }
    }
}

#[cfg(test)]
mod database_tests {
    use super::*;

    #[test]
    fn test_database_url_parsing() {
        // Test PostgreSQL URL parsing
        let pg_url = "postgres://user:pass@localhost:5432/dbname";
        assert!(is_valid_database_url(pg_url));

        // Test MySQL URL parsing
        let mysql_url = "mysql://user:pass@localhost:3306/dbname";
        assert!(is_valid_database_url(mysql_url));

        // Test invalid URLs
        assert!(!is_valid_database_url("http://localhost/db"));
        assert!(!is_valid_database_url("invalid"));
    }

    #[test]
    fn test_connection_pool_size_validation() {
        // Test valid pool sizes
        assert!(is_valid_pool_size(1));
        assert!(is_valid_pool_size(5));
        assert!(is_valid_pool_size(20));

        // Test invalid pool sizes
        assert!(!is_valid_pool_size(0));
        assert!(!is_valid_pool_size(101)); // Assuming max of 100
    }
}

fn is_valid_pool_size(size: u32) -> bool {
    size > 0 && size <= 100
}