kontochronik 0.4.0

Long-Term archive for account transactions
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
use std::{
    cmp::Reverse,
    collections::{HashMap, HashSet},
    fs,
    path::Path,
};

use anyhow::{Context as _, bail};
use rust_decimal::Decimal;

use crate::{
    archive::{self, ArchiveRecord},
    fingerprint::{Fingerprint, FingerprintInput},
    iban::Iban,
    util::canonical_iban,
};

mod gls;
mod sparkasse;

pub use self::{gls::parse_gls, sparkasse::parse_sparkasse};

/// Outcome of one [`import`] run.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ImportSummary {
    /// Entries newly added to the archive.
    pub imported: usize,
    /// Entries of the source file that were already archived.
    pub duplicates: usize,
    /// Entries in the archive after the import.
    pub total: usize,
}

/// Outcome of [`verify_archive`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct VerifySummary {
    /// Entries in the archive.
    pub total: usize,
    /// Entries without a stored fingerprint (hand-added rows);
    /// the next import will fill them in.
    pub missing_fingerprints: usize,
    /// Entries without a balance; invisible to the balance chain.
    pub missing_balances: usize,
}

/// Whether an import may leave a provable gap in the archive (see [`import`]).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GapPolicy {
    Reject,
    /// For the case that the missing range can no longer be exported.
    Accept,
}

/// Verifies the archive at `archive_path` without modifying it:
/// every stored fingerprint must match its entry's content, the
/// archive must hold a single account and currency with amounts
/// representable in two decimal places, and the day-by-day balance
/// chain must be gapless.
/// Entries without a fingerprint are legal and only counted.
pub fn verify_archive<A: AsRef<Path>>(archive_path: A) -> anyhow::Result<VerifySummary> {
    let archive_path = archive_path.as_ref();
    let mut records = archive::read_archive(archive_path)?;
    normalize_direction(&mut records);
    warn_about_implausible_ibans(&records);
    let missing_fingerprints = records
        .iter()
        .filter(|record| record.fingerprint.is_none())
        .count();
    let missing_balances = records
        .iter()
        .filter(|record| record.balance_after_booking.is_none())
        .count();
    complete_and_verify_fingerprints(&mut records)
        .with_context(|| format!("Archive {} is inconsistent", archive_path.display()))?;
    ensure_single_account(records.iter())?;
    ensure_single_currency(records.iter())?;
    ensure_representable_amounts(records.iter())?;
    ensure_balance_chain(&records)?;
    Ok(VerifySummary {
        total: records.len(),
        missing_fingerprints,
        missing_balances,
    })
}

/// Every day's closing balance must equal the previous closing balance
/// plus the day's amounts; a break means missing bookings or a
/// tampered amount, both invisible to the fingerprint check.
/// Checked per day because the order within a day is not guaranteed.
/// The amounts of all rows keep the sum running, but only days that
/// store a balance are verified; the oldest stored balance seeds the
/// chain and is itself unverifiable.
fn ensure_balance_chain(records: &[ArchiveRecord]) -> anyhow::Result<()> {
    // Chronological day groups; the archive itself is newest first.
    let mut days: Vec<&[ArchiveRecord]> = records
        .chunk_by(|a, b| a.booking_date == b.booking_date)
        .collect();
    days.reverse();
    let mut closing: Option<Decimal> = None;
    for day in days {
        let Some(previous) = closing else {
            closing = day.iter().find_map(|record| record.balance_after_booking);
            continue;
        };
        let amounts: Decimal = day.iter().map(|record| record.amount).sum();
        let expected = previous + amounts;
        let stored: Vec<Decimal> = day
            .iter()
            .filter_map(|record| record.balance_after_booking)
            .collect();
        if !stored.is_empty() && !stored.contains(&expected) {
            bail!(
                "Balance chain broken on {}: the previous closing balance {} \
                 plus the day's amounts {} gives {}, but no booking on that \
                 day has this balance",
                day[0].booking_date,
                previous,
                amounts,
                expected
            );
        }
        closing = Some(expected);
    }
    Ok(())
}

/// Imports a file into the archive at `archive_path`, creating
/// the archive if it doesn't exist yet.
/// `parse` decodes the raw bytes of the file into archive records
/// (e.g. [`parse_gls`]).
/// Entries whose fingerprint is already archived are skipped,
/// so overlapping export date ranges are safe.
///
/// New entries must seamlessly continue the archive, proven by the
/// balance chain or, for files without balances, by overlap with
/// already archived entries; `gap_policy` decides whether a missing
/// overlap is an error. A violation leaves the archive untouched.
pub fn import<P, D, A>(
    parse: P,
    data: D,
    archive_path: A,
    gap_policy: GapPolicy,
) -> anyhow::Result<ImportSummary>
where
    P: FnOnce(&[u8]) -> anyhow::Result<Vec<ArchiveRecord>>,
    D: AsRef<Path>,
    A: AsRef<Path>,
{
    let data = data.as_ref();
    let archive_path = archive_path.as_ref();

    let mut records = if archive_path.exists() {
        archive::read_archive(archive_path)?
    } else {
        vec![]
    };

    normalize_direction(&mut records);
    complete_and_verify_fingerprints(&mut records)
        .with_context(|| format!("Archive {} is inconsistent", archive_path.display()))?;

    log::debug!("Read data {}", data.display());
    let bytes =
        fs::read(data).with_context(|| format!("Failed to read data {}", data.display()))?;
    let mut incoming =
        parse(&bytes).with_context(|| format!("Failed to import {}", data.display()))?;
    normalize_direction(&mut incoming);
    warn_about_implausible_ibans(&incoming);

    ensure_single_account(records.iter().chain(incoming.iter()))?;
    ensure_single_currency(records.iter().chain(incoming.iter()))?;
    ensure_representable_amounts(records.iter().chain(incoming.iter()))?;

    assign_fingerprints(&mut incoming);

    let mut known: HashSet<_> = records
        .iter()
        .map(stored_fingerprint)
        .collect::<anyhow::Result<_>>()?;

    let archive_was_empty = records.is_empty();
    let incoming_total = incoming.len();
    let incoming_carries_balances = incoming
        .iter()
        .any(|record| record.balance_after_booking.is_some());

    let mut imported = 0;
    let mut duplicates = 0;

    for record in incoming {
        let fingerprint = stored_fingerprint(&record)?;
        if known.insert(fingerprint) {
            records.push(record);
            imported += 1;
        } else {
            duplicates += 1;
        }
    }

    if !archive_was_empty && incoming_total > 0 && !incoming_carries_balances && duplicates == 0 {
        match gap_policy {
            GapPolicy::Reject => bail!(
                "The file does not overlap the archive: none of its {incoming_total} \
                 entries is already archived, and without balances a gapless \
                 continuation cannot be proven. Export a longer range that reaches \
                 back to an archived entry, or, if the missing range can no longer \
                 be exported, explicitly accept a permanent gap."
            ),
            GapPolicy::Accept => {
                log::warn!("Accepted a possible gap: the file does not overlap the archive");
            }
        }
    }

    // Stable, newest first:
    // existing rows and, within a day, the bank's order stay untouched.
    // A fresh import forms one contiguous block right below the header.
    records.sort_by_key(|record| Reverse(record.booking_date));
    ensure_balance_chain(&records)?;
    archive::write_archive(archive_path, &records)?;

    let summary = ImportSummary {
        imported,
        duplicates,
        total: records.len(),
    };
    log::debug!(
        "Imported {} new entries ({} duplicates skipped), archive now holds {}",
        summary.imported,
        summary.duplicates,
        summary.total
    );
    Ok(summary)
}

/// The archive is ordered newest first, like the online-banking export;
/// oldest-first files are flipped. Detected via first vs. last row.
fn normalize_direction(records: &mut [ArchiveRecord]) {
    let is_ascending = records
        .first()
        .zip(records.last())
        .is_some_and(|(first, last)| first.booking_date < last.booking_date);
    if is_ascending {
        records.reverse();
    }
}

fn fingerprint_input(record: &ArchiveRecord, occurrence: u32) -> FingerprintInput {
    // Only a value that proves to be an IBAN names a counterparty.
    // Everything else the export formats render in this column
    // (placeholders, legacy account numbers,
    // the own account of bank-internal bookings)
    // means "no counterparty", uniformly across formats.
    let participant_iban = record
        .participant_iban
        .as_deref()
        .and_then(|raw| raw.parse().ok());
    FingerprintInput {
        account_iban: record.account_iban.clone(),
        participant_iban,
        amount: record.amount,
        booking_date: record.booking_date,
        value_date: record.value_date,
        occurrence,
    }
}

fn assign_fingerprints(records: &mut [ArchiveRecord]) {
    let mut occurrences: HashMap<String, u32> = HashMap::new();
    for record in records.iter_mut() {
        let key = fingerprint_input(record, 0).content_key();
        let counter = occurrences.entry(key).or_insert(0);
        let fingerprint = Fingerprint::new(&fingerprint_input(record, *counter));
        record.fingerprint = Some(fingerprint.to_string());
        *counter += 1;
    }
}

/// Ensures every archive row carries the fingerprint for its content.
/// Missing fingerprints (hand-added rows) are filled in;
/// mismatches abort, since they indicate edited data or algorithm drift.
/// Groups of identical-content rows are checked set-wise against the
/// occurrence counters 0..n.
fn complete_and_verify_fingerprints(records: &mut [ArchiveRecord]) -> anyhow::Result<()> {
    let mut groups: HashMap<String, Vec<usize>> = HashMap::new();

    for (index, record) in records.iter().enumerate() {
        groups
            .entry(fingerprint_input(record, 0).content_key())
            .or_default()
            .push(index);
    }

    for indices in groups.values() {
        let mut unassigned = (0..u32::try_from(indices.len())?)
            .map(|occurrence| {
                Fingerprint::new(&fingerprint_input(&records[indices[0]], occurrence))
            })
            .collect::<Vec<_>>();

        let mut missing = vec![];

        for &index in indices {
            let line = index + 2; // 1-based, after the header line
            match &records[index].fingerprint {
                Some(stored) => {
                    let stored: Fingerprint = stored
                        .parse()
                        .with_context(|| format!("Line {line}: invalid fingerprint"))?;
                    let Some(position) = unassigned.iter().position(|fp| *fp == stored) else {
                        bail!(
                            "Line {line}: stored fingerprint {stored} does not match the entry's content"
                        );
                    };
                    unassigned.remove(position);
                }
                None => missing.push(index),
            }
        }
        for (&index, fingerprint) in missing.iter().zip(&unassigned) {
            records[index].fingerprint = Some(fingerprint.to_string());
        }
    }
    Ok(())
}

fn stored_fingerprint(record: &ArchiveRecord) -> anyhow::Result<Fingerprint> {
    let stored = record
        .fingerprint
        .as_deref()
        .context("Entry without fingerprint")?;
    Ok(stored.parse()?)
}

/// Warns about counterparty values that look like an IBAN but fail
/// their checksum - an early tripwire for corrupted or mistyped data.
/// Values that don't look like IBANs (e.g. legacy account numbers in
/// the "Kontonummer/IBAN" column) are left alone, and nothing is ever
/// rejected: the archive keeps whatever the bank delivered.
/// (The own account is strictly typed and needs no heuristic.)
fn warn_about_implausible_ibans(records: &[ArchiveRecord]) {
    let mut seen = HashSet::new();
    for record in records {
        let Some(value) = record.participant_iban.as_deref() else {
            continue;
        };
        let canonical = canonical_iban(value);
        if implausible_iban(&canonical) && seen.insert(canonical.clone()) {
            log::warn!("IBAN {canonical} fails its checksum - possibly corrupted data");
        }
    }
}

/// An IBAN-shaped value (two letters, then two check digits)
/// that fails validation.
fn implausible_iban(canonical: &str) -> bool {
    let mut chars = canonical.chars();
    let country = chars.by_ref().take(2).all(|c| c.is_ascii_alphabetic());
    let check = chars.by_ref().take(2).all(|c| c.is_ascii_digit());
    canonical.len() >= 4 && country && check && canonical.parse::<Iban>().is_err()
}

fn ensure_single_account<'a>(
    records: impl Iterator<Item = &'a ArchiveRecord>,
) -> anyhow::Result<()> {
    let ibans: HashSet<&str> = records.map(|record| record.account_iban.as_str()).collect();
    if ibans.len() > 1 {
        bail!("An archive holds entries of a single bank account, found: {ibans:?}");
    }
    Ok(())
}

fn ensure_single_currency<'a>(
    records: impl Iterator<Item = &'a ArchiveRecord>,
) -> anyhow::Result<()> {
    let currencies: HashSet<&str> = records.map(|record| record.currency.as_str()).collect();
    if currencies.len() > 1 {
        bail!("An archive holds entries of a single currency, found: {currencies:?}");
    }
    Ok(())
}

// The archive format fixes amounts to exactly two decimal places.
// Currencies with three decimal places would silently lose data.
fn ensure_representable_amounts<'a>(
    mut records: impl Iterator<Item = &'a ArchiveRecord>,
) -> anyhow::Result<()> {
    fn fits(amount: Decimal) -> bool {
        amount.round_dp(2) == amount
    }
    if let Some(record) = records.find(|r| {
        !fits(r.amount)
            || r.balance_after_booking
                .is_some_and(|balance| !fits(balance))
    }) {
        bail!(
            "Amounts must be exactly representable with two decimal places, \
             found booking on {} with amount {} and balance {}",
            record.booking_date,
            record.amount,
            record
                .balance_after_booking
                .map_or_else(|| "(none)".to_owned(), |balance| balance.to_string())
        );
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use std::fs;

    use time::{Date, Month};

    use super::*;

    fn record(day: u8, amount: Decimal, balance_after_booking: Option<Decimal>) -> ArchiveRecord {
        let date = Date::from_calendar_date(2025, Month::February, day).unwrap();
        ArchiveRecord {
            booking_date: date,
            value_date: date,
            amount,
            currency: "EUR".to_owned(),
            participant_name: None,
            purpose: None,
            booking_text: "Gutschrift".to_owned(),
            participant_iban: None,
            participant_bic: None,
            creditor_id: None,
            mandate_reference: None,
            balance_after_booking,
            account_iban: "DE44500105175407324931".parse().unwrap(),
            account_bic: Some("GENODEM1GLS".to_owned()),
            account_bank_name: Some("Testbank".to_owned()),
            fingerprint: None,
        }
    }

    fn static_importer(
        records: Vec<ArchiveRecord>,
    ) -> impl Fn(&[u8]) -> anyhow::Result<Vec<ArchiveRecord>> {
        move |_bytes| Ok(records.clone())
    }

    #[test]
    fn only_valid_participant_ibans_reach_the_fingerprint() {
        let mut entry = record(1, Decimal::new(100, 2), None);
        for junk in ["0", "0000000000", "0100433182", "DE70000000000000000099"] {
            entry.participant_iban = Some(junk.to_owned());
            assert_eq!(fingerprint_input(&entry, 0).participant_iban, None);
        }
        entry.participant_iban = Some("DE89 3704 0044 0532 0130 00".to_owned());
        assert!(fingerprint_input(&entry, 0).participant_iban.is_some());
    }

    #[test]
    fn iban_plausibility_is_a_warning_heuristic() {
        // A valid IBAN and things that are not IBAN-shaped pass.
        assert!(!implausible_iban("DE89370400440532013000"));
        assert!(!implausible_iban("106531065")); // legacy account number
        assert!(!implausible_iban(""));
        // IBAN-shaped with a broken checksum: the tripwire fires.
        assert!(implausible_iban("DE70000000000000000099"));
        assert!(implausible_iban("DE89370400440532013001"));
    }

    /// The merge only ever sees [`ArchiveRecord`]s:
    /// any parse function can feed the archive.
    #[test]
    fn import_is_independent_of_the_source_format() {
        let dir = tempfile::tempdir().unwrap();
        let archive_path = dir.path().join("archive.csv");
        let export = dir.path().join("export.anything");
        fs::write(&export, b"opaque bytes").unwrap();
        let importer = static_importer(vec![
            record(2, Decimal::new(250, 2), Some(Decimal::new(350, 2))),
            record(1, Decimal::new(100, 2), Some(Decimal::new(100, 2))),
        ]);

        let summary = import(&importer, &export, &archive_path, GapPolicy::Reject).unwrap();
        assert_eq!(
            summary,
            ImportSummary {
                imported: 2,
                duplicates: 0,
                total: 2
            }
        );

        let summary = import(&importer, &export, &archive_path, GapPolicy::Reject).unwrap();
        assert_eq!(
            summary,
            ImportSummary {
                imported: 0,
                duplicates: 2,
                total: 2
            }
        );

        let summary = verify_archive(&archive_path).unwrap();
        assert_eq!(
            summary,
            VerifySummary {
                total: 2,
                missing_fingerprints: 0,
                missing_balances: 0
            }
        );
    }

    #[test]
    fn balance_less_imports_must_overlap_the_archive() {
        let dir = tempfile::tempdir().unwrap();
        let archive_path = dir.path().join("archive.csv");
        let export = dir.path().join("export.anything");
        fs::write(&export, b"opaque bytes").unwrap();

        let first = static_importer(vec![
            record(2, Decimal::new(250, 2), None),
            record(1, Decimal::new(100, 2), None),
        ]);
        import(&first, &export, &archive_path, GapPolicy::Reject).unwrap();
        let before = fs::read(&archive_path).unwrap();

        let disconnected = static_importer(vec![record(4, Decimal::new(-100, 2), None)]);
        let error = import(&disconnected, &export, &archive_path, GapPolicy::Reject).unwrap_err();
        assert!(format!("{error:#}").contains("does not overlap"));
        assert_eq!(fs::read(&archive_path).unwrap(), before);

        let summary = import(&disconnected, &export, &archive_path, GapPolicy::Accept).unwrap();
        assert_eq!(summary.imported, 1);

        let overlapping = static_importer(vec![
            record(5, Decimal::new(-50, 2), None),
            record(4, Decimal::new(-100, 2), None),
        ]);
        let summary = import(&overlapping, &export, &archive_path, GapPolicy::Reject).unwrap();
        assert_eq!(summary.imported, 1);

        let summary = verify_archive(&archive_path).unwrap();
        assert_eq!(
            summary,
            VerifySummary {
                total: 4,
                missing_fingerprints: 0,
                missing_balances: 4
            }
        );
    }

    #[test]
    fn import_rejects_a_broken_balance_chain() {
        let dir = tempfile::tempdir().unwrap();
        let archive_path = dir.path().join("archive.csv");
        let export = dir.path().join("export.anything");
        fs::write(&export, b"opaque bytes").unwrap();
        // Day 1 closes at 9.00, so the -1.00 booking on day 3 should
        // close at 8.00, but the import data claims 5.00.
        let importer = static_importer(vec![
            record(3, Decimal::new(-100, 2), Some(Decimal::new(500, 2))),
            record(1, Decimal::new(-100, 2), Some(Decimal::new(900, 2))),
        ]);

        let error = import(&importer, &export, &archive_path, GapPolicy::Reject).unwrap_err();
        assert!(format!("{error:#}").contains("Balance chain broken on 2025-02-03"));
        assert!(!archive_path.exists());
    }

    #[test]
    fn verify_counts_missing_fingerprints_without_modifying_the_file() {
        let dir = tempfile::tempdir().unwrap();
        let archive_path = dir.path().join("archive.csv");
        let records = [
            record(2, Decimal::new(-100, 2), Some(Decimal::new(800, 2))),
            record(1, Decimal::new(-100, 2), Some(Decimal::new(900, 2))),
        ];
        archive::write_archive(&archive_path, &records).unwrap();
        let before = fs::read(&archive_path).unwrap();

        let summary = verify_archive(&archive_path).unwrap();
        assert_eq!(
            summary,
            VerifySummary {
                total: 2,
                missing_fingerprints: 2,
                missing_balances: 0
            }
        );
        assert_eq!(fs::read(&archive_path).unwrap(), before);
    }

    #[test]
    fn verify_detects_a_broken_balance_chain() {
        let dir = tempfile::tempdir().unwrap();
        let archive_path = dir.path().join("archive.csv");
        // Day 1 closes at 9.00, so the -1.00 booking on day 3 should
        // close at 8.00, but the archive claims 5.00.
        let records = [
            record(3, Decimal::new(-100, 2), Some(Decimal::new(500, 2))),
            record(1, Decimal::new(-100, 2), Some(Decimal::new(900, 2))),
        ];
        archive::write_archive(&archive_path, &records).unwrap();

        let error = verify_archive(&archive_path).unwrap_err();
        assert!(format!("{error:#}").contains("Balance chain broken on 2025-02-03"));
    }

    #[test]
    fn verify_accepts_days_without_bookings() {
        let dir = tempfile::tempdir().unwrap();
        let archive_path = dir.path().join("archive.csv");
        // No booking on day 2; the chain continues on day 3.
        let records = [
            record(3, Decimal::new(-100, 2), Some(Decimal::new(800, 2))),
            record(1, Decimal::new(-100, 2), Some(Decimal::new(900, 2))),
        ];
        archive::write_archive(&archive_path, &records).unwrap();

        assert!(verify_archive(&archive_path).is_ok());
    }

    #[test]
    fn verify_tolerates_reordered_bookings_within_a_day() {
        let dir = tempfile::tempdir().unwrap();
        let archive_path = dir.path().join("archive.csv");
        // Two -1.00 bookings close day 2 at 7.00, but a merge of
        // overlapping exports left the 8.00 row on top.
        let records = [
            record(2, Decimal::new(-100, 2), Some(Decimal::new(800, 2))),
            record(2, Decimal::new(-100, 2), Some(Decimal::new(700, 2))),
            record(1, Decimal::new(-100, 2), Some(Decimal::new(900, 2))),
        ];
        archive::write_archive(&archive_path, &records).unwrap();

        let summary = verify_archive(&archive_path).unwrap();
        assert_eq!(summary.total, 3);
    }

    #[test]
    fn verify_accepts_an_archive_without_balances() {
        let dir = tempfile::tempdir().unwrap();
        let archive_path = dir.path().join("archive.csv");
        let records = [
            record(3, Decimal::new(-100, 2), None),
            record(1, Decimal::new(-100, 2), None),
        ];
        archive::write_archive(&archive_path, &records).unwrap();

        let summary = verify_archive(&archive_path).unwrap();
        assert_eq!(summary.missing_balances, 2);
    }

    #[test]
    fn chain_bridges_balance_less_rows() {
        let dir = tempfile::tempdir().unwrap();
        let archive_path = dir.path().join("archive.csv");
        // Day 1 closes at 9.00, day 2 carries no balance, so day 3
        // must close at 9.00 - 1.00 - 1.00 = 7.00, and it does.
        let consistent = [
            record(3, Decimal::new(-100, 2), Some(Decimal::new(700, 2))),
            record(2, Decimal::new(-100, 2), None),
            record(1, Decimal::new(-100, 2), Some(Decimal::new(900, 2))),
        ];
        archive::write_archive(&archive_path, &consistent).unwrap();
        assert!(verify_archive(&archive_path).is_ok());

        // The same days, but day 3 claims 5.00.
        let broken = [
            record(3, Decimal::new(-100, 2), Some(Decimal::new(500, 2))),
            record(2, Decimal::new(-100, 2), None),
            record(1, Decimal::new(-100, 2), Some(Decimal::new(900, 2))),
        ];
        archive::write_archive(&archive_path, &broken).unwrap();
        let error = verify_archive(&archive_path).unwrap_err();
        assert!(format!("{error:#}").contains("Balance chain broken on 2025-02-03"));
    }
}