use chrono::TimeDelta;

use super::power_to_time;
use crate::Kwh;

#[test]
fn should_min() {
    let dt = power_to_time(Kwh::from(0), Kwh::from(10), TimeDelta::minutes(100)).unwrap();
    assert_eq!(dt, TimeDelta::minutes(0));
}

#[test]
fn should_max() {
    let dt = power_to_time(Kwh::from(10), Kwh::from(10), TimeDelta::minutes(100)).unwrap();
    assert_eq!(dt, TimeDelta::minutes(100));
}

#[test]
fn should_mid() {
    let dt = power_to_time(Kwh::from(5), Kwh::from(10), TimeDelta::minutes(60)).unwrap();
    assert_eq!(dt, TimeDelta::minutes(30));
}

#[test]
fn should_round_repeating_decimal_to_exact_nanoseconds() {
    // 5/10 * 100 minutes = 50 minutes exactly. The intermediate Decimal value (5/10 * 100/60 hours)
    // is a repeating decimal; `to_duration` must not add a phantom nanosecond.
    let dt = power_to_time(Kwh::from(5), Kwh::from(10), TimeDelta::minutes(100)).unwrap();
    assert_eq!(dt, TimeDelta::minutes(50));
}

#[test]
fn should_return_none_when_exceeded() {
    let dt = power_to_time(Kwh::from(11), Kwh::from(10), TimeDelta::minutes(100));
    assert_eq!(dt, None);
}

#[test]
fn should_handle_zero() {
    let dt = power_to_time(Kwh::from(0), Kwh::from(10), TimeDelta::minutes(100)).unwrap();
    assert_eq!(dt, TimeDelta::minutes(0));
}

#[test]
fn should_handle_negative_used() {
    let dt = power_to_time(Kwh::from(-10.0), Kwh::from(10), TimeDelta::minutes(100));
    assert_eq!(dt, None);
}

#[test]
fn should_handle_negative_total() {
    let dt = power_to_time(Kwh::from(10.0), Kwh::from(-10.0), TimeDelta::minutes(100));
    assert_eq!(dt, None);
}
