ocpi-tariffs 0.52.0

OCPI tariff calculations
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
use crate::{json, tariff, Language, Version};

const TARIFF_JSON: &str = include_str!("../tariff.json");

/// Parse a `v2.2.1` tariff JSON, explain it, and return the rendered Markdown.
///
/// The Markdown is emitted via `tracing` so it can be eyeballed with `--nocapture`.
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
}

/// Render the bundled sample tariff so the wording can be reviewed by eye.
#[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}"
    );
    // Every component in the sample has `step_size` 1, so no billing-step note is shown.
    assert!(!rendered.contains("illed in"), "{rendered}");
}

/// Two charging-time elements should read as a bulleted tier list: a bounded first tier followed by
/// a catch-all "for the remaining charging time" tier.
#[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: €0.50 per hour"),
        "{rendered}"
    );
    assert!(
        rendered.contains("- For the remaining charging time: €0.75 per hour"),
        "{rendered}"
    );
}

/// Energy tiers are bounded by consumed kWh rather than by duration.
#[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}"
    );
}

/// A zero rate is narrated as "free" rather than "0.00 per hour".
#[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: free"),
        "{rendered}"
    );
    assert!(
        rendered.contains("- For the remaining idle time: €3.00 per hour"),
        "{rendered}"
    );
}

/// A time window whose end is before its start wraps past midnight.
#[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}"
    );
}

/// A free tier never carries a billing-step note, even when sibling tiers have a different step.
#[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: free"),
        "{rendered}"
    );
    assert!(!rendered.contains("free (billed in"), "{rendered}");
    assert!(
        rendered.contains("(billed in steps of 5 minutes, rounded up)"),
        "{rendered}"
    );
}

/// An element whose start and end times are equal has an empty window and never applies; it is
/// narrated as "never" rather than a misleading "between 08:00 and 08:00".
#[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!(
        rendered.contains("never (its window 08:00 to 08:00 is empty)"),
        "{rendered}"
    );
    assert!(!rendered.contains("between 08:00 and 08:00"), "{rendered}");
}

/// A flat fee restricted to weekend days lists those days.
#[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}"
    );
}

/// A small nonzero rate must not collapse to the currency-zero string and read as free.
#[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}");
}

/// Several FLAT tiers under different conditions are all shown, not just the first.
#[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}"
    );
}

/// A flat fee gated by a consumption bound (`max_duration`) surfaces that condition, rather than
/// being explained as an unconditional fee.
#[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, €1.00 per session"),
        "{rendered}"
    );
}

/// A VAT percentage on a price component is reported; the overall `min/max` bounds and a VAT
/// inclusive `min_price` are narrated too.
#[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}"
    );
}

/// The tariff's own validity window is narrated.
#[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}"
    );
}

/// An element restricted to reservation sessions never applies to a charging session, so when it
/// is the only element the tariff explains as charging for nothing.
#[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}");
}

/// A tariff whose elements define no price components explains that, rather than blaming the tool.
#[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}"
    );
}

/// A tariff whose only charge is a zero flat fee explains that it is free.
#[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}"
    );
}

/// A catch-all tier placed before another tier for the same dimension makes the later tier
/// unreachable; the explanation drops the dead tier and says so.
#[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}");
}

/// When tiers disagree on the billing step, the step is described inline per tier rather than once.
#[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 }]
                }
            ]
        }"#,
    );

    // The first tier's step is 1 (continuous), so it carries no note; only the 5-minute (300
    // second) tier does.
    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}");
}

/// A tier gated by charging power (not by consumption) followed by a catch-all reads as
/// "Otherwise", not "for the remaining energy".
#[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}");
}

/// Only the first price component per dimension in an element is used, so a duplicate within the
/// same element is ignored rather than shown as a second tier.
#[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}");
}

/// The currency symbol comes from `mod currency`, so a non-euro tariff renders its own symbol.
#[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}");
}

/// A tier bounded on both ends reads as a session-duration window, followed by a catch-all.
#[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 into the session: €0.50 per hour"),
        "{rendered}"
    );
    assert!(
        rendered.contains("- For the remaining charging time: €0.75 per hour"),
        "{rendered}"
    );
}

/// A lone tier with only a lower duration bound reads as "after the first N", implying it is free
/// before then.
#[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):** after the first 4 hours, €2.00 per hour"
        ),
        "{rendered}"
    );
}

/// Several restrictions on one tier are listed together, leading the rate.
#[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}"
    );
}

/// A tier can carry both a consumption bound and a qualifier; both lead the rate.
#[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: €0.50 per hour"),
        "{rendered}"
    );
}

/// Sub-hour durations are humanized into hours and minutes.
#[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, €0.10 per hour"),
        "{rendered}"
    );
}

/// A charging-time tier gated by an energy threshold (`min_kwh`) surfaces that threshold rather
/// than dropping it; the energy bound is the secondary bound on a duration dimension.
#[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}"
    );
}

/// An energy tier gated by a duration bound (`max_duration`) surfaces that bound; the duration
/// bound is the secondary bound on an energy dimension.
#[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: €0.20 per kWh"),
        "{rendered}"
    );
    assert!(
        rendered.contains("- Otherwise: €0.30 per kWh"),
        "{rendered}"
    );
}

/// Elements that are gated by nothing but a date range are commonly authored one per price change,
/// in no particular order. Their tiers are listed by date so the rate history reads top to bottom.
#[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"
    );
}

/// A date tier whose whole range an earlier date tier already covers can never supply the rate,
/// because the pricing engine takes the first matching element. It is left out and noted, which is
/// also what makes the surviving tiers safe to list by date rather than in matching order.
#[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 whether two date tiers that overlap partly keep their ordering and so, the earlier
/// listed element wins. Listing those by date would claim the later rate
/// applies, so the list stays in matching order.
#[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"
    );
}

/// A tier gated by more than a date range can be reached under conditions the dates do not describe,
/// so a list containing one stays in matching order.
#[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"
    );
}

/// Flat fees are tiered the same way as the metered dimensions, so a purely date-gated list of fees
/// is listed by date too.
#[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"
    );
}