oximedia-workflow 0.2.0

Comprehensive workflow orchestration engine for OxiMedia
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
//! Publish/subscribe event bus for workflow task communication.
//!
//! Provides a typed, thread-safe event bus that enables decoupled
//! communication between workflow tasks. Tasks can publish events
//! by topic, and other tasks can subscribe to topics with optional
//! filters. Supports event history, topic-based routing, and
//! dead letter tracking for undelivered events.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};

/// Unique identifier for a subscription.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SubscriptionId(u64);

impl SubscriptionId {
    /// Get the raw numeric ID.
    #[must_use]
    pub const fn as_u64(self) -> u64 {
        self.0
    }
}

impl std::fmt::Display for SubscriptionId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "sub-{}", self.0)
    }
}

/// An event published on the bus.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BusEvent {
    /// Event topic (e.g. "task.completed", "workflow.error").
    pub topic: String,
    /// Source identifier (task ID, workflow ID, etc.).
    pub source: String,
    /// Event payload as JSON value.
    pub payload: serde_json::Value,
    /// Timestamp in milliseconds since epoch.
    pub timestamp_ms: u64,
    /// Optional correlation ID for tracing related events.
    pub correlation_id: Option<String>,
    /// Event metadata.
    pub metadata: HashMap<String, String>,
}

impl BusEvent {
    /// Create a new event.
    #[must_use]
    pub fn new(
        topic: impl Into<String>,
        source: impl Into<String>,
        payload: serde_json::Value,
        timestamp_ms: u64,
    ) -> Self {
        Self {
            topic: topic.into(),
            source: source.into(),
            payload,
            timestamp_ms,
            correlation_id: None,
            metadata: HashMap::new(),
        }
    }

    /// Set correlation ID.
    #[must_use]
    pub fn with_correlation_id(mut self, id: impl Into<String>) -> Self {
        self.correlation_id = Some(id.into());
        self
    }

    /// Add metadata.
    #[must_use]
    pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.metadata.insert(key.into(), value.into());
        self
    }
}

/// Filter criteria for subscriptions.
#[derive(Debug, Clone)]
pub struct EventFilter {
    /// Required source prefix (if set, event source must start with this).
    pub source_prefix: Option<String>,
    /// Required metadata keys and values (all must match).
    pub metadata_match: HashMap<String, String>,
    /// Minimum payload field value (for numeric comparisons).
    /// Format: ("field_name", minimum_value).
    pub min_values: Vec<(String, f64)>,
}

impl Default for EventFilter {
    fn default() -> Self {
        Self {
            source_prefix: None,
            metadata_match: HashMap::new(),
            min_values: Vec::new(),
        }
    }
}

impl EventFilter {
    /// Create an empty filter (matches everything).
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Filter by source prefix.
    #[must_use]
    pub fn with_source_prefix(mut self, prefix: impl Into<String>) -> Self {
        self.source_prefix = Some(prefix.into());
        self
    }

    /// Require a metadata key-value match.
    #[must_use]
    pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.metadata_match.insert(key.into(), value.into());
        self
    }

    /// Require a minimum value for a payload field.
    #[must_use]
    pub fn with_min_value(mut self, field: impl Into<String>, min: f64) -> Self {
        self.min_values.push((field.into(), min));
        self
    }

    /// Check if an event matches this filter.
    #[must_use]
    pub fn matches(&self, event: &BusEvent) -> bool {
        // Check source prefix
        if let Some(ref prefix) = self.source_prefix {
            if !event.source.starts_with(prefix) {
                return false;
            }
        }

        // Check metadata
        for (key, expected) in &self.metadata_match {
            match event.metadata.get(key) {
                Some(actual) if actual == expected => {}
                _ => return false,
            }
        }

        // Check minimum values in payload
        for (field, min_val) in &self.min_values {
            if let Some(val) = event.payload.get(field).and_then(|v| v.as_f64()) {
                if val < *min_val {
                    return false;
                }
            } else {
                return false; // field not found or not a number
            }
        }

        true
    }
}

/// Internal subscription record.
#[derive(Debug, Clone)]
struct Subscription {
    id: SubscriptionId,
    topic_pattern: String,
    filter: EventFilter,
    /// Delivered events for this subscription.
    delivered: Vec<BusEvent>,
    /// Maximum events to retain (0 = unlimited).
    max_retained: usize,
    /// Whether this subscription is active.
    active: bool,
}

impl Subscription {
    /// Check if the subscription's topic pattern matches the event topic.
    fn topic_matches(&self, topic: &str) -> bool {
        if self.topic_pattern == "*" {
            return true;
        }
        if self.topic_pattern.ends_with(".*") {
            let prefix = &self.topic_pattern[..self.topic_pattern.len() - 2];
            return topic.starts_with(prefix);
        }
        self.topic_pattern == topic
    }
}

/// Dead letter entry for events that had no subscribers.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeadLetterEntry {
    /// The undelivered event.
    pub event: BusEvent,
    /// Reason it was undelivered.
    pub reason: String,
}

/// Thread-safe event bus for publish/subscribe communication.
#[derive(Debug, Clone)]
pub struct EventBus {
    inner: Arc<Mutex<EventBusInner>>,
}

#[derive(Debug)]
struct EventBusInner {
    subscriptions: Vec<Subscription>,
    next_sub_id: u64,
    /// All published events (for replay/history).
    event_history: Vec<BusEvent>,
    /// Events with no matching subscribers.
    dead_letters: Vec<DeadLetterEntry>,
    /// Maximum history size (0 = unlimited).
    max_history: usize,
    /// Maximum dead letter size.
    max_dead_letters: usize,
    /// Total events published.
    total_published: u64,
    /// Total deliveries across all subscriptions.
    total_delivered: u64,
}

/// Statistics for the event bus.
#[derive(Debug, Clone)]
pub struct EventBusStats {
    /// Number of active subscriptions.
    pub active_subscriptions: usize,
    /// Total subscriptions (including inactive).
    pub total_subscriptions: usize,
    /// Total events published.
    pub total_published: u64,
    /// Total deliveries across all subscriptions.
    pub total_delivered: u64,
    /// Events in history.
    pub history_size: usize,
    /// Dead letter count.
    pub dead_letter_count: usize,
}

/// Configuration for the event bus.
#[derive(Debug, Clone)]
pub struct EventBusConfig {
    /// Maximum event history size (0 = unlimited).
    pub max_history: usize,
    /// Maximum dead letter queue size.
    pub max_dead_letters: usize,
    /// Default max retained events per subscription.
    pub default_max_retained: usize,
}

impl Default for EventBusConfig {
    fn default() -> Self {
        Self {
            max_history: 10_000,
            max_dead_letters: 1_000,
            default_max_retained: 100,
        }
    }
}

impl EventBus {
    /// Create a new event bus with default config.
    #[must_use]
    pub fn new() -> Self {
        Self::with_config(EventBusConfig::default())
    }

    /// Create a new event bus with custom config.
    #[must_use]
    pub fn with_config(config: EventBusConfig) -> Self {
        Self {
            inner: Arc::new(Mutex::new(EventBusInner {
                subscriptions: Vec::new(),
                next_sub_id: 1,
                event_history: Vec::new(),
                dead_letters: Vec::new(),
                max_history: config.max_history,
                max_dead_letters: config.max_dead_letters,
                total_published: 0,
                total_delivered: 0,
            })),
        }
    }

    /// Subscribe to a topic pattern.
    ///
    /// Topic patterns:
    /// - Exact: `"task.completed"` matches only `"task.completed"`
    /// - Wildcard: `"task.*"` matches `"task.completed"`, `"task.failed"`, etc.
    /// - Catch-all: `"*"` matches everything
    ///
    /// Returns a subscription ID that can be used to retrieve delivered events
    /// or unsubscribe.
    pub fn subscribe(
        &self,
        topic_pattern: impl Into<String>,
        filter: EventFilter,
    ) -> SubscriptionId {
        let mut inner = self.inner.lock().unwrap_or_else(|e| e.into_inner());
        let id = SubscriptionId(inner.next_sub_id);
        inner.next_sub_id += 1;

        inner.subscriptions.push(Subscription {
            id,
            topic_pattern: topic_pattern.into(),
            filter,
            delivered: Vec::new(),
            max_retained: 100,
            active: true,
        });

        id
    }

    /// Subscribe with a custom max retained events count.
    pub fn subscribe_with_retention(
        &self,
        topic_pattern: impl Into<String>,
        filter: EventFilter,
        max_retained: usize,
    ) -> SubscriptionId {
        let mut inner = self.inner.lock().unwrap_or_else(|e| e.into_inner());
        let id = SubscriptionId(inner.next_sub_id);
        inner.next_sub_id += 1;

        inner.subscriptions.push(Subscription {
            id,
            topic_pattern: topic_pattern.into(),
            filter,
            delivered: Vec::new(),
            max_retained,
            active: true,
        });

        id
    }

    /// Unsubscribe (marks subscription as inactive).
    pub fn unsubscribe(&self, sub_id: SubscriptionId) -> bool {
        let mut inner = self.inner.lock().unwrap_or_else(|e| e.into_inner());
        if let Some(sub) = inner.subscriptions.iter_mut().find(|s| s.id == sub_id) {
            sub.active = false;
            true
        } else {
            false
        }
    }

    /// Publish an event to the bus.
    ///
    /// Returns the number of subscriptions that received the event.
    pub fn publish(&self, event: BusEvent) -> usize {
        let mut inner = self.inner.lock().unwrap_or_else(|e| e.into_inner());
        inner.total_published += 1;
        let mut delivery_count = 0usize;

        // Deliver to matching active subscriptions
        for sub in inner.subscriptions.iter_mut() {
            if !sub.active {
                continue;
            }
            if !sub.topic_matches(&event.topic) {
                continue;
            }
            if !sub.filter.matches(&event) {
                continue;
            }

            sub.delivered.push(event.clone());
            delivery_count += 1;

            // Trim retained events if over limit
            if sub.max_retained > 0 && sub.delivered.len() > sub.max_retained {
                let excess = sub.delivered.len() - sub.max_retained;
                sub.delivered.drain(..excess);
            }
        }

        inner.total_delivered += delivery_count as u64;

        // Track dead letters
        if delivery_count == 0 {
            inner.dead_letters.push(DeadLetterEntry {
                event: event.clone(),
                reason: "no matching subscribers".to_string(),
            });
            if inner.max_dead_letters > 0 && inner.dead_letters.len() > inner.max_dead_letters {
                inner.dead_letters.remove(0);
            }
        }

        // Add to history
        inner.event_history.push(event);
        if inner.max_history > 0 && inner.event_history.len() > inner.max_history {
            inner.event_history.remove(0);
        }

        delivery_count
    }

    /// Get events delivered to a subscription.
    #[must_use]
    pub fn get_events(&self, sub_id: SubscriptionId) -> Vec<BusEvent> {
        let inner = self.inner.lock().unwrap_or_else(|e| e.into_inner());
        inner
            .subscriptions
            .iter()
            .find(|s| s.id == sub_id)
            .map_or_else(Vec::new, |s| s.delivered.clone())
    }

    /// Drain (consume) events from a subscription, returning and clearing them.
    pub fn drain_events(&self, sub_id: SubscriptionId) -> Vec<BusEvent> {
        let mut inner = self.inner.lock().unwrap_or_else(|e| e.into_inner());
        inner
            .subscriptions
            .iter_mut()
            .find(|s| s.id == sub_id)
            .map_or_else(Vec::new, |s| std::mem::take(&mut s.delivered))
    }

    /// Get dead letters.
    #[must_use]
    pub fn dead_letters(&self) -> Vec<DeadLetterEntry> {
        let inner = self.inner.lock().unwrap_or_else(|e| e.into_inner());
        inner.dead_letters.clone()
    }

    /// Get event history.
    #[must_use]
    pub fn event_history(&self) -> Vec<BusEvent> {
        let inner = self.inner.lock().unwrap_or_else(|e| e.into_inner());
        inner.event_history.clone()
    }

    /// Get events from history matching a specific topic.
    #[must_use]
    pub fn events_by_topic(&self, topic: &str) -> Vec<BusEvent> {
        let inner = self.inner.lock().unwrap_or_else(|e| e.into_inner());
        inner
            .event_history
            .iter()
            .filter(|e| e.topic == topic)
            .cloned()
            .collect()
    }

    /// Get bus statistics.
    #[must_use]
    pub fn stats(&self) -> EventBusStats {
        let inner = self.inner.lock().unwrap_or_else(|e| e.into_inner());
        EventBusStats {
            active_subscriptions: inner.subscriptions.iter().filter(|s| s.active).count(),
            total_subscriptions: inner.subscriptions.len(),
            total_published: inner.total_published,
            total_delivered: inner.total_delivered,
            history_size: inner.event_history.len(),
            dead_letter_count: inner.dead_letters.len(),
        }
    }

    /// Clear all history, dead letters, and delivered events.
    pub fn clear(&self) {
        let mut inner = self.inner.lock().unwrap_or_else(|e| e.into_inner());
        inner.event_history.clear();
        inner.dead_letters.clear();
        for sub in &mut inner.subscriptions {
            sub.delivered.clear();
        }
    }

    /// Replay events from history to a new subscription.
    ///
    /// Returns the number of events replayed.
    pub fn replay_to(&self, sub_id: SubscriptionId) -> usize {
        let mut inner = self.inner.lock().unwrap_or_else(|e| e.into_inner());

        // Find the subscription index
        let sub_idx = match inner.subscriptions.iter().position(|s| s.id == sub_id) {
            Some(idx) => idx,
            None => return 0,
        };

        // Clone history to avoid borrow conflicts
        let history = inner.event_history.clone();
        let mut count = 0;

        for event in &history {
            let sub = &inner.subscriptions[sub_idx];
            if sub.active && sub.topic_matches(&event.topic) && sub.filter.matches(event) {
                inner.subscriptions[sub_idx].delivered.push(event.clone());
                count += 1;
            }
        }

        count
    }
}

impl Default for EventBus {
    fn default() -> Self {
        Self::new()
    }
}

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

    fn make_event(topic: &str, source: &str) -> BusEvent {
        BusEvent::new(topic, source, serde_json::json!({}), 1000)
    }

    // --- BusEvent ---

    #[test]
    fn test_event_creation() {
        let event = make_event("task.done", "task-1");
        assert_eq!(event.topic, "task.done");
        assert_eq!(event.source, "task-1");
        assert_eq!(event.timestamp_ms, 1000);
        assert!(event.correlation_id.is_none());
    }

    #[test]
    fn test_event_with_correlation() {
        let event = make_event("task.done", "task-1").with_correlation_id("corr-123");
        assert_eq!(event.correlation_id, Some("corr-123".to_string()));
    }

    #[test]
    fn test_event_with_metadata() {
        let event = make_event("task.done", "task-1").with_metadata("workflow", "wf-1");
        assert_eq!(event.metadata.get("workflow"), Some(&"wf-1".to_string()));
    }

    // --- EventFilter ---

    #[test]
    fn test_empty_filter_matches_all() {
        let filter = EventFilter::new();
        let event = make_event("any.topic", "any-source");
        assert!(filter.matches(&event));
    }

    #[test]
    fn test_source_prefix_filter() {
        let filter = EventFilter::new().with_source_prefix("task-");
        assert!(filter.matches(&make_event("x", "task-1")));
        assert!(!filter.matches(&make_event("x", "workflow-1")));
    }

    #[test]
    fn test_metadata_filter() {
        let filter = EventFilter::new().with_metadata("env", "production");
        let event = make_event("x", "y").with_metadata("env", "production");
        assert!(filter.matches(&event));

        let event2 = make_event("x", "y").with_metadata("env", "staging");
        assert!(!filter.matches(&event2));
    }

    #[test]
    fn test_min_value_filter() {
        let filter = EventFilter::new().with_min_value("quality", 90.0);
        let event = BusEvent::new("x", "y", serde_json::json!({"quality": 95.0}), 0);
        assert!(filter.matches(&event));

        let event2 = BusEvent::new("x", "y", serde_json::json!({"quality": 80.0}), 0);
        assert!(!filter.matches(&event2));
    }

    #[test]
    fn test_min_value_missing_field() {
        let filter = EventFilter::new().with_min_value("quality", 90.0);
        let event = BusEvent::new("x", "y", serde_json::json!({"other": 95.0}), 0);
        assert!(!filter.matches(&event));
    }

    // --- EventBus core ---

    #[test]
    fn test_bus_subscribe_and_publish() {
        let bus = EventBus::new();
        let sub = bus.subscribe("task.done", EventFilter::new());
        let delivered = bus.publish(make_event("task.done", "task-1"));
        assert_eq!(delivered, 1);

        let events = bus.get_events(sub);
        assert_eq!(events.len(), 1);
        assert_eq!(events[0].source, "task-1");
    }

    #[test]
    fn test_bus_no_match() {
        let bus = EventBus::new();
        bus.subscribe("task.done", EventFilter::new());
        let delivered = bus.publish(make_event("workflow.started", "wf-1"));
        assert_eq!(delivered, 0);
    }

    #[test]
    fn test_bus_wildcard_topic() {
        let bus = EventBus::new();
        let sub = bus.subscribe("task.*", EventFilter::new());
        bus.publish(make_event("task.done", "t1"));
        bus.publish(make_event("task.failed", "t2"));
        bus.publish(make_event("workflow.started", "wf1"));

        let events = bus.get_events(sub);
        assert_eq!(events.len(), 2);
    }

    #[test]
    fn test_bus_catch_all() {
        let bus = EventBus::new();
        let sub = bus.subscribe("*", EventFilter::new());
        bus.publish(make_event("task.done", "t1"));
        bus.publish(make_event("workflow.started", "wf1"));

        let events = bus.get_events(sub);
        assert_eq!(events.len(), 2);
    }

    #[test]
    fn test_bus_multiple_subscribers() {
        let bus = EventBus::new();
        let sub1 = bus.subscribe("task.done", EventFilter::new());
        let sub2 = bus.subscribe("task.done", EventFilter::new());
        let sub3 = bus.subscribe("task.failed", EventFilter::new());

        let delivered = bus.publish(make_event("task.done", "t1"));
        assert_eq!(delivered, 2);
        assert_eq!(bus.get_events(sub1).len(), 1);
        assert_eq!(bus.get_events(sub2).len(), 1);
        assert_eq!(bus.get_events(sub3).len(), 0);
    }

    #[test]
    fn test_bus_unsubscribe() {
        let bus = EventBus::new();
        let sub = bus.subscribe("task.done", EventFilter::new());

        bus.publish(make_event("task.done", "t1"));
        assert_eq!(bus.get_events(sub).len(), 1);

        assert!(bus.unsubscribe(sub));
        bus.publish(make_event("task.done", "t2"));
        // No new events after unsubscribe
        assert_eq!(bus.get_events(sub).len(), 1);
    }

    #[test]
    fn test_bus_unsubscribe_nonexistent() {
        let bus = EventBus::new();
        assert!(!bus.unsubscribe(SubscriptionId(999)));
    }

    #[test]
    fn test_bus_drain_events() {
        let bus = EventBus::new();
        let sub = bus.subscribe("task.done", EventFilter::new());
        bus.publish(make_event("task.done", "t1"));
        bus.publish(make_event("task.done", "t2"));

        let drained = bus.drain_events(sub);
        assert_eq!(drained.len(), 2);
        assert_eq!(bus.get_events(sub).len(), 0);
    }

    // --- Dead letters ---

    #[test]
    fn test_dead_letters() {
        let bus = EventBus::new();
        // No subscribers, so event goes to dead letters
        bus.publish(make_event("orphan.event", "src"));
        let dead = bus.dead_letters();
        assert_eq!(dead.len(), 1);
        assert_eq!(dead[0].event.topic, "orphan.event");
    }

    #[test]
    fn test_dead_letters_not_for_delivered() {
        let bus = EventBus::new();
        bus.subscribe("task.done", EventFilter::new());
        bus.publish(make_event("task.done", "t1"));
        assert!(bus.dead_letters().is_empty());
    }

    // --- History ---

    #[test]
    fn test_event_history() {
        let bus = EventBus::new();
        bus.publish(make_event("a", "s1"));
        bus.publish(make_event("b", "s2"));

        let history = bus.event_history();
        assert_eq!(history.len(), 2);
    }

    #[test]
    fn test_events_by_topic() {
        let bus = EventBus::new();
        bus.publish(make_event("task.done", "t1"));
        bus.publish(make_event("task.failed", "t2"));
        bus.publish(make_event("task.done", "t3"));

        let done_events = bus.events_by_topic("task.done");
        assert_eq!(done_events.len(), 2);
    }

    // --- Stats ---

    #[test]
    fn test_stats() {
        let bus = EventBus::new();
        let _sub1 = bus.subscribe("task.done", EventFilter::new());
        let sub2 = bus.subscribe("task.done", EventFilter::new());
        bus.unsubscribe(sub2);

        bus.publish(make_event("task.done", "t1"));
        bus.publish(make_event("orphan", "s1"));

        let stats = bus.stats();
        assert_eq!(stats.active_subscriptions, 1);
        assert_eq!(stats.total_subscriptions, 2);
        assert_eq!(stats.total_published, 2);
        assert_eq!(stats.total_delivered, 1);
        assert_eq!(stats.history_size, 2);
        assert_eq!(stats.dead_letter_count, 1);
    }

    // --- Retention limits ---

    #[test]
    fn test_retention_limit() {
        let bus = EventBus::new();
        let sub = bus.subscribe_with_retention("task.done", EventFilter::new(), 3);

        for i in 0..5 {
            bus.publish(make_event("task.done", &format!("t{i}")));
        }

        let events = bus.get_events(sub);
        assert_eq!(events.len(), 3);
        // Should have the latest 3
        assert_eq!(events[0].source, "t2");
        assert_eq!(events[2].source, "t4");
    }

    // --- Clear ---

    #[test]
    fn test_clear() {
        let bus = EventBus::new();
        let sub = bus.subscribe("task.done", EventFilter::new());
        bus.publish(make_event("task.done", "t1"));
        bus.publish(make_event("orphan", "s1"));

        bus.clear();
        assert!(bus.event_history().is_empty());
        assert!(bus.dead_letters().is_empty());
        assert!(bus.get_events(sub).is_empty());
    }

    // --- Replay ---

    #[test]
    fn test_replay_to_subscription() {
        let bus = EventBus::new();

        // Publish before subscribing
        bus.publish(make_event("task.done", "t1"));
        bus.publish(make_event("task.done", "t2"));
        bus.publish(make_event("workflow.started", "wf1"));

        // Late subscriber
        let sub = bus.subscribe("task.done", EventFilter::new());
        assert_eq!(bus.get_events(sub).len(), 0);

        // Replay history
        let replayed = bus.replay_to(sub);
        assert_eq!(replayed, 2);
        assert_eq!(bus.get_events(sub).len(), 2);
    }

    // --- Filter combined with topic ---

    #[test]
    fn test_topic_and_filter_combined() {
        let bus = EventBus::new();
        let filter = EventFilter::new().with_source_prefix("encoder-");
        let sub = bus.subscribe("task.done", filter);

        bus.publish(make_event("task.done", "encoder-1"));
        bus.publish(make_event("task.done", "decoder-1"));

        let events = bus.get_events(sub);
        assert_eq!(events.len(), 1);
        assert_eq!(events[0].source, "encoder-1");
    }

    #[test]
    fn test_subscription_id_display() {
        let id = SubscriptionId(42);
        assert_eq!(id.to_string(), "sub-42");
        assert_eq!(id.as_u64(), 42);
    }

    // --- Thread safety ---

    #[test]
    fn test_bus_is_clone_and_send() {
        let bus = EventBus::new();
        let bus2 = bus.clone();
        let sub = bus.subscribe("test", EventFilter::new());
        bus2.publish(make_event("test", "src"));
        assert_eq!(bus.get_events(sub).len(), 1);
    }
}