use crate::cloudhv::errors::*;
use std::io;

#[test]
fn test_io_error_conversion() {
    let io_err = io::Error::new(io::ErrorKind::NotFound, "file not found");
    let ch_err: CloudHypervisorError = io_err.into();

    match ch_err {
        CloudHypervisorError::Io(_) => {}
        _ => panic!("Expected Io error"),
    }
}

#[test]
fn test_api_error() {
    let err = CloudHypervisorError::Api("API call failed".to_string());
    assert_eq!(err.to_string(), "API error: API call failed");
}

#[test]
fn test_process_error() {
    let err = CloudHypervisorError::Process("Process failed to start".to_string());
    assert_eq!(err.to_string(), "Process error: Process failed to start");
}

#[test]
fn test_config_error() {
    let err = CloudHypervisorError::Config("Invalid configuration".to_string());
    assert_eq!(err.to_string(), "Configuration error: Invalid configuration");
}

#[test]
fn test_vm_not_found_error() {
    let err = CloudHypervisorError::VmNotFound("vm123".to_string());
    assert_eq!(err.to_string(), "VM not found: vm123");
}

#[test]
fn test_vm_already_exists_error() {
    let err = CloudHypervisorError::VmAlreadyExists("vm123".to_string());
    assert_eq!(err.to_string(), "VM already exists: vm123");
}

#[test]
fn test_invalid_state_error() {
    let err = CloudHypervisorError::InvalidState("Cannot pause running VM".to_string());
    assert_eq!(err.to_string(), "Invalid state: Cannot pause running VM");
}

#[test]
fn test_device_error() {
    let err = CloudHypervisorError::Device("Device not found".to_string());
    assert_eq!(err.to_string(), "Device error: Device not found");
}

#[test]
fn test_snapshot_error() {
    let err = CloudHypervisorError::Snapshot("Snapshot creation failed".to_string());
    assert_eq!(err.to_string(), "Snapshot error: Snapshot creation failed");
}

#[test]
fn test_script_error() {
    let err = CloudHypervisorError::Script("Script execution failed".to_string());
    assert_eq!(err.to_string(), "Script error: Script execution failed");
}

#[test]
fn test_timeout_error() {
    let err = CloudHypervisorError::Timeout("Operation timed out".to_string());
    assert_eq!(err.to_string(), "Timeout error: Operation timed out");
}

#[test]
fn test_resource_error() {
    let err = CloudHypervisorError::Resource("Out of memory".to_string());
    assert_eq!(err.to_string(), "Resource error: Out of memory");
}

#[test]
fn test_validation_error() {
    let err = CloudHypervisorError::Validation("Invalid input".to_string());
    assert_eq!(err.to_string(), "Validation error: Invalid input");
}

#[test]
fn test_json_error_conversion() {
    let json_str = "{invalid json";
    let result: std::result::Result<serde_json::Value, _> = serde_json::from_str(json_str);
    
    if let Err(json_err) = result {
        let ch_err: CloudHypervisorError = json_err.into();
        match ch_err {
            CloudHypervisorError::Json(_) => {}
            _ => panic!("Expected Json error"),
        }
    }
}

#[test]
fn test_error_debug_format() {
    let err = CloudHypervisorError::Api("test error".to_string());
    let debug_str = format!("{:?}", err);
    assert!(debug_str.contains("Api"));
    assert!(debug_str.contains("test error"));
}

#[test]
fn test_result_type_alias() {
    fn returns_result() -> Result<i32> {
        Ok(42)
    }

    let result = returns_result();
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), 42);
}

#[test]
fn test_result_type_alias_error() {
    fn returns_error() -> Result<i32> {
        Err(CloudHypervisorError::Api("error".to_string()))
    }

    let result = returns_error();
    assert!(result.is_err());
}
