use jiff::SignedDuration;
use rust_decimal_macros::dec;
use super::{Consumed, Period};
use crate::{Kwh, Version, json, price, tariff, test, warning};
#[test]
fn should_end_each_period_where_the_next_one_starts_when_given_out_of_order() {
const TARIFF_JSON: &str = include_str!(
"../../test_data/v211/real_world/time_and_parking_time_separate_tariff/tariff.json"
);
test::setup();
let (checked, warnings) =
tariff::check(json::parse_object(TARIFF_JSON).unwrap(), Version::V211).into_parts();
test::assert_schema_warnings(&warnings, &[]);
let (tariff, warnings) = tariff::build(checked).unwrap().into_parts();
assert!(warnings.is_empty(), "{:#?}", warnings.path_id_map());
let first = test::datetime("2025-04-09", "16:00");
let second = test::datetime("2025-04-09", "16:15");
let third = test::datetime("2025-04-09", "16:30");
let end = test::datetime("2025-04-09", "16:45");
let periods = [third, second, first]
.into_iter()
.map(|start_date_time| Period {
start_date_time,
consumed: Consumed {
duration_charging: Some(SignedDuration::from_mins(15)),
energy: Some(Kwh::from(dec!(2))),
..Default::default()
},
})
.collect();
let mut price_warnings = warning::Gatherer::new();
let report = price::periods(
end,
&jiff::tz::get!("Europe/Amsterdam"),
tariff.as_v221(),
periods,
tariff.as_element(),
&mut price_warnings,
);
let (report, warnings) = price_warnings.into_verdict(report).unwrap().into_parts();
assert!(warnings.is_empty(), "{:#?}", warnings.path_id_map());
let boundaries = report
.periods
.iter()
.map(|period| (period.start_date_time, period.end_date_time))
.collect::<Vec<_>>();
assert_eq!(boundaries, [(first, second), (second, third), (third, end)]);
}