zeph-channels 0.22.4

Multi-channel I/O adapters (CLI, Telegram, Discord, Slack) for Zeph
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
// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Discord channel adapter using Gateway WebSocket + REST API.

pub mod gateway;
pub mod rest;

use std::time::Duration;

use tokio::sync::mpsc;
use zeph_common::TaskSupervisor;
use zeph_core::channel::{
    Channel, ChannelError, ChannelMessage, ElicitationRequest, ElicitationResponse,
};

use self::gateway::IncomingMessage;
use crate::streaming::{StreamingBuffer, StreamingSend};

const MAX_MESSAGE_LEN: usize = 2000;
const EDIT_THROTTLE: Duration = Duration::from_millis(1500);

/// Discord channel adapter implementing edit-in-place streaming.
pub struct DiscordChannel {
    rx: mpsc::Receiver<IncomingMessage>,
    rest: rest::RestClient,
    /// Gateway WebSocket listener handle. `None` when the gateway is supervised by
    /// a [`TaskSupervisor`] (lifecycle is owned by the supervisor in that case).
    _gateway_handle: Option<tokio::task::JoinHandle<()>>,
    channel_id: Option<String>,
    allowed_user_ids: Vec<String>,
    allowed_role_ids: Vec<String>,
    allowed_channel_ids: Vec<String>,
    buffer: StreamingBuffer,
    message_id: Option<String>,
    /// Optional supervisor used to register discord tasks in the workspace-wide task
    /// registry with automatic restart on panic and lifecycle observability.
    supervisor: Option<TaskSupervisor>,
}

impl std::fmt::Debug for DiscordChannel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DiscordChannel")
            .field("channel_id", &self.channel_id)
            .field("supervisor", &self.supervisor.is_some())
            .finish_non_exhaustive()
    }
}

impl DiscordChannel {
    /// Create a new Discord channel and spawn the gateway listener.
    ///
    /// Slash commands are registered at startup in a supervised fire-and-forget task.
    /// If registration fails, a warning is logged and the bot continues normally.
    ///
    /// When `supervisor` is provided the gateway and slash-command tasks are registered
    /// in the workspace-wide task registry with automatic restart on panic and lifecycle
    /// observability. Without a supervisor both tasks fall back to plain `tokio::spawn`
    /// with a warning — acceptable in tests but not recommended for production.
    ///
    /// # Errors
    ///
    /// Returns [`ChannelError::Other`] when `allowed_user_ids` and
    /// `allowed_role_ids` are both empty. An unconfigured allowlist is refused
    /// rather than treated as "allow everyone" — mirrors the Telegram channel's
    /// fail-closed startup check (see [`crate::auth::require_configured_allowlist`]).
    /// The check runs before any gateway connection or slash-command registration,
    /// so a misconfigured adapter has no observable side effect.
    pub fn new(
        token: String,
        allowed_user_ids: Vec<String>,
        allowed_role_ids: Vec<String>,
        allowed_channel_ids: Vec<String>,
        supervisor: Option<&TaskSupervisor>,
    ) -> Result<Self, ChannelError> {
        crate::auth::require_configured_allowlist(
            "discord",
            &[&allowed_user_ids, &allowed_role_ids],
        )?;
        let rest = rest::RestClient::new(token.clone());
        let (gateway_handle, rx) = gateway::spawn_gateway(token, supervisor);
        Self::register_slash_commands(rest.clone(), supervisor);
        Ok(Self {
            rx,
            rest,
            _gateway_handle: gateway_handle,
            channel_id: None,
            allowed_user_ids,
            allowed_role_ids,
            allowed_channel_ids,
            buffer: StreamingBuffer::new(EDIT_THROTTLE),
            message_id: None,
            supervisor: supervisor.cloned(),
        })
    }

    /// Register Discord slash commands in a supervised fire-and-forget task.
    ///
    /// When a supervisor is provided the task is registered as `"discord_register_commands"`
    /// so it is visible in TUI status. Without a supervisor falls back to plain `tokio::spawn`.
    fn register_slash_commands(rest: rest::RestClient, supervisor: Option<&TaskSupervisor>) {
        let factory = move || {
            let rest = rest.clone();
            async move {
                rest.register_slash_commands().await;
            }
        };
        if let Some(sup) = supervisor {
            sup.spawn(zeph_common::TaskDescriptor {
                name: "discord_register_commands",
                restart: zeph_common::RestartPolicy::RunOnce,
                factory,
            });
        } else {
            tokio::spawn(factory());
        }
    }

    fn is_authorized(&self, msg: &IncomingMessage) -> bool {
        if !self.allowed_channel_ids.is_empty()
            && !self.allowed_channel_ids.contains(&msg.channel_id)
        {
            return false;
        }
        if crate::auth::all_lists_empty(&[&self.allowed_user_ids, &self.allowed_role_ids]) {
            return true;
        }
        self.allowed_user_ids.contains(&msg.author_id)
            || msg
                .author_roles
                .iter()
                .any(|r| self.allowed_role_ids.contains(r))
    }

    #[cfg_attr(
        feature = "profiling",
        tracing::instrument(name = "channels.discord.send_or_edit", skip_all, level = "debug", fields(buf_len = %self.buffer.len()))
    )]
    async fn send_or_edit(&mut self) -> Result<(), ChannelError> {
        let channel_id = self
            .channel_id
            .clone()
            .ok_or(ChannelError::NoActiveSession)?;

        let text = if self.buffer.is_empty() {
            "...".to_owned()
        } else {
            self.buffer.text().to_owned()
        };

        if text.len() > MAX_MESSAGE_LEN {
            let chunks = crate::markdown::utf8_chunks(&text, MAX_MESSAGE_LEN);
            for chunk in chunks {
                self.rest
                    .send_message(&channel_id, chunk)
                    .await
                    .map_err(ChannelError::other)?;
            }
            self.message_id = None;
            return Ok(());
        }

        match self.message_id.clone() {
            None => {
                let msg = self
                    .rest
                    .send_message(&channel_id, &text)
                    .await
                    .map_err(ChannelError::other)?;
                self.message_id = Some(msg.id);
            }
            Some(msg_id) => {
                if let Err(e) = self.rest.edit_message(&channel_id, &msg_id, &text).await {
                    tracing::warn!("discord edit failed: {e}, sending new message");
                    self.message_id = None;
                    let msg = self
                        .rest
                        .send_message(&channel_id, &text)
                        .await
                        .map_err(ChannelError::other)?;
                    self.message_id = Some(msg.id);
                }
            }
        }

        self.buffer.mark_flushed();
        Ok(())
    }
}

impl StreamingSend for DiscordChannel {
    async fn send_or_edit(&mut self) -> Result<(), ChannelError> {
        Self::send_or_edit(self).await
    }

    fn streaming_buffer(&self) -> &StreamingBuffer {
        &self.buffer
    }

    fn streaming_buffer_mut(&mut self) -> &mut StreamingBuffer {
        &mut self.buffer
    }

    fn has_pending_message(&self) -> bool {
        self.message_id.is_some()
    }

    fn clear_pending_message(&mut self) {
        self.message_id = None;
    }
}

impl crate::confirm::ConfirmLoop for DiscordChannel {
    type Incoming = IncomingMessage;

    fn confirm_label(&self) -> &'static str {
        "discord"
    }

    fn confirm_receiver(&mut self) -> &mut mpsc::Receiver<IncomingMessage> {
        &mut self.rx
    }

    fn confirm_accepts(&self, incoming: &IncomingMessage) -> bool {
        self.is_authorized(incoming)
    }

    fn confirm_reply_text<'a>(&self, incoming: &'a IncomingMessage) -> &'a str {
        &incoming.content
    }

    async fn confirm_send_prompt(&mut self, text: &str) -> Result<(), ChannelError> {
        self.send(text).await
    }
}

impl Channel for DiscordChannel {
    fn supports_exit(&self) -> bool {
        false
    }

    /// Returns `true` — Discord users are external, untrusted input. The residual text
    /// that survives command dispatch is sanitized centrally in `Agent::run` before it
    /// reaches the LLM context (see `Channel::requires_input_sanitization`).
    fn requires_input_sanitization(&self) -> bool {
        true
    }

    fn try_recv(&mut self) -> Option<ChannelMessage> {
        loop {
            let incoming = self.rx.try_recv().ok()?;
            if !self.is_authorized(&incoming) {
                tracing::warn!(
                    "rejected discord message from unauthorized user: {}",
                    incoming.author_id
                );
                continue;
            }
            self.channel_id = Some(incoming.channel_id);
            return Some(ChannelMessage {
                text: incoming.content,
                attachments: vec![],
                is_guest_context: false,
                is_from_bot: false,
                owner_key: None,
            });
        }
    }

    #[cfg_attr(
        feature = "profiling",
        tracing::instrument(name = "channels.discord.recv", skip_all, fields(msg_len = tracing::field::Empty))
    )]
    async fn recv(&mut self) -> Result<Option<ChannelMessage>, ChannelError> {
        loop {
            let Some(incoming) = self.rx.recv().await else {
                return Ok(None);
            };

            if !self.is_authorized(&incoming) {
                tracing::warn!(
                    "rejected discord message from unauthorized user: {}",
                    incoming.author_id
                );
                continue;
            }

            self.channel_id = Some(incoming.channel_id);
            self.buffer.reset();
            self.message_id = None;

            return Ok(Some(ChannelMessage {
                text: incoming.content,
                attachments: vec![],
                is_guest_context: false,
                is_from_bot: false,
                owner_key: None,
            }));
        }
    }

    #[cfg_attr(
        feature = "profiling",
        tracing::instrument(name = "channels.discord.send", skip_all, fields(msg_len = %text.len()))
    )]
    async fn send(&mut self, text: &str) -> Result<(), ChannelError> {
        let channel_id = self
            .channel_id
            .as_deref()
            .ok_or(ChannelError::NoActiveSession)?;

        if text.len() <= MAX_MESSAGE_LEN {
            self.rest
                .send_message(channel_id, text)
                .await
                .map_err(ChannelError::other)?;
        } else {
            let chunks = crate::markdown::utf8_chunks(text, MAX_MESSAGE_LEN);
            for chunk in chunks {
                self.rest
                    .send_message(channel_id, chunk)
                    .await
                    .map_err(ChannelError::other)?;
            }
        }
        Ok(())
    }

    #[cfg_attr(
        feature = "profiling",
        tracing::instrument(name = "channels.discord.send_chunk", skip_all, level = "debug", fields(chunk_len = %chunk.len()))
    )]
    async fn send_chunk(&mut self, chunk: &str) -> Result<(), ChannelError> {
        StreamingSend::streaming_send_chunk(self, chunk).await
    }

    #[cfg_attr(
        feature = "profiling",
        tracing::instrument(name = "channels.discord.flush_chunks", skip_all, level = "debug")
    )]
    async fn flush_chunks(&mut self) -> Result<(), ChannelError> {
        StreamingSend::streaming_flush_chunks(self).await
    }

    #[cfg_attr(
        feature = "profiling",
        tracing::instrument(name = "channels.discord.send_typing", skip_all, level = "debug")
    )]
    async fn send_typing(&mut self) -> Result<(), ChannelError> {
        let Some(channel_id) = self.channel_id.as_deref() else {
            return Ok(());
        };
        let _ = self.rest.trigger_typing(channel_id).await;
        Ok(())
    }

    async fn send_status(&mut self, text: &str) -> Result<(), ChannelError> {
        if text.is_empty() {
            return Ok(());
        }
        let Some(channel_id) = self.channel_id.as_deref() else {
            return Ok(());
        };
        self.rest
            .send_message(channel_id, text)
            .await
            .map_err(ChannelError::other)?;
        Ok(())
    }

    #[cfg_attr(
        feature = "profiling",
        tracing::instrument(name = "channels.discord.confirm", skip_all, fields(prompt_len = %prompt.len()))
    )]
    async fn confirm(&mut self, prompt: &str) -> Result<bool, ChannelError> {
        crate::confirm::ConfirmLoop::run_confirm(self, prompt).await
    }

    async fn elicit(
        &mut self,
        request: ElicitationRequest,
    ) -> Result<ElicitationResponse, ChannelError> {
        tracing::warn!(
            server = %request.server_name,
            "elicit() not supported on Discord channel — declining"
        );
        Ok(ElicitationResponse::Declined)
    }
}

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

    fn make_channel() -> DiscordChannel {
        let (_tx, rx) = mpsc::channel(16);
        let rest = rest::RestClient::new("test-token".into());
        DiscordChannel {
            rx,
            rest,
            _gateway_handle: Some(tokio::spawn(std::future::pending())),
            channel_id: None,
            allowed_user_ids: vec![],
            allowed_role_ids: vec![],
            allowed_channel_ids: vec![],
            buffer: StreamingBuffer::new(EDIT_THROTTLE),
            message_id: None,
            supervisor: None,
        }
    }

    fn make_incoming(author_id: &str, channel_id: &str, roles: Vec<String>) -> IncomingMessage {
        IncomingMessage {
            channel_id: channel_id.into(),
            content: "hello".into(),
            author_id: author_id.into(),
            author_roles: roles,
        }
    }

    #[tokio::test]
    async fn is_authorized_allows_all_when_empty_lists() {
        let ch = make_channel();
        let msg = make_incoming("user1", "ch1", vec![]);
        assert!(ch.is_authorized(&msg));
    }

    #[tokio::test]
    async fn is_authorized_rejects_channel_not_in_allowlist() {
        let mut ch = make_channel();
        ch.allowed_channel_ids = vec!["ch-allowed".into()];
        let msg = make_incoming("user1", "ch-other", vec![]);
        assert!(!ch.is_authorized(&msg));
    }

    #[tokio::test]
    async fn is_authorized_allows_channel_in_allowlist() {
        let mut ch = make_channel();
        ch.allowed_channel_ids = vec!["ch1".into()];
        let msg = make_incoming("user1", "ch1", vec![]);
        assert!(ch.is_authorized(&msg));
    }

    #[tokio::test]
    async fn is_authorized_allows_user_in_allowlist() {
        let mut ch = make_channel();
        ch.allowed_user_ids = vec!["user1".into()];
        let msg = make_incoming("user1", "ch1", vec![]);
        assert!(ch.is_authorized(&msg));
    }

    #[tokio::test]
    async fn is_authorized_rejects_user_not_in_allowlist() {
        let mut ch = make_channel();
        ch.allowed_user_ids = vec!["user-other".into()];
        let msg = make_incoming("user1", "ch1", vec![]);
        assert!(!ch.is_authorized(&msg));
    }

    #[tokio::test]
    async fn is_authorized_allows_role_in_allowlist() {
        let mut ch = make_channel();
        ch.allowed_role_ids = vec!["admin".into()];
        let msg = make_incoming("user1", "ch1", vec!["admin".into()]);
        assert!(ch.is_authorized(&msg));
    }

    #[tokio::test]
    async fn is_authorized_rejects_when_no_matching_role_or_user() {
        let mut ch = make_channel();
        ch.allowed_user_ids = vec!["user-other".into()];
        ch.allowed_role_ids = vec!["admin".into()];
        let msg = make_incoming("user1", "ch1", vec!["member".into()]);
        assert!(!ch.is_authorized(&msg));
    }

    #[tokio::test]
    async fn buffer_should_flush_true_when_no_last_edit() {
        let ch = make_channel();
        assert!(ch.buffer.should_flush());
    }

    #[tokio::test]
    async fn buffer_should_flush_false_within_throttle() {
        let mut ch = make_channel();
        ch.buffer.push("x");
        ch.buffer.mark_flushed();
        assert!(!ch.buffer.should_flush());
    }

    #[test]
    fn buffer_should_flush_true_after_throttle() {
        let mut buf = StreamingBuffer::new(Duration::from_millis(1));
        buf.push("x");
        buf.mark_flushed();
        std::thread::sleep(Duration::from_millis(5));
        assert!(buf.should_flush());
    }

    #[tokio::test]
    async fn send_chunk_accumulates() {
        let mut ch = make_channel();
        ch.buffer.push("hello ");
        ch.buffer.push("world");
        assert_eq!(ch.buffer.text(), "hello world");
    }

    #[tokio::test]
    async fn flush_chunks_clears_state() {
        let mut ch = make_channel();
        ch.buffer.push("test");
        ch.buffer.mark_flushed();
        // message_id is None, buffer not empty — send_or_edit will be called but fails without REST
        // So reset state manually and check post-condition directly:
        // Re-init with empty buffer and no message_id to test the clear-only path.
        let mut ch2 = make_channel();
        // message_id is None, buffer is empty — send_or_edit is NOT called
        ch2.flush_chunks().await.unwrap();
        assert!(ch2.buffer.is_empty());
        assert!(ch2.message_id.is_none());
    }

    #[tokio::test]
    async fn try_recv_sets_channel_id() {
        let (tx, rx) = mpsc::channel(16);
        let rest = rest::RestClient::new("test-token".into());
        let mut ch = DiscordChannel {
            rx,
            rest,
            _gateway_handle: Some(tokio::spawn(std::future::pending())),
            channel_id: None,
            allowed_user_ids: vec![],
            allowed_role_ids: vec![],
            allowed_channel_ids: vec![],
            buffer: StreamingBuffer::new(EDIT_THROTTLE),
            message_id: None,
            supervisor: None,
        };
        tx.try_send(make_incoming("user1", "ch42", vec![])).unwrap();
        let msg = ch.try_recv().unwrap();
        assert_eq!(msg.text, "hello");
        assert_eq!(ch.channel_id.as_deref(), Some("ch42"));
    }

    /// Regression test for #5460: `recv`/`try_recv` must return raw, unsanitized text so
    /// command dispatch in `Agent::run` still matches recognized commands over Discord.
    /// Sanitization for the residual non-command text happens centrally in the agent loop,
    /// gated on `requires_input_sanitization`, not at the channel adapter.
    #[tokio::test]
    async fn requires_input_sanitization_is_true() {
        let ch = make_channel();
        assert!(ch.requires_input_sanitization());
    }

    #[tokio::test]
    async fn try_recv_skips_unauthorized() {
        let (tx, rx) = mpsc::channel(16);
        let rest = rest::RestClient::new("test-token".into());
        let mut ch = DiscordChannel {
            rx,
            rest,
            _gateway_handle: Some(tokio::spawn(std::future::pending())),
            channel_id: None,
            allowed_user_ids: vec!["allowed-user".into()],
            allowed_role_ids: vec![],
            allowed_channel_ids: vec![],
            buffer: StreamingBuffer::new(EDIT_THROTTLE),
            message_id: None,
            supervisor: None,
        };
        tx.try_send(make_incoming("unauthorized", "ch1", vec![]))
            .unwrap();
        assert!(ch.try_recv().is_none());
    }

    #[tokio::test]
    async fn debug_impl() {
        let ch = make_channel();
        let debug = format!("{ch:?}");
        assert!(debug.contains("DiscordChannel"));
    }

    #[test]
    fn max_message_len_constant() {
        assert_eq!(MAX_MESSAGE_LEN, 2000);
    }

    #[test]
    fn edit_throttle_constant() {
        assert_eq!(EDIT_THROTTLE, Duration::from_millis(1500));
    }

    #[tokio::test]
    async fn confirm_returns_err_without_active_channel() {
        // confirm() calls send() first. Without channel_id, send() returns
        // Err("no active channel") and confirm() propagates it via `?`.
        // This test verifies that confirm() is callable and errors correctly.
        let mut ch = make_channel();
        // channel_id is None in make_channel() — send() will fail immediately.
        let result = ch.confirm("delete everything?").await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn confirm_timeout_logic_denies_on_timeout() {
        // Verify the timeout + recv logic used inside confirm() in isolation.
        // Full integration testing of confirm() (including the send() REST call)
        // requires a mock HTTP server and is covered by live agent testing.
        tokio::time::pause();
        let (_tx, mut rx) = mpsc::channel::<IncomingMessage>(1);
        // Advance past CONFIRM_TIMEOUT while _tx is still alive (no message sent).
        let timeout_fut = tokio::time::timeout(crate::CONFIRM_TIMEOUT, rx.recv());
        tokio::time::advance(crate::CONFIRM_TIMEOUT + Duration::from_millis(1)).await;
        let result = timeout_fut.await;
        // Should time out (Err), not receive a message.
        assert!(result.is_err(), "expected timeout Err, got recv result");
    }

    #[tokio::test]
    async fn confirm_skips_unauthorized_and_accepts_authorized() {
        tokio::time::pause();
        let (tx, rx) = mpsc::channel(16);
        let rest = rest::RestClient::new("test-token".into());
        let mut ch = DiscordChannel {
            rx,
            rest,
            _gateway_handle: Some(tokio::spawn(std::future::pending())),
            channel_id: Some("ch1".into()),
            allowed_user_ids: vec!["allowed-user".into()],
            allowed_role_ids: vec![],
            allowed_channel_ids: vec![],
            buffer: StreamingBuffer::new(EDIT_THROTTLE),
            message_id: None,
            supervisor: None,
        };
        // Unauthorized message first, then authorized "yes".
        tx.try_send(make_incoming("intruder", "ch1", vec![]))
            .unwrap();
        tx.try_send(make_incoming("allowed-user", "ch1", vec![]))
            .unwrap();
        // Test the loop logic directly (confirm() calls send() which needs REST).
        let deadline = tokio::time::Instant::now() + crate::CONFIRM_TIMEOUT;
        let mut confirmed = false;
        loop {
            let remaining = deadline.saturating_duration_since(tokio::time::Instant::now());
            assert!(!remaining.is_zero(), "timed out unexpectedly");
            match tokio::time::timeout(remaining, ch.rx.recv()).await {
                Ok(Some(msg)) => {
                    if !ch.is_authorized(&msg) {
                        continue;
                    }
                    confirmed = msg.content.trim().eq_ignore_ascii_case("hello");
                    break;
                }
                Ok(None) | Err(_) => break,
            }
        }
        assert!(confirmed);
    }

    /// Regression test for #6472: an unconfigured allowlist must refuse to
    /// start (fail-closed), matching Telegram's `start()` semantics, instead
    /// of silently accepting messages from any user.
    #[test]
    fn new_rejects_empty_allowlists() {
        let result = DiscordChannel::new("test-token".into(), vec![], vec![], vec![], None);
        assert!(matches!(result, Err(ChannelError::Other(_))));
    }

    /// A non-empty role allowlist alone is sufficient to pass the startup gate,
    /// even when `allowed_user_ids` is empty — mirrors `is_authorized`'s
    /// role-only path.
    #[tokio::test]
    async fn new_allows_role_only_allowlist() {
        let result = DiscordChannel::new(
            "test-token".into(),
            vec![],
            vec!["admin-role".into()],
            vec![],
            None,
        );
        assert!(result.is_ok());
    }
}