automapper-validation 0.1.59

AHB condition expression parsing, evaluation, and EDIFACT validation
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
//! Integration tests proving tree-based validation correctly evaluates UB1.
//!
//! UB1 = `([931] ∧ [932] [490]) ⊻ ([931] ∧ [933] [491])`
//!
//! - [931] = timezone is +00 (UTC)
//! - [932] = HHMM = 2200
//! - [933] = HHMM = 2300
//! - [490] = date is in MESZ (summer time) range
//! - [491] = date is in MEZ (winter time) range
//!
//! The bug was: without tree-based validation, format conditions checked
//! DTM+137 (creation date) instead of DTM+92 (the field being validated),
//! because `find_segments("DTM")` returns the first DTM segment it finds.

use automapper_validation::eval::NoOpExternalProvider;
use automapper_validation::fv2504::UtilmdStromConditionEvaluatorFV2504;
use automapper_validation::validator::validate::{AhbFieldRule, AhbWorkflow};
use automapper_validation::{
    build_validated_tree, ConditionParser, EdifactValidator, ValidationLevel,
};
use mig_assembly::assembler::{
    AssembledGroup, AssembledGroupInstance, AssembledSegment, AssembledTree,
};
use mig_types::segment::OwnedSegment;
use std::collections::{BTreeMap, HashMap};

/// Build a minimal assembled tree with DTM+92 and DTM+137 in SG4.
fn make_tree(dtm92_value: &str, dtm137_value: &str) -> AssembledTree {
    AssembledTree {
        segments: vec![],
        groups: vec![AssembledGroup {
            group_id: "SG4".to_string(),
            repetitions: vec![AssembledGroupInstance {
                segments: vec![
                    AssembledSegment {
                        tag: "DTM".to_string(),
                        elements: vec![vec![
                            "92".to_string(),
                            dtm92_value.to_string(),
                            "303".to_string(),
                        ]],
                        mig_number: Some("0082".to_string()),
                        segment_number: None,
                    },
                    AssembledSegment {
                        tag: "DTM".to_string(),
                        elements: vec![vec![
                            "137".to_string(),
                            dtm137_value.to_string(),
                            "303".to_string(),
                        ]],
                        mig_number: Some("0081".to_string()),
                        segment_number: None,
                    },
                ],
                child_groups: vec![],
                entry_mig_number: None,
                variant_mig_numbers: vec![],
                skipped_segments: vec![],
                skipped_positions: Vec::new(),
            }],
        }],
        post_group_start: 0,
        inter_group_segments: BTreeMap::new(),
    }
}

/// Build an AHB workflow with a single field rule for DTM+92/C507/2380
/// that has status "X [UB1]", plus the UB1 definition.
fn make_workflow() -> AhbWorkflow {
    let ub1_expr = ConditionParser::parse_raw("([931] ∧ [932] [490]) ⊻ ([931] ∧ [933] [491])")
        .expect("UB1 should parse")
        .expect("UB1 should not be empty");

    let mut ub_definitions = HashMap::new();
    ub_definitions.insert("UB1".to_string(), ub1_expr);

    AhbWorkflow {
        pruefidentifikator: "55001".to_string(),
        description: "Test workflow".to_string(),
        communication_direction: None,
        fields: vec![AhbFieldRule {
            segment_path: "SG4/DTM/C507/2380".to_string(),
            name: "Eingangsdatum".to_string(),
            ahb_status: "X [UB1]".to_string(),
            codes: vec![],
            parent_group_ahb_status: None,
            segment_ahb_status: None,
            // The date value is element 0, component 1
            // (elements = [["92", "202604302200+00", "303"]])
            element_index: Some(0),
            component_index: Some(1),
            mig_number: Some("0082".to_string()),
        }],
        ub_definitions,
    }
}

/// Build flat OwnedSegments matching the tree for the `segments` parameter.
fn make_flat_segments(dtm92_value: &str, dtm137_value: &str) -> Vec<OwnedSegment> {
    vec![
        OwnedSegment {
            id: "DTM".to_string(),
            elements: vec![vec![
                "92".to_string(),
                dtm92_value.to_string(),
                "303".to_string(),
            ]],
            segment_number: 1,
        },
        OwnedSegment {
            id: "DTM".to_string(),
            elements: vec![vec![
                "137".to_string(),
                dtm137_value.to_string(),
                "303".to_string(),
            ]],
            segment_number: 2,
        },
    ]
}

/// UB1 passes when DTM+92 has a valid MESZ summer date at 22:00 UTC.
///
/// DTM+92 = `202604302200+00` (April 30, 2026, 22:00 UTC):
/// - [931] timezone +00 -> True
/// - [932] HHMM 2200 -> True
/// - [490] April = MESZ -> True
/// - First branch: True AND True AND True = True
/// - UB1 = True XOR False = True
///
/// DTM+137 = `202503311329+00` (irrelevant creation date with HHMM=1329).
/// Without tree-based validation, the evaluator would check DTM+137 first
/// and [932] would be False because 1329 != 2200.
#[test]
fn ub1_passes_with_tree_based_validation() {
    let tree = make_tree("202604302200+00", "202503311329+00");
    let workflow = make_workflow();
    let flat_segments = make_flat_segments("202604302200+00", "202503311329+00");

    let validated_tree = build_validated_tree(&workflow, &tree);

    let evaluator = UtilmdStromConditionEvaluatorFV2504::default();
    let validator = EdifactValidator::new(evaluator);
    let external = NoOpExternalProvider;

    let report = validator.validate_tree(
        &validated_tree,
        &flat_segments,
        &external,
        ValidationLevel::Conditions,
        None,
    );

    // Collect AHB003 errors (conditional rule violations).
    let ahb003_errors: Vec<_> = report
        .errors()
        .filter(|issue| issue.code == "AHB003")
        .collect();

    assert!(
        ahb003_errors.is_empty(),
        "UB1 should pass for DTM+92 with 202604302200+00, but got AHB003 errors: {ahb003_errors:?}"
    );
}

/// Missing group variant: NAD+MS absent when NAD+MR present.
///
/// The tree-based validation should detect that NAD+MS rules are unmatched
/// and report them as missing via the flat is_field_present fallback.
#[test]
fn missing_group_variant_reports_error() {
    use automapper_validation::validator::validate::AhbCodeRule;

    let workflow = AhbWorkflow {
        pruefidentifikator: "55001".to_string(),
        description: "Test".to_string(),
        communication_direction: None,
        fields: vec![
            // NAD+MS qualifier (required, mig "0010")
            AhbFieldRule {
                segment_path: "SG2/NAD/3035".to_string(),
                name: "MP-ID Absender Qualifier".to_string(),
                ahb_status: "X".to_string(),
                codes: vec![AhbCodeRule {
                    value: "MS".to_string(),
                    description: "Absender".to_string(),
                    ahb_status: "X".to_string(),
                }],
                parent_group_ahb_status: Some("Muss".to_string()),
                segment_ahb_status: None,
                element_index: Some(0),
                component_index: Some(0),
                mig_number: Some("0010".to_string()),
            },
            // NAD+MR qualifier (required, mig "0011")
            AhbFieldRule {
                segment_path: "SG2/NAD/3035".to_string(),
                name: "MP-ID Empfänger Qualifier".to_string(),
                ahb_status: "X".to_string(),
                codes: vec![AhbCodeRule {
                    value: "MR".to_string(),
                    description: "Empfänger".to_string(),
                    ahb_status: "X".to_string(),
                }],
                parent_group_ahb_status: Some("Muss".to_string()),
                segment_ahb_status: None,
                element_index: Some(0),
                component_index: Some(0),
                mig_number: Some("0011".to_string()),
            },
        ],
        ub_definitions: HashMap::new(),
    };

    // Only NAD+MR in the assembled tree.
    let tree = AssembledTree {
        segments: vec![],
        groups: vec![AssembledGroup {
            group_id: "SG2".to_string(),
            repetitions: vec![AssembledGroupInstance {
                segments: vec![AssembledSegment {
                    tag: "NAD".to_string(),
                    elements: vec![vec!["MR".to_string()]],
                    mig_number: Some("0011".to_string()),
                    segment_number: None,
                }],
                child_groups: vec![],
                entry_mig_number: Some("0011".to_string()),
                variant_mig_numbers: vec!["0011".to_string()],
                skipped_segments: vec![],
                skipped_positions: Vec::new(),
            }],
        }],
        post_group_start: 0,
        inter_group_segments: BTreeMap::new(),
    };

    // Flat segments for the context.
    let flat_segments = vec![OwnedSegment {
        id: "NAD".to_string(),
        elements: vec![vec!["MR".to_string(), "9900269000000".to_string()]],
        segment_number: 1,
    }];

    let validated_tree = build_validated_tree(&workflow, &tree);

    // Verify unmatched rules were detected.
    assert_eq!(
        validated_tree.unmatched_rules.len(),
        1,
        "NAD+MS rule should be unmatched"
    );

    let evaluator = UtilmdStromConditionEvaluatorFV2504::default();
    let validator = EdifactValidator::new(evaluator);
    let external = NoOpExternalProvider;

    let report = validator.validate_tree(
        &validated_tree,
        &flat_segments,
        &external,
        ValidationLevel::Conditions,
        None,
    );

    let missing_errors: Vec<_> = report
        .errors()
        .filter(|issue| issue.code == "AHB001")
        .collect();

    assert!(
        !missing_errors.is_empty(),
        "Should report missing NAD+MS field, but got no AHB001 errors. All issues: {:?}",
        report.issues
    );

    // Verify the error mentions the right field.
    let error = &missing_errors[0];
    assert_eq!(error.field_path.as_deref(), Some("SG2/NAD/3035"));
    assert!(
        error.message.contains("MP-ID Absender"),
        "Error should mention Absender, got: {}",
        error.message
    );
}

/// Missing segment within same group instance: DTM+92 absent when DTM+93 present in SG4.
///
/// The assembled DTM+93 has mig_number "00024" (from assembler's bounded path).
/// DTM+92 rules with mig_number "00023" must NOT match DTM+93 because it has
/// a conflicting mig_number. find_segment returns None → validate_tree reports missing.
#[test]
fn missing_segment_within_instance_reports_error() {
    use automapper_validation::validator::validate::AhbCodeRule;

    let workflow = AhbWorkflow {
        pruefidentifikator: "55001".to_string(),
        description: "Test".to_string(),
        communication_direction: None,
        fields: vec![
            // DTM+92 qualifier (required, mig "00023")
            AhbFieldRule {
                segment_path: "SG4/DTM/C507/2005".to_string(),
                name: "Eingangsdatum Qualifier".to_string(),
                ahb_status: "X".to_string(),
                codes: vec![AhbCodeRule {
                    value: "92".to_string(),
                    description: "Eingangsdatum".to_string(),
                    ahb_status: "X".to_string(),
                }],
                parent_group_ahb_status: None,
                segment_ahb_status: None,
                element_index: Some(0),
                component_index: Some(0),
                mig_number: Some("00023".to_string()),
            },
            // DTM+92 date value (required, mig "00023")
            AhbFieldRule {
                segment_path: "SG4/DTM/C507/2380".to_string(),
                name: "Eingangsdatum Wert".to_string(),
                ahb_status: "X".to_string(),
                codes: vec![],
                parent_group_ahb_status: None,
                segment_ahb_status: None,
                element_index: Some(0),
                component_index: Some(1),
                mig_number: Some("00023".to_string()),
            },
            // DTM+93 qualifier (present, mig "00024")
            AhbFieldRule {
                segment_path: "SG4/DTM/C507/2005".to_string(),
                name: "Enddatum Qualifier".to_string(),
                ahb_status: "X".to_string(),
                codes: vec![AhbCodeRule {
                    value: "93".to_string(),
                    description: "Enddatum".to_string(),
                    ahb_status: "X".to_string(),
                }],
                parent_group_ahb_status: None,
                segment_ahb_status: None,
                element_index: Some(0),
                component_index: Some(0),
                mig_number: Some("00024".to_string()),
            },
            // DTM+93 date value (present, mig "00024")
            AhbFieldRule {
                segment_path: "SG4/DTM/C507/2380".to_string(),
                name: "Enddatum Wert".to_string(),
                ahb_status: "X".to_string(),
                codes: vec![],
                parent_group_ahb_status: None,
                segment_ahb_status: None,
                element_index: Some(0),
                component_index: Some(1),
                mig_number: Some("00024".to_string()),
            },
        ],
        ub_definitions: HashMap::new(),
    };

    // SG4 with DTM+93 present (mig "00024"), DTM+92 absent.
    // The assembled segment has mig_number set (from bounded assembly path).
    let tree = AssembledTree {
        segments: vec![],
        groups: vec![AssembledGroup {
            group_id: "SG4".to_string(),
            repetitions: vec![AssembledGroupInstance {
                segments: vec![AssembledSegment {
                    tag: "DTM".to_string(),
                    elements: vec![vec![
                        "93".to_string(),
                        "202512312300+00".to_string(),
                        "303".to_string(),
                    ]],
                    mig_number: Some("00024".to_string()),
                    segment_number: None,
                }],
                child_groups: vec![],
                entry_mig_number: None,
                variant_mig_numbers: vec!["00023".to_string(), "00024".to_string()],
                skipped_segments: vec![],
                skipped_positions: Vec::new(),
            }],
        }],
        post_group_start: 0,
        inter_group_segments: BTreeMap::new(),
    };

    let flat_segments = vec![OwnedSegment {
        id: "DTM".to_string(),
        elements: vec![vec![
            "93".to_string(),
            "202512312300+00".to_string(),
            "303".to_string(),
        ]],
        segment_number: 1,
    }];

    let validated_tree = build_validated_tree(&workflow, &tree);

    let evaluator = UtilmdStromConditionEvaluatorFV2504::default();
    let validator = EdifactValidator::new(evaluator);
    let external = NoOpExternalProvider;

    let report = validator.validate_tree(
        &validated_tree,
        &flat_segments,
        &external,
        ValidationLevel::Conditions,
        None,
    );

    let missing_errors: Vec<_> = report
        .errors()
        .filter(|issue| issue.code == "AHB001")
        .collect();

    assert!(
        !missing_errors.is_empty(),
        "Should report missing DTM+92 fields, but got no AHB001 errors"
    );

    // Should report missing for DTM+92 qualifier and value
    let dtm92_errors: Vec<_> = missing_errors
        .iter()
        .filter(|e| e.message.contains("Eingangsdatum"))
        .collect();
    assert!(
        dtm92_errors.len() >= 2,
        "Should report at least 2 DTM+92 missing errors (qualifier + value), got {}",
        dtm92_errors.len()
    );

    // DTM+93 should NOT be reported as missing (it's present)
    let dtm93_errors: Vec<_> = missing_errors
        .iter()
        .filter(|e| e.message.contains("Enddatum"))
        .collect();
    assert!(
        dtm93_errors.is_empty(),
        "DTM+93 is present — should NOT be reported as missing. Got: {:?}",
        dtm93_errors.iter().map(|e| &e.message).collect::<Vec<_>>()
    );
}

/// Regression: present segments with mig_number must NOT be reported as missing.
///
/// This test reproduces the false positives from removing the tag fallback entirely:
/// when both DTM+92 and DTM+93 are present (each with their own mig_number from the
/// assembler's bounded path), both should be found by strict mig_number matching.
/// STS (single segment, mig "00035") should also be found.
#[test]
fn present_segments_with_mig_number_not_reported_missing() {
    use automapper_validation::validator::validate::AhbCodeRule;

    let workflow = AhbWorkflow {
        pruefidentifikator: "55001".to_string(),
        description: "Test".to_string(),
        communication_direction: None,
        fields: vec![
            // DTM+92 (present, mig "00023")
            AhbFieldRule {
                segment_path: "SG4/DTM/C507/2005".to_string(),
                name: "Eingangsdatum Qualifier".to_string(),
                ahb_status: "X".to_string(),
                codes: vec![AhbCodeRule {
                    value: "92".to_string(),
                    description: "".to_string(),
                    ahb_status: "X".to_string(),
                }],
                parent_group_ahb_status: None,
                segment_ahb_status: None,
                element_index: Some(0),
                component_index: Some(0),
                mig_number: Some("00023".to_string()),
            },
            // DTM+93 (present, mig "00024")
            AhbFieldRule {
                segment_path: "SG4/DTM/C507/2005".to_string(),
                name: "Enddatum Qualifier".to_string(),
                ahb_status: "X".to_string(),
                codes: vec![AhbCodeRule {
                    value: "93".to_string(),
                    description: "".to_string(),
                    ahb_status: "X".to_string(),
                }],
                parent_group_ahb_status: None,
                segment_ahb_status: None,
                element_index: Some(0),
                component_index: Some(0),
                mig_number: Some("00024".to_string()),
            },
            // STS (present, mig "00035")
            AhbFieldRule {
                segment_path: "SG4/STS/C601/9015".to_string(),
                name: "Statuskategorie".to_string(),
                ahb_status: "X".to_string(),
                codes: vec![],
                parent_group_ahb_status: None,
                segment_ahb_status: None,
                element_index: Some(0),
                component_index: Some(0),
                mig_number: Some("00035".to_string()),
            },
        ],
        ub_definitions: HashMap::new(),
    };

    // All segments present with correct mig_numbers (from bounded assembly).
    let tree = AssembledTree {
        segments: vec![],
        groups: vec![AssembledGroup {
            group_id: "SG4".to_string(),
            repetitions: vec![AssembledGroupInstance {
                segments: vec![
                    AssembledSegment {
                        tag: "DTM".to_string(),
                        elements: vec![vec![
                            "92".to_string(),
                            "202505312200+00".to_string(),
                            "303".to_string(),
                        ]],
                        mig_number: Some("00023".to_string()),
                        segment_number: None,
                    },
                    AssembledSegment {
                        tag: "DTM".to_string(),
                        elements: vec![vec![
                            "93".to_string(),
                            "202512312300+00".to_string(),
                            "303".to_string(),
                        ]],
                        mig_number: Some("00024".to_string()),
                        segment_number: None,
                    },
                    AssembledSegment {
                        tag: "STS".to_string(),
                        elements: vec![vec!["7".to_string()], vec![], vec!["E01".to_string()]],
                        mig_number: Some("00035".to_string()),
                        segment_number: None,
                    },
                ],
                child_groups: vec![],
                entry_mig_number: None,
                variant_mig_numbers: vec![
                    "00023".to_string(),
                    "00024".to_string(),
                    "00035".to_string(),
                ],
                skipped_segments: vec![],
                skipped_positions: Vec::new(),
            }],
        }],
        post_group_start: 0,
        inter_group_segments: BTreeMap::new(),
    };

    let flat_segments = vec![];
    let validated_tree = build_validated_tree(&workflow, &tree);

    let evaluator = UtilmdStromConditionEvaluatorFV2504::default();
    let validator = EdifactValidator::new(evaluator);
    let external = NoOpExternalProvider;

    let report = validator.validate_tree(
        &validated_tree,
        &flat_segments,
        &external,
        ValidationLevel::Conditions,
        None,
    );

    let missing_errors: Vec<_> = report
        .errors()
        .filter(|issue| issue.code == "AHB001")
        .collect();

    assert!(
        missing_errors.is_empty(),
        "All segments are present — should have no AHB001 errors. Got:\n{}",
        missing_errors
            .iter()
            .map(|e| format!(
                "  {} at {}",
                e.message,
                e.field_path.as_deref().unwrap_or("?")
            ))
            .collect::<Vec<_>>()
            .join("\n")
    );
}

/// UB1 fails when DTM+92 has HHMM=1329 (neither 2200 nor 2300).
///
/// DTM+92 = `202604301329+00`:
/// - [931] timezone +00 -> True
/// - [932] HHMM 1329 -> False (not 2200)
/// - First branch: True AND False AND ... = False
/// - [933] HHMM 1329 -> False (not 2300)
/// - Second branch: True AND False AND ... = False
/// - UB1 = False XOR False = False
///
/// Because the field IS present but UB1 evaluates to False, the validator
/// should emit an AHB003 conditional rule violation.
#[test]
fn ub1_fails_when_dtm92_has_wrong_time() {
    let tree = make_tree("202604301329+00", "202503311329+00");
    let workflow = make_workflow();
    let flat_segments = make_flat_segments("202604301329+00", "202503311329+00");

    let validated_tree = build_validated_tree(&workflow, &tree);

    let evaluator = UtilmdStromConditionEvaluatorFV2504::default();
    let validator = EdifactValidator::new(evaluator);
    let external = NoOpExternalProvider;

    let report = validator.validate_tree(
        &validated_tree,
        &flat_segments,
        &external,
        ValidationLevel::Conditions,
        None,
    );

    let ahb003_errors: Vec<_> = report
        .errors()
        .filter(|issue| issue.code == "AHB003")
        .collect();

    assert!(
        !ahb003_errors.is_empty(),
        "UB1 should fail for DTM+92 with 202604301329+00 (HHMM=1329 is neither 2200 nor 2300)"
    );

    // Verify the error references the right field.
    let error = &ahb003_errors[0];
    assert_eq!(
        error.field_path.as_deref(),
        Some("SG4/DTM/C507/2380"),
        "Error should reference DTM+92 field path"
    );
    assert!(
        error.rule.as_deref().unwrap_or("").contains("UB1"),
        "Error rule should mention UB1"
    );
}

/// AHB validation issues must carry the segment counter of the offending
/// segment so a CONTRL response can populate UCS d0096 (segmentPosition).
///
/// Builds a tree with DTM+92 at segment_number 7, fails UB1, and asserts the
/// resulting AHB003 issue's `segment_position.segment_number == 7`.
#[test]
fn ahb003_issue_carries_segment_position_from_assembled_segment() {
    let mut tree = make_tree("202604301329+00", "202503311329+00");
    // Tag the DTM+92 segment with a known counter position. DTM+92 is the
    // first segment in the SG4's first instance.
    tree.groups[0].repetitions[0].segments[0].segment_number = Some(7);

    let workflow = make_workflow();

    // The flat-segment list mirrors what a parser would produce — DTM+92 at
    // counter 7, DTM+137 at counter 3 (irrelevant for the assertion).
    let flat_segments = vec![
        OwnedSegment {
            id: "DTM".to_string(),
            elements: vec![vec![
                "92".to_string(),
                "202604301329+00".to_string(),
                "303".to_string(),
            ]],
            segment_number: 7,
        },
        OwnedSegment {
            id: "DTM".to_string(),
            elements: vec![vec![
                "137".to_string(),
                "202503311329+00".to_string(),
                "303".to_string(),
            ]],
            segment_number: 3,
        },
    ];

    let validated_tree = build_validated_tree(&workflow, &tree);

    let evaluator = UtilmdStromConditionEvaluatorFV2504::default();
    let validator = EdifactValidator::new(evaluator);
    let external = NoOpExternalProvider;

    let report = validator.validate_tree(
        &validated_tree,
        &flat_segments,
        &external,
        ValidationLevel::Conditions,
        None,
    );

    let ahb003 = report
        .errors()
        .find(|issue| issue.code == "AHB003")
        .expect("AHB003 should fire for DTM+92 with HHMM=1329");

    let pos = ahb003
        .segment_position
        .expect("AHB003 issue must carry segment_position");
    assert_eq!(
        pos.segment_number, 7,
        "segment_position must reflect the matched DTM+92's counter"
    );
}