sal-os 0.1.2

SAL OS - Operating system interaction utilities with cross-platform abstraction
Documentation
use sal_os::platform;

#[test]
fn test_platform_detection_consistency() {
    // Test that platform detection functions return consistent results
    let is_osx = platform::is_osx();
    let is_linux = platform::is_linux();

    // On any given system, only one of these should be true
    // (or both false if running on Windows or other OS)
    if is_osx {
        assert!(!is_linux, "Cannot be both macOS and Linux");
    }
    if is_linux {
        assert!(!is_osx, "Cannot be both Linux and macOS");
    }
}

#[test]
fn test_architecture_detection_consistency() {
    // Test that architecture detection functions return consistent results
    let is_arm = platform::is_arm();
    let is_x86 = platform::is_x86();

    // On any given system, only one of these should be true
    // (or both false if running on other architectures)
    if is_arm {
        assert!(!is_x86, "Cannot be both ARM and x86");
    }
    if is_x86 {
        assert!(!is_arm, "Cannot be both x86 and ARM");
    }
}

#[test]
fn test_platform_functions_return_bool() {
    // Test that all platform detection functions return boolean values
    let _: bool = platform::is_osx();
    let _: bool = platform::is_linux();
    let _: bool = platform::is_arm();
    let _: bool = platform::is_x86();
}

#[cfg(target_os = "macos")]
#[test]
fn test_macos_detection() {
    // When compiled for macOS, is_osx should return true
    assert!(platform::is_osx());
    assert!(!platform::is_linux());
}

#[cfg(target_os = "linux")]
#[test]
fn test_linux_detection() {
    // When compiled for Linux, is_linux should return true
    assert!(platform::is_linux());
    assert!(!platform::is_osx());
}

#[cfg(target_arch = "aarch64")]
#[test]
fn test_arm_detection() {
    // When compiled for ARM64, is_arm should return true
    assert!(platform::is_arm());
    assert!(!platform::is_x86());
}

#[cfg(target_arch = "x86_64")]
#[test]
fn test_x86_detection() {
    // When compiled for x86_64, is_x86 should return true
    assert!(platform::is_x86());
    assert!(!platform::is_arm());
}

#[test]
fn test_check_linux_x86() {
    let result = platform::check_linux_x86();

    // The result should depend on the current platform
    #[cfg(all(target_os = "linux", target_arch = "x86_64"))]
    {
        assert!(result.is_ok(), "Should succeed on Linux x86_64");
    }

    #[cfg(not(all(target_os = "linux", target_arch = "x86_64")))]
    {
        assert!(result.is_err(), "Should fail on non-Linux x86_64 platforms");

        // Check that the error message is meaningful
        let error = result.unwrap_err();
        let error_string = error.to_string();
        assert!(
            error_string.contains("Linux x86_64"),
            "Error message should mention Linux x86_64: {}",
            error_string
        );
    }
}

#[test]
fn test_check_macos_arm() {
    let result = platform::check_macos_arm();

    // The result should depend on the current platform
    #[cfg(all(target_os = "macos", target_arch = "aarch64"))]
    {
        assert!(result.is_ok(), "Should succeed on macOS ARM");
    }

    #[cfg(not(all(target_os = "macos", target_arch = "aarch64")))]
    {
        assert!(result.is_err(), "Should fail on non-macOS ARM platforms");

        // Check that the error message is meaningful
        let error = result.unwrap_err();
        let error_string = error.to_string();
        assert!(
            error_string.contains("macOS ARM"),
            "Error message should mention macOS ARM: {}",
            error_string
        );
    }
}

#[test]
fn test_platform_error_creation() {
    use sal_os::platform::PlatformError;

    // Test that we can create platform errors
    let error = PlatformError::new("Test Error", "This is a test error message");
    let error_string = error.to_string();

    assert!(error_string.contains("Test Error"));
    assert!(error_string.contains("This is a test error message"));
}

#[test]
fn test_platform_error_display() {
    use sal_os::platform::PlatformError;

    // Test error display formatting
    let error = PlatformError::Generic("Category".to_string(), "Message".to_string());
    let error_string = format!("{}", error);

    assert!(error_string.contains("Category"));
    assert!(error_string.contains("Message"));
}

#[test]
fn test_platform_error_debug() {
    use sal_os::platform::PlatformError;

    // Test error debug formatting
    let error = PlatformError::Generic("Category".to_string(), "Message".to_string());
    let debug_string = format!("{:?}", error);

    assert!(debug_string.contains("Generic"));
    assert!(debug_string.contains("Category"));
    assert!(debug_string.contains("Message"));
}

#[test]
fn test_platform_functions_are_deterministic() {
    // Platform detection should be deterministic - same result every time
    let osx1 = platform::is_osx();
    let osx2 = platform::is_osx();
    assert_eq!(osx1, osx2);

    let linux1 = platform::is_linux();
    let linux2 = platform::is_linux();
    assert_eq!(linux1, linux2);

    let arm1 = platform::is_arm();
    let arm2 = platform::is_arm();
    assert_eq!(arm1, arm2);

    let x86_1 = platform::is_x86();
    let x86_2 = platform::is_x86();
    assert_eq!(x86_1, x86_2);
}

#[test]
fn test_platform_check_functions_consistency() {
    // The check functions should be consistent with the individual detection functions
    let is_linux_x86 = platform::is_linux() && platform::is_x86();
    let check_linux_x86_result = platform::check_linux_x86().is_ok();
    assert_eq!(is_linux_x86, check_linux_x86_result);

    let is_macos_arm = platform::is_osx() && platform::is_arm();
    let check_macos_arm_result = platform::check_macos_arm().is_ok();
    assert_eq!(is_macos_arm, check_macos_arm_result);
}

#[test]
fn test_current_platform_info() {
    // Print current platform info for debugging (this will show in test output with --nocapture)
    println!("Current platform detection:");
    println!("  is_osx(): {}", platform::is_osx());
    println!("  is_linux(): {}", platform::is_linux());
    println!("  is_arm(): {}", platform::is_arm());
    println!("  is_x86(): {}", platform::is_x86());
    println!("  check_linux_x86(): {:?}", platform::check_linux_x86());
    println!("  check_macos_arm(): {:?}", platform::check_macos_arm());
}