mod common;
use common::{agr, day, day_of, win};
use flexibility::MeasurementPoint;
use flexibility::baseline::{self, Adjustment, Mbma, XofY};
use flexibility::jiff::civil::date;
use flexibility::{Baseline, Deviation, MethodId, Power, Schedule, Series, settle};
fn varied() -> Series<Power> {
let values = (1..=96i64)
.map(|isp| Power::from_watts(3_000 + (isp % 7) * 137 + if isp > 60 { isp * 11 } else { 0 }))
.collect();
Series::new(day(), values).unwrap()
}
fn history() -> Vec<Series<Power>> {
(21..=30)
.map(|d| {
let g = day_of(date(2026, 11, d));
let values = (1..=96i64)
.map(|isp| Power::from_watts(2_000 + i64::from(d) * 101 + (isp % 5) * 53))
.collect();
Series::new(g, values).unwrap()
})
.collect()
}
fn baseline_for(method: MethodId) -> Baseline {
let evening = win(17, 0, 19, 0);
match method {
MethodId::DPrognosis1 => baseline::dprognosis(&Schedule::new(varied(), agr()), evening),
MethodId::Schedule1 => baseline::schedule(&Schedule::new(varied(), agr()), evening),
MethodId::Mbma1 => baseline::mbma(&varied(), evening, Mbma { n: 4, gap: 1 }),
MethodId::XofY1 => baseline::xofy(
&day(),
evening,
&history(),
Some(&varied()),
XofY {
x: 5,
y: 10,
adjustment: Adjustment::Ratio {
window: 4,
gap: 1,
cap_percent: 20,
},
},
),
_ => unreachable!("a new method id needs a snapshot"),
}
.unwrap()
}
fn check(name: &str, actual: &str) {
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests/snapshots")
.join(format!("{name}.json"));
match std::fs::read_to_string(&path) {
Ok(expected) => assert_eq!(
actual,
expected.trim_end(),
"{name} changed: a changed output is a new method id, not a new snapshot"
),
Err(_) if std::env::var_os("FLEXIBILITY_WRITE_SNAPSHOTS").is_some() => {
std::fs::write(&path, format!("{actual}\n")).unwrap();
}
Err(e) => panic!("missing snapshot {}: {e}", path.display()),
}
}
#[test]
fn every_method_id_is_pinned() {
for method in MethodId::ALL {
let b = baseline_for(method);
check(method.as_str(), &b.canonical());
}
}
#[test]
fn a_settlement_is_pinned() {
let b = baseline_for(MethodId::Mbma1);
let evening = win(17, 0, 19, 0);
let actual = varied().over(evening).unwrap();
let ordered = Series::filled(actual.grid().clone(), Deviation::from_watts(-1_500));
check(
"settlement-mbma@1",
&settle(&b, &actual, &ordered, MeasurementPoint::ConnectionPoint)
.unwrap()
.canonical(),
);
}