use crate::{Language, Version, json, tariff};
const TARIFF_JSON: &str = include_str!("../tariff.json");
fn explain_v221(json: &str) -> String {
crate::test::setup();
let json = json::parse_object(json).unwrap();
let tariff = tariff::from_json(json, Version::V221).ignore_warnings();
let rendered = tariff::explain(&tariff, Language::EnUS)
.expect("the tariff should be explainable")
.ignore_warnings();
tracing::info!("\n{rendered}\n");
rendered
}
#[test]
fn explain_sample_tariff() {
crate::test::setup();
let json = json::parse_object(TARIFF_JSON).unwrap();
let version = tariff::infer_version(json);
let tariff = tariff::from_versioned_json(version.unwrap_certain()).ignore_warnings();
let rendered = tariff::explain(&tariff, Language::EnUS)
.expect("the sample tariff should be explainable")
.ignore_warnings();
tracing::info!("\n{rendered}\n");
assert!(rendered.contains("per kWh"), "{rendered}");
assert!(rendered.contains("**Charging time:**"), "{rendered}");
assert!(
rendered.contains("**Idle time (connected but not charging):**"),
"{rendered}"
);
assert!(!rendered.contains("illed in"), "{rendered}");
}
#[test]
fn explain_tiered_charging_time() {
let rendered = explain_v221(
r#"{
"id": "tiered", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "max_duration": 10800 },
"price_components": [{ "type": "TIME", "price": 0.50, "step_size": 1 }]
},
{
"price_components": [{ "type": "TIME", "price": 0.75, "step_size": 1 }]
}
]
}"#,
);
assert!(
rendered.contains("- For the first 3 hours after plugging in: €0.50 per hour"),
"{rendered}"
);
assert!(
rendered.contains("- For the remaining charging time: €0.75 per hour"),
"{rendered}"
);
}
#[test]
fn explain_energy_tiers_by_kwh() {
let rendered = explain_v221(
r#"{
"id": "energy-tiers", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "max_kwh": 20 },
"price_components": [{ "type": "ENERGY", "price": 0.30, "step_size": 1 }]
},
{
"price_components": [{ "type": "ENERGY", "price": 0.45, "step_size": 1 }]
}
]
}"#,
);
assert!(
rendered.contains("- For the first 20 kWh: €0.30 per kWh"),
"{rendered}"
);
assert!(
rendered.contains("- For the remaining energy: €0.45 per kWh"),
"{rendered}"
);
}
#[test]
fn explain_free_then_paid_idle_time() {
let rendered = explain_v221(
r#"{
"id": "free-idle", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "max_duration": 14400 },
"price_components": [{ "type": "PARKING_TIME", "price": 0, "step_size": 60 }]
},
{
"price_components": [{ "type": "PARKING_TIME", "price": 3.00, "step_size": 60 }]
}
]
}"#,
);
assert!(
rendered.contains("- For the first 4 hours after plugging in: free"),
"{rendered}"
);
assert!(
rendered.contains("- For the remaining idle time: €3.00 per hour"),
"{rendered}"
);
}
#[test]
fn explain_wrapping_night_window() {
let rendered = explain_v221(
r#"{
"id": "night", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "start_time": "23:00", "end_time": "07:00" },
"price_components": [{ "type": "ENERGY", "price": 0.20, "step_size": 1 }]
}
]
}"#,
);
assert!(
rendered.contains("between 23:00 and 07:00, €0.20 per kWh"),
"{rendered}"
);
}
#[test]
fn explain_free_tier_has_no_step_note() {
let rendered = explain_v221(
r#"{
"id": "free-step", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "max_duration": 3600 },
"price_components": [{ "type": "PARKING_TIME", "price": 0, "step_size": 60 }]
},
{
"price_components": [{ "type": "PARKING_TIME", "price": 5.00, "step_size": 300 }]
}
]
}"#,
);
assert!(
rendered.contains("- For the first 1 hour after plugging in: free"),
"{rendered}"
);
assert!(!rendered.contains("free (billed in"), "{rendered}");
assert!(
rendered.contains("(billed in steps of 5 minutes, rounded up)"),
"{rendered}"
);
}
#[test]
fn explain_equal_start_end_time_is_never() {
let rendered = explain_v221(
r#"{
"id": "never", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "start_time": "08:00", "end_time": "08:00" },
"price_components": [{ "type": "ENERGY", "price": 0.50, "step_size": 1 }]
}
]
}"#,
);
assert_eq!(
rendered,
"**Energy:** never applies (its window 08:00 to 08:00 is empty), €0.50 per kWh."
);
}
#[test]
fn explain_midnight_start_and_end_time_describes_the_element() {
let rendered = explain_v221(
r#"{
"id": "midnight-pair", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "start_time": "00:00", "end_time": "00:00" },
"price_components": [{ "type": "FLAT", "price": 1.00, "step_size": 1 }]
},
{
"price_components": [{ "type": "FLAT", "price": 2.50, "step_size": 1 }]
}
]
}"#,
);
assert!(
rendered.contains("- From 00:00 onwards: €1.00 per session"),
"{rendered}"
);
assert!(
rendered.contains("- Otherwise: €2.50 per session"),
"{rendered}"
);
}
#[test]
fn explain_lone_midnight_end_time_prints_no_time_bound() {
let rendered = explain_v221(
r#"{
"id": "midnight-end", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "end_time": "00:00" },
"price_components": [{ "type": "FLAT", "price": 1.00, "step_size": 1 }]
}
]
}"#,
);
assert_eq!(rendered, "**Flat fee:** €1.00 per session.");
}
#[test]
fn explain_weekday_restricted_flat_fee() {
let rendered = explain_v221(
r#"{
"id": "weekend", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "day_of_week": ["SATURDAY", "SUNDAY"] },
"price_components": [{ "type": "FLAT", "price": 1.50, "step_size": 1 }]
}
]
}"#,
);
assert!(
rendered.contains("**Flat fee:** on Saturday, Sunday, €1.50 per session"),
"{rendered}"
);
}
#[test]
fn explain_small_rate_is_not_shown_as_zero() {
let rendered = explain_v221(
r#"{
"id": "tiny", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"price_components": [{ "type": "ENERGY", "price": 0.004, "step_size": 1 }]
}
]
}"#,
);
assert!(
rendered.contains("**Energy:** €0.004 per kWh"),
"{rendered}"
);
assert!(!rendered.contains("€0.00 per kWh"), "{rendered}");
}
#[test]
fn explain_rate_that_does_not_fit_two_decimals_is_printed_in_full() {
let rendered = explain_v221(
r#"{
"id": "precise", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"price_components": [
{ "type": "ENERGY", "price": 0.4167, "step_size": 1 },
{ "type": "TIME", "price": 0.165, "step_size": 1 },
{ "type": "FLAT", "price": 0.25, "step_size": 1 }
]
}
]
}"#,
);
assert!(
rendered.contains("**Energy:** €0.4167 per kWh"),
"{rendered}"
);
assert!(
rendered.contains("**Charging time:** €0.165 per hour"),
"{rendered}"
);
assert!(rendered.contains("€0.25 per session"), "{rendered}");
assert!(!rendered.contains("€0.42"), "{rendered}");
assert!(!rendered.contains("€0.17"), "{rendered}");
}
#[test]
fn explain_charge_is_rounded_to_two_decimals() {
let rendered = explain_v221(
r#"{
"id": "sub-cent", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"max_price": { "excl_vat": 41.667 },
"elements": [
{
"price_components": [
{ "type": "FLAT", "price": 0.165, "step_size": 1 },
{ "type": "ENERGY", "price": 0.4167, "step_size": 1 }
]
}
]
}"#,
);
assert!(rendered.contains("€0.16 per session"), "{rendered}");
assert!(
rendered.contains("never costs more than €41.67"),
"{rendered}"
);
assert!(
rendered.contains("**Energy:** €0.4167 per kWh"),
"{rendered}"
);
}
#[test]
fn explain_small_flat_fee_is_not_shown_as_zero() {
let rendered = explain_v221(
r#"{
"id": "tiny-fee", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"price_components": [{ "type": "FLAT", "price": 0.004, "step_size": 1 }]
}
]
}"#,
);
assert!(rendered.contains("€0.004 per session"), "{rendered}");
assert!(!rendered.contains("€0.00 per session"), "{rendered}");
}
#[test]
fn explain_multiple_flat_tiers() {
let rendered = explain_v221(
r#"{
"id": "flat-tiers", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "end_time": "22:00" },
"price_components": [{ "type": "FLAT", "price": 1.00, "step_size": 1 }]
},
{
"price_components": [{ "type": "FLAT", "price": 2.50, "step_size": 1 }]
}
]
}"#,
);
assert!(
rendered.contains("- Before 22:00: €1.00 per session"),
"{rendered}"
);
assert!(
rendered.contains("- Otherwise: €2.50 per session"),
"{rendered}"
);
}
#[test]
fn explain_flat_fee_gated_by_duration() {
let rendered = explain_v221(
r#"{
"id": "flat-dur", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "max_duration": 3600 },
"price_components": [{ "type": "FLAT", "price": 1.00, "step_size": 1 }]
}
]
}"#,
);
assert!(
rendered
.contains("**Flat fee:** for the first 1 hour after plugging in, €1.00 per session"),
"{rendered}"
);
}
#[test]
fn explain_vat_and_price_bounds() {
let rendered = explain_v221(
r#"{
"id": "bounds", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"min_price": { "excl_vat": 1.00, "incl_vat": 1.21 },
"max_price": { "excl_vat": 50.00, "incl_vat": 60.50 },
"elements": [
{
"price_components": [{ "type": "ENERGY", "price": 0.25, "vat": 21.0, "step_size": 1 }]
}
]
}"#,
);
assert!(
rendered.contains("€0.25 per kWh (excl. 21 VAT)"),
"{rendered}"
);
assert!(
rendered.contains("at least €1.00 (€1.21 incl. VAT)"),
"{rendered}"
);
assert!(
rendered.contains("never costs more than €50.00 (€60.50 incl. VAT)"),
"{rendered}"
);
}
#[test]
fn explain_validity_window() {
let rendered = explain_v221(
r#"{
"id": "seasonal", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"start_date_time": "2024-01-01T00:00:00Z",
"end_date_time": "2024-12-31T23:00:00Z",
"elements": [
{
"price_components": [{ "type": "ENERGY", "price": 0.40, "step_size": 1 }]
}
]
}"#,
);
assert!(
rendered.contains("only valid from 2024-01-01 00:00 until 2024-12-31 23:00 (UTC)"),
"{rendered}"
);
}
#[test]
fn explain_reservation_only_element_is_excluded() {
let rendered = explain_v221(
r#"{
"id": "reservation", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "reservation": "RESERVATION" },
"price_components": [{ "type": "TIME", "price": 5.00, "step_size": 1 }]
}
]
}"#,
);
assert!(
rendered.contains("every element applies only to reservation sessions"),
"{rendered}"
);
assert!(!rendered.contains("€5.00"), "{rendered}");
}
#[test]
fn explain_fallback_no_price_components() {
let rendered = explain_v221(
r#"{
"id": "empty", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{ "price_components": [] }
]
}"#,
);
assert!(
rendered.contains("none of its applicable elements define a price component"),
"{rendered}"
);
}
#[test]
fn explain_fallback_free_flat_only() {
let rendered = explain_v221(
r#"{
"id": "gratis", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{ "price_components": [{ "type": "FLAT", "price": 0, "step_size": 1 }] }
]
}"#,
);
assert!(
rendered.contains("is free: its only charge is a flat fee of zero"),
"{rendered}"
);
}
#[test]
fn explain_zero_max_kwh_states_the_bound() {
let rendered = explain_v221(
r#"{
"id": "zero-max-kwh", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "start_date": "2025-10-22", "max_power": 150, "max_kwh": 0 },
"price_components": [{ "type": "ENERGY", "price": 0.7025, "step_size": 1 }]
}
]
}"#,
);
assert_eq!(
rendered,
"**Energy:** from 2025-10-22 onwards, while charging below 150 kW, for the first 0 kWh, \
€0.7025 per kWh."
);
}
#[test]
fn explain_zero_max_duration_states_the_bound() {
let rendered = explain_v221(
r#"{
"id": "zero-max-duration", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "max_duration": 0 },
"price_components": [{ "type": "TIME", "price": 4.99, "step_size": 1 }]
}
]
}"#,
);
assert_eq!(
rendered,
"**Charging time:** for the first 0 seconds after plugging in, €4.99 per hour."
);
}
#[test]
fn explain_unsatisfiable_tier_does_not_set_the_billing_step() {
let rendered = explain_v221(
r#"{
"id": "steps", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "max_kwh": 0 },
"price_components": [{ "type": "ENERGY", "price": 0.70, "step_size": 1 }]
},
{
"restrictions": { "min_kwh": 10 },
"price_components": [{ "type": "ENERGY", "price": 0.45, "step_size": 1000 }]
},
{
"price_components": [{ "type": "ENERGY", "price": 0.50, "step_size": 1000 }]
}
]
}"#,
);
assert_eq!(
rendered,
"**Energy:**\n\n\
- For the first 0 kWh: €0.70 per kWh (billed in steps of 0.001 kWh, rounded up)\n\
- After the first 10 kWh: €0.45 per kWh\n\
- For the remaining energy: €0.50 per kWh\n\n\
_Billed in steps of 1 kWh, rounded up._"
);
}
#[test]
fn explain_all_unsatisfiable_tiers_still_share_a_billing_step() {
let rendered = explain_v221(
r#"{
"id": "dead-only", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "max_duration": 0 },
"price_components": [{ "type": "TIME", "price": 4.99, "step_size": 3600 }]
},
{
"restrictions": { "max_kwh": 0 },
"price_components": [{ "type": "TIME", "price": 3.50, "step_size": 3600 }]
}
]
}"#,
);
assert_eq!(
rendered,
"**Charging time:**\n\n\
- For the first 0 seconds after plugging in: €4.99 per hour\n\
- For the first 0 kWh: €3.50 per hour\n\n\
_Billed in steps of 1 hour, rounded up._"
);
}
#[test]
fn explain_min_kwh_above_max_kwh_sets_no_billing_step() {
let rendered = explain_v221(
r#"{
"id": "contradiction", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "min_kwh": 10, "max_kwh": 5 },
"price_components": [{ "type": "ENERGY", "price": 0.70, "step_size": 1 }]
},
{
"price_components": [{ "type": "ENERGY", "price": 0.50, "step_size": 1000 }]
}
]
}"#,
);
assert_eq!(
rendered,
"**Energy:**\n\n\
- From 10 kWh to 5 kWh: €0.70 per kWh (billed in steps of 0.001 kWh, rounded up)\n\
- For the remaining energy: €0.50 per kWh\n\n\
_Billed in steps of 1 kWh, rounded up._"
);
}
#[test]
fn explain_min_kwh_equal_to_max_kwh_sets_no_billing_step() {
let rendered = explain_v221(
r#"{
"id": "equal-bounds", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "min_kwh": 5, "max_kwh": 5 },
"price_components": [{ "type": "ENERGY", "price": 0.70, "step_size": 1 }]
},
{
"price_components": [{ "type": "ENERGY", "price": 0.50, "step_size": 1000 }]
}
]
}"#,
);
assert_eq!(
rendered,
"**Energy:**\n\n\
- From 5 kWh to 5 kWh: €0.70 per kWh (billed in steps of 0.001 kWh, rounded up)\n\
- For the remaining energy: €0.50 per kWh\n\n\
_Billed in steps of 1 kWh, rounded up._"
);
}
#[test]
fn explain_min_duration_above_max_duration_sets_no_billing_step() {
let rendered = explain_v221(
r#"{
"id": "duration-contradiction", "country_code": "NL", "party_id": "TST",
"currency": "EUR",
"elements": [
{
"restrictions": { "min_duration": 3600, "max_duration": 600 },
"price_components": [{ "type": "ENERGY", "price": 0.70, "step_size": 1 }]
},
{
"price_components": [{ "type": "ENERGY", "price": 0.50, "step_size": 1000 }]
}
]
}"#,
);
assert_eq!(
rendered,
"**Energy:**\n\n\
- Between 1 hour and 10 minutes after plugging in: €0.70 per kWh (billed in steps of \
0.001 kWh, rounded up)\n\
- Otherwise: €0.50 per kWh\n\n\
_Billed in steps of 1 kWh, rounded up._"
);
}
#[test]
fn explain_start_date_after_end_date_sets_no_billing_step() {
let rendered = explain_v221(
r#"{
"id": "date-contradiction", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "start_date": "2026-01-01", "end_date": "2025-01-01" },
"price_components": [{ "type": "ENERGY", "price": 0.70, "step_size": 1 }]
},
{
"price_components": [{ "type": "ENERGY", "price": 0.50, "step_size": 1000 }]
}
]
}"#,
);
assert_eq!(
rendered,
"**Energy:**\n\n\
- From 2026-01-01 until 2025-01-01: €0.70 per kWh (billed in steps of 0.001 kWh, \
rounded up)\n\
- Otherwise: €0.50 per kWh\n\n\
_Billed in steps of 1 kWh, rounded up._"
);
}
#[test]
fn explain_flat_fee_on_an_unsatisfiable_tier_is_stated() {
let rendered = explain_v221(
r#"{
"id": "flat-dead", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "max_kwh": 0 },
"price_components": [{ "type": "FLAT", "price": 2.50, "step_size": 1 }]
},
{
"price_components": [{ "type": "ENERGY", "price": 0.45, "step_size": 1 }]
}
]
}"#,
);
assert_eq!(
rendered,
"**Energy:** €0.45 per kWh.\n\n**Flat fee:** for the first 0 kWh, €2.50 per session."
);
}
#[test]
fn explain_zero_max_kwh_tier_is_listed() {
let rendered = explain_v221(
r#"{
"id": "mixed", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "max_kwh": 0 },
"price_components": [{ "type": "ENERGY", "price": 0.70, "step_size": 1 }]
},
{
"price_components": [{ "type": "ENERGY", "price": 0.45, "step_size": 1 }]
}
]
}"#,
);
assert_eq!(
rendered,
"**Energy:**\n\n- For the first 0 kWh: €0.70 per kWh\n- For the remaining energy: €0.45 \
per kWh"
);
}
#[test]
fn explain_zero_max_power_still_prices() {
let rendered = explain_v221(
r#"{
"id": "zero-max-power", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "max_power": 0 },
"price_components": [{ "type": "ENERGY", "price": 0.50, "step_size": 1 }]
}
]
}"#,
);
assert_eq!(
rendered,
"**Energy:** while charging below 0 kW, €0.50 per kWh."
);
}
#[test]
fn explain_empty_day_of_week_prints_no_day_clause() {
let rendered = explain_v221(
r#"{
"id": "empty-days", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "day_of_week": [] },
"price_components": [{ "type": "ENERGY", "price": 0.50, "step_size": 1 }]
}
]
}"#,
);
assert_eq!(rendered, "**Energy:** €0.50 per kWh.");
}
#[test]
fn explain_unreachable_tier_after_catch_all() {
let rendered = explain_v221(
r#"{
"id": "unreachable", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"price_components": [{ "type": "TIME", "price": 0.50, "step_size": 1 }]
},
{
"restrictions": { "max_duration": 10800 },
"price_components": [{ "type": "TIME", "price": 0.75, "step_size": 1 }]
}
]
}"#,
);
assert!(
rendered.contains("Any later tiers never apply"),
"{rendered}"
);
assert!(!rendered.contains("€0.75"), "{rendered}");
}
#[test]
fn explain_mixed_step_size_per_tier() {
let rendered = explain_v221(
r#"{
"id": "mixed-step", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "max_duration": 10800 },
"price_components": [{ "type": "TIME", "price": 0.50, "step_size": 1 }]
},
{
"price_components": [{ "type": "TIME", "price": 0.75, "step_size": 300 }]
}
]
}"#,
);
assert!(!rendered.contains("steps of 1 second"), "{rendered}");
assert!(
rendered.contains("(billed in steps of 5 minutes, rounded up)"),
"{rendered}"
);
assert!(!rendered.contains("_Billed in"), "{rendered}");
}
#[test]
fn explain_power_gated_tier_reads_as_otherwise() {
let rendered = explain_v221(
r#"{
"id": "fast", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "min_power": 50 },
"price_components": [{ "type": "ENERGY", "price": 0.60, "step_size": 1 }]
},
{
"price_components": [{ "type": "ENERGY", "price": 0.40, "step_size": 1 }]
}
]
}"#,
);
assert!(
rendered.contains("- While charging at 50 kW or more: €0.60 per kWh"),
"{rendered}"
);
assert!(
rendered.contains("- Otherwise: €0.40 per kWh"),
"{rendered}"
);
assert!(!rendered.contains("for the remaining energy"), "{rendered}");
}
#[test]
fn explain_duplicate_component_in_element_is_ignored() {
let rendered = explain_v221(
r#"{
"id": "dup", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"price_components": [
{ "type": "ENERGY", "price": 0.30, "step_size": 1 },
{ "type": "ENERGY", "price": 0.99, "step_size": 1 }
]
}
]
}"#,
);
assert!(rendered.contains("**Energy:** €0.30 per kWh"), "{rendered}");
assert!(!rendered.contains("0.99"), "{rendered}");
}
#[test]
fn explain_non_euro_currency_symbol() {
let rendered = explain_v221(
r#"{
"id": "usd", "country_code": "US", "party_id": "TST", "currency": "USD",
"elements": [
{
"price_components": [{ "type": "ENERGY", "price": 0.35, "step_size": 1 }]
}
]
}"#,
);
assert!(rendered.contains("$0.35 per kWh"), "{rendered}");
}
#[test]
fn explain_banded_duration_window() {
let rendered = explain_v221(
r#"{
"id": "banded", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "min_duration": 3600, "max_duration": 10800 },
"price_components": [{ "type": "TIME", "price": 0.50, "step_size": 1 }]
},
{
"price_components": [{ "type": "TIME", "price": 0.75, "step_size": 1 }]
}
]
}"#,
);
assert!(
rendered.contains("- Between 1 hour and 3 hours after plugging in: €0.50 per hour"),
"{rendered}"
);
assert!(
rendered.contains("- For the remaining charging time: €0.75 per hour"),
"{rendered}"
);
}
#[test]
fn explain_idle_charge_starts_after_delay() {
let rendered = explain_v221(
r#"{
"id": "delayed-idle", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "min_duration": 14400 },
"price_components": [{ "type": "PARKING_TIME", "price": 2.00, "step_size": 60 }]
}
]
}"#,
);
assert!(
rendered.contains(
"**Idle time (connected but not charging):** more than 4 hours after plugging in, \
€2.00 per hour"
),
"{rendered}"
);
}
#[test]
fn explain_duration_bound_is_anchored_to_the_session_start() {
let rendered = explain_v221(
r#"{
"id": "idle-after-quarter", "country_code": "NL", "party_id": "TST",
"currency": "EUR",
"elements": [
{
"price_components": [{ "type": "ENERGY", "price": 0.2417, "step_size": 1 }]
},
{
"restrictions": { "min_duration": 900 },
"price_components": [
{ "type": "PARKING_TIME", "price": 4.998, "step_size": 1 }
]
}
]
}"#,
);
assert_eq!(
rendered,
"**Energy:** €0.2417 per kWh.\n\n**Idle time (connected but not charging):** more than \
15 minutes after plugging in, €4.998 per hour."
);
}
#[test]
fn explain_stacked_qualifiers() {
let rendered = explain_v221(
r#"{
"id": "stacked", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": {
"start_time": "07:00",
"end_time": "19:00",
"day_of_week": ["MONDAY", "FRIDAY"],
"start_date": "2024-06-01"
},
"price_components": [{ "type": "ENERGY", "price": 0.50, "step_size": 1 }]
}
]
}"#,
);
assert!(
rendered.contains(
"**Energy:** between 07:00 and 19:00, on Monday, Friday, from 2024-06-01 onwards, €0.50 per kWh"
),
"{rendered}"
);
}
#[test]
fn explain_bound_and_qualifier_together() {
let rendered = explain_v221(
r#"{
"id": "bound-and-qual", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "max_duration": 10800, "day_of_week": ["SATURDAY", "SUNDAY"] },
"price_components": [{ "type": "TIME", "price": 0.50, "step_size": 1 }]
},
{
"price_components": [{ "type": "TIME", "price": 0.75, "step_size": 1 }]
}
]
}"#,
);
assert!(
rendered.contains(
"- On Saturday, Sunday, for the first 3 hours after plugging in: €0.50 per hour"
),
"{rendered}"
);
}
#[test]
fn explain_duration_humanized_to_hours_and_minutes() {
let rendered = explain_v221(
r#"{
"id": "ninety", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "max_duration": 5400 },
"price_components": [{ "type": "TIME", "price": 0.10, "step_size": 1 }]
}
]
}"#,
);
assert!(
rendered.contains("for the first 1 hour 30 minutes after plugging in, €0.10 per hour"),
"{rendered}"
);
}
#[test]
fn explain_time_tier_surfaces_energy_threshold() {
let rendered = explain_v221(
r#"{
"id": "kwh-gate", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "min_kwh": 5 },
"price_components": [{ "type": "TIME", "price": 0.30, "step_size": 60 }]
},
{
"price_components": [{ "type": "TIME", "price": 0, "step_size": 1 }]
}
]
}"#,
);
assert!(
rendered.contains("- After the first 5 kWh: €0.30 per hour"),
"{rendered}"
);
}
#[test]
fn explain_energy_tier_surfaces_duration_bound() {
let rendered = explain_v221(
r#"{
"id": "dur-gate", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "max_duration": 3600 },
"price_components": [{ "type": "ENERGY", "price": 0.20, "step_size": 1 }]
},
{
"price_components": [{ "type": "ENERGY", "price": 0.30, "step_size": 1 }]
}
]
}"#,
);
assert!(
rendered.contains("- For the first 1 hour after plugging in: €0.20 per kWh"),
"{rendered}"
);
assert!(
rendered.contains("- Otherwise: €0.30 per kWh"),
"{rendered}"
);
}
#[test]
fn explain_date_tiers_are_listed_chronologically() {
let rendered = explain_v221(
r#"{
"id": "dates", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "start_date": "2025-01-01" },
"price_components": [{ "type": "ENERGY", "price": 0.30, "step_size": 1 }]
},
{
"restrictions": { "start_date": "2023-01-01", "end_date": "2024-01-01" },
"price_components": [{ "type": "ENERGY", "price": 0.10, "step_size": 1 }]
},
{
"restrictions": { "start_date": "2024-01-01", "end_date": "2025-01-01" },
"price_components": [{ "type": "ENERGY", "price": 0.20, "step_size": 1 }]
}
]
}"#,
);
assert_eq!(
rendered,
"**Energy:**\n\n\
- From 2023-01-01 until 2024-01-01: €0.10 per kWh\n\
- From 2024-01-01 until 2025-01-01: €0.20 per kWh\n\
- From 2025-01-01 onwards: €0.30 per kWh"
);
}
#[test]
fn explain_date_tier_shadowed_by_earlier_range_is_dropped() {
let rendered = explain_v221(
r#"{
"id": "shadowed", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "start_date": "2024-01-01", "end_date": "2025-01-01" },
"price_components": [{ "type": "ENERGY", "price": 0.20, "step_size": 1 }]
},
{
"restrictions": { "start_date": "2025-01-01" },
"price_components": [{ "type": "ENERGY", "price": 0.30, "step_size": 1 }]
},
{
"restrictions": { "start_date": "2024-03-01", "end_date": "2024-06-01" },
"price_components": [{ "type": "ENERGY", "price": 0.99, "step_size": 1 }]
}
]
}"#,
);
assert!(!rendered.contains("€0.99"), "{rendered}");
assert!(
rendered.contains("Tiers that never apply are not listed"),
"{rendered}"
);
assert!(
rendered.contains("- From 2024-01-01 until 2025-01-01: €0.20 per kWh"),
"{rendered}"
);
}
#[test]
fn explain_partly_overlapping_date_tiers_keep_matching_order() {
let rendered = explain_v221(
r#"{
"id": "overlap", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "start_date": "2024-03-01", "end_date": "2024-09-01" },
"price_components": [{ "type": "ENERGY", "price": 0.20, "step_size": 1 }]
},
{
"restrictions": { "start_date": "2024-01-01", "end_date": "2024-06-01" },
"price_components": [{ "type": "ENERGY", "price": 0.10, "step_size": 1 }]
}
]
}"#,
);
assert_eq!(
rendered,
"**Energy:**\n\n\
- From 2024-03-01 until 2024-09-01: €0.20 per kWh\n\
- From 2024-01-01 until 2024-06-01: €0.10 per kWh"
);
}
#[test]
fn explain_date_tiers_with_other_restrictions_keep_matching_order() {
let rendered = explain_v221(
r#"{
"id": "date-and-time", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "start_date": "2025-01-01" },
"price_components": [{ "type": "ENERGY", "price": 0.30, "step_size": 1 }]
},
{
"restrictions": {
"start_date": "2024-01-01", "end_date": "2025-01-01",
"start_time": "21:00", "end_time": "07:00"
},
"price_components": [{ "type": "ENERGY", "price": 0.10, "step_size": 1 }]
}
]
}"#,
);
assert_eq!(
rendered,
"**Energy:**\n\n\
- From 2025-01-01 onwards: €0.30 per kWh\n\
- Between 21:00 and 07:00, from 2024-01-01 until 2025-01-01: €0.10 per kWh"
);
}
#[test]
fn explain_date_gated_flat_fees_are_listed_chronologically() {
let rendered = explain_v221(
r#"{
"id": "flat-dates", "country_code": "NL", "party_id": "TST", "currency": "EUR",
"elements": [
{
"restrictions": { "start_date": "2025-01-01" },
"price_components": [{ "type": "FLAT", "price": 1.50, "step_size": 1 }]
},
{
"restrictions": { "start_date": "2024-01-01", "end_date": "2025-01-01" },
"price_components": [{ "type": "FLAT", "price": 1.00, "step_size": 1 }]
}
]
}"#,
);
assert_eq!(
rendered,
"**Flat fee:**\n\n\
- From 2024-01-01 until 2025-01-01: €1.00 per session\n\
- From 2025-01-01 onwards: €1.50 per session"
);
}