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
//! Dutch (`nl-NL`) renderings of `explain`.
//!
//! These mirror a representative subset of the English tests in [`super::test_en_us`], checking
//! that the Dutch arm of the renderer produces the expected vocabulary, decimal-comma number
//! formatting and word order. The semantic decisions are shared with English (and covered there),
//! so these focus on the language surface.

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

/// Parse a `v2.2.1` tariff JSON, explain it in Dutch, and return the rendered Markdown.
fn explain_nl(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::NlNl)
        .expect("the tariff should be explainable")
        .ignore_warnings();

    tracing::info!("\n{rendered}\n");

    rendered
}

/// Amounts use a decimal comma and the currency symbol is spaced from the amount.
#[test]
fn dutch_energy_rate_uses_decimal_comma() {
    let rendered = explain_nl(
        r#"{
            "id": "energy", "country_code": "NL", "party_id": "TST", "currency": "EUR",
            "elements": [
                {
                    "price_components": [{ "type": "ENERGY", "price": 0.30, "step_size": 1 }]
                }
            ]
        }"#,
    );

    assert_eq!(rendered, "**Energietarief:** € 0,30 per kWh.");
}

/// A price that does not fit in two decimals keeps its own precision in Dutch too, with
/// the decimal comma applied to all four digits.
#[test]
fn dutch_rate_that_does_not_fit_two_decimals_is_printed_in_full() {
    let rendered = explain_nl(
        r#"{
            "id": "precise", "country_code": "NL", "party_id": "TST", "currency": "EUR",
            "elements": [
                {
                    "price_components": [{ "type": "ENERGY", "price": 0.4167, "step_size": 1 }]
                }
            ]
        }"#,
    );

    assert_eq!(rendered, "**Energietarief:** € 0,4167 per kWh.");
}

/// Two charging-time tiers read as a bulleted list with a "remaining charging time" catch-all.
#[test]
fn dutch_tiered_charging_time() {
    let rendered = explain_nl(
        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("- De eerste 3 uur na het aansluiten: € 0,50 per uur"),
        "{rendered}"
    );
    assert!(
        rendered.contains("- De resterende laadtijd: € 0,75 per uur"),
        "{rendered}"
    );
}

/// Energy tiers are bounded by consumed kWh.
#[test]
fn dutch_energy_tiers_by_kwh() {
    let rendered = explain_nl(
        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("- De eerste 20 kWh: € 0,30 per kWh"),
        "{rendered}"
    );
    assert!(
        rendered.contains("- De resterende energie: € 0,45 per kWh"),
        "{rendered}"
    );
}

/// A zero rate is narrated as "gratis".
#[test]
fn dutch_free_then_paid_idle_time() {
    let rendered = explain_nl(
        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("**Parkeertijd (aangesloten maar niet aan het laden):**"),
        "{rendered}"
    );
    assert!(
        rendered.contains("- De eerste 4 uur na het aansluiten: gratis"),
        "{rendered}"
    );
    assert!(
        rendered.contains("- De resterende parkeertijd: € 3,00 per uur"),
        "{rendered}"
    );
    assert!(
        rendered.contains("_Berekend in stappen van 1 minuut, naar boven afgerond._"),
        "{rendered}"
    );
}

/// A window whose end precedes its start wraps past midnight.
#[test]
fn dutch_wrapping_night_window() {
    let rendered = explain_nl(
        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_eq!(
        rendered,
        "**Energietarief:** tussen 23:00 en 07:00, € 0,20 per kWh."
    );
}

/// A weekend-restricted flat fee lists the days, lowercased as Dutch weekdays are.
#[test]
fn dutch_weekday_restricted_flat_fee() {
    let rendered = explain_nl(
        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_eq!(
        rendered,
        "**Vast tarief:** op zaterdag, zondag, € 1,50 per sessie."
    );
}

/// A power-gated tier followed by a catch-all reads as "Anders", not a "remaining" phrase.
#[test]
fn dutch_power_gated_tier_reads_as_otherwise() {
    let rendered = explain_nl(
        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("- Bij laadsnelheid van 50 kW of meer: € 0,60 per kWh"),
        "{rendered}"
    );
    assert!(rendered.contains("- Anders: € 0,40 per kWh"), "{rendered}");
    assert!(!rendered.contains("resterende"), "{rendered}");
}

/// A per-tier billing step is noted inline in Dutch.
#[test]
fn dutch_mixed_step_size_per_tier() {
    let rendered = explain_nl(
        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(
            "- De resterende laadtijd: € 0,75 per uur (berekend in stappen van 5 minuten, naar \
             boven afgerond)"
        ),
        "{rendered}"
    );
}

/// VAT and the overall price bounds are narrated in Dutch, with VAT-inclusive amounts.
#[test]
fn dutch_vat_and_price_bounds() {
    let rendered = explain_nl(
        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 btw)"),
        "{rendered}"
    );
    assert!(
        rendered.contains("Een sessie kost altijd minstens € 1,00 (€ 1,21 incl. btw)."),
        "{rendered}"
    );
    assert!(
        rendered.contains("Een sessie kost nooit meer dan € 50,00 (€ 60,50 incl. btw)."),
        "{rendered}"
    );
}

/// The tariff's own validity window is narrated in Dutch.
#[test]
fn dutch_validity_window() {
    let rendered = explain_nl(
        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(
            "Dit tarief is alleen geldig van 2024-01-01 00:00 tot 2024-12-31 23:00 (UTC)."
        ),
        "{rendered}"
    );
}

/// A duration is humanized with Dutch units; note "uur" does not inflect for number.
#[test]
fn dutch_duration_humanized_to_hours_and_minutes() {
    let rendered = explain_nl(
        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("de eerste 1 uur 30 minuten na het aansluiten, € 0,10 per uur"),
        "{rendered}"
    );
}

/// A reservation-only element leaves nothing to charge; the fallback is explained in Dutch.
#[test]
fn dutch_reservation_only_fallback() {
    let rendered = explain_nl(
        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_eq!(
        rendered,
        "Dit tarief brengt nooit kosten in rekening voor een gewone laadsessie: elk element geldt \
         alleen voor reserveringssessies."
    );
}

/// An element whose start and end times are equal is narrated as "nooit" in Dutch, with the
/// window that no session falls in.
#[test]
fn dutch_equal_start_end_time_is_never() {
    let rendered = explain_nl(
        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,
        "**Energietarief:** geldt nooit (het venster 08:00 tot 08:00 is leeg), € 0,50 per kWh."
    );
}

/// The Dutch explanation describes an element bounded by a zero `max_kwh`.
#[test]
fn dutch_zero_max_kwh_states_the_bound() {
    let rendered = explain_nl(
        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,
        "**Energietarief:** vanaf 2025-10-22, bij laadsnelheid onder 150 kW, de eerste 0 kWh, \
         € 0,7025 per kWh."
    );
}

/// The Dutch explanation lists both tiers, in matching order.
#[test]
fn dutch_zero_max_kwh_tier_is_listed() {
    let rendered = explain_nl(
        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,
        "**Energietarief:**\n\n- De eerste 0 kWh: € 0,70 per kWh\n- De resterende energie: \
         € 0,45 per kWh"
    );
}

/// A duration bound is anchored to the start of the session in Dutch too, so "de eerste 15
/// minuten" cannot be read as 15 minutes of the dimension it is listed under.
#[test]
fn dutch_duration_bound_is_anchored_to_the_session_start() {
    let rendered = explain_nl(
        r#"{
            "id": "idle-after-quarter", "country_code": "NL", "party_id": "TST",
            "currency": "EUR",
            "elements": [
                {
                    "restrictions": { "min_duration": 900 },
                    "price_components": [
                        { "type": "PARKING_TIME", "price": 4.998, "step_size": 1 }
                    ]
                }
            ]
        }"#,
    );

    assert_eq!(
        rendered,
        "**Parkeertijd (aangesloten maar niet aan het laden):** meer dan 15 minuten na het \
         aansluiten, € 4,998 per uur."
    );
}

/// Dutch agrees the article of an "eerste" phrase with the number of the amount: a lone hour takes
/// "het", any counted amount takes "de".
#[test]
fn dutch_first_article_agrees_with_the_amount() {
    let singular = explain_nl(
        r#"{
            "id": "one-hour", "country_code": "NL", "party_id": "TST", "currency": "EUR",
            "elements": [
                {
                    "restrictions": { "max_duration": 3600 },
                    "price_components": [{ "type": "TIME", "price": 0.50, "step_size": 1 }]
                }
            ]
        }"#,
    );

    assert_eq!(
        singular,
        "**Tarief tijdens het laden:** het eerste 1 uur na het aansluiten, € 0,50 per uur."
    );

    let plural = explain_nl(
        r#"{
            "id": "three-hours", "country_code": "NL", "party_id": "TST", "currency": "EUR",
            "elements": [
                {
                    "restrictions": { "max_duration": 10800 },
                    "price_components": [{ "type": "TIME", "price": 0.50, "step_size": 1 }]
                }
            ]
        }"#,
    );

    assert_eq!(
        plural,
        "**Tarief tijdens het laden:** de eerste 3 uur na het aansluiten, € 0,50 per uur."
    );
}