zeptoclaw 0.5.5

Ultra-lightweight personal AI assistant
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
//! Channel Manager for ZeptoClaw
//!
//! This module provides the `ChannelManager` which is responsible for:
//! - Registering and managing multiple communication channels
//! - Starting and stopping all channels
//! - Dispatching outbound messages to the appropriate channels
//! - Supervising channel health and restarting dead channels

use std::collections::HashMap;
use std::sync::Arc;
use std::time::Instant;
use tokio::sync::{watch, Mutex, RwLock};
use tokio::task::JoinHandle;
use tracing::{debug, error, info, warn};

use crate::bus::{MessageBus, OutboundMessage};
use crate::config::Config;
use crate::error::Result;
use crate::health::{HealthCheck, HealthRegistry, HealthStatus};

use super::Channel;

type SharedChannel = Arc<Mutex<Box<dyn Channel>>>;

/// Supervisor polling interval.
const SUPERVISOR_POLL_SECS: u64 = 15;
/// Minimum cooldown between restart attempts for the same channel.
const SUPERVISOR_COOLDOWN_SECS: u64 = 60;
/// Maximum number of restart attempts before giving up on a channel.
const SUPERVISOR_MAX_RESTARTS: u32 = 5;

/// Per-channel supervisor state.
struct SupervisorEntry {
    restart_count: u32,
    last_restart: Option<Instant>,
    started: bool,
}

/// The `ChannelManager` manages the lifecycle of all communication channels.
///
/// It provides methods to:
/// - Register new channels
/// - Start and stop all channels
/// - Route outbound messages to the correct channel
/// - List all registered channels
/// - Supervise running channels and restart dead ones
///
/// # Architecture
///
/// ```text
/// ┌─────────────────────────────────────────────────────────────┐
/// │                     ChannelManager                          │
/// │  ┌─────────┐  ┌─────────┐  ┌─────────┐  ┌─────────┐       │
/// │  │Telegram │  │ Discord │  │  Slack  │  │WhatsApp │  ...  │
/// │  └────┬────┘  └────┬────┘  └────┬────┘  └────┬────┘       │
/// │       │            │            │            │              │
/// │       └────────────┴─────┬──────┴────────────┘              │
/// │                          │                                  │
/// │                    ┌─────┴─────┐                           │
/// │                    │MessageBus │                           │
/// │                    └───────────┘                           │
/// └─────────────────────────────────────────────────────────────┘
/// ```
///
/// # Example
///
/// ```ignore
/// use std::sync::Arc;
/// use zeptoclaw::bus::MessageBus;
/// use zeptoclaw::config::Config;
/// use zeptoclaw::channels::ChannelManager;
///
/// #[tokio::main]
/// async fn main() {
///     let bus = Arc::new(MessageBus::new());
///     let config = Config::default();
///     let manager = ChannelManager::new(bus, config);
///
///     // Register channels
///     // manager.register(Box::new(TelegramChannel::new(...))).await;
///
///     // Start all channels
///     manager.start_all().await.unwrap();
/// }
/// ```
pub struct ChannelManager {
    /// Map of channel name to channel instance
    channels: Arc<RwLock<HashMap<String, SharedChannel>>>,
    /// Reference to the message bus for routing
    bus: Arc<MessageBus>,
    /// Global configuration
    #[allow(dead_code)]
    config: Config,
    /// Shutdown signal sender for dispatcher
    shutdown_tx: watch::Sender<bool>,
    /// Shutdown signal receiver (cloneable)
    shutdown_rx: watch::Receiver<bool>,
    /// Handle to the dispatcher task (if running)
    dispatcher_handle: Arc<RwLock<Option<JoinHandle<()>>>>,
    /// Optional health registry for reporting channel status
    health_registry: Option<HealthRegistry>,
    /// Handle to the supervisor task (if running)
    supervisor_handle: Arc<RwLock<Option<JoinHandle<()>>>>,
}

impl ChannelManager {
    /// Creates a new `ChannelManager` with the given message bus and configuration.
    ///
    /// # Arguments
    ///
    /// * `bus` - The message bus for routing messages
    /// * `config` - The global configuration
    ///
    /// # Example
    ///
    /// ```
    /// use std::sync::Arc;
    /// use zeptoclaw::bus::MessageBus;
    /// use zeptoclaw::config::Config;
    /// use zeptoclaw::channels::ChannelManager;
    ///
    /// let bus = Arc::new(MessageBus::new());
    /// let config = Config::default();
    /// let manager = ChannelManager::new(bus, config);
    /// ```
    pub fn new(bus: Arc<MessageBus>, config: Config) -> Self {
        let (shutdown_tx, shutdown_rx) = watch::channel(false);
        Self {
            channels: Arc::new(RwLock::new(HashMap::new())),
            bus,
            config,
            shutdown_tx,
            shutdown_rx,
            dispatcher_handle: Arc::new(RwLock::new(None)),
            health_registry: None,
            supervisor_handle: Arc::new(RwLock::new(None)),
        }
    }

    /// Sets the health registry for channel status reporting.
    pub fn set_health_registry(&mut self, registry: HealthRegistry) {
        self.health_registry = Some(registry);
    }

    /// Registers a new channel with the manager.
    ///
    /// The channel is stored by its name and can be started later with `start_all()`.
    ///
    /// # Arguments
    ///
    /// * `channel` - The channel to register
    ///
    /// # Example
    ///
    /// ```ignore
    /// let manager = ChannelManager::new(bus, config);
    /// manager.register(Box::new(telegram_channel)).await;
    /// ```
    pub async fn register(&self, channel: Box<dyn Channel>) {
        let name = channel.name().to_string();
        info!("Registering channel: {}", name);
        let mut channels = self.channels.write().await;
        channels.insert(name, Arc::new(Mutex::new(channel)));
    }

    /// Returns a list of all registered channel names.
    ///
    /// # Example
    ///
    /// ```
    /// use std::sync::Arc;
    /// use zeptoclaw::bus::MessageBus;
    /// use zeptoclaw::config::Config;
    /// use zeptoclaw::channels::ChannelManager;
    ///
    /// # tokio_test::block_on(async {
    /// let bus = Arc::new(MessageBus::new());
    /// let config = Config::default();
    /// let manager = ChannelManager::new(bus, config);
    ///
    /// let channels = manager.channels().await;
    /// assert!(channels.is_empty());
    /// # })
    /// ```
    pub async fn channels(&self) -> Vec<String> {
        let channels = self.channels.read().await;
        channels.keys().cloned().collect()
    }

    /// Returns the number of registered channels.
    pub async fn channel_count(&self) -> usize {
        let channels = self.channels.read().await;
        channels.len()
    }

    /// Checks if a channel with the given name is registered.
    ///
    /// # Arguments
    ///
    /// * `name` - The name of the channel to check
    pub async fn has_channel(&self, name: &str) -> bool {
        let channels = self.channels.read().await;
        channels.contains_key(name)
    }

    /// Starts all registered channels.
    ///
    /// This method:
    /// 1. Iterates over all registered channels and starts each one
    /// 2. Spawns a background task to dispatch outbound messages
    /// 3. Starts the supervisor loop to monitor channel health
    ///
    /// Errors from individual channels are logged but do not prevent
    /// other channels from starting.
    ///
    /// # Errors
    ///
    /// Returns `Ok(())` even if individual channels fail to start.
    /// Check logs for channel-specific errors.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let manager = ChannelManager::new(bus, config);
    /// manager.register(Box::new(telegram_channel)).await;
    /// manager.start_all().await?;
    /// ```
    pub async fn start_all(&self) -> Result<()> {
        // Check if dispatcher is already running to prevent multiple dispatcher tasks
        {
            let dispatcher_handle = self.dispatcher_handle.read().await;
            if let Some(ref handle) = *dispatcher_handle {
                if !handle.is_finished() {
                    warn!("Dispatcher already running, skipping start");
                    return Ok(());
                }
            }
        }

        let channels_to_start = {
            let channels = self.channels.read().await;
            channels
                .iter()
                .map(|(name, channel)| (name.clone(), Arc::clone(channel)))
                .collect::<Vec<_>>()
        };

        let mut started_channels = Vec::new();
        for (name, channel) in channels_to_start {
            info!("Starting channel: {}", name);
            let mut channel = channel.lock().await;
            if let Err(e) = channel.start().await {
                error!("Failed to start channel {}: {}", name, e);
            } else {
                started_channels.push(name);
            }
        }

        // Register started channels with health registry
        if let Some(ref registry) = self.health_registry {
            for name in &started_channels {
                registry.register(HealthCheck {
                    name: name.clone(),
                    status: HealthStatus::Ok,
                    ..Default::default()
                });
            }
        }

        // Reset shutdown signal for fresh start
        let _ = self.shutdown_tx.send(false);

        // Start outbound dispatcher
        let bus = self.bus.clone();
        let channels_ref = self.channels.clone();
        let shutdown_rx = self.shutdown_rx.clone();
        let handle = tokio::spawn(async move {
            dispatch_outbound(bus, channels_ref, shutdown_rx).await;
        });

        // Store the handle so we can wait for it to stop
        let mut dispatcher_handle = self.dispatcher_handle.write().await;
        *dispatcher_handle = Some(handle);

        // Start supervisor
        self.start_supervisor(started_channels).await;

        Ok(())
    }

    /// Starts the supervisor loop that monitors channel health.
    async fn start_supervisor(&self, started_channels: Vec<String>) {
        let channels = self.channels.clone();
        let mut shutdown_rx = self.shutdown_rx.clone();
        let health_registry = self.health_registry.clone();

        info!(
            "Channel supervisor starting (poll={}s, cooldown={}s, max_restarts={})",
            SUPERVISOR_POLL_SECS, SUPERVISOR_COOLDOWN_SECS, SUPERVISOR_MAX_RESTARTS
        );

        let handle = tokio::spawn(async move {
            // Build initial supervisor state
            let mut entries: HashMap<String, SupervisorEntry> = started_channels
                .into_iter()
                .map(|name| {
                    (
                        name,
                        SupervisorEntry {
                            restart_count: 0,
                            last_restart: None,
                            started: true,
                        },
                    )
                })
                .collect();

            loop {
                // Wait for poll interval or shutdown
                tokio::select! {
                    _ = shutdown_rx.changed() => {
                        if *shutdown_rx.borrow() {
                            info!("Channel supervisor shutting down");
                            return;
                        }
                    }
                    _ = tokio::time::sleep(std::time::Duration::from_secs(SUPERVISOR_POLL_SECS)) => {}
                }

                // Check shutdown again after waking
                if *shutdown_rx.borrow() {
                    return;
                }

                debug!("Supervisor polling {} channel(s)", entries.len());

                // Check each supervised channel
                let channel_snapshot = {
                    let chs = channels.read().await;
                    entries
                        .keys()
                        .filter_map(|name| chs.get(name).map(|ch| (name.clone(), Arc::clone(ch))))
                        .collect::<Vec<_>>()
                };

                for (name, channel) in channel_snapshot {
                    let entry = match entries.get_mut(&name) {
                        Some(e) => e,
                        None => continue,
                    };

                    if !entry.started {
                        continue;
                    }

                    // Check if channel is alive
                    let is_running = {
                        let ch = channel.lock().await;
                        ch.is_running()
                    };

                    if is_running {
                        continue;
                    }

                    // Channel is dead — check if we should restart
                    if entry.restart_count >= SUPERVISOR_MAX_RESTARTS {
                        // Already gave up on this channel
                        continue;
                    }

                    // Check cooldown
                    if let Some(last) = entry.last_restart {
                        if last.elapsed().as_secs() < SUPERVISOR_COOLDOWN_SECS {
                            continue;
                        }
                    }

                    // Attempt restart
                    warn!(
                        "Supervisor: channel '{}' is dead (restart {}/{}), restarting",
                        name,
                        entry.restart_count + 1,
                        SUPERVISOR_MAX_RESTARTS
                    );

                    let restart_ok = {
                        let mut ch = channel.lock().await;
                        // Stop first to clean up state
                        let _ = ch.stop().await;
                        ch.start().await.is_ok()
                    };

                    entry.restart_count += 1;
                    entry.last_restart = Some(Instant::now());

                    if restart_ok {
                        info!("Supervisor: channel '{}' restarted successfully", name);
                        if let Some(ref registry) = health_registry {
                            registry.update(&name, HealthStatus::Ok, None);
                            registry.bump_restart(&name);
                        }
                    } else {
                        error!("Supervisor: channel '{}' restart failed", name);
                        if let Some(ref registry) = health_registry {
                            registry.bump_restart(&name);
                        }
                    }

                    // If max restarts reached, mark as Down
                    if entry.restart_count >= SUPERVISOR_MAX_RESTARTS {
                        error!(
                            "Supervisor: channel '{}' exceeded max restarts ({}), giving up",
                            name, SUPERVISOR_MAX_RESTARTS
                        );
                        if let Some(ref registry) = health_registry {
                            registry.set_error(
                                &name,
                                &format!("exceeded max restarts ({})", SUPERVISOR_MAX_RESTARTS),
                            );
                        }
                    }
                }
            }
        });

        let mut supervisor_handle = self.supervisor_handle.write().await;
        *supervisor_handle = Some(handle);
    }

    /// Stops all registered channels.
    ///
    /// Errors from individual channels are logged but do not prevent
    /// other channels from stopping.
    ///
    /// # Errors
    ///
    /// Returns `Ok(())` even if individual channels fail to stop.
    /// Check logs for channel-specific errors.
    ///
    /// # Example
    ///
    /// ```ignore
    /// manager.stop_all().await?;
    /// ```
    pub async fn stop_all(&self) -> Result<()> {
        // Signal the dispatcher and supervisor to stop
        info!("Signaling dispatcher to stop");
        let _ = self.shutdown_tx.send(true);

        // Abort supervisor first
        {
            let mut supervisor_handle = self.supervisor_handle.write().await;
            if let Some(handle) = supervisor_handle.take() {
                match tokio::time::timeout(std::time::Duration::from_secs(2), handle).await {
                    Ok(_) => info!("Supervisor stopped cleanly"),
                    Err(_) => warn!("Supervisor did not stop within timeout"),
                }
            }
        }

        // Wait for dispatcher to finish (with timeout)
        let mut dispatcher_handle = self.dispatcher_handle.write().await;
        if let Some(handle) = dispatcher_handle.take() {
            match tokio::time::timeout(std::time::Duration::from_secs(5), handle).await {
                Ok(_) => info!("Dispatcher stopped cleanly"),
                Err(_) => warn!("Dispatcher did not stop within timeout"),
            }
        }

        // Stop all channels
        let channels_to_stop = {
            let channels = self.channels.read().await;
            channels
                .iter()
                .map(|(name, channel)| (name.clone(), Arc::clone(channel)))
                .collect::<Vec<_>>()
        };

        for (name, channel) in channels_to_stop {
            info!("Stopping channel: {}", name);
            let mut channel = channel.lock().await;
            if let Err(e) = channel.stop().await {
                error!("Failed to stop channel {}: {}", name, e);
            }
        }
        Ok(())
    }

    /// Sends a message to a specific channel.
    ///
    /// # Arguments
    ///
    /// * `channel_name` - The name of the channel to send to
    /// * `msg` - The outbound message to send
    ///
    /// # Errors
    ///
    /// Returns an error if the channel fails to send the message.
    /// If the channel is not found, a warning is logged and `Ok(())` is returned.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let msg = OutboundMessage::new("telegram", "chat123", "Hello!");
    /// manager.send("telegram", msg).await?;
    /// ```
    pub async fn send(&self, channel_name: &str, msg: OutboundMessage) -> Result<()> {
        let channel = {
            let channels = self.channels.read().await;
            channels.get(channel_name).cloned()
        };

        if let Some(channel) = channel {
            let channel = channel.lock().await;
            channel.send(msg).await
        } else {
            // Pseudo-channels (e.g. "heartbeat") have no outbound handler — debug-level only
            debug!(
                "Channel not found: {} (may be a pseudo-channel like 'heartbeat')",
                channel_name
            );
            Ok(())
        }
    }

    /// Returns a reference to the message bus.
    pub fn bus(&self) -> Arc<MessageBus> {
        self.bus.clone()
    }
}

/// Background task that dispatches outbound messages from the bus to channels.
///
/// This function runs in a loop, consuming outbound messages from the bus
/// and routing them to the appropriate channel based on the message's
/// `channel` field. It stops when the shutdown signal is received.
///
/// # Arguments
///
/// * `bus` - The message bus to consume from
/// * `channels` - The shared map of channels
/// * `shutdown_rx` - Receiver for shutdown signals
async fn dispatch_outbound(
    bus: Arc<MessageBus>,
    channels: Arc<RwLock<HashMap<String, SharedChannel>>>,
    mut shutdown_rx: watch::Receiver<bool>,
) {
    info!("Outbound dispatcher started");
    loop {
        tokio::select! {
            // Check for shutdown signal
            _ = shutdown_rx.changed() => {
                if *shutdown_rx.borrow() {
                    info!("Outbound dispatcher received shutdown signal");
                    break;
                }
            }
            // Wait for outbound messages
            msg = bus.consume_outbound() => {
                if let Some(msg) = msg {
                    let channel_name = msg.channel.clone();
                    let channel = {
                        let channels = channels.read().await;
                        channels.get(&channel_name).cloned()
                    };

                    if let Some(channel) = channel {
                        let channel = channel.lock().await;
                        if let Err(e) = channel.send(msg).await {
                            error!("Failed to send message to {}: {}", channel_name, e);
                        }
                    } else {
                        // Pseudo-channels (e.g. "heartbeat") have no outbound handler — debug-level only
                        debug!("Unknown channel for outbound message: {} (may be a pseudo-channel like 'heartbeat')", channel_name);
                    }
                } else {
                    // Channel closed
                    info!("Outbound channel closed");
                    break;
                }
            }
        }
    }
    info!("Outbound dispatcher stopped");
}

#[cfg(test)]
mod tests {
    use super::*;
    use async_trait::async_trait;
    use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};

    /// A mock channel for testing
    struct MockChannel {
        name: String,
        running: Arc<AtomicBool>,
        allowlist: Vec<String>,
    }

    impl MockChannel {
        fn new(name: &str) -> Self {
            Self {
                name: name.to_string(),
                running: Arc::new(AtomicBool::new(false)),
                allowlist: Vec::new(),
            }
        }

        fn with_allowlist(name: &str, allowlist: Vec<String>) -> Self {
            Self {
                name: name.to_string(),
                running: Arc::new(AtomicBool::new(false)),
                allowlist,
            }
        }
    }

    #[async_trait]
    impl Channel for MockChannel {
        fn name(&self) -> &str {
            &self.name
        }

        async fn start(&mut self) -> Result<()> {
            self.running.store(true, Ordering::SeqCst);
            Ok(())
        }

        async fn stop(&mut self) -> Result<()> {
            self.running.store(false, Ordering::SeqCst);
            Ok(())
        }

        async fn send(&self, _msg: OutboundMessage) -> Result<()> {
            Ok(())
        }

        fn is_running(&self) -> bool {
            self.running.load(Ordering::SeqCst)
        }

        fn is_allowed(&self, user_id: &str) -> bool {
            self.allowlist.is_empty() || self.allowlist.contains(&user_id.to_string())
        }
    }

    /// A mock channel that dies after start (simulates task exit/panic).
    struct DyingChannel {
        name: String,
        running: Arc<AtomicBool>,
        start_count: Arc<AtomicU32>,
    }

    impl DyingChannel {
        fn new(name: &str) -> Self {
            Self {
                name: name.to_string(),
                running: Arc::new(AtomicBool::new(false)),
                start_count: Arc::new(AtomicU32::new(0)),
            }
        }
    }

    #[async_trait]
    impl Channel for DyingChannel {
        fn name(&self) -> &str {
            &self.name
        }

        async fn start(&mut self) -> Result<()> {
            self.start_count.fetch_add(1, Ordering::SeqCst);
            // Simulate: starts running, then the task dies immediately
            // (running stays false to simulate the fix from Task 1)
            self.running.store(false, Ordering::SeqCst);
            Ok(())
        }

        async fn stop(&mut self) -> Result<()> {
            self.running.store(false, Ordering::SeqCst);
            Ok(())
        }

        async fn send(&self, _msg: OutboundMessage) -> Result<()> {
            Ok(())
        }

        fn is_running(&self) -> bool {
            self.running.load(Ordering::SeqCst)
        }

        fn is_allowed(&self, _user_id: &str) -> bool {
            true
        }
    }

    #[tokio::test]
    async fn test_channel_manager_creation() {
        let bus = Arc::new(MessageBus::new());
        let config = Config::default();
        let manager = ChannelManager::new(bus, config);
        assert!(manager.channels().await.is_empty());
    }

    #[tokio::test]
    async fn test_register_channel() {
        let bus = Arc::new(MessageBus::new());
        let config = Config::default();
        let manager = ChannelManager::new(bus, config);

        let channel = MockChannel::new("test");
        manager.register(Box::new(channel)).await;

        let channels = manager.channels().await;
        assert_eq!(channels.len(), 1);
        assert!(channels.contains(&"test".to_string()));
    }

    #[tokio::test]
    async fn test_register_multiple_channels() {
        let bus = Arc::new(MessageBus::new());
        let config = Config::default();
        let manager = ChannelManager::new(bus, config);

        manager
            .register(Box::new(MockChannel::new("telegram")))
            .await;
        manager
            .register(Box::new(MockChannel::new("discord")))
            .await;
        manager.register(Box::new(MockChannel::new("slack"))).await;

        assert_eq!(manager.channel_count().await, 3);
        assert!(manager.has_channel("telegram").await);
        assert!(manager.has_channel("discord").await);
        assert!(manager.has_channel("slack").await);
        assert!(!manager.has_channel("whatsapp").await);
    }

    #[tokio::test]
    async fn test_start_all() {
        let bus = Arc::new(MessageBus::new());
        let config = Config::default();
        let manager = ChannelManager::new(bus, config);

        let channel = MockChannel::new("test");
        manager.register(Box::new(channel)).await;

        manager.start_all().await.unwrap();

        // Give the dispatcher task time to start
        tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
    }

    #[tokio::test]
    async fn test_stop_all() {
        let bus = Arc::new(MessageBus::new());
        let config = Config::default();
        let manager = ChannelManager::new(bus, config);

        manager.register(Box::new(MockChannel::new("test"))).await;
        manager.start_all().await.unwrap();
        manager.stop_all().await.unwrap();
    }

    #[tokio::test]
    async fn test_double_start_prevented() {
        // Regression test: calling start_all() twice should not spawn multiple dispatchers
        let bus = Arc::new(MessageBus::new());
        let config = Config::default();
        let manager = ChannelManager::new(bus, config);

        manager.register(Box::new(MockChannel::new("test"))).await;

        // First start
        manager.start_all().await.unwrap();
        tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;

        // Second start should be a no-op (dispatcher already running)
        manager.start_all().await.unwrap();

        // Clean shutdown
        manager.stop_all().await.unwrap();
    }

    #[tokio::test]
    async fn test_send_to_unknown_channel() {
        let bus = Arc::new(MessageBus::new());
        let config = Config::default();
        let manager = ChannelManager::new(bus, config);

        let msg = OutboundMessage::new("unknown", "chat123", "Hello");
        let result = manager.send("unknown", msg).await;
        assert!(result.is_ok()); // Should not error, just warn
    }

    #[tokio::test]
    async fn test_send_to_registered_channel() {
        let bus = Arc::new(MessageBus::new());
        let config = Config::default();
        let manager = ChannelManager::new(bus, config);

        manager.register(Box::new(MockChannel::new("test"))).await;

        let msg = OutboundMessage::new("test", "chat123", "Hello");
        let result = manager.send("test", msg).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_channel_allowlist() {
        let channel = MockChannel::with_allowlist("test", vec!["user1".to_string()]);
        assert!(channel.is_allowed("user1"));
        assert!(!channel.is_allowed("user2"));
    }

    #[tokio::test]
    async fn test_channel_empty_allowlist() {
        let channel = MockChannel::new("test");
        assert!(channel.is_allowed("anyone"));
    }

    #[tokio::test]
    async fn test_bus_reference() {
        let bus = Arc::new(MessageBus::new());
        let config = Config::default();
        let manager = ChannelManager::new(bus.clone(), config);

        // The bus reference should be the same
        assert!(Arc::ptr_eq(&bus, &manager.bus()));
    }

    #[tokio::test]
    async fn test_set_health_registry() {
        let bus = Arc::new(MessageBus::new());
        let config = Config::default();
        let mut manager = ChannelManager::new(bus, config);

        let registry = HealthRegistry::new();
        manager.set_health_registry(registry);
        assert!(manager.health_registry.is_some());
    }

    #[tokio::test]
    async fn test_start_all_registers_health_checks() {
        let bus = Arc::new(MessageBus::new());
        let config = Config::default();
        let mut manager = ChannelManager::new(bus, config);

        let registry = HealthRegistry::new();
        manager.set_health_registry(registry.clone());

        manager
            .register(Box::new(MockChannel::new("telegram")))
            .await;
        manager
            .register(Box::new(MockChannel::new("discord")))
            .await;

        manager.start_all().await.unwrap();
        tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;

        // Channels should be registered in health registry
        let checks = registry.all_checks();
        assert_eq!(checks.len(), 2);
        assert!(checks.iter().all(|c| c.status == HealthStatus::Ok));

        manager.stop_all().await.unwrap();
    }

    #[tokio::test]
    async fn test_supervisor_detects_dead_channel() {
        // Use a DyingChannel that reports is_running()=false immediately
        let bus = Arc::new(MessageBus::new());
        let config = Config::default();
        let mut manager = ChannelManager::new(bus, config);

        let registry = HealthRegistry::new();
        manager.set_health_registry(registry.clone());

        let dying = DyingChannel::new("dying");
        let start_count = Arc::clone(&dying.start_count);
        manager.register(Box::new(dying)).await;

        manager.start_all().await.unwrap();

        // First start call happened
        assert_eq!(start_count.load(Ordering::SeqCst), 1);

        // Wait for at least one supervisor poll cycle (15s + margin)
        // Use a shorter sleep and check — the supervisor should restart it
        tokio::time::sleep(tokio::time::Duration::from_secs(17)).await;

        // Supervisor should have attempted a restart
        assert!(
            start_count.load(Ordering::SeqCst) >= 2,
            "expected at least 2 start calls, got {}",
            start_count.load(Ordering::SeqCst)
        );

        // Health registry should show restart
        let checks = registry.all_checks();
        let dying_check = checks.iter().find(|c| c.name == "dying");
        assert!(dying_check.is_some());
        assert!(dying_check.unwrap().restart_count >= 1);

        manager.stop_all().await.unwrap();
    }

    #[tokio::test]
    async fn test_supervisor_respects_max_restarts() {
        let bus = Arc::new(MessageBus::new());
        let config = Config::default();
        let mut manager = ChannelManager::new(bus, config);

        let registry = HealthRegistry::new();
        manager.set_health_registry(registry.clone());

        let dying = DyingChannel::new("dying");
        let start_count = Arc::clone(&dying.start_count);
        manager.register(Box::new(dying)).await;

        manager.start_all().await.unwrap();

        // Wait long enough for supervisor to exhaust restarts
        // 5 restarts * 60s cooldown is too long for a test, but the first few
        // restarts happen without cooldown (no last_restart set initially)
        // Actually: first restart has no cooldown. Subsequent ones wait 60s.
        // So in ~17s we get 1 restart, then need to wait 60s+15s for the next.
        // For testing, just verify the start_count > 1 and that after enough
        // time the registry shows the error.
        tokio::time::sleep(tokio::time::Duration::from_secs(17)).await;

        // At least one restart happened
        assert!(start_count.load(Ordering::SeqCst) >= 2);

        manager.stop_all().await.unwrap();
    }

    #[tokio::test]
    async fn test_supervisor_stops_on_shutdown() {
        let bus = Arc::new(MessageBus::new());
        let config = Config::default();
        let manager = ChannelManager::new(bus, config);

        manager.register(Box::new(MockChannel::new("test"))).await;
        manager.start_all().await.unwrap();

        // Supervisor should be running
        {
            let handle = manager.supervisor_handle.read().await;
            assert!(handle.is_some());
            assert!(!handle.as_ref().unwrap().is_finished());
        }

        // Stop should cleanly shut down the supervisor
        manager.stop_all().await.unwrap();

        tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;
        {
            let handle = manager.supervisor_handle.read().await;
            assert!(handle.is_none()); // Taken by stop_all
        }
    }
}