#![cfg(feature = "eebus")]
mod common;
use std::time::Duration;
use common::{at, day, dso};
use flexibility::eebus::ScaledNumber;
use flexibility::eebus::{self, Direction, LimitWrite};
use flexibility::jiff::Timestamp;
use flexibility::{Envelope, Error, Guarantee, Limit, Power, Rounding, Window, kw, w};
use proptest::prelude::*;
const TWO_HOURS: Duration = Duration::from_secs(7_200);
fn horizon() -> Timestamp {
day().end()
}
#[test]
fn an_lpc_limit_is_a_consumption_cap_for_its_duration() {
let write = LimitWrite::active_for(4_200, TWO_HOURS);
let cap = eebus::limit(
Direction::Consumption,
&write,
at(17, 0),
horizon(),
Rounding::Exact,
dso(),
)
.unwrap()
.unwrap();
assert_eq!(cap.bounds().max(), Some(kw!(4.2)));
assert_eq!(cap.bounds().min(), None);
assert_eq!(cap.window(), Window::new(at(17, 0), at(19, 0)).unwrap());
assert_eq!(cap.provenance(), &dso());
}
#[test]
fn an_lpp_limit_is_a_feed_in_cap() {
let write = LimitWrite::active(3_000);
let cap = eebus::limit(
Direction::Production,
&write,
at(12, 0),
horizon(),
Rounding::Exact,
dso(),
)
.unwrap()
.unwrap();
assert_eq!(cap.bounds().min(), Some(kw!(-3)));
assert_eq!(cap.bounds().max(), None);
assert_eq!(
cap.window().until(),
horizon(),
"no duration: the caller's horizon"
);
}
#[test]
fn a_deactivated_write_sets_no_limit() {
assert_eq!(
eebus::limit(
Direction::Consumption,
&LimitWrite::deactivated(),
at(17, 0),
horizon(),
Rounding::Exact,
dso()
)
.unwrap(),
None
);
}
#[test]
fn fractions_and_negative_values_are_refused() {
for value in [ScaledNumber::new(42_005, -1), ScaledNumber::whole(-1)] {
let write = LimitWrite::active(value);
let err = eebus::limit(
Direction::Consumption,
&write,
at(17, 0),
horizon(),
Rounding::Exact,
dso(),
)
.unwrap_err();
assert!(
matches!(
err,
Error::Unrepresentable {
protocol: "EEBUS",
field: "limit",
..
}
),
"{err}"
);
}
assert!(err_text(ScaledNumber::new(42_005, -1)).contains("4200.5"));
let milli = LimitWrite::active(ScaledNumber::new(4_200_000, -3));
let cap = eebus::limit(
Direction::Consumption,
&milli,
at(17, 0),
horizon(),
Rounding::Exact,
dso(),
)
.unwrap()
.unwrap();
assert_eq!(cap.bounds().max(), Some(w!(4200)));
}
fn err_text(value: ScaledNumber) -> String {
eebus::limit(
Direction::Consumption,
&LimitWrite::active(value),
at(17, 0),
horizon(),
Rounding::Exact,
dso(),
)
.unwrap_err()
.to_string()
}
#[test]
fn a_duration_past_the_time_range_is_refused() {
let write = LimitWrite::active_for(1, Duration::MAX);
assert!(matches!(
eebus::limit(
Direction::Consumption,
&write,
at(17, 0),
horizon(),
Rounding::Exact,
dso()
),
Err(Error::Unrepresentable {
field: "duration",
..
})
));
}
#[test]
fn a_failsafe_below_the_guarantee_is_refused() {
let g = day();
let all = Window::new(g.start(), g.end()).unwrap();
let rule = flexibility::Provenance::new("bnetza.de", "§ 14a minimum", Timestamp::UNIX_EPOCH);
let site = Envelope::unbounded(g)
.guarantee(&Guarantee::consumption(all, kw!(4.2), rule).unwrap())
.unwrap();
let fallback = Window::new(at(17, 0), at(19, 0)).unwrap();
let ok = eebus::failsafe(
Direction::Consumption,
4_200,
fallback,
Rounding::Exact,
dso(),
)
.unwrap();
assert!(site.apply(&ok).is_ok());
let low = eebus::failsafe(
Direction::Consumption,
3_000,
fallback,
Rounding::Exact,
dso(),
)
.unwrap();
assert!(matches!(site.apply(&low), Err(Error::GuaranteeBreached(_))));
}
#[test]
fn a_limit_is_written_with_the_duration_left() {
let cap = Limit::consumption_cap(Window::new(at(17, 0), at(19, 0)).unwrap(), kw!(4.2), dso());
let (direction, write) = eebus::write(&cap, at(17, 30)).unwrap();
assert_eq!(direction, Direction::Consumption);
assert!(write.is_active);
assert_eq!(write.value.to_i64(), Some(4_200));
assert_eq!(write.duration, Some(Duration::from_secs(90 * 60)));
let feed_in =
Limit::feed_in_cap(Window::new(at(12, 0), at(13, 0)).unwrap(), kw!(3), dso()).unwrap();
let (direction, write) = eebus::write(&feed_in, at(12, 0)).unwrap();
assert_eq!(
(direction, write.value.to_i64()),
(Direction::Production, Some(3_000))
);
}
#[test]
fn what_eebus_cannot_carry_is_refused() {
let evening = Window::new(at(17, 0), at(19, 0)).unwrap();
let band = Limit::new(
evening,
flexibility::Bounds::new(Some(kw!(-1)), Some(kw!(1))).unwrap(),
dso(),
);
let below_zero = Limit::consumption_cap(evening, kw!(-1), dso());
let cap = Limit::consumption_cap(evening, kw!(4.2), dso());
for (limit, when) in [
(&band, at(17, 0)),
(&below_zero, at(17, 0)),
(&cap, at(19, 0)),
(&cap, at(16, 0)),
] {
assert!(
matches!(
eebus::write(limit, when),
Err(Error::Unrepresentable {
protocol: "EEBUS",
..
})
),
"{:?} at {when}",
limit.bounds()
);
}
}
#[test]
fn a_limit_written_open_ended_has_no_duration_and_reads_back_to_the_horizon() {
let cap = Limit::consumption_cap(Window::new(at(16, 0), at(19, 0)).unwrap(), kw!(4.2), dso());
let (direction, write) = eebus::write_open(&cap, at(16, 0)).unwrap();
assert_eq!(direction, Direction::Consumption);
assert_eq!(write, LimitWrite::active(4_200));
assert_eq!(write.duration, None);
let back = eebus::limit(
direction,
&write,
at(16, 0),
horizon(),
Rounding::Exact,
dso(),
)
.unwrap()
.unwrap();
assert_eq!(back.bounds(), cap.bounds());
assert_eq!(back.window(), Window::new(at(16, 0), horizon()).unwrap());
let feed_in =
Limit::feed_in_cap(Window::new(at(12, 0), at(13, 0)).unwrap(), kw!(3), dso()).unwrap();
assert_eq!(
eebus::write_open(&feed_in, at(12, 30)).unwrap(),
(Direction::Production, LimitWrite::active(3_000))
);
}
#[test]
fn an_open_ended_write_refuses_what_a_timed_write_refuses() {
let evening = Window::new(at(17, 0), at(19, 0)).unwrap();
let band = Limit::new(
evening,
flexibility::Bounds::new(Some(kw!(-1)), Some(kw!(1))).unwrap(),
dso(),
);
let below_zero = Limit::consumption_cap(evening, kw!(-1), dso());
let cap = Limit::consumption_cap(evening, kw!(4.2), dso());
for (limit, when) in [
(&band, at(17, 0)),
(&below_zero, at(17, 0)),
(&cap, at(19, 0)),
(&cap, at(16, 0)),
] {
assert_eq!(
eebus::write_open(limit, when).unwrap_err(),
eebus::write(limit, when).unwrap_err()
);
}
}
fn read(direction: Direction, value: ScaledNumber, rounding: Rounding) -> Result<Limit, Error> {
eebus::limit(
direction,
&LimitWrite::active(value),
at(17, 0),
horizon(),
rounding,
dso(),
)
.map(Option::unwrap)
}
#[test]
fn an_acknowledged_fraction_reads_as_the_rounding_names() {
let ack = ScaledNumber::new(42_005, -1);
let lpc = |r| read(Direction::Consumption, ack, r).map(|l| l.bounds().max());
assert_eq!(lpc(Rounding::TowardZero).unwrap(), Some(w!(4200)));
assert_eq!(lpc(Rounding::HalfAwayFromZero).unwrap(), Some(w!(4201)));
assert!(
lpc(Rounding::Exact)
.unwrap_err()
.to_string()
.contains("4200.5")
);
let lpp = read(Direction::Production, ack, Rounding::TowardZero).unwrap();
assert_eq!(lpp.bounds().min(), Some(w!(-4200)));
let fallback = Window::new(at(17, 0), at(19, 0)).unwrap();
let failsafe = |r| eebus::failsafe(Direction::Consumption, ack, fallback, r, dso());
assert_eq!(
failsafe(Rounding::TowardZero).unwrap().bounds().max(),
Some(w!(4200))
);
assert!(failsafe(Rounding::Exact).is_err());
}
#[test]
fn a_whole_value_reads_the_same_under_every_rounding() {
for rounding in [
Rounding::Exact,
Rounding::TowardZero,
Rounding::HalfAwayFromZero,
] {
let cap = read(Direction::Consumption, ScaledNumber::new(42, 2), rounding).unwrap();
assert_eq!(cap.bounds().max(), Some(w!(4200)));
}
}
#[test]
fn extreme_scales_round_to_zero_or_are_refused() {
let tiny = ScaledNumber::new(5, i16::MIN);
assert_eq!(
read(Direction::Consumption, tiny, Rounding::TowardZero)
.unwrap()
.bounds()
.max(),
Some(Power::ZERO)
);
assert!(read(Direction::Consumption, tiny, Rounding::Exact).is_err());
for huge in [
ScaledNumber::new(1, 19),
ScaledNumber::new(i64::MAX, i16::MAX),
] {
for rounding in [
Rounding::Exact,
Rounding::TowardZero,
Rounding::HalfAwayFromZero,
] {
let err = read(Direction::Consumption, huge, rounding).unwrap_err();
assert!(err.to_string().contains("range"), "{err}");
}
}
for rounding in [Rounding::TowardZero, Rounding::HalfAwayFromZero] {
assert!(read(Direction::Consumption, ScaledNumber::new(-1, -1), rounding).is_err());
assert_eq!(
eebus::limit(
Direction::Consumption,
&LimitWrite::deactivated(),
at(17, 0),
horizon(),
rounding,
dso()
)
.unwrap(),
None
);
}
}
proptest! {
#[test]
fn every_representable_limit_round_trips(watts in 0i64..=10_000_000, minutes in 1i64..=1_440, lpp: bool) {
let from = at(0, 0);
let window = Window::new(from, from + flexibility::jiff::SignedDuration::from_mins(minutes)).unwrap();
let p = Power::from_watts(watts);
let limit = if lpp {
Limit::feed_in_cap(window, p, dso()).unwrap()
} else {
Limit::consumption_cap(window, p, dso())
};
let (direction, write) = eebus::write(&limit, from).unwrap();
let back = eebus::limit(direction, &write, from, horizon(), Rounding::Exact, dso()).unwrap().unwrap();
prop_assert_eq!(back, limit);
}
#[test]
fn toward_zero_never_loosens(number in 0i64..=i64::MAX / 1_000, scale in -6i16..=2, lpp: bool) {
let direction = if lpp { Direction::Production } else { Direction::Consumption };
let value = ScaledNumber::new(number, scale);
let cap = read(direction, value, Rounding::TowardZero).unwrap();
let bound = if lpp { cap.bounds().min() } else { cap.bounds().max() };
let magnitude = i128::from(bound.unwrap().watts().abs());
let value_uw = i128::from(number) * 10_i128.pow(u32::try_from(6 + scale).unwrap());
prop_assert!(magnitude * 1_000_000 <= value_uw && value_uw < (magnitude + 1) * 1_000_000);
prop_assert_eq!(
read(direction, value, Rounding::Exact).is_ok(),
magnitude * 1_000_000 == value_uw
);
}
}