#![cfg(feature = "cls")]
mod common;
use common::{at, day, dso};
use flexibility::cls::{self, wire};
use flexibility::jiff::Timestamp;
use flexibility::{Envelope, Error, Guarantee, Limit, Provenance, Window, kw, w};
use proptest::prelude::*;
use wire::wire::Control;
fn control(json: &str) -> Control {
serde_json::from_str(json).unwrap()
}
fn horizon() -> Timestamp {
at(23, 0)
}
fn limits(c: &Control) -> Result<Vec<Limit>, Error> {
cls::limits(c, at(17, 0), horizon(), &dso())
}
#[test]
fn consumption_and_production_limits_keep_their_sign() {
let c = control(
r#"{"limits": {"power": {"active": {"consumption": {"value": 4200, "active": true, "duration": 3600}}}}}"#,
);
let caps = limits(&c).unwrap();
assert_eq!(caps[0].bounds().max(), Some(kw!(4.2)));
assert_eq!(caps[0].window(), Window::new(at(17, 0), at(18, 0)).unwrap());
let p = control(
r#"{"limits": {"power": {"active": {"production": {"value": 3000, "active": true}}}}}"#,
);
let caps = limits(&p).unwrap();
assert_eq!(caps[0].bounds().min(), Some(kw!(-3)));
assert_eq!(caps[0].window().until(), horizon());
}
#[test]
fn inactive_limits_set_nothing_and_invalid_ones_are_refused() {
let off = control(
r#"{"limits": {"power": {"active": {"consumption": {"value": 0, "active": false}}}}}"#,
);
assert_eq!(limits(&off).unwrap(), []);
for bad in [
r#"{"limits": {"power": {"active": {"consumption": {"value": 0, "active": true}}}}}"#,
r#"{"limits": {"power": {"active": {"consumption": {"value": -1, "active": true}}}}}"#,
r#"{"limits": {"power": {"active": {"consumption": {"value": 4200, "active": true, "duration": 0}}}}}"#,
] {
assert!(
matches!(
limits(&control(bad)),
Err(Error::Unrepresentable {
protocol: "CLS.EEDI",
..
})
),
"{bad}"
);
}
assert_eq!(limits(&control("{}")).unwrap(), []);
}
#[test]
fn failsafes_hold_over_the_callers_window() {
let c = control(r#"{"failsafes": {"power": {"active": {"consumption": 4200}}}}"#);
let window = Window::new(at(17, 0), at(19, 0)).unwrap();
let caps = cls::failsafe(&c, window, &dso()).unwrap();
assert_eq!(caps[0].bounds().max(), Some(kw!(4.2)));
assert_eq!(caps[0].window(), window);
}
#[test]
fn a_cap_below_the_14a_minimum_is_refused_by_the_envelope() {
let c = control(
r#"{"limits": {"power": {"active": {"consumption": {"value": 3000, "active": true, "duration": 3600}}}}}"#,
);
let all = Window::new(day().start(), day().end()).unwrap();
let bnetza = Provenance::new("bnetza.de", "BK6-22-300 Anlage 1 Ziff. 4.5", at(0, 0));
let guarded = Envelope::unbounded(day())
.guarantee(&Guarantee::consumption(all, w!(4200), bnetza).unwrap())
.unwrap();
let err = guarded.apply(&limits(&c).unwrap()[0]).unwrap_err();
assert!(matches!(err, Error::GuaranteeBreached(_)), "{err}");
}
#[test]
fn what_cls_cannot_carry_is_refused() {
let both = flexibility::Limit::band(
Window::new(at(17, 0), at(18, 0)).unwrap(),
kw!(-1),
kw!(5),
dso(),
)
.unwrap();
assert!(cls::control(&both, at(17, 0)).is_err());
let later = Limit::consumption_cap(Window::new(at(18, 0), at(19, 0)).unwrap(), kw!(5), dso());
assert!(cls::control(&later, at(17, 0)).is_err());
let zero = Limit::consumption_cap(Window::new(at(17, 0), at(18, 0)).unwrap(), w!(0), dso());
assert!(cls::control(&zero, at(17, 0)).is_err());
}
proptest! {
#[test]
fn a_cap_round_trips(watts in 1i64..=1_000_000, feed_in in any::<bool>(), minutes in 1i64..=600) {
let from = at(17, 0);
let until = from.checked_add(flexibility::jiff::SignedDuration::from_mins(minutes)).unwrap();
let window = Window::new(from, until).unwrap();
let p = flexibility::Power::from_watts(watts);
let ours = if feed_in {
Limit::feed_in_cap(window, p, dso()).unwrap()
} else {
Limit::consumption_cap(window, p, dso())
};
let c = cls::control(&ours, from).unwrap();
let back = cls::limits(&c, from, horizon(), &dso()).unwrap();
prop_assert_eq!(back, vec![ours]);
}
}