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
#![allow(clippy::missing_panics_doc, reason = "tests are allowed to panic")]
#![allow(clippy::panic, reason = "tests are allowed to panic")]

use rust_decimal_macros::dec;

use crate::{
    Version, cdr, json, price,
    price::test::UnwrapReport as _,
    tariff,
    test::{self},
};

/// A one-hour session charging `energy_kwh`, from 10:00 to 11:00 on 2022-01-13.
///
/// OCPI requires a CDR to state `total_cost`, so this one states `0`. `cdr::price` reports that
/// value in `price::Total::cdr`. Every assertion in this file reads `price::Total::calculated`,
/// so no test depends on the stated value.
fn simple_cdr_json(energy_kwh: f64) -> String {
    serde_json::json!({
        "country_code": "NL",
        "party_id": "TDR",
        "start_date_time": "2022-01-13T10:00:00Z",
        "end_date_time": "2022-01-13T11:00:00Z",
        "currency": "EUR",
        "tariffs": [],
        "cdr_location": { "country": "NLD" },
        "charging_periods": [
            {
                "start_date_time": "2022-01-13T10:00:00Z",
                "dimensions": [
                    { "type": "ENERGY", "volume": energy_kwh }
                ]
            }
        ],
        "total_cost": { "excl_vat": 0.00, "incl_vat": 0.00 },
        "total_time": 1.0,
        "total_energy": energy_kwh,
        "last_updated": "2022-01-13T00:00:00Z"
    })
    .to_string()
}

// Tariff: 0.50 EUR/kWh excl. VAT, 10% VAT, min_price = 5.00 / 5.50
fn tariff_with_min_price_json() -> String {
    serde_json::json!({
        "id": "T1",
        "country_code": "NL",
        "party_id": "TDR",
        "currency": "EUR",
        "min_price": { "excl_vat": 5.00, "incl_vat": 5.50 },
        "elements": [
            {
                "price_components": [
                    { "type": "ENERGY", "price": 0.50, "vat": 10.0, "step_size": 1 }
                ]
            }
        ],
        "last_updated": "2022-01-01T00:00:00Z"
    })
    .to_string()
}

// Tariff: 0.50 EUR/kWh excl. VAT, 10% VAT, max_price = 3.00 / 3.30
fn tariff_with_max_price_json() -> String {
    serde_json::json!({
        "id": "T2",
        "country_code": "NL",
        "party_id": "TDR",
        "currency": "EUR",
        "max_price": { "excl_vat": 3.00, "incl_vat": 3.30 },
        "elements": [
            {
                "price_components": [
                    { "type": "ENERGY", "price": 0.50, "vat": 10.0, "step_size": 1 }
                ]
            }
        ],
        "last_updated": "2022-01-01T00:00:00Z"
    })
    .to_string()
}

#[test]
fn should_clamp_total_cost_to_min_price() {
    test::setup();

    // 4 kWh * 0.50 = 2.00 excl., 2.20 incl. -- below min of 5.00 / 5.50
    let cdr_json = simple_cdr_json(4.0);
    let tariff_json = tariff_with_min_price_json();

    let (report, warning_ids) = price_against(&cdr_json, &tariff_json);

    let total_cost = report.total_cost.calculated.unwrap();
    assert_eq!(total_cost.excl_vat, dec!(5.00).into());
    assert_eq!(total_cost.incl_vat, Some(dec!(5.50).into()));

    assert!(
        warning_ids
            .iter()
            .any(|id| id == "total_cost_clamped_to_min"),
        "expected TotalCostClampedToMin warning, got: {warning_ids:#?}"
    );
}

#[test]
fn should_clamp_total_cost_to_max_price() {
    test::setup();

    // 10 kWh * 0.50 = 5.00 excl., 5.50 incl. -- above max of 3.00 / 3.30
    let cdr_json = simple_cdr_json(10.0);
    let tariff_json = tariff_with_max_price_json();

    let (report, warning_ids) = price_against(&cdr_json, &tariff_json);

    let total_cost = report.total_cost.calculated.unwrap();
    assert_eq!(total_cost.excl_vat, dec!(3.00).into());
    assert_eq!(total_cost.incl_vat, Some(dec!(3.30).into()));

    assert!(
        warning_ids
            .iter()
            .any(|id| id == "total_cost_clamped_to_max"),
        "expected TotalCostClampedToMax warning, got: {warning_ids:#?}"
    );
}

#[test]
fn should_not_clamp_when_cost_within_bounds() {
    test::setup();

    // 8 kWh * 0.50 = 4.00 excl., 4.40 incl. -- between min 3.00 and max 6.00, no clamping
    let cdr_json = simple_cdr_json(8.0);
    let tariff_json = serde_json::json!({
        "id": "T3",
        "country_code": "NL",
        "party_id": "TDR",
        "currency": "EUR",
        "min_price": { "excl_vat": 3.00, "incl_vat": 3.30 },
        "max_price": { "excl_vat": 6.00, "incl_vat": 6.60 },
        "elements": [
            {
                "price_components": [
                    { "type": "ENERGY", "price": 0.50, "vat": 10.0, "step_size": 1 }
                ]
            }
        ],
        "last_updated": "2022-01-01T00:00:00Z"
    })
    .to_string();

    let (report, warning_ids) = price_against(&cdr_json, &tariff_json);

    let total_cost = report.total_cost.calculated.unwrap();
    assert_eq!(total_cost.excl_vat, dec!(4.00).into());
    assert_eq!(total_cost.incl_vat, Some(dec!(4.40).into()));

    assert!(
        !warning_ids
            .iter()
            .any(|id| id == "total_cost_clamped_to_min" || id == "total_cost_clamped_to_max"),
        "unexpected clamping warning: {warning_ids:#?}"
    );
}

/// The reproduction from `#499`, a `min_price` on a session that no element priced.
///
/// The only element is restricted to the weekend, so a Thursday session prices nothing and the
/// calculated total is `None`. The `min_price` has to seed that total rather than clamp one
/// that does not exist.
fn tariff_with_min_price_and_weekend_only_element_json() -> String {
    serde_json::json!({
        "id": "T4",
        "country_code": "NL",
        "party_id": "TDR",
        "currency": "EUR",
        "min_price": { "excl_vat": 5.00, "incl_vat": 5.50 },
        "elements": [
            {
                "price_components": [
                    { "type": "ENERGY", "price": 0.50, "vat": 10.0, "step_size": 1 }
                ],
                "restrictions": { "day_of_week": ["SATURDAY", "SUNDAY"] }
            }
        ],
        "last_updated": "2022-01-01T00:00:00Z"
    })
    .to_string()
}

#[track_caller]
fn price_against(cdr_json: &str, tariff_json: &str) -> (price::Report, Vec<String>) {
    // A test CDR or tariff states only the fields its test depends on, so missing fields are
    // expected. Any other warning, such as a misspelled key reported as an unexpected field, means
    // the input is not what the test says it is.
    let (cdr, mut cdr_warnings) =
        cdr::from_json(json::parse_object(cdr_json).unwrap(), Version::V221).into_parts();
    cdr_warnings.remove_missing_fields();
    test::assert_schema_warnings(&cdr_warnings, &[]);

    let (tariff, mut tariff_warnings) =
        tariff::from_json(json::parse_object(tariff_json).unwrap(), Version::V221).into_parts();
    tariff_warnings.remove_missing_fields();
    test::assert_schema_warnings(&tariff_warnings, &[]);

    let report = cdr::price(
        &cdr,
        price::TariffSource::Override(vec![tariff]),
        chrono_tz::Tz::UTC,
    )
    .unwrap_report(cdr.as_json_str());

    let (report, warnings) = report.into_parts();
    let ids = warnings
        .path_id_map()
        .values()
        .flatten()
        .map(|id| id.as_str().to_owned())
        .collect();

    (report, ids)
}

#[test]
fn should_raise_total_cost_to_min_price_when_no_element_priced_the_session() {
    test::setup();

    // 2022-01-13 is a Thursday, so the weekend-only element never applies and every
    // calculated total is `None`.
    let cdr_json = simple_cdr_json(4.0);
    let tariff_json = tariff_with_min_price_and_weekend_only_element_json();

    let (report, warning_ids) = price_against(&cdr_json, &tariff_json);

    let total_cost = report.total_cost.calculated.unwrap_or_else(|| {
        panic!("`min_price` should seed a total for a session no element priced");
    });
    assert_eq!(total_cost.excl_vat, dec!(5.00).into());
    assert_eq!(total_cost.incl_vat, Some(dec!(5.50).into()));

    assert!(
        warning_ids
            .iter()
            .any(|id| id == "total_cost_clamped_to_min"),
        "expected TotalCostClampedToMin warning, got: {warning_ids:#?}"
    );
}

#[test]
fn should_leave_total_cost_absent_when_no_element_priced_and_no_min_price_is_set() {
    test::setup();

    // The same weekend-only element without a `min_price`. Nothing priced the session and no
    // `min_price` seeds a total, so the calculated total is absent rather than zero.
    let cdr_json = simple_cdr_json(4.0);
    let tariff_json = serde_json::json!({
        "id": "T5",
        "country_code": "NL",
        "party_id": "TDR",
        "currency": "EUR",
        "elements": [
            {
                "price_components": [
                    { "type": "ENERGY", "price": 0.50, "vat": 10.0, "step_size": 1 }
                ],
                "restrictions": { "day_of_week": ["SATURDAY", "SUNDAY"] }
            }
        ],
        "last_updated": "2022-01-01T00:00:00Z"
    })
    .to_string();

    let (report, warning_ids) = price_against(&cdr_json, &tariff_json);

    assert_eq!(report.total_cost.calculated, None);
    assert!(
        !warning_ids
            .iter()
            .any(|id| id == "total_cost_clamped_to_min"),
        "expected no clamp warning, got: {warning_ids:#?}"
    );
}

#[test]
fn should_clamp_a_seeded_min_price_that_exceeds_max_price() {
    test::setup();

    // A tariff whose `min_price` is above its own `max_price`, which both `lint` and the
    // pricer report. The `min_price` seeds the total first and the `max_price` then clamps
    // the total down, so the order the two run in is pinned rather than left to chance.
    let cdr_json = simple_cdr_json(4.0);
    let tariff_json = serde_json::json!({
        "id": "T6",
        "country_code": "NL",
        "party_id": "TDR",
        "currency": "EUR",
        "min_price": { "excl_vat": 5.00, "incl_vat": 5.50 },
        "max_price": { "excl_vat": 3.00, "incl_vat": 3.30 },
        "elements": [
            {
                "price_components": [
                    { "type": "ENERGY", "price": 0.50, "vat": 10.0, "step_size": 1 }
                ],
                "restrictions": { "day_of_week": ["SATURDAY", "SUNDAY"] }
            }
        ],
        "last_updated": "2022-01-01T00:00:00Z"
    })
    .to_string();

    let (report, warning_ids) = price_against(&cdr_json, &tariff_json);

    let total_cost = report.total_cost.calculated.unwrap();
    assert_eq!(total_cost.excl_vat, dec!(3.00).into());
    assert_eq!(total_cost.incl_vat, Some(dec!(3.30).into()));

    assert!(
        warning_ids
            .iter()
            .any(|id| id == "total_cost_clamped_to_min"),
        "expected TotalCostClampedToMin warning, got: {warning_ids:#?}"
    );
    assert!(
        warning_ids
            .iter()
            .any(|id| id == "total_cost_clamped_to_max"),
        "expected TotalCostClampedToMax warning, got: {warning_ids:#?}"
    );
}

/// A session of 10 kWh at EUR 0.49 with 21% VAT, costing 4.90 excl VAT and 5.9290 incl VAT.
///
/// The tests here pair that session with bounds that hold on one value and fail on the other.
/// A bound has to imply a VAT rate other than 21% for that to happen, so bounds at the
/// session's own rate cannot reach these cases.
fn cdr_at_21_percent_vat_json() -> String {
    simple_cdr_json(10.0)
}

/// The session of `cdr_at_21_percent_vat_json` with its energy negated, so the pricer calculates
/// a `total_cost.excl_vat` of -4.90 and a `total_cost.incl_vat` of -5.9290.
///
/// Both calculated values are negative, so the value further from zero is the smaller number. On
/// a positive session, `total_cost.excl_vat` at or below `total_cost.incl_vat` means the VAT is
/// not negative. On a negative session that comparison fails even when the total is correct. On a
/// positive session the comparison and `money::Price::vat_is_consistent` agree and a test cannot
/// tell them apart. On this session they disagree, which is what the tests here need.
///
/// This CDR is not a Credit CDR. Setting `credit` to `true` would make the pricer negate the total
/// after `min_price` and `max_price` have bounded it, so the tests here would read a positive
/// total. The CDR states the `total_cost` of `0` from `simple_cdr_json`, which no test reads.
fn negative_cdr_at_21_percent_vat_json() -> String {
    simple_cdr_json(-10.0)
}

fn tariff_at_21_percent_vat_json(id: &str, bounds: &serde_json::Value) -> String {
    let mut tariff = serde_json::json!({
        "id": id,
        "country_code": "NL",
        "party_id": "TDR",
        "currency": "EUR",
        "elements": [
            {
                "price_components": [
                    { "type": "ENERGY", "price": 0.49, "vat": 21.0, "step_size": 1 }
                ]
            }
        ],
        "last_updated": "2022-01-01T00:00:00Z"
    });

    let object = tariff.as_object_mut().unwrap();
    for (field, value) in bounds.as_object().unwrap() {
        object.insert(field.clone(), value.clone());
    }

    tariff.to_string()
}

#[test]
fn should_clamp_the_incl_vat_total_while_the_excl_vat_total_is_within_max_price() {
    test::setup();

    // 4.90 excl VAT is under the 5.00 bound, so only the `incl_vat` rule applies. Comparing
    // whole `Price` values finds the total below the bound and leaves 5.9290 incl VAT standing
    // against a 5.45 bound, unclamped and unwarned.
    //
    // A clamp that assigned the whole `max_price`, rather than only its `incl_vat`, would overwrite
    // `excl_vat` with 5.00. The first assertion catches that.
    let tariff_json = tariff_at_21_percent_vat_json(
        "V1",
        &serde_json::json!({ "max_price": { "excl_vat": 5.00, "incl_vat": 5.45 } }),
    );

    let (report, warning_ids) = price_against(&cdr_at_21_percent_vat_json(), &tariff_json);

    let total_cost = report.total_cost.calculated.unwrap();
    assert_eq!(total_cost.excl_vat, dec!(4.90).into());
    assert_eq!(total_cost.incl_vat, Some(dec!(5.45).into()));

    assert!(
        warning_ids
            .iter()
            .any(|id| id == "total_cost_clamped_to_max"),
        "expected TotalCostClampedToMax warning, got: {warning_ids:#?}"
    );
}

#[test]
fn should_raise_the_incl_vat_total_while_the_excl_vat_total_is_above_min_price() {
    test::setup();

    // The `min_price` mirror of `should_clamp_the_incl_vat_total_while_the_excl_vat_total_is_within_max_price`.
    // 4.90 excl VAT clears the 4.00 bound, while 5.9290 incl VAT falls short of the 6.00 bound.
    let tariff_json = tariff_at_21_percent_vat_json(
        "V2",
        &serde_json::json!({ "min_price": { "excl_vat": 4.00, "incl_vat": 6.00 } }),
    );

    let (report, warning_ids) = price_against(&cdr_at_21_percent_vat_json(), &tariff_json);

    let total_cost = report.total_cost.calculated.unwrap();
    assert_eq!(total_cost.excl_vat, dec!(4.90).into());
    assert_eq!(total_cost.incl_vat, Some(dec!(6.00).into()));

    assert!(
        warning_ids
            .iter()
            .any(|id| id == "total_cost_clamped_to_min"),
        "expected TotalCostClampedToMin warning, got: {warning_ids:#?}"
    );
}

#[test]
fn should_report_min_above_max_when_pricing_rather_than_only_when_linting() {
    test::setup();

    // Pricing does not run the lint pass, so without this warning a contradictory tariff
    // reports two clamps and nothing about the bounds contradicting each other. Here the
    // `min_price` is above the `max_price` on `excl_vat` alone.
    let tariff_json = tariff_at_21_percent_vat_json(
        "V9",
        &serde_json::json!({
            "min_price": { "excl_vat": 6.00 },
            "max_price": { "excl_vat": 5.00 },
        }),
    );

    let (_report, warning_ids) = price_against(&cdr_at_21_percent_vat_json(), &tariff_json);

    assert!(
        warning_ids
            .iter()
            .any(|id| id == "min_price_is_greater_than_max"),
        "expected MinPriceIsGreaterThanMax warning, got: {warning_ids:#?}"
    );
}

#[test]
fn should_not_report_min_above_max_when_min_price_and_max_price_each_clamp_one_value() {
    test::setup();

    // Both clamps fire while the `min_price` stays below the `max_price`. Deriving
    // `min_above_max` from both clamps having fired would report this tariff as contradictory.
    let tariff_json = tariff_at_21_percent_vat_json(
        "VA",
        &serde_json::json!({
            "min_price": { "excl_vat": 5.00, "incl_vat": 5.00 },
            "max_price": { "excl_vat": 10.00, "incl_vat": 5.50 },
        }),
    );

    let (_report, warning_ids) = price_against(&cdr_at_21_percent_vat_json(), &tariff_json);

    assert!(
        warning_ids
            .iter()
            .any(|id| id == "total_cost_clamped_to_min")
            && warning_ids
                .iter()
                .any(|id| id == "total_cost_clamped_to_max"),
        "expected both clamp warnings, got: {warning_ids:#?}"
    );
    assert!(
        !warning_ids
            .iter()
            .any(|id| id == "min_price_is_greater_than_max"),
        "unexpected MinPriceIsGreaterThanMax warning, got: {warning_ids:#?}"
    );
}

#[test]
fn should_bound_both_values_when_each_is_outside_a_different_bound() {
    test::setup();

    // 4.90 excl VAT is below the 5.00 `min_price` and 5.9290 incl VAT is above the 5.80
    // `max_price`. Both bounds clamp one value each, in a single pass.
    let tariff_json = tariff_at_21_percent_vat_json(
        "V4",
        &serde_json::json!({
            "min_price": { "excl_vat": 5.00, "incl_vat": 5.00 },
            "max_price": { "excl_vat": 99.00, "incl_vat": 5.80 }
        }),
    );

    let (report, warning_ids) = price_against(&cdr_at_21_percent_vat_json(), &tariff_json);

    let total_cost = report.total_cost.calculated.unwrap();
    assert_eq!(total_cost.excl_vat, dec!(5.00).into());
    assert_eq!(total_cost.incl_vat, Some(dec!(5.80).into()));

    assert!(
        warning_ids
            .iter()
            .any(|id| id == "total_cost_clamped_to_min"),
        "expected TotalCostClampedToMin warning, got: {warning_ids:#?}"
    );
    assert!(
        warning_ids
            .iter()
            .any(|id| id == "total_cost_clamped_to_max"),
        "expected TotalCostClampedToMax warning, got: {warning_ids:#?}"
    );
}

#[test]
fn should_report_the_incl_vat_total_as_unknown_when_an_excl_only_min_price_raises_past_it() {
    test::setup();

    // `min_price.excl_vat` is 6.00 and `min_price.incl_vat` is absent. Replacing
    // `total_cost.excl_vat` with 6.00 does not change `total_cost.incl_vat`, which is 5.9290.
    // The two then imply a VAT rate below `0`. That is what
    // `money::Warning::ImpliedVatIsNegative` reports on a price a tariff states, so the pricer
    // discards `total_cost.incl_vat`.
    let tariff_json = tariff_at_21_percent_vat_json(
        "V5",
        &serde_json::json!({ "min_price": { "excl_vat": 6.00 } }),
    );

    let (report, warning_ids) = price_against(&cdr_at_21_percent_vat_json(), &tariff_json);

    let total_cost = report.total_cost.calculated.unwrap();
    assert_eq!(total_cost.excl_vat, dec!(6.00).into());
    assert_eq!(total_cost.incl_vat, None);

    assert!(
        warning_ids
            .iter()
            .any(|id| id == "total_cost_clamped_to_min"),
        "expected TotalCostClampedToMin warning, got: {warning_ids:#?}"
    );

    // An absent inclusive total is otherwise indistinguishable from one a v2.1.1 tariff never
    // had. The warning reports the discard, not the clamp.
    assert!(
        warning_ids
            .iter()
            .any(|id| id == "total_cost_inclusive_vat_unknown"),
        "expected TotalCostInclusiveVatUnknown warning, got: {warning_ids:#?}"
    );
}

#[test]
fn should_keep_the_incl_vat_total_when_an_excl_only_min_price_stays_below_it() {
    test::setup();

    // `min_price.excl_vat` is 5.00 and `min_price.incl_vat` is absent. Replacing
    // `total_cost.excl_vat` with 5.00 does not change `total_cost.incl_vat`, which is 5.9290.
    // The two imply a VAT rate above `0`, so the pricer keeps `total_cost.incl_vat`. Discarding
    // on every replacement would lose the only such value the session produced.
    let tariff_json = tariff_at_21_percent_vat_json(
        "V8",
        &serde_json::json!({ "min_price": { "excl_vat": 5.00 } }),
    );

    let (report, warning_ids) = price_against(&cdr_at_21_percent_vat_json(), &tariff_json);

    let total_cost = report.total_cost.calculated.unwrap();
    assert_eq!(total_cost.excl_vat, dec!(5.00).into());
    assert_eq!(total_cost.incl_vat, Some(dec!(5.9290).into()));

    assert!(
        warning_ids
            .iter()
            .any(|id| id == "total_cost_clamped_to_min"),
        "expected TotalCostClampedToMin warning, got: {warning_ids:#?}"
    );

    assert!(
        !warning_ids
            .iter()
            .any(|id| id == "total_cost_inclusive_vat_unknown"),
        "unexpected TotalCostInclusiveVatUnknown warning, got: {warning_ids:#?}"
    );
}

#[test]
fn should_keep_the_incl_vat_total_when_an_excl_only_min_price_raises_a_negative_total() {
    test::setup();

    // `min_price.excl_vat` is -4.00 and `min_price.incl_vat` is absent. Replacing
    // `total_cost.excl_vat` with -4.00 does not change `total_cost.incl_vat`, which is -5.9290.
    // The two imply a VAT rate above `0`, so the pricer keeps `total_cost.incl_vat`.
    //
    // Asking whether `total_cost.excl_vat` is above `total_cost.incl_vat` would discard here,
    // because -4.00 is above -5.9290.
    let tariff_json = tariff_at_21_percent_vat_json(
        "V11",
        &serde_json::json!({ "min_price": { "excl_vat": -4.00 } }),
    );

    let (report, warning_ids) = price_against(&negative_cdr_at_21_percent_vat_json(), &tariff_json);

    let total_cost = report.total_cost.calculated.unwrap();
    assert_eq!(total_cost.excl_vat, dec!(-4.00).into());
    assert_eq!(total_cost.incl_vat, Some(dec!(-5.9290).into()));

    assert!(
        warning_ids
            .iter()
            .any(|id| id == "total_cost_clamped_to_min"),
        "expected TotalCostClampedToMin warning, got: {warning_ids:#?}"
    );

    assert!(
        !warning_ids
            .iter()
            .any(|id| id == "total_cost_inclusive_vat_unknown"),
        "unexpected TotalCostInclusiveVatUnknown warning, got: {warning_ids:#?}"
    );
}

#[test]
fn should_report_the_incl_vat_total_as_unknown_when_a_min_price_raises_a_negative_total_past_zero()
{
    test::setup();

    // `min_price.excl_vat` is 1.00 and `min_price.incl_vat` is absent. Replacing
    // `total_cost.excl_vat` with 1.00 does not change `total_cost.incl_vat`, which is -5.9290.
    // The two differ in sign, one of the two shapes
    // `money::Warning::ImpliedVatIsNegative` reports, so the pricer discards
    // `total_cost.incl_vat`.
    //
    // A `total_cost.excl_vat` above `total_cost.incl_vat` would discard here too, so this case
    // pins the sign half of the rule rather than guarding the fix.
    let tariff_json = tariff_at_21_percent_vat_json(
        "V10",
        &serde_json::json!({ "min_price": { "excl_vat": 1.00 } }),
    );

    let (report, warning_ids) = price_against(&negative_cdr_at_21_percent_vat_json(), &tariff_json);

    let total_cost = report.total_cost.calculated.unwrap();
    assert_eq!(total_cost.excl_vat, dec!(1.00).into());
    assert_eq!(total_cost.incl_vat, None);

    assert!(
        warning_ids
            .iter()
            .any(|id| id == "total_cost_inclusive_vat_unknown"),
        "expected TotalCostInclusiveVatUnknown warning, got: {warning_ids:#?}"
    );
}

#[test]
fn should_keep_the_incl_vat_total_when_an_excl_only_min_price_raises_nothing() {
    test::setup();

    // 4.90 excl VAT already clears the 4.00 `min_price`, so the clamp changes nothing. The
    // `min_price` states no inclusive value, and the session's own 5.9290 incl VAT stands.
    let tariff_json = tariff_at_21_percent_vat_json(
        "V6",
        &serde_json::json!({ "min_price": { "excl_vat": 4.00 } }),
    );

    let (report, warning_ids) = price_against(&cdr_at_21_percent_vat_json(), &tariff_json);

    let total_cost = report.total_cost.calculated.unwrap();
    assert_eq!(total_cost.excl_vat, dec!(4.90).into());
    assert_eq!(total_cost.incl_vat, Some(dec!(5.9290).into()));

    assert!(
        !warning_ids
            .iter()
            .any(|id| id == "total_cost_clamped_to_min"),
        "unexpected TotalCostClampedToMin warning, got: {warning_ids:#?}"
    );

    assert!(
        !warning_ids
            .iter()
            .any(|id| id == "total_cost_inclusive_vat_unknown"),
        "unexpected TotalCostInclusiveVatUnknown warning, got: {warning_ids:#?}"
    );
}

#[test]
fn should_keep_the_incl_vat_total_when_an_excl_only_max_price_lowers_it() {
    test::setup();

    // `max_price.excl_vat` is 4.00 and `max_price.incl_vat` is absent. Replacing
    // `total_cost.excl_vat` with 4.00 does not change `total_cost.incl_vat`, which is 5.9290.
    // The `max_price` arm never compares the two, so `total_cost.incl_vat` survives whatever
    // rate they imply. This case pins only that the pricer does not discard it.
    let tariff_json = tariff_at_21_percent_vat_json(
        "V7",
        &serde_json::json!({ "max_price": { "excl_vat": 4.00 } }),
    );

    let (report, warning_ids) = price_against(&cdr_at_21_percent_vat_json(), &tariff_json);

    let total_cost = report.total_cost.calculated.unwrap();
    assert_eq!(total_cost.excl_vat, dec!(4.00).into());
    assert_eq!(total_cost.incl_vat, Some(dec!(5.9290).into()));

    assert!(
        warning_ids
            .iter()
            .any(|id| id == "total_cost_clamped_to_max"),
        "expected TotalCostClampedToMax warning, got: {warning_ids:#?}"
    );
}