sal-git 0.1.0

SAL Git - Git repository management and operations
Documentation
use sal_git::*;
use std::collections::HashMap;

#[test]
fn test_git_executor_new() {
    let executor = GitExecutor::new();
    // We can't directly access the config field since it's private,
    // but we can test that the executor was created successfully
    let _executor = executor;
}

#[test]
fn test_git_executor_default() {
    let executor = GitExecutor::default();
    let _executor = executor;
}

#[test]
fn test_git_config_status_serialization() {
    let status_ok = GitConfigStatus::Ok;
    let status_error = GitConfigStatus::Error;

    let json_ok = serde_json::to_string(&status_ok).unwrap();
    let json_error = serde_json::to_string(&status_error).unwrap();

    assert_eq!(json_ok, "\"ok\"");
    assert_eq!(json_error, "\"error\"");
}

#[test]
fn test_git_config_status_deserialization() {
    let status_ok: GitConfigStatus = serde_json::from_str("\"ok\"").unwrap();
    let status_error: GitConfigStatus = serde_json::from_str("\"error\"").unwrap();

    assert_eq!(status_ok, GitConfigStatus::Ok);
    assert_eq!(status_error, GitConfigStatus::Error);
}

#[test]
fn test_git_server_auth_serialization() {
    let auth = GitServerAuth {
        sshagent: Some(true),
        key: None,
        username: None,
        password: None,
    };

    let json = serde_json::to_string(&auth).unwrap();
    assert!(json.contains("\"sshagent\":true"));
}

#[test]
fn test_git_server_auth_deserialization() {
    let json = r#"{"sshagent":true,"key":null,"username":null,"password":null}"#;
    let auth: GitServerAuth = serde_json::from_str(json).unwrap();

    assert_eq!(auth.sshagent, Some(true));
    assert_eq!(auth.key, None);
    assert_eq!(auth.username, None);
    assert_eq!(auth.password, None);
}

#[test]
fn test_git_config_serialization() {
    let mut auth_map = HashMap::new();
    auth_map.insert(
        "github.com".to_string(),
        GitServerAuth {
            sshagent: Some(true),
            key: None,
            username: None,
            password: None,
        },
    );

    let config = GitConfig {
        status: GitConfigStatus::Ok,
        auth: auth_map,
    };

    let json = serde_json::to_string(&config).unwrap();
    assert!(json.contains("\"status\":\"ok\""));
    assert!(json.contains("\"github.com\""));
}

#[test]
fn test_git_config_deserialization() {
    let json = r#"{"status":"ok","auth":{"github.com":{"sshagent":true,"key":null,"username":null,"password":null}}}"#;
    let config: GitConfig = serde_json::from_str(json).unwrap();

    assert_eq!(config.status, GitConfigStatus::Ok);
    assert!(config.auth.contains_key("github.com"));
    assert_eq!(config.auth["github.com"].sshagent, Some(true));
}

#[test]
fn test_git_executor_error_display() {
    let error = GitExecutorError::GitCommandFailed("command failed".to_string());
    assert_eq!(format!("{}", error), "Git command failed: command failed");

    let error = GitExecutorError::SshAgentNotLoaded;
    assert_eq!(format!("{}", error), "SSH agent is not loaded");

    let error = GitExecutorError::AuthenticationError("auth failed".to_string());
    assert_eq!(format!("{}", error), "Authentication error: auth failed");
}

#[test]
fn test_git_executor_error_from_redis_error() {
    let redis_error = redis::RedisError::from((redis::ErrorKind::TypeError, "type error"));
    let git_error = GitExecutorError::from(redis_error);

    match git_error {
        GitExecutorError::RedisError(_) => {}
        _ => panic!("Expected RedisError variant"),
    }
}

#[test]
fn test_git_executor_error_from_serde_error() {
    let serde_error = serde_json::from_str::<GitConfig>("invalid json").unwrap_err();
    let git_error = GitExecutorError::from(serde_error);

    match git_error {
        GitExecutorError::JsonError(_) => {}
        _ => panic!("Expected JsonError variant"),
    }
}

#[test]
fn test_git_executor_error_from_io_error() {
    let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
    let git_error = GitExecutorError::from(io_error);

    match git_error {
        GitExecutorError::CommandExecutionError(_) => {}
        _ => panic!("Expected CommandExecutionError variant"),
    }
}

#[test]
fn test_redis_url_configuration() {
    // Test default Redis URL
    std::env::remove_var("REDIS_URL");
    std::env::remove_var("SAL_REDIS_URL");

    // This is testing the internal function, but we can't access it directly
    // Instead, we test that GitExecutor can be created without panicking
    let executor = GitExecutor::new();
    let _executor = executor; // Just verify it was created successfully
}

#[test]
fn test_redis_url_from_environment() {
    // Test REDIS_URL environment variable
    std::env::set_var("REDIS_URL", "redis://test:6379/1");

    // Create executor - should use the environment variable
    let executor = GitExecutor::new();
    let _executor = executor; // Just verify it was created successfully

    // Clean up
    std::env::remove_var("REDIS_URL");
}

#[test]
fn test_sal_redis_url_from_environment() {
    // Test SAL_REDIS_URL environment variable (fallback)
    std::env::remove_var("REDIS_URL");
    std::env::set_var("SAL_REDIS_URL", "redis://sal-test:6379/2");

    // Create executor - should use the SAL_REDIS_URL
    let executor = GitExecutor::new();
    let _executor = executor; // Just verify it was created successfully

    // Clean up
    std::env::remove_var("SAL_REDIS_URL");
}