telco-interop 0.1.0

The German telecom carrier interfaces in one dependency: WBCI (the § 59 TKG provider switch) and S/PRI (open-access orders), re-exported behind features, with the types they share.
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
//! The binary on the fixtures of both interfaces: each command's output and exit code, a
//! switch replayed from files written with the libraries, and the output's safety.
#![cfg(feature = "cli")]
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::indexing_slicing)]

use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;

use telco_interop::{spri, wbci};

// -- harness --------------------------------------------------------------------------------

/// Runs the binary; returns standard output (and error, for usage errors) and the exit code.
fn run<S: AsRef<std::ffi::OsStr>>(args: &[S]) -> (String, i32) {
    let o = Command::new(env!("CARGO_BIN_EXE_telco-interop")).args(args).output().unwrap();
    let mut text = String::from_utf8(o.stdout).unwrap();
    text.push_str(&String::from_utf8(o.stderr).unwrap());
    (text, o.status.code().unwrap())
}

fn wbci_fixture(name: &str) -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR")).join("../wbci/tests/fixtures").join(name)
}

fn spri_fixture(name: &str) -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR")).join("../spri/tests/fixtures").join(name)
}

fn fixtures(dir: &Path) -> Vec<PathBuf> {
    let mut v: Vec<PathBuf> = fs::read_dir(dir).unwrap().map(|e| e.unwrap().path()).collect();
    v.sort();
    v
}

fn all_fixtures() -> Vec<PathBuf> {
    let mut v = fixtures(&wbci_fixture(""));
    v.extend(fixtures(&spri_fixture("")));
    v
}

/// A fresh, empty folder under the target directory.
fn temp(name: &str) -> PathBuf {
    let dir = Path::new(env!("CARGO_TARGET_TMPDIR")).join("cli").join(name);
    let _ = fs::remove_dir_all(&dir);
    fs::create_dir_all(&dir).unwrap();
    dir
}

#[track_caller]
fn contains_all(out: &str, parts: &[&str]) {
    for p in parts {
        assert!(out.contains(p), "missing {p:?} in:\n{out}");
    }
}

// -- read -----------------------------------------------------------------------------------

#[test]
fn read_names_kind_case_id_and_codes_of_a_wbci_message() {
    let (out, code) = run(&[wbci_fixture("abbm.xml")]);
    assert_eq!(code, 2, "a subcommand is required:\n{out}");
    let (out, code) = run(&[Path::new("read"), &wbci_fixture("abbm.xml")]);
    assert_eq!(code, 0, "{out}");
    contains_all(
        &out,
        &[
            "interface: WBCI",
            "kind: ABBM",
            "business case: VA-KUE-MRN",
            "Vorabstimmungs-ID: DEU.EKP1.V000000013",
            "sender: DEU.EKP2 (EKPabg)",
            "code: 8103 Rufnummer nicht geschaltet (RNG)",
        ],
    );
}

#[test]
fn read_names_an_spri_order_and_message() {
    let (out, code) = run(&[Path::new("read"), &spri_fixture("auftrag-pv.xml"), &spri_fixture("meldung-ruem-pv.xml")]);
    assert_eq!(code, 0, "{out}");
    contains_all(
        &out,
        &[
            "interface: S/PRI",
            "kind: PV",
            "externe Auftragsnummer: A-2026-0001",
            "Vorabstimmungs-ID: DEU.EKP1.V000000013",
            "Wunschtermin: 2027-01-15",
            "kind: RUEM-PV",
            "code: 0021 Antwort des abgebenden Providers.",
        ],
    );
}

#[test]
fn read_detects_the_interface_inside_soap() {
    let dir = temp("soap");
    let p = spri::Payload::from_xml(&fs::read(spri_fixture("meldung-qeb.xml")).unwrap()).unwrap();
    let file = dir.join("qeb-soap.xml");
    fs::write(&file, p.to_soap().unwrap()).unwrap();
    let w = wbci::Payload::from_xml(&fs::read(wbci_fixture("ruem-va.xml")).unwrap()).unwrap();
    let wfile = dir.join("ruem-va-soap.xml");
    fs::write(&wfile, w.to_soap().unwrap()).unwrap();
    let (out, code) = run(&[Path::new("read"), &file, &wfile]);
    assert_eq!(code, 0, "{out}");
    contains_all(&out, &["interface: S/PRI", "kind: QEB", "interface: WBCI", "kind: RUEM-VA", "code: 8001"]);
}

#[test]
fn a_doctype_is_refused_with_teq_0995() {
    let dir = temp("doctype");
    let file = dir.join("doctype.xml");
    let xml = fs::read_to_string(wbci_fixture("abbm.xml")).unwrap();
    fs::write(&file, format!("<!DOCTYPE x [<!ENTITY a \"b\">]>\n{xml}")).unwrap();
    let (out, code) = run(&[Path::new("read"), &wbci_fixture("abbm.xml"), &file]);
    assert_eq!(code, 1, "{out}");
    contains_all(&out, &["doctype.xml", "WBCI: DOCTYPE declarations are not accepted", "TEQ 0995"]);
    let (out, code) = run(&[Path::new("check"), &file]);
    assert_eq!(code, 1, "{out}");
    contains_all(&out, &["TEQ 0995"]);
}

#[test]
fn neither_interface_and_missing_files_are_named() {
    let dir = temp("neither");
    let file = dir.join("other.xml");
    fs::write(&file, "<a xmlns=\"urn:other\"/>").unwrap();
    let (out, code) = run(&[Path::new("read"), &file, &dir.join("missing.xml")]);
    assert_eq!(code, 1, "{out}");
    contains_all(&out, &["neither a WBCI nor an S/PRI payload", "TEQ 0995", "missing.xml", "cannot open"]);
}

#[test]
fn every_fixture_reads() {
    let mut args = vec![PathBuf::from("read")];
    args.extend(all_fixtures());
    let (out, code) = run(&args);
    assert_eq!(code, 0, "{out}");
    assert!(!out.contains("unreadable"), "{out}");
}

// -- check ----------------------------------------------------------------------------------

#[test]
fn check_reports_wbci_issues_and_spri_readability() {
    let (out, code) = run(&[Path::new("check"), &wbci_fixture("va-kue-mrn.xml"), &spri_fixture("auftrag-pv.xml")]);
    assert_eq!(code, 0, "{out}");
    contains_all(&out, &["WBCI: no issues", "S/PRI: readable"]);
    // A warning does not fail the check.
    let (out, code) = run(&[Path::new("check"), &wbci_fixture("str-auf-ekpabg.xml")]);
    assert_eq!(code, 0, "{out}");
    contains_all(&out, &["warning [str-auf.ekpabg-grund] Anfrage/Stornogrund", "(Anl. 1 V5.4.1 §2.9.2)"]);
}

#[test]
fn check_fails_on_an_error() {
    // An ABBM code in a RUEM-VA: the code does not apply to the message type.
    let dir = temp("check-error");
    let file = dir.join("ruem-va-8103.xml");
    let xml = fs::read_to_string(wbci_fixture("ruem-va.xml")).unwrap();
    assert!(xml.contains("<Meldungscode>8001</Meldungscode>"));
    fs::write(&file, xml.replace("<Meldungscode>8001</Meldungscode>", "<Meldungscode>8103</Meldungscode>")).unwrap();
    let (out, code) = run(&[Path::new("check"), &file]);
    assert_eq!(code, 1, "{out}");
    contains_all(&out, &["error [", "Meldungscode", "8103"]);
}

// -- code -----------------------------------------------------------------------------------

#[test]
fn code_explains_both_interfaces() {
    let (out, code) = run(&["code", "8103"]);
    assert_eq!(code, 0, "{out}");
    contains_all(
        &out,
        &[
            "WBCI 8103 Rufnummer nicht geschaltet (RNG)",
            "short name: RNG",
            "ABBM in VA-KUE-MRN, VA-KUE-ORN, VA-RRNP",
            "S/PRI: 8103 is not an S/PRI code",
        ],
    );
    let (out, code) = run(&["code", "0021", "--spri"]);
    assert_eq!(code, 0, "{out}");
    contains_all(
        &out,
        &["S/PRI 0021 Antwort des abgebenden Providers.", "message types: RUEM-PV", "business cases: PV"],
    );
    assert!(!out.contains("WBCI"), "{out}");
    let (out, code) = run(&["code", "OK"]);
    assert_eq!(code, 0, "{out}");
    contains_all(&out, &["WBCI OK OK.", "S/PRI OK OK."]);
}

#[test]
fn an_unknown_code_fails() {
    let (out, code) = run(&["code", "0021", "--wbci"]);
    assert_eq!(code, 1, "{out}");
    contains_all(&out, &["WBCI: 0021 is not a WBCI code"]);
    let (_, code) = run(&["code", "1", "--wbci", "--spri"]);
    assert_eq!(code, 2, "usage error");
}

// -- timeline -------------------------------------------------------------------------------

const ID: &str = "DEU.EKP1.V000000013";
const PV_LINE: &str = "DEU.NB01.1754639821";

fn at(s: &str) -> wbci::Timestamp {
    format!("{s}+01:00").parse().unwrap()
}

fn wbci_payload(name: &str) -> wbci::Payload {
    wbci::Payload::from_xml(&fs::read(wbci_fixture(name)).unwrap()).unwrap()
}

fn spri_payload(name: &str) -> spri::Payload {
    spri::Payload::from_xml(&fs::read(spri_fixture(name)).unwrap()).unwrap()
}

/// A WBCI fixture payload sent at `when`.
fn wbci_at(mut p: wbci::Payload, when: &str) -> wbci::Payload {
    let z = wbci::Zeitstempel::berlin(at(when));
    match &mut p {
        wbci::Payload::AnfrageRequest { control, .. }
        | wbci::Payload::AnfrageResponse { control, .. }
        | wbci::Payload::MeldungRequest { control, .. }
        | wbci::Payload::MeldungResponse { control, .. } => control.zeitstempel = z,
    }
    p
}

fn spri_at(mut p: spri::Payload, when: &str) -> spri::Payload {
    let z = spri::Zeitstempel::berlin(at(when));
    match &mut p {
        spri::Payload::AuftragRequest { control, .. }
        | spri::Payload::AuftragResponse { control, .. }
        | spri::Payload::MeldungRequest { control, .. }
        | spri::Payload::MeldungResponse { control, .. } => control.zeitstempel = z,
    }
    p
}

/// The positive WBCI receipt for a message, sent by `absender` at `when`.
fn wbci_teq(absender: &str, when: &str) -> wbci::Payload {
    let control = wbci_payload("teq-meldung.xml").control().clone();
    let quittung = wbci::Teq {
        absender: absender.parse().unwrap(),
        vorabstimmungs_id: ID.parse().unwrap(),
        positionen: vec![wbci::Position::new(wbci::Meldungscode::Ok)],
        storno_id: None,
        aenderungs_id: None,
    };
    wbci_at(wbci::Payload::MeldungResponse { control, quittung }, when)
}

/// An S/PRI message fixture as a message of business case PV.
fn spri_pv_message(name: &str, when: &str, edit: impl FnOnce(&mut spri::Meldung)) -> spri::Payload {
    let mut p = spri_payload(name);
    if let spri::Payload::MeldungRequest { meldung, .. } = &mut p {
        meldung.auftragstyp.geschaeftsfall = spri::GeschaeftsfallCode::Pv;
        edit(meldung);
    }
    spri_at(p, when)
}

/// The positive S/PRI receipt for a message of order A-2026-0001.
fn spri_teq(when: &str) -> spri::Payload {
    let spri::Payload::AuftragResponse { control, .. } = spri_payload("teq-auftrag.xml") else { panic!() };
    let quittung = spri::Teq::new(
        spri::Meldungscode::Ok,
        Some(spri::ExterneAuftragsnummer::new("A-2026-0001").unwrap()),
        Some(spri::Auftraggebernummer::new("4711").unwrap()),
    );
    spri_at(spri::Payload::MeldungResponse { control, quittung }, when)
}

/// The files of one switch, as the gaining provider captured them: WBCI VA, RUEM-VA and
/// AKM-TR, S/PRI PV (requested for `pv_date`), QEB and ABM, each with its receipt. The PV is
/// SOAP-wrapped; file names do not follow the time order.
fn write_switch(dir: &Path, pv_date: &str) {
    let write = |name: &str, xml: String| fs::write(dir.join(name), xml).unwrap();
    write("01-va.xml", wbci_at(wbci_payload("va-kue-mrn.xml"), "2026-11-23T09:00:00").to_xml().unwrap());
    write("02-teq.xml", wbci_at(wbci_payload("teq-anfrage.xml"), "2026-11-23T09:00:05").to_xml().unwrap());
    write("03-ruem-va.xml", wbci_at(wbci_payload("ruem-va.xml"), "2026-11-24T10:00:00").to_xml().unwrap());
    write("04-teq.xml", wbci_teq("DEU.EKP1", "2026-11-24T10:00:05").to_xml().unwrap());
    let mut akm = wbci_payload("akm-tr.xml");
    if let wbci::Payload::MeldungRequest { meldung: wbci::Meldung::AkmTr(t), .. } = &mut akm {
        t.uebernahme_leitung = vec![wbci::UebernahmeLeitung::LineId(PV_LINE.parse().unwrap())];
    }
    write("05-akm-tr.xml", wbci_at(akm, "2026-11-25T08:00:00").to_xml().unwrap());
    write("06-teq.xml", wbci_teq("DEU.EKP2", "2026-11-25T08:00:05").to_xml().unwrap());

    let mut pv = spri_payload("auftrag-pv.xml");
    if let spri::Payload::AuftragRequest { auftrag, .. } = &mut pv
        && let spri::Geschaeftsfall::Pv(p) = &mut auftrag.geschaeftsfall
    {
        p.wunschtermin.datum = pv_date.parse().unwrap();
    }
    write("a-pv.xml", spri_at(pv, "2026-11-26T09:00:00").to_soap().unwrap());
    write("b-teq.xml", spri_at(spri_payload("teq-auftrag.xml"), "2026-11-26T09:00:05").to_xml().unwrap());
    write("c-qeb.xml", spri_pv_message("meldung-qeb.xml", "2026-11-26T09:05:00", |_| {}).to_xml().unwrap());
    write("d-teq.xml", spri_teq("2026-11-26T09:05:05").to_xml().unwrap());
    let abm = spri_pv_message("meldung-abm.xml", "2026-11-27T11:00:00", |m| {
        if let spri::Meldungsinhalt::Abm(a) = &mut m.inhalt {
            a.verbindlicher_liefertermin.datum = "2027-01-15".parse().unwrap();
        }
    });
    write("e-abm.xml", abm.to_xml().unwrap());
    write("f-teq.xml", spri_teq("2026-11-27T11:00:05").to_xml().unwrap());
}

/// The lines of the output's timeline section.
fn timeline_lines(out: &str) -> Vec<&str> {
    out.lines().skip_while(|l| *l != "Timeline:").skip(1).take_while(|l| l.starts_with("  ")).map(str::trim).collect()
}

#[test]
fn a_consistent_switch_replays_without_findings() {
    let dir = temp("switch");
    write_switch(&dir, "2027-01-15");
    let (out, code) = run(&[Path::new("timeline"), &dir, Path::new("--as"), Path::new("ekpauf")]);
    assert_eq!(code, 0, "{out}");
    contains_all(
        &out,
        &[&format!("Switch {ID}, replayed as EKPauf"), "(the latest control timestamp)", "Findings: none"],
    );
    let lines = timeline_lines(&out);
    assert_eq!(
        lines,
        [
            "Mo 23.11.2026 09:00 WBCI EKPauf → VA-KUE-MRN",
            "Mo 23.11.2026 09:00 WBCI EKPabg → TEQ OK",
            "Di 24.11.2026 10:00 WBCI EKPabg → RUEM-VA 8001 8104",
            "Di 24.11.2026 10:00 WBCI EKPauf → TEQ OK",
            "Mi 25.11.2026 08:00 WBCI EKPauf → AKM-TR 8010",
            "Mi 25.11.2026 08:00 WBCI EKPabg → TEQ OK",
            "Do 26.11.2026 09:00 S/PRI EKPauf → PV",
            "Do 26.11.2026 09:00 S/PRI Netzbetreiber → TEQ OK",
            "Do 26.11.2026 09:05 S/PRI Netzbetreiber → QEB 0000",
            "Do 26.11.2026 09:05 S/PRI EKPauf → TEQ OK",
            "Fr 27.11.2026 11:00 S/PRI Netzbetreiber → ABM 0000 0002",
            "Fr 27.11.2026 11:00 S/PRI EKPauf → TEQ OK",
        ],
        "{out}"
    );
    assert!(!out.contains("OVERDUE") && !out.contains("Refused") && !out.contains("Skipped"), "{out}");
}

#[test]
fn a_pv_date_other_than_the_switch_date_is_a_finding() {
    let dir = temp("switch-termin");
    write_switch(&dir, "2027-01-22");
    let (out, code) = run(&[Path::new("timeline"), &dir, Path::new("--as"), Path::new("ekpauf")]);
    assert_eq!(code, 1, "{out}");
    contains_all(
        &out,
        &[
            "Findings:",
            "wechsel.termin",
            "order A-2026-0001: requested Fr 22.01.2027 — the WBCI switch date is Fr 15.01.2027",
        ],
    );
}

#[test]
fn deadlines_are_judged_at_now() {
    let dir = temp("switch-now");
    write_switch(&dir, "2027-01-15");
    // Without the last receipt the ABM awaits one; a minute later it is overdue.
    fs::remove_file(dir.join("f-teq.xml")).unwrap();
    let (out, code) = run(&[
        Path::new("timeline"),
        &dir,
        Path::new("--as"),
        Path::new("ekpauf"),
        Path::new("--now"),
        Path::new("2026-11-27T11:01:00+01:00"),
    ]);
    assert_eq!(code, 1, "{out}");
    contains_all(&out, &["(--now)", "OVERDUE S/PRI EKPauf"]);
    let (out, code) = run(&[
        Path::new("timeline"),
        &dir,
        Path::new("--as"),
        Path::new("ekpauf"),
        Path::new("--now"),
        Path::new("not a time"),
    ]);
    assert_eq!(code, 2, "{out}");
}

#[test]
fn files_of_other_switches_or_views_are_listed_not_dropped() {
    let dir = temp("switch-others");
    write_switch(&dir, "2027-01-15");
    // The AKM-PV goes from the network owner to the losing provider: not the EKPauf's view.
    let akm = spri_pv_message("meldung-akm-pv.xml", "2026-11-26T10:00:00", |_| {});
    fs::write(dir.join("g-akm-pv.xml"), akm.to_xml().unwrap()).unwrap();
    // A pre-agreement of another switch.
    let mut other = wbci_payload("va-kue-orn.xml");
    if let wbci::Payload::AnfrageRequest { anfrage: wbci::Anfrage::Vorabstimmung(va), .. } = &mut other {
        va.id = "DEU.EKP1.V000000099".parse().unwrap();
    }
    fs::write(dir.join("h-va.xml"), wbci_at(other, "2026-11-28T09:00:00").to_xml().unwrap()).unwrap();
    // An ERLM before its time: the order is only confirmed, not yet due — refused or not,
    // it is named.
    fs::write(dir.join("i-broken.xml"), "<not-xml").unwrap();
    let (out, code) = run(&[Path::new("timeline"), &dir, Path::new("--as"), Path::new("ekpauf")]);
    assert_eq!(code, 1, "the unreadable file fails the replay:\n{out}");
    contains_all(
        &out,
        &[
            "Skipped:",
            "g-akm-pv.xml — S/PRI: not in the EKPauf's view",
            "h-va.xml — WBCI: Vorabstimmungs-ID DEU.EKP1.V000000099, another switch",
            "Unreadable:",
            "i-broken.xml",
        ],
    );
}

/// The leg between the network owner and the losing provider: AKM-PV, RUEM-PV and ABM-PV,
/// each with its receipt.
fn write_pv_leg(dir: &Path) {
    let write = |name: &str, p: spri::Payload| fs::write(dir.join(name), p.to_xml().unwrap()).unwrap();
    write("g-akm-pv.xml", spri_pv_message("meldung-akm-pv.xml", "2026-11-26T10:00:00", |_| {}));
    write("g-teq.xml", spri_teq("2026-11-26T10:00:05"));
    write("h-ruem-pv.xml", spri_pv_message("meldung-ruem-pv.xml", "2026-11-27T10:00:00", |_| {}));
    write("h-teq.xml", spri_teq("2026-11-27T10:00:05"));
    write("i-abm-pv.xml", spri_pv_message("meldung-abm-pv.xml", "2026-11-27T11:01:00", |_| {}));
    write("i-teq.xml", spri_teq("2026-11-27T11:01:05"));
}

#[test]
fn the_network_owner_replays_spri_alone() {
    let dir = temp("switch-nb");
    write_switch(&dir, "2027-01-15");
    write_pv_leg(&dir);
    let (out, code) = run(&[Path::new("timeline"), &dir, Path::new("--as"), Path::new("netzbetreiber")]);
    assert_eq!(code, 0, "{out}");
    contains_all(
        &out,
        &[
            "replayed as Netzbetreiber",
            "Do 26.11.2026 09:00 S/PRI EKPauf → PV",
            "Do 26.11.2026 10:00 S/PRI Netzbetreiber → AKM-PV",
            "Fr 27.11.2026 10:00 S/PRI EKPabg → RUEM-PV 0021",
            "Fr 27.11.2026 11:00 S/PRI Netzbetreiber → ABM 0000 0002",
            "Fr 27.11.2026 11:01 S/PRI Netzbetreiber → ABM-PV",
            "the network owner sees no WBCI",
            "01-va.xml — WBCI: the network owner is no WBCI party",
        ],
    );
    assert!(!out.contains("Refused"), "{out}");
}

#[test]
fn the_losing_provider_replays_its_view() {
    let dir = temp("switch-abg");
    write_switch(&dir, "2027-01-15");
    write_pv_leg(&dir);
    let (out, code) = run(&[Path::new("timeline"), &dir, Path::new("--as"), Path::new("ekpabg")]);
    assert_eq!(code, 0, "{out}");
    contains_all(
        &out,
        &[
            "replayed as EKPabg",
            "Di 24.11.2026 10:00 WBCI EKPabg → RUEM-VA 8001 8104",
            "Do 26.11.2026 10:00 S/PRI Netzbetreiber → AKM-PV",
            "Fr 27.11.2026 10:00 S/PRI EKPabg → RUEM-PV 0021",
            "Findings: none",
            "a-pv.xml — S/PRI: order A-2026-0001 is placed by the EKPauf",
            "c-qeb.xml — S/PRI: not in the EKPabg's view",
        ],
    );
    assert!(!out.contains("Refused"), "{out}");
}

#[test]
fn an_empty_folder_fails() {
    let dir = temp("empty");
    let (out, code) = run(&[Path::new("timeline"), &dir, Path::new("--as"), Path::new("ekpabg")]);
    assert_eq!(code, 1, "{out}");
    contains_all(&out, &["No switch"]);
}

// -- security -------------------------------------------------------------------------------

#[test]
fn control_characters_from_input_are_replaced() {
    // cli/SR-001
    let dir = temp("escape");
    let file = dir.join("abbm-esc.xml");
    let xml = fs::read_to_string(wbci_fixture("abbm.xml")).unwrap();
    assert!(xml.contains("<Meldungscode>8103</Meldungscode>"));
    fs::write(&file, xml.replace("<Meldungscode>8103</Meldungscode>", "<Meldungscode>\u{1b}[2J</Meldungscode>"))
        .unwrap();
    // The reader keeps an unknown code as it is.
    let p = wbci::Payload::from_xml(&fs::read(&file).unwrap()).unwrap();
    let wbci::Payload::MeldungRequest { meldung, .. } = &p else { panic!() };
    assert_eq!(meldung.codes().next().unwrap().as_str(), "\u{1b}[2J");
    for cmd in ["read", "check"] {
        let (out, _) = run(&[Path::new(cmd), &file]);
        assert!(!out.contains('\u{1b}'), "{cmd}: {out:?}");
        assert!(out.contains("\u{FFFD}[2J"), "{cmd}: {out:?}");
    }
    let (out, code) = run(&["code", "\u{1b}[2J"]);
    assert_eq!(code, 1);
    assert!(!out.contains('\u{1b}') && out.contains('\u{FFFD}'), "{out:?}");
    // A file name with a control character is printed safely too.
    #[cfg(unix)]
    {
        let named = dir.join("evil\u{1b}[2J.xml");
        fs::write(&named, xml).unwrap();
        let (out, _) = run(&[Path::new("timeline"), &dir, Path::new("--as"), Path::new("ekpauf")]);
        assert!(!out.contains('\u{1b}'), "{out:?}");
        assert!(out.contains("evil\u{FFFD}[2J.xml"), "{out:?}");
    }
}

#[test]
fn no_personal_data_in_any_output() {
    // cli/SR-002
    let dir = temp("pii");
    write_switch(&dir, "2027-01-22");
    let mut outputs = String::new();
    for cmd in ["read", "check"] {
        let mut args = vec![PathBuf::from(cmd)];
        args.extend(all_fixtures());
        args.extend(fixtures(&dir));
        outputs.push_str(&run(&args).0);
    }
    for party in ["ekpauf", "ekpabg", "netzbetreiber"] {
        outputs.push_str(&run(&[Path::new("timeline"), &dir, Path::new("--as"), Path::new(party)]).0);
        outputs.push_str(&run(&[Path::new("timeline"), &spri_fixture(""), Path::new("--as"), Path::new(party)]).0);
        outputs.push_str(&run(&[Path::new("timeline"), &wbci_fixture(""), Path::new("--as"), Path::new(party)]).0);
    }
    assert!(outputs.contains("Findings:") && outputs.contains("interface: S/PRI"), "{outputs}");
    for secret in [
        "Mustermann",
        "Erika",
        "Max ",
        "Beispiel",
        "Vormann",
        "Hauptstraße",
        "10115",
        "10117",
        "Berlin",
        "Mitte",
        "1234567",
        "1234568",
        "7654321",
        "9876543",
        "030 1234",
        "example.de",
        "example.org",
        "Neuer Anbieter",
        "Alter Anbieter",
        "RN unbekannt",
        "Umzug",
        "Widerruf",
        "Netzumbau",
        "Tiefbau",
        "Spleiß",
        "Kunde ist ab",
        "ONT wurde",
        "Bitte vorher",
        "Kein Sync",
    ] {
        assert!(!outputs.contains(secret), "{secret} in:\n{outputs}");
    }
}

#[test]
fn a_replayed_folder_skips_links_and_sub_folders() {
    // cli/SR-003
    let dir = temp("links");
    write_switch(&dir, "2027-01-15");
    let sub = dir.join("nested");
    fs::create_dir(&sub).unwrap();
    fs::copy(wbci_fixture("abbm.xml"), sub.join("abbm.xml")).unwrap();
    #[cfg(unix)]
    std::os::unix::fs::symlink(wbci_fixture("abbm.xml"), dir.join("link.xml")).unwrap();
    let (out, code) = run(&[Path::new("timeline"), &dir, Path::new("--as"), Path::new("ekpauf")]);
    assert_eq!(code, 0, "skipping is not a failure:\n{out}");
    contains_all(&out, &["Skipped:", "nested — sub-folder, not searched"]);
    #[cfg(unix)]
    contains_all(&out, &["link.xml — symbolic link, not followed"]);
    // The file inside the sub-folder and behind the link were not read: the ABBM is not in
    // the timeline.
    assert!(!out.contains("ABBM"), "{out}");
}

#[test]
fn a_folder_above_the_limit_is_refused() {
    // cli/SR-003
    let dir = temp("many");
    for i in 0..=10_000 {
        fs::write(dir.join(format!("{i:05}.xml")), "").unwrap();
    }
    let (out, code) = run(&[Path::new("timeline"), &dir, Path::new("--as"), Path::new("ekpauf")]);
    assert_eq!(code, 1, "{out}");
    contains_all(&out, &["error: more than 10000 files"]);
}