aegis-streaming 0.2.6

Real-time streaming for Aegis database
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
//! Aegis Streaming Engine
//!
//! Core engine for real-time event streaming.
//!
//! @version 0.1.0
//! @author AutomataNexus Development Team

use crate::cdc::{CdcConfig, ChangeEvent};
use crate::channel::{Channel, ChannelConfig, ChannelError, ChannelId, ChannelReceiver};
use crate::event::{Event, EventFilter};
use crate::subscriber::{ConsumerGroup, Subscriber, SubscriberId, Subscription};
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, RwLock};

// =============================================================================
// Engine Configuration
// =============================================================================

/// Configuration for the streaming engine.
#[derive(Debug, Clone)]
pub struct EngineConfig {
    pub max_channels: usize,
    pub max_subscribers: usize,
    pub default_channel_config: ChannelConfig,
    pub cdc_config: CdcConfig,
}

impl Default for EngineConfig {
    fn default() -> Self {
        Self {
            max_channels: 1000,
            max_subscribers: 10000,
            default_channel_config: ChannelConfig::default(),
            cdc_config: CdcConfig::default(),
        }
    }
}

// =============================================================================
// Streaming Engine
// =============================================================================

/// The main streaming engine for pub/sub and CDC.
pub struct StreamingEngine {
    config: EngineConfig,
    channels: RwLock<HashMap<ChannelId, Channel>>,
    subscribers: RwLock<HashMap<SubscriberId, Subscriber>>,
    consumer_groups: Arc<RwLock<HashMap<String, ConsumerGroup>>>,
    stats: RwLock<EngineStats>,
}

impl StreamingEngine {
    /// Create a new streaming engine.
    pub fn new() -> Self {
        Self::with_config(EngineConfig::default())
    }

    /// Create an engine with custom configuration.
    pub fn with_config(config: EngineConfig) -> Self {
        Self {
            config,
            channels: RwLock::new(HashMap::new()),
            subscribers: RwLock::new(HashMap::new()),
            consumer_groups: Arc::new(RwLock::new(HashMap::new())),
            stats: RwLock::new(EngineStats::default()),
        }
    }

    // -------------------------------------------------------------------------
    // Channel Management
    // -------------------------------------------------------------------------

    /// Create a new channel.
    pub fn create_channel(&self, id: impl Into<ChannelId>) -> Result<(), EngineError> {
        let id = id.into();
        let mut channels = self
            .channels
            .write()
            .expect("channels RwLock poisoned in create_channel");

        if channels.len() >= self.config.max_channels {
            return Err(EngineError::TooManyChannels);
        }

        if channels.contains_key(&id) {
            return Err(EngineError::ChannelExists(id));
        }

        let channel = Channel::with_config(id.clone(), self.config.default_channel_config.clone());
        channels.insert(id, channel);

        Ok(())
    }

    /// Create a channel with custom configuration.
    pub fn create_channel_with_config(
        &self,
        id: impl Into<ChannelId>,
        config: ChannelConfig,
    ) -> Result<(), EngineError> {
        let id = id.into();
        let mut channels = self
            .channels
            .write()
            .expect("channels RwLock poisoned in create_channel_with_config");

        if channels.len() >= self.config.max_channels {
            return Err(EngineError::TooManyChannels);
        }

        if channels.contains_key(&id) {
            return Err(EngineError::ChannelExists(id));
        }

        let channel = Channel::with_config(id.clone(), config);
        channels.insert(id, channel);

        Ok(())
    }

    /// Delete a channel.
    pub fn delete_channel(&self, id: &ChannelId) -> Result<(), EngineError> {
        let mut channels = self
            .channels
            .write()
            .expect("channels RwLock poisoned in delete_channel");

        if channels.remove(id).is_none() {
            return Err(EngineError::ChannelNotFound(id.clone()));
        }

        Ok(())
    }

    /// List all channels.
    pub fn list_channels(&self) -> Vec<ChannelId> {
        let channels = self
            .channels
            .read()
            .expect("channels RwLock poisoned in list_channels");
        channels.keys().cloned().collect()
    }

    /// Check if a channel exists.
    pub fn channel_exists(&self, id: &ChannelId) -> bool {
        let channels = self
            .channels
            .read()
            .expect("channels RwLock poisoned in channel_exists");
        channels.contains_key(id)
    }

    // -------------------------------------------------------------------------
    // Publishing
    // -------------------------------------------------------------------------

    /// Publish an event to a channel.
    pub fn publish(&self, channel_id: &ChannelId, event: Event) -> Result<usize, EngineError> {
        let channels = self
            .channels
            .read()
            .expect("channels RwLock poisoned in publish");
        let channel = channels
            .get(channel_id)
            .ok_or_else(|| EngineError::ChannelNotFound(channel_id.clone()))?;

        let receivers = channel.publish(event).map_err(EngineError::Channel)?;

        drop(channels);

        {
            let mut stats = self
                .stats
                .write()
                .expect("stats RwLock poisoned in publish");
            stats.events_published += 1;
        }

        Ok(receivers)
    }

    /// Publish a CDC change event.
    pub fn publish_change(
        &self,
        channel_id: &ChannelId,
        change: ChangeEvent,
    ) -> Result<usize, EngineError> {
        let event = change.to_event();
        self.publish(channel_id, event)
    }

    /// Publish to multiple channels.
    pub fn publish_to_many(
        &self,
        channel_ids: &[ChannelId],
        event: Event,
    ) -> HashMap<ChannelId, Result<usize, EngineError>> {
        let mut results = HashMap::new();

        for id in channel_ids {
            results.insert(id.clone(), self.publish(id, event.clone()));
        }

        results
    }

    // -------------------------------------------------------------------------
    // Subscribing
    // -------------------------------------------------------------------------

    /// Subscribe to a channel.
    pub fn subscribe(
        &self,
        channel_id: &ChannelId,
        subscriber_id: impl Into<SubscriberId>,
    ) -> Result<ChannelReceiver, EngineError> {
        let subscriber_id = subscriber_id.into();
        let channels = self
            .channels
            .read()
            .expect("channels RwLock poisoned in subscribe");
        let channel = channels
            .get(channel_id)
            .ok_or_else(|| EngineError::ChannelNotFound(channel_id.clone()))?;

        let receiver = channel
            .subscribe(subscriber_id.clone())
            .map_err(EngineError::Channel)?;

        drop(channels);

        self.ensure_subscriber(&subscriber_id, channel_id);

        {
            let mut stats = self
                .stats
                .write()
                .expect("stats RwLock poisoned in subscribe");
            stats.active_subscriptions += 1;
        }

        Ok(receiver)
    }

    /// Subscribe with a filter.
    pub fn subscribe_with_filter(
        &self,
        channel_id: &ChannelId,
        subscriber_id: impl Into<SubscriberId>,
        filter: EventFilter,
    ) -> Result<ChannelReceiver, EngineError> {
        let subscriber_id = subscriber_id.into();
        let channels = self
            .channels
            .read()
            .expect("channels RwLock poisoned in subscribe_with_filter");
        let channel = channels
            .get(channel_id)
            .ok_or_else(|| EngineError::ChannelNotFound(channel_id.clone()))?;

        let receiver = channel
            .subscribe_with_filter(subscriber_id.clone(), filter)
            .map_err(EngineError::Channel)?;

        drop(channels);

        self.ensure_subscriber(&subscriber_id, channel_id);

        {
            let mut stats = self
                .stats
                .write()
                .expect("stats RwLock poisoned in subscribe_with_filter");
            stats.active_subscriptions += 1;
        }

        Ok(receiver)
    }

    /// Unsubscribe from a channel.
    pub fn unsubscribe(&self, channel_id: &ChannelId, subscriber_id: &SubscriberId) {
        let channels = self
            .channels
            .read()
            .expect("channels RwLock poisoned in unsubscribe");
        if let Some(channel) = channels.get(channel_id) {
            channel.unsubscribe(subscriber_id);
        }

        let mut stats = self
            .stats
            .write()
            .expect("stats RwLock poisoned in unsubscribe");
        stats.active_subscriptions = stats.active_subscriptions.saturating_sub(1);
    }

    fn ensure_subscriber(&self, subscriber_id: &SubscriberId, channel_id: &ChannelId) {
        let mut subscribers = self
            .subscribers
            .write()
            .expect("subscribers RwLock poisoned in ensure_subscriber");

        let subscriber = subscribers
            .entry(subscriber_id.clone())
            .or_insert_with(|| Subscriber::new(subscriber_id.clone()));

        let mut subscription = Subscription::new(subscriber_id.clone());
        subscription.add_channel(channel_id.clone());
        subscriber.add_subscription(subscription);
    }

    // -------------------------------------------------------------------------
    // Subscriber Management
    // -------------------------------------------------------------------------

    /// Get a subscriber.
    pub fn get_subscriber(&self, id: &SubscriberId) -> Option<Subscriber> {
        let subscribers = self
            .subscribers
            .read()
            .expect("subscribers RwLock poisoned in get_subscriber");
        subscribers.get(id).cloned()
    }

    /// List all subscribers.
    pub fn list_subscribers(&self) -> Vec<SubscriberId> {
        let subscribers = self
            .subscribers
            .read()
            .expect("subscribers RwLock poisoned in list_subscribers");
        subscribers.keys().cloned().collect()
    }

    /// Remove a subscriber.
    pub fn remove_subscriber(&self, id: &SubscriberId) {
        let mut subscribers = self
            .subscribers
            .write()
            .expect("subscribers RwLock poisoned in remove_subscriber");
        subscribers.remove(id);
    }

    // -------------------------------------------------------------------------
    // Consumer Group Management
    // -------------------------------------------------------------------------

    /// Create a new consumer group with the given ID.
    pub fn create_consumer_group(&self, group_id: impl Into<String>) -> Result<(), EngineError> {
        let group_id = group_id.into();
        let mut groups = self
            .consumer_groups
            .write()
            .expect("consumer_groups RwLock poisoned in create_consumer_group");

        if groups.contains_key(&group_id) {
            return Err(EngineError::ConsumerGroupExists(group_id));
        }

        groups.insert(group_id.clone(), ConsumerGroup::new(group_id));
        Ok(())
    }

    /// Delete a consumer group.
    pub fn delete_consumer_group(&self, group_id: &str) -> Result<(), EngineError> {
        let mut groups = self
            .consumer_groups
            .write()
            .expect("consumer_groups RwLock poisoned in delete_consumer_group");

        if groups.remove(group_id).is_none() {
            return Err(EngineError::ConsumerGroupNotFound(group_id.to_string()));
        }

        Ok(())
    }

    /// Join a consumer group. The subscriber is added as a member with the given
    /// set of assigned channels.
    pub fn join_consumer_group(
        &self,
        group_id: &str,
        subscriber_id: SubscriberId,
        channels: HashSet<String>,
    ) -> Result<(), EngineError> {
        let mut groups = self
            .consumer_groups
            .write()
            .expect("consumer_groups RwLock poisoned in join_consumer_group");

        let group = groups
            .get_mut(group_id)
            .ok_or_else(|| EngineError::ConsumerGroupNotFound(group_id.to_string()))?;

        group.add_member(subscriber_id, channels);
        Ok(())
    }

    /// Leave a consumer group. Returns the channels that were assigned to the
    /// subscriber, or an error if the group or member was not found.
    pub fn leave_consumer_group(
        &self,
        group_id: &str,
        subscriber_id: &SubscriberId,
    ) -> Result<HashSet<String>, EngineError> {
        let mut groups = self
            .consumer_groups
            .write()
            .expect("consumer_groups RwLock poisoned in leave_consumer_group");

        let group = groups
            .get_mut(group_id)
            .ok_or_else(|| EngineError::ConsumerGroupNotFound(group_id.to_string()))?;

        group.remove_member(subscriber_id).ok_or_else(|| {
            EngineError::SubscriberNotInGroup(subscriber_id.clone(), group_id.to_string())
        })
    }

    /// Commit an offset for a channel within a consumer group.
    pub fn commit_offset(
        &self,
        group_id: &str,
        channel_name: impl Into<String>,
        offset: u64,
    ) -> Result<(), EngineError> {
        let mut groups = self
            .consumer_groups
            .write()
            .expect("consumer_groups RwLock poisoned in commit_offset");

        let group = groups
            .get_mut(group_id)
            .ok_or_else(|| EngineError::ConsumerGroupNotFound(group_id.to_string()))?;

        group.commit_offset(channel_name, offset);
        Ok(())
    }

    /// Get the committed offset for a channel within a consumer group.
    pub fn get_committed_offset(
        &self,
        group_id: &str,
        channel_name: &str,
    ) -> Result<Option<u64>, EngineError> {
        let groups = self
            .consumer_groups
            .read()
            .expect("consumer_groups RwLock poisoned in get_committed_offset");

        let group = groups
            .get(group_id)
            .ok_or_else(|| EngineError::ConsumerGroupNotFound(group_id.to_string()))?;

        Ok(group.get_offset(channel_name))
    }

    /// List all consumer groups.
    pub fn list_consumer_groups(&self) -> Vec<String> {
        let groups = self
            .consumer_groups
            .read()
            .expect("consumer_groups RwLock poisoned in list_consumer_groups");
        groups.keys().cloned().collect()
    }

    /// Get a snapshot of a consumer group.
    pub fn get_consumer_group(&self, group_id: &str) -> Option<ConsumerGroup> {
        let groups = self
            .consumer_groups
            .read()
            .expect("consumer_groups RwLock poisoned in get_consumer_group");
        groups.get(group_id).cloned()
    }

    // -------------------------------------------------------------------------
    // History and Replay
    // -------------------------------------------------------------------------

    /// Get recent events from a channel.
    pub fn get_history(
        &self,
        channel_id: &ChannelId,
        count: usize,
    ) -> Result<Vec<Event>, EngineError> {
        let channels = self
            .channels
            .read()
            .expect("channels RwLock poisoned in get_history");
        let channel = channels
            .get(channel_id)
            .ok_or_else(|| EngineError::ChannelNotFound(channel_id.clone()))?;

        Ok(channel.get_history(count))
    }

    /// Get events after a timestamp.
    pub fn get_history_after(
        &self,
        channel_id: &ChannelId,
        timestamp: u64,
    ) -> Result<Vec<Event>, EngineError> {
        let channels = self
            .channels
            .read()
            .expect("channels RwLock poisoned in get_history_after");
        let channel = channels
            .get(channel_id)
            .ok_or_else(|| EngineError::ChannelNotFound(channel_id.clone()))?;

        Ok(channel.get_history_after(timestamp))
    }

    // -------------------------------------------------------------------------
    // Statistics
    // -------------------------------------------------------------------------

    /// Get engine statistics.
    pub fn stats(&self) -> EngineStats {
        let stats = self.stats.read().expect("stats RwLock poisoned in stats");
        stats.clone()
    }

    /// Reset statistics.
    pub fn reset_stats(&self) {
        let mut stats = self
            .stats
            .write()
            .expect("stats RwLock poisoned in reset_stats");
        *stats = EngineStats::default();
    }

    /// Get channel statistics.
    pub fn channel_stats(&self, id: &ChannelId) -> Option<crate::channel::ChannelStats> {
        let channels = self
            .channels
            .read()
            .expect("channels RwLock poisoned in channel_stats");
        channels.get(id).map(|c| c.stats())
    }
}

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

// =============================================================================
// Engine Statistics
// =============================================================================

/// Statistics for the streaming engine.
#[derive(Debug, Clone, Default)]
pub struct EngineStats {
    pub events_published: u64,
    pub active_subscriptions: usize,
    pub channels_created: usize,
}

// =============================================================================
// Engine Error
// =============================================================================

/// Errors that can occur in the streaming engine.
#[derive(Debug, Clone)]
pub enum EngineError {
    ChannelExists(ChannelId),
    ChannelNotFound(ChannelId),
    TooManyChannels,
    TooManySubscribers,
    Channel(ChannelError),
    ConsumerGroupExists(String),
    ConsumerGroupNotFound(String),
    SubscriberNotInGroup(SubscriberId, String),
}

impl std::fmt::Display for EngineError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::ChannelExists(id) => write!(f, "Channel already exists: {}", id),
            Self::ChannelNotFound(id) => write!(f, "Channel not found: {}", id),
            Self::TooManyChannels => write!(f, "Maximum channels reached"),
            Self::TooManySubscribers => write!(f, "Maximum subscribers reached"),
            Self::Channel(err) => write!(f, "Channel error: {}", err),
            Self::ConsumerGroupExists(id) => write!(f, "Consumer group already exists: {}", id),
            Self::ConsumerGroupNotFound(id) => write!(f, "Consumer group not found: {}", id),
            Self::SubscriberNotInGroup(sub_id, group_id) => {
                write!(
                    f,
                    "Subscriber {} is not in consumer group {}",
                    sub_id, group_id
                )
            }
        }
    }
}

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

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::event::{EventData, EventType};

    #[test]
    fn test_engine_creation() {
        let engine = StreamingEngine::new();
        assert!(engine.list_channels().is_empty());
    }

    #[test]
    fn test_channel_management() {
        let engine = StreamingEngine::new();

        engine.create_channel("events").unwrap();
        assert!(engine.channel_exists(&ChannelId::new("events")));

        let channels = engine.list_channels();
        assert_eq!(channels.len(), 1);

        engine.delete_channel(&ChannelId::new("events")).unwrap();
        assert!(!engine.channel_exists(&ChannelId::new("events")));
    }

    #[tokio::test]
    async fn test_publish_subscribe() {
        let engine = StreamingEngine::new();
        engine.create_channel("test").unwrap();

        let channel_id = ChannelId::new("test");
        let mut receiver = engine.subscribe(&channel_id, "sub1").unwrap();

        let event = Event::new(
            EventType::Created,
            "source",
            EventData::String("hello".to_string()),
        );
        engine.publish(&channel_id, event).unwrap();

        let received = receiver.recv().await.unwrap();
        assert_eq!(received.source, "source");
    }

    #[test]
    fn test_duplicate_channel() {
        let engine = StreamingEngine::new();

        engine.create_channel("test").unwrap();
        let result = engine.create_channel("test");

        assert!(matches!(result, Err(EngineError::ChannelExists(_))));
    }

    #[test]
    fn test_stats() {
        let engine = StreamingEngine::new();
        engine.create_channel("test").unwrap();

        let channel_id = ChannelId::new("test");
        engine.subscribe(&channel_id, "sub1").unwrap();

        let event = Event::new(EventType::Created, "source", EventData::Null);
        engine.publish(&channel_id, event).unwrap();

        let stats = engine.stats();
        assert_eq!(stats.events_published, 1);
        assert_eq!(stats.active_subscriptions, 1);
    }

    #[test]
    fn test_history() {
        let config = EngineConfig {
            default_channel_config: ChannelConfig {
                persistent: true,
                retention_count: 100,
                ..Default::default()
            },
            ..Default::default()
        };

        let engine = StreamingEngine::with_config(config);
        engine.create_channel("history").unwrap();

        let channel_id = ChannelId::new("history");

        for i in 0..5 {
            let event = Event::new(EventType::Created, "test", EventData::Int(i));
            engine.publish(&channel_id, event).unwrap();
        }

        let history = engine.get_history(&channel_id, 10).unwrap();
        assert_eq!(history.len(), 5);
    }

    #[test]
    fn test_consumer_group_create_delete() {
        let engine = StreamingEngine::new();

        engine.create_consumer_group("group1").unwrap();
        assert_eq!(engine.list_consumer_groups().len(), 1);

        // Duplicate creation should fail
        let result = engine.create_consumer_group("group1");
        assert!(matches!(result, Err(EngineError::ConsumerGroupExists(_))));

        engine.delete_consumer_group("group1").unwrap();
        assert!(engine.list_consumer_groups().is_empty());

        // Deleting non-existent group should fail
        let result = engine.delete_consumer_group("group1");
        assert!(matches!(result, Err(EngineError::ConsumerGroupNotFound(_))));
    }

    #[test]
    fn test_consumer_group_join_leave() {
        let engine = StreamingEngine::new();
        engine.create_consumer_group("group1").unwrap();

        let sub1 = SubscriberId::new("sub1");
        let sub2 = SubscriberId::new("sub2");

        let mut channels1 = std::collections::HashSet::new();
        channels1.insert("events".to_string());

        let mut channels2 = std::collections::HashSet::new();
        channels2.insert("logs".to_string());

        engine
            .join_consumer_group("group1", sub1.clone(), channels1)
            .unwrap();
        engine
            .join_consumer_group("group1", sub2.clone(), channels2)
            .unwrap();

        let group = engine.get_consumer_group("group1").unwrap();
        assert_eq!(group.member_count(), 2);
        assert!(group.is_member(&sub1));

        // Leave
        let removed_channels = engine.leave_consumer_group("group1", &sub1).unwrap();
        assert!(removed_channels.contains("events"));

        let group = engine.get_consumer_group("group1").unwrap();
        assert_eq!(group.member_count(), 1);
        assert!(!group.is_member(&sub1));

        // Leaving again should fail
        let result = engine.leave_consumer_group("group1", &sub1);
        assert!(matches!(
            result,
            Err(EngineError::SubscriberNotInGroup(_, _))
        ));
    }

    #[test]
    fn test_consumer_group_join_nonexistent() {
        let engine = StreamingEngine::new();

        let result = engine.join_consumer_group(
            "nonexistent",
            SubscriberId::new("sub1"),
            std::collections::HashSet::new(),
        );
        assert!(matches!(result, Err(EngineError::ConsumerGroupNotFound(_))));
    }

    #[test]
    fn test_consumer_group_offset_tracking() {
        let engine = StreamingEngine::new();
        engine.create_consumer_group("group1").unwrap();

        // No offset committed yet
        let offset = engine.get_committed_offset("group1", "events").unwrap();
        assert_eq!(offset, None);

        // Commit offset
        engine.commit_offset("group1", "events", 42).unwrap();
        let offset = engine.get_committed_offset("group1", "events").unwrap();
        assert_eq!(offset, Some(42));

        // Update offset
        engine.commit_offset("group1", "events", 100).unwrap();
        let offset = engine.get_committed_offset("group1", "events").unwrap();
        assert_eq!(offset, Some(100));

        // Different channel
        engine.commit_offset("group1", "logs", 5).unwrap();
        let offset = engine.get_committed_offset("group1", "logs").unwrap();
        assert_eq!(offset, Some(5));
        // Original channel offset unchanged
        let offset = engine.get_committed_offset("group1", "events").unwrap();
        assert_eq!(offset, Some(100));
    }

    #[test]
    fn test_consumer_group_offset_nonexistent_group() {
        let engine = StreamingEngine::new();

        let result = engine.commit_offset("nonexistent", "events", 10);
        assert!(matches!(result, Err(EngineError::ConsumerGroupNotFound(_))));

        let result = engine.get_committed_offset("nonexistent", "events");
        assert!(matches!(result, Err(EngineError::ConsumerGroupNotFound(_))));
    }

    #[test]
    fn test_get_consumer_group() {
        let engine = StreamingEngine::new();

        assert!(engine.get_consumer_group("nonexistent").is_none());

        engine.create_consumer_group("group1").unwrap();
        let group = engine.get_consumer_group("group1");
        assert!(group.is_some());
        assert_eq!(group.unwrap().group_id, "group1");
    }
}