ocpi-tariffs 0.54.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
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
//! American English (`en-US`) renderings of `explain`.
//!
//! This is the full set: every semantic decision the renderer makes about tiers, restrictions,
//! bounds and fallbacks is covered here. The other languages ([`super::test_nl_nl`]) mirror a
//! representative subset and focus on the language surface.

use crate::{Language, Version, json, tariff};

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 after plugging in: €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 after plugging in: 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 after plugging in: 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 a zero-length window and never applies. It
/// is narrated as "never" rather than as 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_eq!(
        rendered,
        "**Energy:** never applies (its window 08:00 to 08:00 is empty), €0.50 per kWh."
    );
}

/// A `00:00`-`00:00` pair covers the whole day, so `explain` describes the element as applying
/// from `00:00` onwards. Reading the raw values instead would take the equal pair for a
/// zero-length window and state that clause on the element that prices every session.
#[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}"
    );
}

/// A lone `end_time` of `00:00` stops at the end of the day, so it bounds nothing and `explain`
/// prints no time clause for it. Printing "before 00:00" would describe a bound the pricer does
/// not enforce.
#[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.");
}

/// 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}");
}

/// A price that does not fit in two decimals is printed at its own precision, because the
/// driver is charged that price and not the rounded one. `0.25` fits and keeps both decimals.
#[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}");
}

/// A flat fee and a price bound are sums a session is billed, so they are rounded to the
/// two decimals that can actually be paid, even though a rate in the same tariff is not.
#[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 }
                    ]
                }
            ]
        }"#,
    );

    // A tie goes to the even digit, so `0.165` reduces to `0.16` rather than `0.17`.
    assert!(rendered.contains("€0.16 per session"), "{rendered}");
    assert!(
        rendered.contains("never costs more than €41.67"),
        "{rendered}"
    );
    // The rate in the same tariff keeps its own precision.
    assert!(
        rendered.contains("**Energy:** €0.4167 per kWh"),
        "{rendered}"
    );
}

/// A flat fee too small to survive two decimals is printed as given, because "€0.00 per
/// session" would read as no fee at all.
#[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}");
}

/// 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 after plugging in, €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 `max_kwh` of zero doesn't allow any session, because the bound is exclusive and consumed
/// energy is never negative. The element is described all the same.
#[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."
    );
}

/// A `max_duration` of zero doesn't allow any session either, and the element is described all
/// the same.
#[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."
    );
}

/// The tiers a session can reach decide the billing step noted for the dimension. An
/// unsatisfiable tier is left out of that, and still has a note of its own, because it bills in a
/// step the dimension's note doesn't cover.
#[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._"
    );
}

/// When no tier of a dimension is reachable, the tiers decide the billing step between them, so
/// two excluded tiers billed in the same step state it once rather than one apiece. This is the
/// only test that fails when the `satisfiable_steps.is_empty()` branch of `build_dimension` is
/// deleted.
#[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._"
    );
}

/// A `min_kwh` at or above its `max_kwh` excludes every session, so the tier it bounds doesn't set
/// the billing step stated for the dimension.
#[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._"
    );
}

/// `is_satisfiable` compares `min_kwh` at or above its `max_kwh` rather than merely above it,
/// because the upper bound is exclusive. Narrowing the comparison to `min > max` would leave this
/// tier setting the billing step.
#[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._"
    );
}

/// A `min_duration` at or above its `max_duration` excludes every session, so the tier it bounds
/// doesn't set the billing step stated for the dimension.
#[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._"
    );
}

/// A `start_date` at or after its `end_date` excludes every session, so the tier it bounds doesn't
/// set the billing step stated for the dimension.
#[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._"
    );
}

/// A flat fee bounded out of every session is stated as the tariff writes it, alongside the
/// energy rate that is charged.
#[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."
    );
}

/// A tier bounded by a zero `max_kwh` is listed alongside the tier that prices the session, in
/// matching order.
#[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"
    );
}

/// An element with a zero `max_power` can match, so its rate is still described. `max_power` is
/// exclusive: the spec keeps the element active while the EV charges with less than the defined
/// power, and the pricing engine compares the bound against the power a CDR reports, so the
/// element still applies to a session that reports no power at all.
///
/// The clause reads oddly because the tariff is odd. Telling an author to drop the restriction is
/// `lint`'s job, through `MaxZeroNeverMatch`; `explain` describes the document it is given.
#[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."
    );
}

/// OCPI reads an empty `day_of_week` the same as an omitted one, so `explain` prints no day clause
/// for the list.
#[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.");
}

/// 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 after plugging in: €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 "more than N after plugging in", implying
/// it is free before then. The bound is on the whole session, not on idle time, so a session that
/// spends its first 4 hours charging is billed for idle time from the start.
#[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}"
    );
}

/// A duration bound is anchored to the start of the session, because it is measured against how
/// long the whole session has lasted rather than against the dimension it is listed under. Without
/// that anchor, "after the first 15 minutes" under an idle-time heading reads as 15 minutes of idle
/// time: a session that charges for 20 minutes and then idles for 5 would read as free, where the
/// pricing engine bills all 5 idle minutes because the threshold was crossed while charging.
#[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."
    );
}

/// 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 after plugging in: €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 after plugging in, €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 after plugging in: €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"
    );
}