sal-git 0.1.0

SAL Git - Git repository management and operations
Documentation
use rhai::Engine;
use sal_git::rhai::*;

#[test]
fn test_register_git_module() {
    let mut engine = Engine::new();
    let result = register_git_module(&mut engine);
    assert!(result.is_ok());
}

#[test]
fn test_git_tree_new_function_registered() {
    let mut engine = Engine::new();
    register_git_module(&mut engine).unwrap();

    // Test that the function is registered by trying to call it
    // This will fail because /nonexistent doesn't exist, but it proves the function is registered
    let result = engine.eval::<String>(
        r#"
        let result = "";
        try {
            let git_tree = git_tree_new("/nonexistent");
            result = "success";
        } catch(e) {
            result = "error_caught";
        }
        result
    "#,
    );

    assert!(result.is_ok());
    assert_eq!(result.unwrap(), "error_caught");
}

#[test]
fn test_git_clone_function_registered() {
    let mut engine = Engine::new();
    register_git_module(&mut engine).unwrap();

    // Test that git_clone function is registered by testing with invalid URL
    let result = engine.eval::<String>(
        r#"
        let result = "";
        try {
            git_clone("invalid-url-format");
            result = "unexpected_success";
        } catch(e) {
            // Should catch error for invalid URL
            if e.contains("Git error") {
                result = "error_caught_correctly";
            } else {
                result = "wrong_error_type";
            }
        }
        result
    "#,
    );

    assert!(result.is_ok());
    assert_eq!(result.unwrap(), "error_caught_correctly");
}

#[test]
fn test_git_clone_with_valid_public_repo() {
    let mut engine = Engine::new();
    register_git_module(&mut engine).unwrap();

    // Test with a real public repository (small one for testing)
    let result = engine.eval::<String>(
        r#"
        let result = "";
        try {
            let repo = git_clone("https://github.com/octocat/Hello-World.git");
            // If successful, repo should have a valid path
            let path = repo.path();
            if path.len() > 0 {
                result = "clone_successful";
            } else {
                result = "clone_failed_no_path";
            }
        } catch(e) {
            // Network issues or git not available are acceptable failures
            if e.contains("Git error") || e.contains("command") {
                result = "acceptable_failure";
            } else {
                result = "unexpected_error";
            }
        }
        result
    "#,
    );

    assert!(result.is_ok());
    let outcome = result.unwrap();
    // Accept either successful clone or acceptable failure (network/git issues)
    assert!(
        outcome == "clone_successful" || outcome == "acceptable_failure",
        "Unexpected outcome: {}",
        outcome
    );
}