asx-rs 0.14.0

AS2 and AS4 B2B messaging library for Rust — signing, encryption, MDN, and ebMS3/AS4 profile support
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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
use super::*;
use crate::observability::audit_sink::{InMemoryAuditSink, ReplayCursor};
use std::sync::Mutex;
use std::sync::atomic::{AtomicBool, Ordering};
use tokio::time::{Duration, timeout};

fn session(session_id: &str, partner_id: &str) -> SessionContext {
    SessionContext::new(session_id, partner_id, "strict").expect("session context")
}

#[derive(Debug, Default)]
struct RecordingMetricsSink {
    counters: Mutex<Vec<RecordedCounter>>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct RecordedCounter {
    name: &'static str,
    value: u64,
    labels: Vec<(&'static str, String)>,
}

impl MetricsSink for RecordingMetricsSink {
    fn increment_counter(&self, name: &'static str, value: u64, labels: &[(&'static str, &str)]) {
        let mut counters = self.counters.lock().expect("metrics lock");
        counters.push(RecordedCounter {
            name,
            value,
            labels: labels.iter().map(|(k, v)| (*k, (*v).to_string())).collect(),
        });
    }

    fn record_histogram(&self, _name: &'static str, _value: f64, _labels: &[(&'static str, &str)]) {
    }

    fn set_gauge(&self, _name: &'static str, _value: f64, _labels: &[(&'static str, &str)]) {}
}

#[tokio::test]
async fn scoped_stream_contains_session_ids() {
    let bus = EventBus::new(16).expect("event bus");
    let mut rx = bus.subscribe_scoped_events();
    let s1 = session("s1", "p1");

    bus.emit(
        &s1,
        AsxEvent::MessageSigned {
            message_id: "m1".into(),
        },
    )
    .expect("emit");

    let evt = timeout(Duration::from_millis(200), rx.recv())
        .await
        .expect("timely recv")
        .expect("broadcast recv");
    assert_eq!(evt.session_id, "s1");
    assert_eq!(evt.partner_id, "p1");
}

#[tokio::test]
async fn session_subscription_does_not_leak_other_sessions() {
    let bus = EventBus::new(16).expect("event bus");
    let _scoped = bus.subscribe_scoped_events();
    let mut s1_rx = bus.subscribe_session_events("s1").expect("subscribe s1");
    let s1 = session("s1", "p1");
    let s2 = session("s2", "p2");

    bus.emit(
        &s2,
        AsxEvent::MessageSigned {
            message_id: "m2".into(),
        },
    )
    .expect("emit s2");

    bus.emit(
        &s1,
        AsxEvent::MessageSigned {
            message_id: "m1".into(),
        },
    )
    .expect("emit s1");

    let received = timeout(Duration::from_millis(200), s1_rx.recv())
        .await
        .expect("timely recv")
        .expect("event present");

    // Session subscription delivers raw AsxEvent; session isolation is the focus.
    // partner_id is available on ScopedAsxEvent from subscribe_scoped_events().
    match received.as_ref() {
        AsxEvent::MessageSigned { message_id } => {
            assert_eq!(message_id.as_ref(), "m1");
        }
        _ => panic!("unexpected event variant"),
    }
}

#[tokio::test]
async fn ordering_is_preserved_per_session() {
    let bus = EventBus::new(32).expect("event bus");
    let _scoped = bus.subscribe_scoped_events();
    let mut rx = bus.subscribe_session_events("s1").expect("subscribe s1");
    let s1 = session("s1", "p1");

    for i in 0..5 {
        bus.emit(
            &s1,
            AsxEvent::RetryScheduled {
                message_id: format!("m{i}").into(),
                attempt: i,
                reason: "transient",
            },
        )
        .expect("emit ordered event");
    }

    for expected in 0..5 {
        let evt = timeout(Duration::from_millis(200), rx.recv())
            .await
            .expect("timely recv")
            .expect("event present");
        match evt.as_ref() {
            AsxEvent::RetryScheduled { attempt, .. } => assert_eq!(*attempt, expected),
            _ => panic!("unexpected event variant"),
        }
    }
}

#[tokio::test]
async fn session_fanout_reuses_shared_event_handle() {
    let bus = EventBus::new(16).expect("event bus");
    let _scoped = bus.subscribe_scoped_events();
    let s1 = session("s1", "p1");
    let mut rx_a = bus.subscribe_session_events("s1").expect("subscribe a");
    let mut rx_b = bus.subscribe_session_events("s1").expect("subscribe b");

    bus.emit(
        &s1,
        AsxEvent::MessageSigned {
            message_id: "m1".into(),
        },
    )
    .expect("emit");

    let a = timeout(Duration::from_millis(200), rx_a.recv())
        .await
        .expect("timely recv a")
        .expect("event a");
    let b = timeout(Duration::from_millis(200), rx_b.recv())
        .await
        .expect("timely recv b")
        .expect("event b");

    assert!(
        Arc::ptr_eq(&a, &b),
        "fanout should share one event allocation across subscribers"
    );
}

#[test]
fn strict_mode_rejects_no_subscribers() {
    let bus = EventBus::new(16).expect("event bus");
    let sess = session("s-no-subscriber", "p1");

    let err = bus
        .emit(
            &sess,
            AsxEvent::MessageSigned {
                message_id: "m1".into(),
            },
        )
        .expect_err("strict mode must fail when no subscribers");

    assert_eq!(bus.emission_mode(), EventEmissionMode::StrictTransactional);
    assert_eq!(err.code, ErrorCode::ReliabilityFailure);
    assert_eq!(bus.metrics().dropped(), 1);
}

#[test]
fn strict_constructors_default_to_transactional_mode() {
    let strict = EventBus::new(16).expect("strict bus");
    assert_eq!(
        strict.emission_mode(),
        EventEmissionMode::StrictTransactional
    );

    let strict_with_config = EventBus::new_with_config_and_mode(
        16,
        None,
        BackpressurePolicy::default(),
        EventEmissionMode::StrictTransactional,
    )
    .expect("strict config bus");
    assert_eq!(
        strict_with_config.emission_mode(),
        EventEmissionMode::StrictTransactional
    );

    let strict_with_metrics = EventBus::new_with_config_and_mode_and_metrics(
        16,
        None,
        BackpressurePolicy::default(),
        EventEmissionMode::StrictTransactional,
        Arc::new(NoopMetricsSink),
    )
    .expect("strict metrics bus");
    assert_eq!(
        strict_with_metrics.emission_mode(),
        EventEmissionMode::StrictTransactional
    );

    let strict_with_durable = EventBus::new_with_config_and_mode(
        16,
        Some(Arc::new(InMemoryAuditSink::new().expect("audit sink"))),
        BackpressurePolicy::default(),
        EventEmissionMode::StrictTransactional,
    )
    .expect("strict durable bus");
    assert_eq!(
        strict_with_durable.emission_mode(),
        EventEmissionMode::StrictTransactional
    );
}

#[test]
fn best_effort_mode_tracks_drop_without_failing() {
    let bus = EventBus::new_with_config_and_mode(
        16,
        None,
        BackpressurePolicy::default(),
        EventEmissionMode::BestEffort,
    )
    .expect("event bus");
    let sess = session("s-best-effort", "p1");

    bus.emit(
        &sess,
        AsxEvent::MessageSigned {
            message_id: "m1".into(),
        },
    )
    .expect("best-effort mode should not fail when no subscribers are active");

    assert_eq!(bus.emission_mode(), EventEmissionMode::BestEffort);
    assert_eq!(bus.metrics().dropped(), 1);
}

#[test]
fn emit_does_not_fail_under_subscription_churn() {
    let bus = EventBus::new(16).expect("event bus");
    let _scoped = bus.subscribe_scoped_events();
    let sess = session("s-lock-contention", "p1");
    let _sub = bus
        .subscribe_session_events("s-lock-contention")
        .expect("subscribe session");

    bus.emit(
        &sess,
        AsxEvent::MessageSigned {
            message_id: "m1".into(),
        },
    )
    .expect("emit should not fail from registry contention");
}

#[test]
fn subscribe_succeeds_during_parallel_emits() {
    let bus = EventBus::new_with_config_and_mode(
        16,
        None,
        BackpressurePolicy::default(),
        EventEmissionMode::BestEffort,
    )
    .expect("event bus");
    let _scoped = bus.subscribe_scoped_events();
    let sess = session("s-lock-contention", "p1");

    // Seed one subscription so emit takes the session path while we add another.
    let _sub_a = bus
        .subscribe_session_events("s-lock-contention")
        .expect("subscribe A");

    for _ in 0..32 {
        bus.emit(
            &sess,
            AsxEvent::MessageSigned {
                message_id: "m1".into(),
            },
        )
        .expect("emit under churn");

        let _sub_b = bus
            .subscribe_session_events("s-lock-contention")
            .expect("subscribe B under emit churn");
    }
}

#[derive(Debug)]
struct FailingAuditSink;

impl DurableAuditSink for FailingAuditSink {
    fn store_event(&self, _event: &AuditEvent) -> Result<()> {
        Err(AsxError::new(
            ErrorCode::ReliabilityFailure,
            "forced durable sink failure",
            ErrorContext::new("failing_audit_sink"),
        ))
    }

    fn retrieve_events_from(
        &self,
        _cursor: &ReplayCursor,
        _limit: usize,
    ) -> Result<Vec<AuditEvent>> {
        Ok(Vec::new())
    }

    fn current_cursor(&self) -> Result<ReplayCursor> {
        Ok(ReplayCursor {
            last_event_id: "0".into(),
            position: 0,
            last_timestamp: 0,
            integrity_tag_b64: String::new(),
        })
    }

    fn acknowledge_cursor(&self, _cursor: &ReplayCursor) -> Result<()> {
        Ok(())
    }

    fn clear(&self) -> Result<()> {
        Ok(())
    }
}

#[derive(Debug)]
struct DurableTestAuditSink {
    inner: InMemoryAuditSink,
}

impl DurableTestAuditSink {
    fn new() -> Self {
        Self {
            inner: InMemoryAuditSink::new().expect("audit sink"),
        }
    }
}

impl DurableAuditSink for DurableTestAuditSink {
    fn durability(&self) -> AuditSinkDurability {
        AuditSinkDurability::Durable
    }

    fn has_replay_cursor_integrity_protection(&self) -> bool {
        self.inner.has_replay_cursor_integrity_protection()
    }

    fn store_event(&self, event: &AuditEvent) -> Result<()> {
        self.inner.store_event(event)
    }

    fn retrieve_events_from(&self, cursor: &ReplayCursor, limit: usize) -> Result<Vec<AuditEvent>> {
        self.inner.retrieve_events_from(cursor, limit)
    }

    fn current_cursor(&self) -> Result<ReplayCursor> {
        self.inner.current_cursor()
    }

    fn verify_replay_cursor_integrity(&self, cursor: &ReplayCursor) -> Result<()> {
        self.inner.verify_replay_cursor_integrity(cursor)
    }

    fn acknowledge_cursor(&self, cursor: &ReplayCursor) -> Result<()> {
        self.inner.acknowledge_cursor(cursor)
    }

    fn clear(&self) -> Result<()> {
        self.inner.clear()
    }
}

#[derive(Debug)]
struct ReentrantAuditSink {
    bus: Mutex<Option<EventBus>>,
    attempted_reentry: AtomicBool,
}

impl ReentrantAuditSink {
    fn new() -> Self {
        Self {
            bus: Mutex::new(None),
            attempted_reentry: AtomicBool::new(false),
        }
    }

    fn set_bus(&self, bus: EventBus) {
        *self.bus.lock().expect("bus lock") = Some(bus);
    }
}

impl DurableAuditSink for ReentrantAuditSink {
    fn durability(&self) -> AuditSinkDurability {
        AuditSinkDurability::Durable
    }

    fn store_event(&self, _event: &AuditEvent) -> Result<()> {
        if !self.attempted_reentry.swap(true, Ordering::SeqCst)
            && let Some(bus) = self.bus.lock().expect("bus lock").as_ref()
        {
            let nested = emit_audit_event(
                bus,
                &session("s-reentrant", "p-reentrant"),
                AsxEvent::InteropGuardrailEvaluated {
                    message_id: "nested-msg".into(),
                    code: "nested",
                    outcome: "SecurityBlocked",
                    detail: "nested",
                },
                true,
                "reentrant_nested",
            );
            if nested.is_err() {
                return Err(AsxError::new(
                    ErrorCode::ReliabilityFailure,
                    "nested emit rejected",
                    ErrorContext::new("reentrant_audit_sink"),
                ));
            }
        }
        Ok(())
    }

    fn retrieve_events_from(
        &self,
        _cursor: &ReplayCursor,
        _limit: usize,
    ) -> Result<Vec<AuditEvent>> {
        Ok(Vec::new())
    }

    fn current_cursor(&self) -> Result<ReplayCursor> {
        Ok(ReplayCursor {
            last_event_id: "0".into(),
            position: 0,
            last_timestamp: 0,
            integrity_tag_b64: String::new(),
        })
    }

    fn acknowledge_cursor(&self, _cursor: &ReplayCursor) -> Result<()> {
        Ok(())
    }

    fn clear(&self) -> Result<()> {
        Ok(())
    }
}

#[test]
fn emit_rejects_reentrant_audit_sink_store_event() {
    let sink = Arc::new(ReentrantAuditSink::new());
    let bus = EventBus::new_with_config_and_mode(
        16,
        Some(sink.clone()),
        BackpressurePolicy::default(),
        EventEmissionMode::StrictTransactional,
    )
    .expect("event bus");
    sink.set_bus(bus.clone());

    let _subscriber = bus.subscribe_scoped_events();
    let err = emit_audit_event(
        &bus,
        &session("s-reentrant", "p-reentrant"),
        AsxEvent::InteropGuardrailEvaluated {
            message_id: "outer-msg".into(),
            code: "outer",
            outcome: "SecurityBlocked",
            detail: "outer",
        },
        true,
        "reentrant_test",
    )
    .expect_err("reentrant sink must fail closed");

    assert_eq!(err.code, ErrorCode::ReliabilityFailure);
    assert!(sink.attempted_reentry.load(Ordering::SeqCst));
}

#[test]
fn emit_audit_event_persists_to_durable_sink() {
    let sink = Arc::new(InMemoryAuditSink::new().expect("audit sink"));
    let bus = EventBus::new_with_config_and_mode(
        16,
        Some(sink.clone()),
        BackpressurePolicy::default(),
        EventEmissionMode::StrictTransactional,
    )
    .expect("event bus");
    let sess = session("s-audit", "p-audit");
    let _subscriber = bus.subscribe_scoped_events();

    emit_audit_event(
        &bus,
        &sess,
        AsxEvent::InteropGuardrailEvaluated {
            message_id: "msg-1".into(),
            code: "test_guardrail",
            outcome: "SecurityBlocked",
            detail: "detail",
        },
        true,
        "audit_stage",
    )
    .expect("audit event emit");

    let events = sink
        .retrieve_events_from(
            &ReplayCursor {
                last_event_id: "0".into(),
                position: 0,
                last_timestamp: 0,
                integrity_tag_b64: String::new(),
            },
            10,
        )
        .expect("retrieve events");
    assert_eq!(events.len(), 1);
    assert_eq!(events[0].code, "interop_guardrail_evaluated");
    assert_eq!(events[0].metadata.stage.as_deref(), Some("audit_stage"));
}

/// Regression: `emit_audit_event` under `StrictWithAuditFallback` with **no**
/// subscribers must persist the event exactly once. Previously it pre-persisted
/// and then `emit`'s no-subscriber fallback persisted a second copy under a
/// different `event_id`, inflating the compliance log.
#[test]
fn emit_audit_event_persists_exactly_once_in_audit_fallback_without_subscribers() {
    let sink = Arc::new(DurableTestAuditSink::new());
    let bus = EventBus::new_with_config_and_mode(
        16,
        Some(sink.clone()),
        BackpressurePolicy::default(),
        EventEmissionMode::StrictWithAuditFallback,
    )
    .expect("event bus");
    let sess = session("s-audit-once", "p-audit");
    // Intentionally no subscriber → emit takes the audit-fallback path.

    emit_audit_event(
        &bus,
        &sess,
        AsxEvent::InteropGuardrailEvaluated {
            message_id: "msg-once".into(),
            code: "test_guardrail",
            outcome: "SecurityBlocked",
            detail: "detail",
        },
        true,
        "audit_stage",
    )
    .expect("audit event emit");

    let events = sink
        .retrieve_events_from(
            &ReplayCursor {
                last_event_id: "0".into(),
                position: 0,
                last_timestamp: 0,
                integrity_tag_b64: String::new(),
            },
            10,
        )
        .expect("retrieve events");
    assert_eq!(
        events.len(),
        1,
        "audit event must be persisted exactly once"
    );
}

#[test]
fn emit_audit_event_fail_closed_when_sink_write_fails() {
    let sink = Arc::new(FailingAuditSink);
    let bus = EventBus::new_with_config_and_mode(
        16,
        Some(sink),
        BackpressurePolicy::default(),
        EventEmissionMode::StrictTransactional,
    )
    .expect("event bus");
    let sess = session("s-audit", "p-audit");

    let err = emit_audit_event(
        &bus,
        &sess,
        AsxEvent::InteropGuardrailEvaluated {
            message_id: "msg-1".into(),
            code: "test_guardrail",
            outcome: "SecurityBlocked",
            detail: "detail",
        },
        true,
        "audit_stage",
    )
    .expect_err("fail-closed must error");

    assert_eq!(err.code, ErrorCode::ReliabilityFailure);
}

#[test]
fn strict_with_audit_fallback_fails_closed_when_sink_write_fails() {
    let sink = Arc::new(FailingAuditSink);
    let bus = EventBus::new_strict_with_audit_fallback(16, sink).expect("event bus");
    let sess = session("s-fallback-fail", "p-fallback-fail");

    let err = bus
        .emit(
            &sess,
            AsxEvent::InteropGuardrailEvaluated {
                message_id: "msg-fallback-fail".into(),
                code: "guardrail",
                outcome: "SecurityBlocked",
                detail: "fallback",
            },
        )
        .expect_err("strict audit fallback must fail closed when sink write fails");

    assert_eq!(err.code, ErrorCode::ReliabilityFailure);
}

#[test]
fn strict_with_audit_fallback_persists_without_subscribers() {
    let sink = Arc::new(DurableTestAuditSink::new());
    let bus = EventBus::new_strict_with_audit_fallback(16, sink.clone()).expect("event bus");
    let sess = session("s-fallback-ok", "p-fallback-ok");

    bus.emit(
        &sess,
        AsxEvent::InteropGuardrailEvaluated {
            message_id: "msg-fallback-ok".into(),
            code: "guardrail",
            outcome: "Allowed",
            detail: "fallback",
        },
    )
    .expect("fallback emit should persist and succeed");

    assert_eq!(bus.metrics().emitted(), 1);
    assert_eq!(bus.metrics().dropped(), 0);

    let events = sink
        .retrieve_events_from(
            &ReplayCursor {
                last_event_id: "0".into(),
                position: 0,
                last_timestamp: 0,
                integrity_tag_b64: String::new(),
            },
            10,
        )
        .expect("retrieve events");
    assert_eq!(events.len(), 1);
    assert_eq!(events[0].code, "interop_guardrail_evaluated");
}

#[test]
fn audit_replay_and_acknowledge_cursor_round_trip() {
    let sink = Arc::new(InMemoryAuditSink::new().expect("audit sink"));
    let bus = EventBus::new_with_config_and_mode(
        16,
        Some(sink),
        BackpressurePolicy::default(),
        EventEmissionMode::StrictTransactional,
    )
    .expect("event bus");
    let sess = session("s-audit", "p-audit");
    let _subscriber = bus.subscribe_scoped_events();

    emit_audit_event(
        &bus,
        &sess,
        AsxEvent::InteropGuardrailEvaluated {
            message_id: "msg-1".into(),
            code: "c1",
            outcome: "Allowed",
            detail: "d1",
        },
        true,
        "audit_stage",
    )
    .expect("emit #1");
    emit_audit_event(
        &bus,
        &sess,
        AsxEvent::InteropGuardrailEvaluated {
            message_id: "msg-2".into(),
            code: "c2",
            outcome: "Allowed",
            detail: "d2",
        },
        true,
        "audit_stage",
    )
    .expect("emit #2");

    let replay = bus
        .replay_audit_events_from(
            &ReplayCursor {
                last_event_id: "0".into(),
                position: 0,
                last_timestamp: 0,
                integrity_tag_b64: String::new(),
            },
            10,
        )
        .expect("replay");
    assert_eq!(replay.len(), 2);

    let signed_ack_cursor = bus.current_audit_cursor().expect("signed cursor");
    bus.acknowledge_audit_cursor(&signed_ack_cursor)
        .expect("ack");
    let cursor = bus.current_audit_cursor().expect("cursor");
    assert_eq!(cursor.position, 2);
}

#[test]
fn audit_replay_without_sink_returns_error() {
    let bus = EventBus::new(16).expect("event bus");
    let err = bus
        .current_audit_cursor()
        .expect_err("missing sink must error");
    assert_eq!(err.code, ErrorCode::InvalidInput);
}

#[test]
fn regulated_profile_enforces_strict_defaults() {
    let sink = Arc::new(DurableTestAuditSink::new());
    let bus = EventBus::new_regulated(16, sink).expect("event bus");
    assert_eq!(bus.emission_mode(), EventEmissionMode::StrictTransactional);

    // In strict mode with no active subscribers, event emission fails closed.
    let sess = session("s-reg", "p-reg");
    let err = bus
        .emit(
            &sess,
            AsxEvent::MessageSigned {
                message_id: "m-reg".into(),
            },
        )
        .expect_err("regulated mode requires active subscribers");
    assert_eq!(err.code, ErrorCode::ReliabilityFailure);
}

#[tokio::test]
async fn transactional_mode_fails_before_broadcast_when_session_queue_is_full() {
    let bus = EventBus::new_with_config_and_mode(
        16,
        None,
        BackpressurePolicy {
            session_channel_capacity: 1,
            ..BackpressurePolicy::default()
        },
        EventEmissionMode::StrictTransactional,
    )
    .expect("event bus");

    let sess = session("s-transactional", "p1");
    let mut scoped = bus.subscribe_scoped_events();
    let mut session_rx = bus
        .subscribe_session_events("s-transactional")
        .expect("subscribe session");

    bus.emit(
        &sess,
        AsxEvent::MessageSigned {
            message_id: "m1".into(),
        },
    )
    .expect("first emit should succeed");

    // Keep session queue full by not draining `session_rx` yet.
    let err = bus
        .emit(
            &sess,
            AsxEvent::MessageSigned {
                message_id: "m2".into(),
            },
        )
        .expect_err("transactional emit must fail when session queue is full");
    assert_eq!(err.code, ErrorCode::ReliabilityFailure);

    // Scoped stream should only contain the first event.
    let first = scoped.try_recv().expect("first scoped event present");
    assert_eq!(first.event.kind(), "message_signed");
    assert!(matches!(
        scoped.try_recv(),
        Err(ScopedEventTryRecvError::Empty)
    ));

    // Drain and confirm no hidden second session delivery happened.
    let first_session = timeout(Duration::from_millis(50), session_rx.recv())
        .await
        .expect("timely first session event")
        .expect("first session event present");
    assert_eq!(first_session.kind(), "message_signed");
    assert!(
        timeout(Duration::from_millis(20), session_rx.recv())
            .await
            .is_err(),
        "second session event must not be delivered"
    );
}

#[tokio::test]
async fn strict_with_audit_fallback_fails_when_session_queue_is_full() {
    let bus = EventBus::new_with_config_and_mode(
        16,
        Some(Arc::new(DurableTestAuditSink::new())),
        BackpressurePolicy {
            session_channel_capacity: 1,
            ..BackpressurePolicy::default()
        },
        EventEmissionMode::StrictWithAuditFallback,
    )
    .expect("event bus");

    let sess = session("s-strict-session-overflow", "p1");
    let _scoped = bus.subscribe_scoped_events();
    let mut session_rx = bus
        .subscribe_session_events("s-strict-session-overflow")
        .expect("subscribe session");

    bus.emit(
        &sess,
        AsxEvent::MessageSigned {
            message_id: "m1".into(),
        },
    )
    .expect("first emit should succeed");

    let err = bus
        .emit(
            &sess,
            AsxEvent::MessageSigned {
                message_id: "m2".into(),
            },
        )
        .expect_err("strict fallback mode must fail when session queue is full");
    assert_eq!(err.code, ErrorCode::ReliabilityFailure);
    assert!(err.message.contains("session subscriber queue is full"));

    // Drain and confirm only the first event was delivered.
    let first = timeout(Duration::from_millis(50), session_rx.recv())
        .await
        .expect("timely first session event")
        .expect("first session event present");
    assert_eq!(first.kind(), "message_signed");
    assert!(
        timeout(Duration::from_millis(20), session_rx.recv())
            .await
            .is_err(),
        "second session event must not be delivered"
    );
}

#[test]
fn strict_with_audit_fallback_requires_sink() {
    let err = match EventBus::new_with_config_and_mode(
        16,
        None,
        BackpressurePolicy::default(),
        EventEmissionMode::StrictWithAuditFallback,
    ) {
        Ok(_) => panic!("strict fallback without sink must fail"),
        Err(err) => err,
    };

    assert_eq!(err.code, ErrorCode::InvalidInput);
    assert!(
        err.message
            .contains("requires a configured durable audit sink")
    );
}

#[test]
fn strict_with_audit_fallback_rejects_ephemeral_sink() {
    let err = match EventBus::new_with_config_and_mode(
        16,
        Some(Arc::new(InMemoryAuditSink::new().expect("audit sink"))),
        BackpressurePolicy::default(),
        EventEmissionMode::StrictWithAuditFallback,
    ) {
        Ok(_) => panic!("strict fallback with ephemeral sink must fail"),
        Err(err) => err,
    };

    assert_eq!(err.code, ErrorCode::InvalidInput);
    assert!(
        err.message
            .contains("requires a production-durable audit sink")
    );
}

#[test]
fn regulated_profile_rejects_ephemeral_audit_sink() {
    let sink = Arc::new(InMemoryAuditSink::new().expect("audit sink"));
    let err = match EventBus::new_regulated(16, sink) {
        Ok(_) => panic!("ephemeral sink must be rejected"),
        Err(err) => err,
    };
    assert_eq!(err.code, ErrorCode::InvalidInput);
    assert!(err.message.contains("production-durable audit sink"));
}

#[cfg(any(feature = "as2", feature = "as4"))]
#[test]
fn durable_sink_not_required_when_fail_closed_disabled() {
    let bus = EventBus::new(16).expect("event bus");
    let sess = session("s-audit-optional", "p-optional");

    let result = require_durable_audit_sink(&sess, &bus, false, "as4_receive_push");

    assert!(result.is_ok());
}

#[cfg(any(feature = "as2", feature = "as4"))]
#[test]
fn durable_sink_required_when_fail_closed_enabled() {
    let bus = EventBus::new(16).expect("event bus");
    let sess = session("s-audit-required", "p-required");

    let err = require_durable_audit_sink(&sess, &bus, true, "as4_receive_push")
        .expect_err("fail-closed mode requires durable sink");

    assert_eq!(err.code, ErrorCode::ReliabilityFailure);
    assert!(err.message.contains("requires"));
    assert!(err.message.contains("audit sink"));
}

#[cfg(all(not(feature = "testing"), any(feature = "as2", feature = "as4")))]
#[test]
fn fail_closed_requires_production_durable_sink_in_non_testing_builds() {
    let bus = EventBus::new_with_config_and_mode(
        16,
        Some(Arc::new(InMemoryAuditSink::new().expect("audit sink"))),
        BackpressurePolicy::default(),
        EventEmissionMode::StrictTransactional,
    )
    .expect("event bus");
    let sess = session("s-audit-prod-required", "p-required");

    let err = require_durable_audit_sink(&sess, &bus, true, "as4_receive_push")
        .expect_err("non-testing fail-closed mode requires production durability");

    assert_eq!(err.code, ErrorCode::ReliabilityFailure);
    assert!(err.message.contains("production-durable audit sink"));
}

#[test]
fn production_durable_sink_rejects_ephemeral_sink() {
    let bus = EventBus::new_with_config_and_mode(
        16,
        Some(Arc::new(InMemoryAuditSink::new().expect("audit sink"))),
        BackpressurePolicy::default(),
        EventEmissionMode::StrictTransactional,
    )
    .expect("event bus");
    assert!(!bus.has_production_durable_audit_sink());
}

#[test]
fn production_durable_sink_accepts_durable_sink() {
    let bus = EventBus::new_with_config_and_mode(
        16,
        Some(Arc::new(DurableTestAuditSink::new())),
        BackpressurePolicy::default(),
        EventEmissionMode::StrictTransactional,
    )
    .expect("event bus");
    assert!(bus.has_production_durable_audit_sink());
}

#[test]
fn backpressure_track_mode_never_fails_on_drop() {
    // Default policy (Track) — should allow any number of drops without error.
    let bus = EventBus::new_with_config_and_mode(
        16,
        None,
        BackpressurePolicy {
            max_dropped: Some(1),
            max_lagged: None,
            action: BackpressureAction::Track,
            window_secs: 60,
            session_channel_capacity: 64,
        },
        EventEmissionMode::BestEffort,
    )
    .expect("event bus");
    let sess = session("s-bp-track", "p1");

    bus.emit(
        &sess,
        AsxEvent::MessageSigned {
            message_id: "m".into(),
        },
    )
    .expect("track mode must not fail on dropped events in best-effort mode");
    assert_eq!(bus.metrics().dropped(), 1);
}

#[test]
fn backpressure_fail_closed_on_drop_threshold() {
    let bus = EventBus::new_with_config_and_mode(
        16,
        None,
        BackpressurePolicy {
            max_dropped: Some(3),
            max_lagged: None,
            action: BackpressureAction::FailClosed,
            window_secs: 60,
            session_channel_capacity: 64,
        },
        EventEmissionMode::BestEffort,
    )
    .expect("event bus");
    let sess = session("s-bp-drop", "p1");

    bus.emit(
        &sess,
        AsxEvent::MessageSigned {
            message_id: "m-1".into(),
        },
    )
    .expect("below threshold should not fail");

    bus.emit(
        &sess,
        AsxEvent::MessageSigned {
            message_id: "m-2".into(),
        },
    )
    .expect("below threshold should not fail");

    let err = bus
        .emit(
            &sess,
            AsxEvent::MessageSigned {
                message_id: "m-3".into(),
            },
        )
        .expect_err("at threshold must fail closed");
    assert_eq!(err.code, ErrorCode::ReliabilityFailure);
    assert!(err.message.contains("dropped"));
}

#[test]
fn backpressure_fail_closed_lagged_window_self_heals_after_expiry() {
    use std::sync::atomic::Ordering;
    use std::time::{SystemTime, UNIX_EPOCH};

    let bus = EventBus::new_with_config_and_mode(
        16,
        None,
        BackpressurePolicy {
            max_dropped: None,
            max_lagged: Some(2),
            action: BackpressureAction::FailClosed,
            window_secs: 60,
            session_channel_capacity: 64,
        },
        EventEmissionMode::BestEffort,
    )
    .expect("event bus");
    let sess = session("s-bp-lag", "p1");

    // Simulate a *past* window that saturated the lagged counter. Before the
    // read-path reset fix, this stale count would wedge FailClosed forever.
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_secs();
    bus.metrics
        .window_epoch
        .store(now.saturating_sub(120), Ordering::SeqCst);
    bus.metrics.window_lagged.store(99, Ordering::SeqCst);

    // The window has expired, so the effective count is 0 and emit succeeds.
    assert_eq!(bus.metrics.current_window_lagged(), 0);
    bus.emit(
        &sess,
        AsxEvent::MessageSigned {
            message_id: "m-after-window".into(),
        },
    )
    .expect("expired-window lag count must not wedge FailClosed");
}

#[test]
fn init_rejects_zero_session_channel_capacity() {
    let result = EventBus::new_with_config_and_mode(
        16,
        None,
        BackpressurePolicy {
            max_dropped: None,
            max_lagged: None,
            action: BackpressureAction::Track,
            window_secs: 60,
            session_channel_capacity: 0,
        },
        EventEmissionMode::StrictTransactional,
    );

    let err = match result {
        Ok(_) => panic!("zero session channel capacity must be rejected"),
        Err(err) => err,
    };

    assert_eq!(err.code, ErrorCode::InvalidInput);
    assert!(err.message.contains("session channel capacity"));
}

#[test]
fn init_rejects_zero_backpressure_window_secs() {
    let result = EventBus::new_with_config_and_mode(
        16,
        None,
        BackpressurePolicy {
            max_dropped: None,
            max_lagged: None,
            action: BackpressureAction::Track,
            window_secs: 0,
            session_channel_capacity: 64,
        },
        EventEmissionMode::StrictTransactional,
    );

    let err = match result {
        Ok(_) => panic!("zero backpressure window must be rejected"),
        Err(err) => err,
    };

    assert_eq!(err.code, ErrorCode::InvalidInput);
    assert!(err.message.contains("window_secs"));
}

#[test]
fn receipt_taxonomy_outcomes_export_label_stable_metric_counter() {
    let metrics_sink = Arc::new(RecordingMetricsSink::default());
    let bus = EventBus::new_with_config_and_mode_and_metrics(
        16,
        None,
        BackpressurePolicy::default(),
        EventEmissionMode::BestEffort,
        metrics_sink.clone(),
    )
    .expect("event bus");
    let sess = session("s-taxonomy", "p1");

    bus.emit(
        &sess,
        AsxEvent::ReceiptTaxonomyOutcome {
            message_id: "msg-1".into(),
            signal: "as4",
            outcome: "security_verification_failed",
            detail: "receipt_signature_verification_failed",
        },
    )
    .expect("emit taxonomy outcome");

    let counters = metrics_sink.counters.lock().expect("metrics lock");
    let taxonomy = counters
        .iter()
        .find(|c| c.name == "asx_as4_receipt_taxonomy_outcome_total")
        .expect("taxonomy metric");

    assert_eq!(taxonomy.value, 1);
    assert!(taxonomy.labels.contains(&("protocol", "as4".to_string())));
    assert!(taxonomy.labels.contains(&("signal", "as4".to_string())));
    assert!(
        taxonomy
            .labels
            .contains(&("outcome", "security_verification_failed".to_string()))
    );
    assert!(taxonomy.labels.contains(&(
        "detail",
        "receipt_signature_verification_failed".to_string()
    )));
}

#[test]
fn spool_key_provider_health_check_failed_event_metadata_is_stable() {
    let event = AsxEvent::SpoolKeyProviderHealthCheckFailed {
        provider: "kms-env",
        health_state: "failing",
        phase: "key_resolution",
        error_code: "policy_violation",
    };

    assert_eq!(event.kind(), "spool_key_provider_health_check_failed");
    assert_eq!(event_code(&event), "spool_key_provider_health_check_failed");
    assert_eq!(
        event_message(&event),
        "Spool key provider health check failed"
    );
}

#[test]
fn spool_key_provider_health_checked_event_metadata_is_stable() {
    let event = AsxEvent::SpoolKeyProviderHealthChecked {
        provider: "local-env",
        health_state: "healthy",
        resolve_key_ms: 2,
    };

    assert_eq!(event.kind(), "spool_key_provider_health_checked");
    assert_eq!(event_code(&event), "spool_key_provider_health_checked");
    assert_eq!(event_message(&event), "Spool key provider health checked");
}

#[test]
fn spool_headroom_checked_event_metadata_is_stable() {
    let event = AsxEvent::SpoolHeadroomChecked {
        stage: "as2_receive_stream",
        free_bytes: 1024,
        min_required_bytes: 512,
    };

    assert_eq!(event.kind(), "spool_headroom_checked");
    assert_eq!(event_code(&event), "spool_headroom_checked");
    assert_eq!(event_message(&event), "Spool headroom checked");
}

#[test]
fn spool_key_provider_health_state_changed_event_metadata_is_stable() {
    let event = AsxEvent::SpoolKeyProviderHealthStateChanged {
        provider: "local-env",
        previous_state: "failing",
        current_state: "healthy",
        reason: "policy_ready",
    };

    assert_eq!(event.kind(), "spool_key_provider_health_state_changed");
    assert_eq!(
        event_code(&event),
        "spool_key_provider_health_state_changed"
    );
    assert_eq!(
        event_message(&event),
        "Spool key provider health state changed"
    );
}

// ── FR-2 regression: EventBus::new_for_testing ─────────────────────────────

#[cfg(feature = "testing")]
#[test]
fn new_for_testing_is_best_effort_and_infallible() {
    let bus = EventBus::new_for_testing();
    assert_eq!(bus.emission_mode(), EventEmissionMode::BestEffort);
}

#[cfg(feature = "testing")]
#[test]
fn new_for_testing_silently_drops_events_without_subscriber() {
    let bus = EventBus::new_for_testing();
    let sess = crate::core::SessionContext::new("s-testing", "p1", "strict").expect("session");
    // Emitting without a subscriber must not fail in BestEffort mode.
    let result = bus.emit(
        &sess,
        crate::observability::AsxEvent::MessageSigned {
            message_id: "m1".into(),
        },
    );
    assert!(
        result.is_ok(),
        "new_for_testing must never fail on emit: {result:?}"
    );
    assert_eq!(
        bus.metrics().dropped(),
        1,
        "event should be counted as dropped"
    );
}