mod common;
use common::{berlin, day, win};
use flexibility::baseline::{self, Mbma};
use flexibility::{
Deviation, Difference, Error, MeasurementPoint, Power, Series, Settlement, Window, kw, kwh,
settle,
};
use proptest::prelude::*;
const MBMA: Mbma = Mbma { n: 4, gap: 1 };
fn measured(during: Power, after: Power) -> Series<Power> {
let g = day();
Series::new(
g.clone(),
g.numbers()
.map(|isp| match isp {
69..=76 => during,
77..=80 => after,
_ => kw!(7),
})
.collect(),
)
.unwrap()
}
fn settled(m: &Series<Power>, order: Deviation, at: MeasurementPoint) -> Settlement {
let evening = win(17, 0, 19, 0);
let actual = m.over(evening).unwrap();
let ordered = Series::filled(actual.grid().clone(), order);
settle(
&baseline::mbma(m, evening, MBMA).unwrap(),
&actual,
&ordered,
at,
)
.unwrap()
}
fn through() -> Window {
win(17, 0, 20, 0)
}
#[test]
fn the_measurement_point_is_in_the_receipt_and_the_diff() {
let m = measured(kw!(4.5), kw!(7));
let fiscal = settled(
&m,
Deviation::from_watts(-3_000),
MeasurementPoint::ConnectionPoint,
);
let sub = settled(
&m,
Deviation::from_watts(-3_000),
MeasurementPoint::device("hp-1").unwrap(),
);
assert_ne!(fiscal.digest(), sub.digest());
let d = fiscal.diff(&sub).unwrap();
assert!(matches!(d, Difference::MeasurementPoint { .. }));
assert_eq!(
d.to_string(),
"measured at: ours connection point, theirs device hp-1"
);
let back = Settlement::from_canonical(&sub.canonical(), berlin()).unwrap();
assert_eq!(
back.measured_at(),
&MeasurementPoint::device("hp-1").unwrap()
);
assert!(
sub.canonical()
.contains("\"measured_at\":{\"device\":\"hp-1\"}")
);
}
#[test]
fn device_ids_are_checked() {
for bad in ["", "a\nb", &"x".repeat(65)] {
assert!(
matches!(MeasurementPoint::device(bad), Err(Error::DeviceId { .. })),
"{bad:?}"
);
}
assert!(MeasurementPoint::device(&"x".repeat(64)).is_ok());
}
#[test]
fn rebound_after_a_reduction_is_consumption_above_the_baseline() {
let m = measured(kw!(4.5), kw!(9));
let s = settled(
&m,
Deviation::from_watts(-3_000),
MeasurementPoint::ConnectionPoint,
);
let extended = baseline::mbma(&m, through(), MBMA).unwrap();
let r = s
.rebound(
&extended,
&m.over(win(19, 0, 20, 0)).unwrap(),
MeasurementPoint::ConnectionPoint,
)
.unwrap();
assert_eq!(r.lines().len(), 4);
assert!(r.lines().iter().all(|l| l.rebound == kw!(2)));
assert_eq!(r.total(), kwh!(2));
assert_eq!(r.settlement(), s.digest());
assert!(
r.canonical()
.contains(&format!("\"settlement\":\"{}\"", s.digest()))
);
assert!(
r.to_string()
.starts_with("ISP 77 (19:00–19:15 +01:00): baseline 7 kW, actual 9 kW, rebound 2 kW")
);
}
#[test]
fn continued_reduction_is_no_rebound() {
let m = measured(kw!(4.5), kw!(5));
let s = settled(
&m,
Deviation::from_watts(-3_000),
MeasurementPoint::ConnectionPoint,
);
let r = s
.rebound(
&baseline::mbma(&m, through(), MBMA).unwrap(),
&m.over(win(19, 0, 20, 0)).unwrap(),
MeasurementPoint::ConnectionPoint,
)
.unwrap();
assert_eq!(r.total(), kwh!(0));
}
#[test]
fn rebound_after_an_increase_is_consumption_below_the_baseline() {
let m = measured(kw!(10), kw!(6));
let s = settled(
&m,
Deviation::from_watts(3_000),
MeasurementPoint::ConnectionPoint,
);
let r = s
.rebound(
&baseline::mbma(&m, through(), MBMA).unwrap(),
&m.over(win(19, 0, 20, 0)).unwrap(),
MeasurementPoint::ConnectionPoint,
)
.unwrap();
assert!(r.lines().iter().all(|l| l.rebound == kw!(1)));
}
#[test]
fn rebound_is_anchored_to_its_settlement() {
let m = measured(kw!(4.5), kw!(9));
let s = settled(
&m,
Deviation::from_watts(-3_000),
MeasurementPoint::ConnectionPoint,
);
let ext = baseline::mbma(&m, through(), MBMA).unwrap();
let after = m.over(win(19, 0, 20, 0)).unwrap();
let at = MeasurementPoint::ConnectionPoint;
let late = m.over(win(19, 15, 20, 0)).unwrap();
assert!(matches!(
s.rebound(&ext, &late, at.clone()),
Err(Error::Mismatch {
field: "rebound window start",
..
})
));
assert!(matches!(
s.rebound(&ext, &after, MeasurementPoint::device("hp-1").unwrap()),
Err(Error::Mismatch {
field: "measured_at",
..
})
));
let other = baseline::mbma(
&measured(kw!(4.5), kw!(9)).map(|p| p.checked_add(kw!(1)).unwrap()),
through(),
MBMA,
)
.unwrap();
assert!(matches!(
s.rebound(&other, &after, at.clone()),
Err(Error::Mismatch {
field: "baseline during the order",
..
})
));
let declared = baseline::dprognosis(
&flexibility::Schedule::new(Series::filled(day(), kw!(7)), common::agr()),
through(),
)
.unwrap();
assert!(matches!(
s.rebound(&declared, &after, at.clone()),
Err(Error::Mismatch {
field: "method",
..
})
));
let none = settled(&m, Deviation::from_watts(0), at.clone());
assert!(matches!(
none.rebound(&ext, &after, at),
Err(Error::Method { .. })
));
}
proptest! {
#[test]
fn rebound_is_never_negative(after in -20_000i64..20_000, order in prop_oneof![-5_000i64..-1, 1i64..5_000]) {
let m = measured(kw!(4.5), Power::from_watts(after));
let s = settled(&m, Deviation::from_watts(order), MeasurementPoint::ConnectionPoint);
let r = s.rebound(&baseline::mbma(&m, through(), MBMA).unwrap(), &m.over(win(19, 0, 20, 0)).unwrap(), MeasurementPoint::ConnectionPoint).unwrap();
prop_assert!(r.lines().iter().all(|l| l.rebound >= Power::ZERO));
if after == 7_000 {
prop_assert_eq!(r.total(), kwh!(0));
}
}
}