//! Thermal throttling detection tests

use super::throttle::{
    ThermalPlatform, ThermalSeverity, detect_thermal_state, is_thermal_detection_supported,
    is_thermal_throttling,
};

#[test]
fn test_detect_thermal_state() {
    // Test that thermal detection runs without error on supported platforms
    if is_thermal_detection_supported() {
        let result = detect_thermal_state();
        assert!(
            result.is_ok(),
            "Thermal detection should not error on supported platforms"
        );

        if let Ok(Some(state)) = result {
            // Verify the thermal state structure is valid
            assert!(
                state.temperature_celsius.is_none() || state.temperature_celsius.unwrap() >= 0.0,
                "Temperature should be non-negative if present"
            );

            // Verify severity is within expected range
            match state.severity {
                ThermalSeverity::Normal
                | ThermalSeverity::Light
                | ThermalSeverity::Moderate
                | ThermalSeverity::Critical => {
                    // All valid severities
                }
            }

            // Verify platform is set
            match state.platform {
                ThermalPlatform::Linux | ThermalPlatform::MacOS | ThermalPlatform::Unknown => {
                    // Valid platforms
                }
            }

            // Verify zones count is reasonable if present
            if let Some(zones) = state.zones_checked {
                assert!(zones > 0, "Should check at least one thermal zone");
            }
        }
    } else {
        // On unsupported platforms, should return Ok(None)
        let result = detect_thermal_state();
        assert!(
            result.is_ok(),
            "Should not error even on unsupported platforms"
        );
        assert!(
            result.unwrap().is_none(),
            "Should return None on unsupported platforms"
        );
    }
}

#[test]
fn test_is_thermal_throttling() {
    // Test the simplified throttling detection
    if is_thermal_detection_supported() {
        let result = is_thermal_throttling();
        assert!(
            result.is_ok(),
            "Throttling detection should not error on supported platforms"
        );

        if let Ok(Some(is_throttling)) = result {
            // Should return a boolean value
            assert!(
                is_throttling == true || is_throttling == false,
                "Should return a valid boolean"
            );
        }
    } else {
        // On unsupported platforms, should return Ok(None)
        let result = is_thermal_throttling();
        assert!(
            result.is_ok(),
            "Should not error even on unsupported platforms"
        );
        assert!(
            result.unwrap().is_none(),
            "Should return None on unsupported platforms"
        );
    }
}

#[test]
fn test_is_thermal_detection_supported() {
    // Test platform detection
    let supported = is_thermal_detection_supported();

    // Should be a boolean
    assert!(
        supported == true || supported == false,
        "Should return a valid boolean"
    );

    // Should be consistent with platform
    #[cfg(any(target_os = "linux", target_os = "macos"))]
    assert!(supported, "Should be supported on Linux and macOS");

    #[cfg(not(any(target_os = "linux", target_os = "macos")))]
    assert!(!supported, "Should not be supported on other platforms");
}

#[test]
fn test_thermal_severity_ordering() {
    // Test that severity levels are properly ordered
    assert!(ThermalSeverity::Normal < ThermalSeverity::Light);
    assert!(ThermalSeverity::Light < ThermalSeverity::Moderate);
    assert!(ThermalSeverity::Moderate < ThermalSeverity::Critical);
    assert!(ThermalSeverity::Normal < ThermalSeverity::Critical);
}

#[test]
fn test_thermal_state_consistency() {
    // Test that thermal state fields are consistent
    if is_thermal_detection_supported() {
        if let Ok(Some(state)) = detect_thermal_state() {
            // If throttling is detected, severity should be at least Moderate
            if state.is_throttling {
                assert!(
                    state.severity >= ThermalSeverity::Moderate,
                    "Throttling should imply at least Moderate severity"
                );
            }

            // If severity is Critical, throttling should be detected
            if state.severity == ThermalSeverity::Critical {
                assert!(
                    state.is_throttling,
                    "Critical severity should imply throttling"
                );
            }

            // Trip point should only be present if temperature is present
            if state.trip_point_celsius.is_some() {
                assert!(
                    state.temperature_celsius.is_some(),
                    "Trip point should only be present with temperature"
                );
            }

            // Throttled zones should only be present if throttling is detected
            if state.throttled_zones.is_some() {
                assert!(
                    state.is_throttling,
                    "Throttled zones should only be present when throttling"
                );
            }
        }
    }
}
