oxirs-stream 0.2.4

Real-time streaming support with Kafka/NATS/MQTT/OPC-UA I/O, RDF Patch, and SPARQL Update delta
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
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
//! Stream message routing engine.
//!
//! Provides content-based, topic-based, and header-based message routing with
//! round-robin distribution, priority evaluation, regex pattern matching, and
//! dead-letter routing for unroutable messages.

use std::collections::{HashMap, VecDeque};

// ──────────────────────────────────────────────────────────────────────────────
// Types
// ──────────────────────────────────────────────────────────────────────────────

/// The kind of matching a routing rule uses.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RoutingStrategy {
    /// Match when a payload field equals a specific value.
    ContentBased { field: String, value: String },
    /// Match when the topic name equals a literal string.
    TopicExact(String),
    /// Match when the topic name matches a regex pattern.
    TopicRegex(String),
    /// Match when a header key equals a specific value.
    HeaderBased { key: String, value: String },
    /// Distribute messages round-robin across destinations.
    RoundRobin(Vec<String>),
}

/// A single routing rule with priority and target destination.
#[derive(Debug, Clone)]
pub struct RoutingRule {
    /// Unique identifier for the rule.
    pub id: String,
    /// Evaluation priority (higher is evaluated first).
    pub priority: u32,
    /// The matching strategy.
    pub strategy: RoutingStrategy,
    /// Target destination name (topic, queue, etc.).
    pub destination: String,
    /// Whether the rule is currently active.
    pub enabled: bool,
}

/// A message to be routed.
#[derive(Debug, Clone)]
pub struct RoutableMessage {
    /// Unique message identifier.
    pub id: String,
    /// The topic the message was published to.
    pub topic: String,
    /// Key-value headers attached to the message.
    pub headers: HashMap<String, String>,
    /// Key-value payload fields (simplified for routing decisions).
    pub payload_fields: HashMap<String, String>,
    /// Raw payload bytes.
    pub payload: Vec<u8>,
}

/// The outcome of routing a single message.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RoutingOutcome {
    /// Successfully routed to a destination.
    Routed {
        destination: String,
        rule_id: String,
    },
    /// No matching rule — sent to DLQ.
    DeadLettered,
}

/// Aggregate routing statistics.
#[derive(Debug, Clone, Default)]
pub struct RoutingStats {
    /// Total messages evaluated.
    pub total_evaluated: u64,
    /// Messages successfully routed.
    pub total_routed: u64,
    /// Messages sent to DLQ (no matching rule).
    pub total_dead_lettered: u64,
    /// Per-destination message counts.
    pub per_destination: HashMap<String, u64>,
    /// Per-rule match counts.
    pub per_rule: HashMap<String, u64>,
}

/// Configuration for the [`StreamRouter`].
#[derive(Debug, Clone)]
pub struct RouterConfig {
    /// Maximum number of dead-lettered messages to retain.
    pub dlq_capacity: usize,
    /// Whether to enable the dead-letter queue.
    pub enable_dlq: bool,
}

impl Default for RouterConfig {
    fn default() -> Self {
        Self {
            dlq_capacity: 10_000,
            enable_dlq: true,
        }
    }
}

/// Router error types.
#[derive(Debug)]
pub enum RouterError {
    /// A rule with this ID already exists.
    DuplicateRuleId(String),
    /// The referenced rule ID was not found.
    RuleNotFound(String),
    /// The DLQ is full.
    DlqFull,
    /// Invalid regex pattern.
    InvalidRegex(String),
}

impl std::fmt::Display for RouterError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            RouterError::DuplicateRuleId(id) => write!(f, "duplicate rule id: {}", id),
            RouterError::RuleNotFound(id) => write!(f, "rule not found: {}", id),
            RouterError::DlqFull => write!(f, "dead letter queue is full"),
            RouterError::InvalidRegex(pat) => write!(f, "invalid regex pattern: {}", pat),
        }
    }
}

impl std::error::Error for RouterError {}

// ──────────────────────────────────────────────────────────────────────────────
// StreamRouter
// ──────────────────────────────────────────────────────────────────────────────

/// Stream message router with content-, topic-, and header-based routing,
/// round-robin distribution, priority ordering, and a dead-letter queue.
pub struct StreamRouter {
    config: RouterConfig,
    /// Rules sorted by descending priority (highest first).
    rules: Vec<RoutingRule>,
    /// Dead-lettered messages.
    dlq: VecDeque<RoutableMessage>,
    /// Running statistics.
    stats: RoutingStats,
    /// Round-robin counters keyed by rule ID.
    round_robin_counters: HashMap<String, usize>,
}

impl StreamRouter {
    /// Create a new router with the given configuration.
    pub fn new(config: RouterConfig) -> Self {
        Self {
            config,
            rules: Vec::new(),
            dlq: VecDeque::new(),
            stats: RoutingStats::default(),
            round_robin_counters: HashMap::new(),
        }
    }

    /// Create a router with default configuration.
    pub fn with_defaults() -> Self {
        Self::new(RouterConfig::default())
    }

    /// Add a routing rule. Rules are kept sorted by descending priority.
    pub fn add_rule(&mut self, rule: RoutingRule) -> Result<(), RouterError> {
        if self.rules.iter().any(|r| r.id == rule.id) {
            return Err(RouterError::DuplicateRuleId(rule.id));
        }
        // Validate regex patterns eagerly
        if let RoutingStrategy::TopicRegex(ref pat) = rule.strategy {
            Self::compile_regex(pat)?;
        }
        self.rules.push(rule);
        self.rules.sort_by_key(|b| std::cmp::Reverse(b.priority));
        Ok(())
    }

    /// Remove a routing rule by ID.
    pub fn remove_rule(&mut self, rule_id: &str) -> Result<RoutingRule, RouterError> {
        let idx = self
            .rules
            .iter()
            .position(|r| r.id == rule_id)
            .ok_or_else(|| RouterError::RuleNotFound(rule_id.to_string()))?;
        let removed = self.rules.remove(idx);
        self.round_robin_counters.remove(rule_id);
        Ok(removed)
    }

    /// Update an existing rule in place.
    pub fn update_rule(&mut self, rule: RoutingRule) -> Result<(), RouterError> {
        let idx = self
            .rules
            .iter()
            .position(|r| r.id == rule.id)
            .ok_or_else(|| RouterError::RuleNotFound(rule.id.clone()))?;
        // Validate regex patterns eagerly
        if let RoutingStrategy::TopicRegex(ref pat) = rule.strategy {
            Self::compile_regex(pat)?;
        }
        self.rules[idx] = rule;
        self.rules.sort_by_key(|b| std::cmp::Reverse(b.priority));
        Ok(())
    }

    /// Enable or disable a rule.
    pub fn set_rule_enabled(&mut self, rule_id: &str, enabled: bool) -> Result<(), RouterError> {
        let rule = self
            .rules
            .iter_mut()
            .find(|r| r.id == rule_id)
            .ok_or_else(|| RouterError::RuleNotFound(rule_id.to_string()))?;
        rule.enabled = enabled;
        Ok(())
    }

    /// Return a snapshot of current routing statistics.
    pub fn stats(&self) -> &RoutingStats {
        &self.stats
    }

    /// Return the number of active (enabled) rules.
    pub fn active_rule_count(&self) -> usize {
        self.rules.iter().filter(|r| r.enabled).count()
    }

    /// Return total number of rules.
    pub fn rule_count(&self) -> usize {
        self.rules.len()
    }

    /// Return the dead-letter queue contents.
    pub fn dlq(&self) -> &VecDeque<RoutableMessage> {
        &self.dlq
    }

    /// Pop the oldest dead-lettered message.
    pub fn pop_dlq(&mut self) -> Option<RoutableMessage> {
        self.dlq.pop_front()
    }

    /// Clear all dead-lettered messages.
    pub fn clear_dlq(&mut self) {
        self.dlq.clear();
    }

    /// Route a single message through the rule chain.
    ///
    /// The first matching enabled rule (by priority) wins. If no rule matches,
    /// the message is dead-lettered (if DLQ is enabled).
    pub fn route(&mut self, message: RoutableMessage) -> RoutingOutcome {
        self.stats.total_evaluated += 1;

        // Find the matching rule index first (immutable pass), then update
        // counters and resolve destination (mutable pass) to satisfy the borrow checker.
        let matched_idx = self
            .rules
            .iter()
            .position(|rule| rule.enabled && Self::matches_rule_static(rule, &message));

        if let Some(idx) = matched_idx {
            let rule_id = self.rules[idx].id.clone();
            let destination = self.resolve_destination_by_index(idx);
            self.stats.total_routed += 1;
            *self
                .stats
                .per_destination
                .entry(destination.clone())
                .or_insert(0) += 1;
            *self.stats.per_rule.entry(rule_id.clone()).or_insert(0) += 1;
            return RoutingOutcome::Routed {
                destination,
                rule_id,
            };
        }

        // No rule matched — dead letter
        self.stats.total_dead_lettered += 1;
        if self.config.enable_dlq {
            if self.dlq.len() >= self.config.dlq_capacity {
                // Evict the oldest entry to make room
                self.dlq.pop_front();
            }
            self.dlq.push_back(message);
        }
        RoutingOutcome::DeadLettered
    }

    /// Route a batch of messages. Returns one outcome per message.
    pub fn route_batch(&mut self, messages: Vec<RoutableMessage>) -> Vec<RoutingOutcome> {
        messages.into_iter().map(|m| self.route(m)).collect()
    }

    /// Reset statistics counters to zero.
    pub fn reset_stats(&mut self) {
        self.stats = RoutingStats::default();
    }

    /// Return all rule IDs in priority order (highest first).
    pub fn rule_ids(&self) -> Vec<&str> {
        self.rules.iter().map(|r| r.id.as_str()).collect()
    }

    /// Return a reference to a rule by ID, if it exists.
    pub fn get_rule(&self, rule_id: &str) -> Option<&RoutingRule> {
        self.rules.iter().find(|r| r.id == rule_id)
    }

    // ── Private helpers ──────────────────────────────────────────────────────

    /// Test whether a rule matches a given message (static, no &self needed).
    fn matches_rule_static(rule: &RoutingRule, message: &RoutableMessage) -> bool {
        match &rule.strategy {
            RoutingStrategy::ContentBased { field, value } => {
                message.payload_fields.get(field) == Some(value)
            }
            RoutingStrategy::TopicExact(topic) => message.topic == *topic,
            RoutingStrategy::TopicRegex(pattern) => Self::regex_matches(pattern, &message.topic),
            RoutingStrategy::HeaderBased { key, value } => message.headers.get(key) == Some(value),
            RoutingStrategy::RoundRobin(destinations) => !destinations.is_empty(),
        }
    }

    /// Resolve the actual destination string by rule index. For round-robin, pick the next target.
    fn resolve_destination_by_index(&mut self, idx: usize) -> String {
        let rule = &self.rules[idx];
        match &rule.strategy {
            RoutingStrategy::RoundRobin(destinations) if !destinations.is_empty() => {
                let rule_id = rule.id.clone();
                let counter = self.round_robin_counters.entry(rule_id).or_insert(0);
                let dest_idx = *counter % destinations.len();
                *counter = counter.wrapping_add(1);
                destinations[dest_idx].clone()
            }
            _ => rule.destination.clone(),
        }
    }

    /// Compile a regex pattern, returning an error if invalid.
    fn compile_regex(pattern: &str) -> Result<(), RouterError> {
        // Simple regex-like matching: we support `*` as wildcard and `^`/`$` anchors.
        // For full regex we validate the pattern manually.
        if pattern.is_empty() {
            return Err(RouterError::InvalidRegex("empty pattern".to_string()));
        }
        // Check for unbalanced parentheses
        let mut depth: i32 = 0;
        for ch in pattern.chars() {
            match ch {
                '(' => depth += 1,
                ')' => depth -= 1,
                _ => {}
            }
            if depth < 0 {
                return Err(RouterError::InvalidRegex(
                    "unbalanced parentheses".to_string(),
                ));
            }
        }
        if depth != 0 {
            return Err(RouterError::InvalidRegex(
                "unbalanced parentheses".to_string(),
            ));
        }
        Ok(())
    }

    /// Simple regex-like topic matching.
    ///
    /// Supported syntax:
    /// - `*` matches any sequence of characters (equivalent to `.*`)
    /// - Literal characters match themselves
    /// - `^` and `$` anchor to start/end
    fn regex_matches(pattern: &str, input: &str) -> bool {
        // Convert our simplified pattern to segment-based matching
        let anchored_start = pattern.starts_with('^');
        let anchored_end = pattern.ends_with('$');

        let trimmed = pattern.trim_start_matches('^').trim_end_matches('$');

        if trimmed.is_empty() {
            return input.is_empty();
        }

        // Split on `*` to get literal segments
        let segments: Vec<&str> = trimmed.split('*').collect();

        if segments.len() == 1 {
            // No wildcards
            let seg = segments[0];
            if anchored_start && anchored_end {
                return input == seg;
            }
            if anchored_start {
                return input.starts_with(seg);
            }
            if anchored_end {
                return input.ends_with(seg);
            }
            return input.contains(seg);
        }

        // Multiple segments separated by wildcards
        let mut pos = 0usize;

        for (i, seg) in segments.iter().enumerate() {
            if seg.is_empty() {
                continue;
            }
            if i == 0 && anchored_start {
                if !input.starts_with(seg) {
                    return false;
                }
                pos = seg.len();
                continue;
            }

            match input[pos..].find(seg) {
                Some(found) => {
                    pos += found + seg.len();
                }
                None => return false,
            }
        }

        if anchored_end {
            let last_seg = segments.last().unwrap_or(&"");
            if last_seg.is_empty() {
                return true; // pattern ends with `*`
            }
            return input.ends_with(last_seg);
        }

        true
    }
}

// ──────────────────────────────────────────────────────────────────────────────
// Tests
// ──────────────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    fn make_message(id: &str, topic: &str) -> RoutableMessage {
        RoutableMessage {
            id: id.to_string(),
            topic: topic.to_string(),
            headers: HashMap::new(),
            payload_fields: HashMap::new(),
            payload: Vec::new(),
        }
    }

    fn make_message_with_header(id: &str, topic: &str, key: &str, val: &str) -> RoutableMessage {
        let mut msg = make_message(id, topic);
        msg.headers.insert(key.to_string(), val.to_string());
        msg
    }

    fn make_message_with_field(id: &str, topic: &str, key: &str, val: &str) -> RoutableMessage {
        let mut msg = make_message(id, topic);
        msg.payload_fields.insert(key.to_string(), val.to_string());
        msg
    }

    // ── Basic construction ───────────────────────────────────────────────────

    #[test]
    fn test_router_creation_defaults() {
        let router = StreamRouter::with_defaults();
        assert_eq!(router.rule_count(), 0);
        assert_eq!(router.active_rule_count(), 0);
        assert_eq!(router.stats().total_evaluated, 0);
    }

    #[test]
    fn test_router_creation_custom_config() {
        let cfg = RouterConfig {
            dlq_capacity: 500,
            enable_dlq: false,
        };
        let router = StreamRouter::new(cfg);
        assert_eq!(router.rule_count(), 0);
        assert!(router.dlq().is_empty());
    }

    // ── Rule management ──────────────────────────────────────────────────────

    #[test]
    fn test_add_rule() {
        let mut router = StreamRouter::with_defaults();
        let rule = RoutingRule {
            id: "r1".to_string(),
            priority: 10,
            strategy: RoutingStrategy::TopicExact("orders".to_string()),
            destination: "order-queue".to_string(),
            enabled: true,
        };
        assert!(router.add_rule(rule).is_ok());
        assert_eq!(router.rule_count(), 1);
    }

    #[test]
    fn test_add_duplicate_rule() {
        let mut router = StreamRouter::with_defaults();
        let rule = RoutingRule {
            id: "r1".to_string(),
            priority: 10,
            strategy: RoutingStrategy::TopicExact("orders".to_string()),
            destination: "q".to_string(),
            enabled: true,
        };
        assert!(router.add_rule(rule.clone()).is_ok());
        assert!(router.add_rule(rule).is_err());
    }

    #[test]
    fn test_remove_rule() {
        let mut router = StreamRouter::with_defaults();
        let rule = RoutingRule {
            id: "r1".to_string(),
            priority: 10,
            strategy: RoutingStrategy::TopicExact("x".to_string()),
            destination: "y".to_string(),
            enabled: true,
        };
        router.add_rule(rule).ok();
        let removed = router.remove_rule("r1");
        assert!(removed.is_ok());
        assert_eq!(router.rule_count(), 0);
    }

    #[test]
    fn test_remove_nonexistent_rule() {
        let mut router = StreamRouter::with_defaults();
        assert!(router.remove_rule("nope").is_err());
    }

    #[test]
    fn test_update_rule() {
        let mut router = StreamRouter::with_defaults();
        let rule = RoutingRule {
            id: "r1".to_string(),
            priority: 5,
            strategy: RoutingStrategy::TopicExact("a".to_string()),
            destination: "dest1".to_string(),
            enabled: true,
        };
        router.add_rule(rule).ok();

        let updated = RoutingRule {
            id: "r1".to_string(),
            priority: 20,
            strategy: RoutingStrategy::TopicExact("b".to_string()),
            destination: "dest2".to_string(),
            enabled: true,
        };
        assert!(router.update_rule(updated).is_ok());
        assert_eq!(
            router.get_rule("r1").map(|r| &r.destination),
            Some(&"dest2".to_string())
        );
    }

    #[test]
    fn test_update_nonexistent_rule() {
        let mut router = StreamRouter::with_defaults();
        let rule = RoutingRule {
            id: "nope".to_string(),
            priority: 1,
            strategy: RoutingStrategy::TopicExact("x".to_string()),
            destination: "y".to_string(),
            enabled: true,
        };
        assert!(router.update_rule(rule).is_err());
    }

    #[test]
    fn test_enable_disable_rule() {
        let mut router = StreamRouter::with_defaults();
        let rule = RoutingRule {
            id: "r1".to_string(),
            priority: 1,
            strategy: RoutingStrategy::TopicExact("x".to_string()),
            destination: "y".to_string(),
            enabled: true,
        };
        router.add_rule(rule).ok();
        assert_eq!(router.active_rule_count(), 1);

        router.set_rule_enabled("r1", false).ok();
        assert_eq!(router.active_rule_count(), 0);

        router.set_rule_enabled("r1", true).ok();
        assert_eq!(router.active_rule_count(), 1);
    }

    #[test]
    fn test_enable_nonexistent_rule() {
        let mut router = StreamRouter::with_defaults();
        assert!(router.set_rule_enabled("nope", true).is_err());
    }

    // ── Topic-exact routing ──────────────────────────────────────────────────

    #[test]
    fn test_topic_exact_match() {
        let mut router = StreamRouter::with_defaults();
        let rule = RoutingRule {
            id: "r1".to_string(),
            priority: 10,
            strategy: RoutingStrategy::TopicExact("orders".to_string()),
            destination: "order-queue".to_string(),
            enabled: true,
        };
        router.add_rule(rule).ok();

        let msg = make_message("m1", "orders");
        let outcome = router.route(msg);
        assert_eq!(
            outcome,
            RoutingOutcome::Routed {
                destination: "order-queue".to_string(),
                rule_id: "r1".to_string()
            }
        );
    }

    #[test]
    fn test_topic_exact_no_match() {
        let mut router = StreamRouter::with_defaults();
        let rule = RoutingRule {
            id: "r1".to_string(),
            priority: 10,
            strategy: RoutingStrategy::TopicExact("orders".to_string()),
            destination: "order-queue".to_string(),
            enabled: true,
        };
        router.add_rule(rule).ok();

        let msg = make_message("m1", "payments");
        let outcome = router.route(msg);
        assert_eq!(outcome, RoutingOutcome::DeadLettered);
    }

    // ── Topic-regex routing ──────────────────────────────────────────────────

    #[test]
    fn test_topic_regex_wildcard() {
        let mut router = StreamRouter::with_defaults();
        let rule = RoutingRule {
            id: "r1".to_string(),
            priority: 10,
            strategy: RoutingStrategy::TopicRegex("orders.*".to_string()),
            destination: "all-orders".to_string(),
            enabled: true,
        };
        router.add_rule(rule).ok();

        let msg = make_message("m1", "orders.us");
        assert!(matches!(router.route(msg), RoutingOutcome::Routed { .. }));
    }

    #[test]
    fn test_topic_regex_anchored() {
        let mut router = StreamRouter::with_defaults();
        let rule = RoutingRule {
            id: "r1".to_string(),
            priority: 10,
            strategy: RoutingStrategy::TopicRegex("^orders$".to_string()),
            destination: "exact-orders".to_string(),
            enabled: true,
        };
        router.add_rule(rule).ok();

        let match_msg = make_message("m1", "orders");
        assert!(matches!(
            router.route(match_msg),
            RoutingOutcome::Routed { .. }
        ));

        let no_match = make_message("m2", "orders.eu");
        assert_eq!(router.route(no_match), RoutingOutcome::DeadLettered);
    }

    #[test]
    fn test_topic_regex_contains() {
        let mut router = StreamRouter::with_defaults();
        let rule = RoutingRule {
            id: "r1".to_string(),
            priority: 10,
            strategy: RoutingStrategy::TopicRegex("ship".to_string()),
            destination: "shipping".to_string(),
            enabled: true,
        };
        router.add_rule(rule).ok();

        let msg = make_message("m1", "order-shipping-us");
        assert!(matches!(router.route(msg), RoutingOutcome::Routed { .. }));
    }

    #[test]
    fn test_invalid_regex_rejected() {
        let mut router = StreamRouter::with_defaults();
        let rule = RoutingRule {
            id: "r1".to_string(),
            priority: 10,
            strategy: RoutingStrategy::TopicRegex("(".to_string()),
            destination: "dest".to_string(),
            enabled: true,
        };
        assert!(router.add_rule(rule).is_err());
    }

    #[test]
    fn test_empty_regex_rejected() {
        let mut router = StreamRouter::with_defaults();
        let rule = RoutingRule {
            id: "r1".to_string(),
            priority: 10,
            strategy: RoutingStrategy::TopicRegex(String::new()),
            destination: "dest".to_string(),
            enabled: true,
        };
        assert!(router.add_rule(rule).is_err());
    }

    // ── Content-based routing ────────────────────────────────────────────────

    #[test]
    fn test_content_based_match() {
        let mut router = StreamRouter::with_defaults();
        let rule = RoutingRule {
            id: "r1".to_string(),
            priority: 10,
            strategy: RoutingStrategy::ContentBased {
                field: "type".to_string(),
                value: "order".to_string(),
            },
            destination: "order-queue".to_string(),
            enabled: true,
        };
        router.add_rule(rule).ok();

        let msg = make_message_with_field("m1", "events", "type", "order");
        assert!(matches!(router.route(msg), RoutingOutcome::Routed { .. }));
    }

    #[test]
    fn test_content_based_no_match() {
        let mut router = StreamRouter::with_defaults();
        let rule = RoutingRule {
            id: "r1".to_string(),
            priority: 10,
            strategy: RoutingStrategy::ContentBased {
                field: "type".to_string(),
                value: "order".to_string(),
            },
            destination: "order-queue".to_string(),
            enabled: true,
        };
        router.add_rule(rule).ok();

        let msg = make_message_with_field("m1", "events", "type", "payment");
        assert_eq!(router.route(msg), RoutingOutcome::DeadLettered);
    }

    #[test]
    fn test_content_based_missing_field() {
        let mut router = StreamRouter::with_defaults();
        let rule = RoutingRule {
            id: "r1".to_string(),
            priority: 10,
            strategy: RoutingStrategy::ContentBased {
                field: "region".to_string(),
                value: "us".to_string(),
            },
            destination: "us-queue".to_string(),
            enabled: true,
        };
        router.add_rule(rule).ok();

        let msg = make_message("m1", "events");
        assert_eq!(router.route(msg), RoutingOutcome::DeadLettered);
    }

    // ── Header-based routing ─────────────────────────────────────────────────

    #[test]
    fn test_header_based_match() {
        let mut router = StreamRouter::with_defaults();
        let rule = RoutingRule {
            id: "r1".to_string(),
            priority: 10,
            strategy: RoutingStrategy::HeaderBased {
                key: "X-Priority".to_string(),
                value: "high".to_string(),
            },
            destination: "priority-queue".to_string(),
            enabled: true,
        };
        router.add_rule(rule).ok();

        let msg = make_message_with_header("m1", "events", "X-Priority", "high");
        assert!(matches!(router.route(msg), RoutingOutcome::Routed { .. }));
    }

    #[test]
    fn test_header_based_no_match_wrong_value() {
        let mut router = StreamRouter::with_defaults();
        let rule = RoutingRule {
            id: "r1".to_string(),
            priority: 10,
            strategy: RoutingStrategy::HeaderBased {
                key: "X-Priority".to_string(),
                value: "high".to_string(),
            },
            destination: "priority-queue".to_string(),
            enabled: true,
        };
        router.add_rule(rule).ok();

        let msg = make_message_with_header("m1", "events", "X-Priority", "low");
        assert_eq!(router.route(msg), RoutingOutcome::DeadLettered);
    }

    #[test]
    fn test_header_based_missing_header() {
        let mut router = StreamRouter::with_defaults();
        let rule = RoutingRule {
            id: "r1".to_string(),
            priority: 10,
            strategy: RoutingStrategy::HeaderBased {
                key: "X-Priority".to_string(),
                value: "high".to_string(),
            },
            destination: "priority-queue".to_string(),
            enabled: true,
        };
        router.add_rule(rule).ok();

        let msg = make_message("m1", "events");
        assert_eq!(router.route(msg), RoutingOutcome::DeadLettered);
    }

    // ── Round-robin routing ──────────────────────────────────────────────────

    #[test]
    fn test_round_robin_distribution() {
        let mut router = StreamRouter::with_defaults();
        let rule = RoutingRule {
            id: "rr1".to_string(),
            priority: 10,
            strategy: RoutingStrategy::RoundRobin(vec![
                "dest-a".to_string(),
                "dest-b".to_string(),
                "dest-c".to_string(),
            ]),
            destination: String::new(), // not used for RR
            enabled: true,
        };
        router.add_rule(rule).ok();

        let o1 = router.route(make_message("m1", "any"));
        let o2 = router.route(make_message("m2", "any"));
        let o3 = router.route(make_message("m3", "any"));
        let o4 = router.route(make_message("m4", "any"));

        assert_eq!(
            o1,
            RoutingOutcome::Routed {
                destination: "dest-a".to_string(),
                rule_id: "rr1".to_string()
            }
        );
        assert_eq!(
            o2,
            RoutingOutcome::Routed {
                destination: "dest-b".to_string(),
                rule_id: "rr1".to_string()
            }
        );
        assert_eq!(
            o3,
            RoutingOutcome::Routed {
                destination: "dest-c".to_string(),
                rule_id: "rr1".to_string()
            }
        );
        assert_eq!(
            o4,
            RoutingOutcome::Routed {
                destination: "dest-a".to_string(),
                rule_id: "rr1".to_string()
            }
        );
    }

    #[test]
    fn test_round_robin_empty_destinations() {
        let mut router = StreamRouter::with_defaults();
        let rule = RoutingRule {
            id: "rr1".to_string(),
            priority: 10,
            strategy: RoutingStrategy::RoundRobin(vec![]),
            destination: String::new(),
            enabled: true,
        };
        router.add_rule(rule).ok();

        let outcome = router.route(make_message("m1", "any"));
        assert_eq!(outcome, RoutingOutcome::DeadLettered);
    }

    // ── Priority ordering ────────────────────────────────────────────────────

    #[test]
    fn test_priority_ordering() {
        let mut router = StreamRouter::with_defaults();

        // Low priority
        let low = RoutingRule {
            id: "low".to_string(),
            priority: 1,
            strategy: RoutingStrategy::TopicExact("orders".to_string()),
            destination: "low-queue".to_string(),
            enabled: true,
        };
        // High priority
        let high = RoutingRule {
            id: "high".to_string(),
            priority: 100,
            strategy: RoutingStrategy::TopicExact("orders".to_string()),
            destination: "high-queue".to_string(),
            enabled: true,
        };

        // Add low first, then high — high should still win
        router.add_rule(low).ok();
        router.add_rule(high).ok();

        let outcome = router.route(make_message("m1", "orders"));
        assert_eq!(
            outcome,
            RoutingOutcome::Routed {
                destination: "high-queue".to_string(),
                rule_id: "high".to_string()
            }
        );
    }

    #[test]
    fn test_disabled_rule_skipped_in_priority() {
        let mut router = StreamRouter::with_defaults();
        let high = RoutingRule {
            id: "high".to_string(),
            priority: 100,
            strategy: RoutingStrategy::TopicExact("orders".to_string()),
            destination: "high-queue".to_string(),
            enabled: false, // disabled
        };
        let low = RoutingRule {
            id: "low".to_string(),
            priority: 1,
            strategy: RoutingStrategy::TopicExact("orders".to_string()),
            destination: "low-queue".to_string(),
            enabled: true,
        };
        router.add_rule(high).ok();
        router.add_rule(low).ok();

        let outcome = router.route(make_message("m1", "orders"));
        assert_eq!(
            outcome,
            RoutingOutcome::Routed {
                destination: "low-queue".to_string(),
                rule_id: "low".to_string()
            }
        );
    }

    // ── Dead letter queue ────────────────────────────────────────────────────

    #[test]
    fn test_dlq_receives_unroutable() {
        let mut router = StreamRouter::with_defaults();
        let msg = make_message("m1", "unknown-topic");
        let outcome = router.route(msg);
        assert_eq!(outcome, RoutingOutcome::DeadLettered);
        assert_eq!(router.dlq().len(), 1);
    }

    #[test]
    fn test_dlq_disabled() {
        let cfg = RouterConfig {
            dlq_capacity: 100,
            enable_dlq: false,
        };
        let mut router = StreamRouter::new(cfg);
        let msg = make_message("m1", "unknown");
        router.route(msg);
        assert!(router.dlq().is_empty());
    }

    #[test]
    fn test_dlq_capacity_eviction() {
        let cfg = RouterConfig {
            dlq_capacity: 2,
            enable_dlq: true,
        };
        let mut router = StreamRouter::new(cfg);
        router.route(make_message("m1", "x"));
        router.route(make_message("m2", "x"));
        router.route(make_message("m3", "x"));
        assert_eq!(router.dlq().len(), 2);
        // m1 should have been evicted
        assert_eq!(router.dlq().front().map(|m| m.id.as_str()), Some("m2"));
    }

    #[test]
    fn test_pop_dlq() {
        let mut router = StreamRouter::with_defaults();
        router.route(make_message("m1", "x"));
        router.route(make_message("m2", "x"));
        let popped = router.pop_dlq();
        assert_eq!(popped.map(|m| m.id), Some("m1".to_string()));
        assert_eq!(router.dlq().len(), 1);
    }

    #[test]
    fn test_clear_dlq() {
        let mut router = StreamRouter::with_defaults();
        router.route(make_message("m1", "x"));
        router.route(make_message("m2", "x"));
        router.clear_dlq();
        assert!(router.dlq().is_empty());
    }

    // ── Statistics ────────────────────────────────────────────────────────────

    #[test]
    fn test_stats_tracking() {
        let mut router = StreamRouter::with_defaults();
        let rule = RoutingRule {
            id: "r1".to_string(),
            priority: 10,
            strategy: RoutingStrategy::TopicExact("orders".to_string()),
            destination: "q".to_string(),
            enabled: true,
        };
        router.add_rule(rule).ok();

        router.route(make_message("m1", "orders"));
        router.route(make_message("m2", "orders"));
        router.route(make_message("m3", "unknown"));

        assert_eq!(router.stats().total_evaluated, 3);
        assert_eq!(router.stats().total_routed, 2);
        assert_eq!(router.stats().total_dead_lettered, 1);
        assert_eq!(router.stats().per_destination.get("q"), Some(&2));
        assert_eq!(router.stats().per_rule.get("r1"), Some(&2));
    }

    #[test]
    fn test_reset_stats() {
        let mut router = StreamRouter::with_defaults();
        router.route(make_message("m1", "x"));
        router.reset_stats();
        assert_eq!(router.stats().total_evaluated, 0);
        assert_eq!(router.stats().total_routed, 0);
        assert_eq!(router.stats().total_dead_lettered, 0);
    }

    // ── Batch routing ────────────────────────────────────────────────────────

    #[test]
    fn test_batch_routing() {
        let mut router = StreamRouter::with_defaults();
        let rule = RoutingRule {
            id: "r1".to_string(),
            priority: 10,
            strategy: RoutingStrategy::TopicExact("orders".to_string()),
            destination: "q".to_string(),
            enabled: true,
        };
        router.add_rule(rule).ok();

        let messages = vec![
            make_message("m1", "orders"),
            make_message("m2", "payments"),
            make_message("m3", "orders"),
        ];
        let outcomes = router.route_batch(messages);
        assert_eq!(outcomes.len(), 3);
        assert!(matches!(outcomes[0], RoutingOutcome::Routed { .. }));
        assert_eq!(outcomes[1], RoutingOutcome::DeadLettered);
        assert!(matches!(outcomes[2], RoutingOutcome::Routed { .. }));
    }

    // ── Rule accessors ───────────────────────────────────────────────────────

    #[test]
    fn test_rule_ids_in_priority_order() {
        let mut router = StreamRouter::with_defaults();
        let r1 = RoutingRule {
            id: "low".to_string(),
            priority: 1,
            strategy: RoutingStrategy::TopicExact("a".to_string()),
            destination: "d".to_string(),
            enabled: true,
        };
        let r2 = RoutingRule {
            id: "high".to_string(),
            priority: 100,
            strategy: RoutingStrategy::TopicExact("b".to_string()),
            destination: "d".to_string(),
            enabled: true,
        };
        router.add_rule(r1).ok();
        router.add_rule(r2).ok();
        let ids = router.rule_ids();
        assert_eq!(ids, vec!["high", "low"]);
    }

    #[test]
    fn test_get_rule() {
        let mut router = StreamRouter::with_defaults();
        let rule = RoutingRule {
            id: "r1".to_string(),
            priority: 5,
            strategy: RoutingStrategy::TopicExact("x".to_string()),
            destination: "y".to_string(),
            enabled: true,
        };
        router.add_rule(rule).ok();
        assert!(router.get_rule("r1").is_some());
        assert!(router.get_rule("nope").is_none());
    }

    // ── Mixed strategy tests ─────────────────────────────────────────────────

    #[test]
    fn test_multiple_strategies() {
        let mut router = StreamRouter::with_defaults();
        let topic_rule = RoutingRule {
            id: "topic".to_string(),
            priority: 5,
            strategy: RoutingStrategy::TopicExact("orders".to_string()),
            destination: "topic-dest".to_string(),
            enabled: true,
        };
        let header_rule = RoutingRule {
            id: "header".to_string(),
            priority: 10,
            strategy: RoutingStrategy::HeaderBased {
                key: "X-VIP".to_string(),
                value: "true".to_string(),
            },
            destination: "vip-dest".to_string(),
            enabled: true,
        };
        router.add_rule(topic_rule).ok();
        router.add_rule(header_rule).ok();

        // Header rule has higher priority and matches
        let msg = make_message_with_header("m1", "orders", "X-VIP", "true");
        let outcome = router.route(msg);
        assert_eq!(
            outcome,
            RoutingOutcome::Routed {
                destination: "vip-dest".to_string(),
                rule_id: "header".to_string()
            }
        );
    }

    #[test]
    fn test_fallthrough_to_lower_priority() {
        let mut router = StreamRouter::with_defaults();
        let header_rule = RoutingRule {
            id: "header".to_string(),
            priority: 10,
            strategy: RoutingStrategy::HeaderBased {
                key: "X-VIP".to_string(),
                value: "true".to_string(),
            },
            destination: "vip-dest".to_string(),
            enabled: true,
        };
        let topic_rule = RoutingRule {
            id: "topic".to_string(),
            priority: 1,
            strategy: RoutingStrategy::TopicExact("orders".to_string()),
            destination: "topic-dest".to_string(),
            enabled: true,
        };
        router.add_rule(header_rule).ok();
        router.add_rule(topic_rule).ok();

        // No VIP header, falls through to topic match
        let msg = make_message("m1", "orders");
        let outcome = router.route(msg);
        assert_eq!(
            outcome,
            RoutingOutcome::Routed {
                destination: "topic-dest".to_string(),
                rule_id: "topic".to_string()
            }
        );
    }

    // ── Regex edge cases ─────────────────────────────────────────────────────

    #[test]
    fn test_regex_start_anchor() {
        let mut router = StreamRouter::with_defaults();
        let rule = RoutingRule {
            id: "r1".to_string(),
            priority: 10,
            strategy: RoutingStrategy::TopicRegex("^orders".to_string()),
            destination: "d".to_string(),
            enabled: true,
        };
        router.add_rule(rule).ok();

        assert!(matches!(
            router.route(make_message("m", "orders.us")),
            RoutingOutcome::Routed { .. }
        ));
        assert_eq!(
            router.route(make_message("m", "pre-orders")),
            RoutingOutcome::DeadLettered
        );
    }

    #[test]
    fn test_regex_end_anchor() {
        let mut router = StreamRouter::with_defaults();
        let rule = RoutingRule {
            id: "r1".to_string(),
            priority: 10,
            strategy: RoutingStrategy::TopicRegex("orders$".to_string()),
            destination: "d".to_string(),
            enabled: true,
        };
        router.add_rule(rule).ok();

        assert!(matches!(
            router.route(make_message("m", "all-orders")),
            RoutingOutcome::Routed { .. }
        ));
        assert_eq!(
            router.route(make_message("m", "orders-new")),
            RoutingOutcome::DeadLettered
        );
    }

    #[test]
    fn test_regex_wildcard_middle() {
        let mut router = StreamRouter::with_defaults();
        let rule = RoutingRule {
            id: "r1".to_string(),
            priority: 10,
            strategy: RoutingStrategy::TopicRegex("^order*done$".to_string()),
            destination: "d".to_string(),
            enabled: true,
        };
        router.add_rule(rule).ok();

        assert!(matches!(
            router.route(make_message("m", "order-processing-done")),
            RoutingOutcome::Routed { .. }
        ));
        assert!(matches!(
            router.route(make_message("m", "orderdone")),
            RoutingOutcome::Routed { .. }
        ));
    }

    // ── Router error display ─────────────────────────────────────────────────

    #[test]
    fn test_router_error_display() {
        let e1 = RouterError::DuplicateRuleId("r1".to_string());
        assert!(format!("{}", e1).contains("r1"));

        let e2 = RouterError::RuleNotFound("r2".to_string());
        assert!(format!("{}", e2).contains("r2"));

        let e3 = RouterError::DlqFull;
        assert!(format!("{}", e3).contains("full"));

        let e4 = RouterError::InvalidRegex("bad".to_string());
        assert!(format!("{}", e4).contains("bad"));
    }

    #[test]
    fn test_router_error_is_error() {
        let e: Box<dyn std::error::Error> = Box::new(RouterError::DuplicateRuleId("x".to_string()));
        assert!(!e.to_string().is_empty());
    }

    // ── Routing stats default ────────────────────────────────────────────────

    #[test]
    fn test_routing_stats_default() {
        let stats = RoutingStats::default();
        assert_eq!(stats.total_evaluated, 0);
        assert_eq!(stats.total_routed, 0);
        assert_eq!(stats.total_dead_lettered, 0);
        assert!(stats.per_destination.is_empty());
        assert!(stats.per_rule.is_empty());
    }

    // ── Config default ───────────────────────────────────────────────────────

    #[test]
    fn test_router_config_default() {
        let cfg = RouterConfig::default();
        assert_eq!(cfg.dlq_capacity, 10_000);
        assert!(cfg.enable_dlq);
    }

    // ── RoutingStrategy clone + eq ───────────────────────────────────────────

    #[test]
    fn test_routing_strategy_clone_eq() {
        let s1 = RoutingStrategy::TopicExact("x".to_string());
        let s2 = s1.clone();
        assert_eq!(s1, s2);

        let s3 = RoutingStrategy::ContentBased {
            field: "a".to_string(),
            value: "b".to_string(),
        };
        let s4 = s3.clone();
        assert_eq!(s3, s4);

        let s5 = RoutingStrategy::HeaderBased {
            key: "k".to_string(),
            value: "v".to_string(),
        };
        assert_eq!(s5.clone(), s5);

        let s6 = RoutingStrategy::RoundRobin(vec!["a".to_string()]);
        assert_eq!(s6.clone(), s6);

        let s7 = RoutingStrategy::TopicRegex("pat".to_string());
        assert_eq!(s7.clone(), s7);
    }

    // ── RoutingOutcome ───────────────────────────────────────────────────────

    #[test]
    fn test_routing_outcome_dead_lettered_eq() {
        assert_eq!(RoutingOutcome::DeadLettered, RoutingOutcome::DeadLettered);
    }

    #[test]
    fn test_routing_outcome_routed_eq() {
        let a = RoutingOutcome::Routed {
            destination: "x".to_string(),
            rule_id: "r".to_string(),
        };
        let b = a.clone();
        assert_eq!(a, b);
    }
}