zeptoclaw 0.7.3

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
//! Slack channel implementation.
//!
//! Supports:
//! - outbound messaging via Slack Web API (`chat.postMessage`)
//! - inbound messaging via Slack Socket Mode (`apps.connections.open`)

use async_trait::async_trait;
use futures::{FutureExt, SinkExt, StreamExt};
use serde::Deserialize;
use serde_json::{json, Value};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::mpsc;
use tokio_tungstenite::connect_async;
use tokio_tungstenite::tungstenite::Message as WsMessage;
use tracing::{debug, error, info, warn};

use crate::bus::{InboundMessage, MediaAttachment, MediaType, MessageBus, OutboundMessage};
use crate::config::SlackConfig;
use crate::error::{Result, ZeptoError};

use super::{BaseChannelConfig, Channel};

const SLACK_CHAT_POST_MESSAGE_URL: &str = "https://slack.com/api/chat.postMessage";
const SLACK_SOCKET_OPEN_URL: &str = "https://slack.com/api/apps.connections.open";
const SLACK_RECONNECT_DELAY_SECS: u64 = 2;

#[derive(Debug, Deserialize)]
struct SlackSocketOpenResponse {
    ok: bool,
    url: Option<String>,
    error: Option<String>,
}

#[derive(Debug, Deserialize)]
struct SlackSocketEnvelope {
    #[serde(default)]
    envelope_id: Option<String>,
    #[serde(rename = "type")]
    envelope_type: String,
    #[serde(default)]
    payload: Option<SlackSocketPayload>,
}

#[derive(Debug, Deserialize)]
struct SlackSocketPayload {
    #[serde(default)]
    event: Option<SlackEvent>,
}

/// A file shared in a Slack message (Socket Mode events_api).
#[derive(Debug, Deserialize)]
struct SlackFile {
    /// Private download URL (requires bot token auth).
    #[serde(default)]
    url_private_download: Option<String>,
    /// MIME type reported by Slack (e.g. "image/png").
    #[serde(default)]
    mimetype: Option<String>,
    /// Original filename.
    #[serde(default)]
    name: Option<String>,
    /// File size in bytes.
    #[serde(default)]
    size: Option<u64>,
}

#[derive(Debug, Deserialize)]
struct SlackEvent {
    #[serde(rename = "type")]
    event_type: String,
    #[serde(default)]
    subtype: Option<String>,
    #[serde(default)]
    user: Option<String>,
    #[serde(default)]
    bot_id: Option<String>,
    #[serde(default)]
    channel: Option<String>,
    #[serde(default)]
    text: Option<String>,
    #[serde(default)]
    ts: Option<String>,
    #[serde(default)]
    thread_ts: Option<String>,
    /// Files attached to this message.
    #[serde(default)]
    files: Vec<SlackFile>,
}

struct ParsedSocketMessage {
    ack_message: Option<String>,
    inbound_message: Option<InboundMessage>,
    /// Files extracted from the event payload for async downloading.
    files: Vec<SlackFile>,
}

/// Slack channel implementation backed by Slack Web API and Socket Mode.
pub struct SlackChannel {
    config: SlackConfig,
    base_config: BaseChannelConfig,
    bus: Arc<MessageBus>,
    running: Arc<AtomicBool>,
    client: reqwest::Client,
    shutdown_tx: Option<mpsc::Sender<()>>,
}

impl SlackChannel {
    /// Creates a new Slack channel.
    pub fn new(config: SlackConfig, bus: Arc<MessageBus>) -> Self {
        let base_config = BaseChannelConfig {
            name: "slack".to_string(),
            allowlist: config.allow_from.clone(),
            deny_by_default: config.deny_by_default,
        };

        Self {
            config,
            base_config,
            bus,
            running: Arc::new(AtomicBool::new(false)),
            client: reqwest::Client::new(),
            shutdown_tx: None,
        }
    }

    /// Returns a reference to the Slack configuration.
    pub fn slack_config(&self) -> &SlackConfig {
        &self.config
    }

    /// Returns whether the channel is enabled in configuration.
    pub fn is_enabled(&self) -> bool {
        self.config.enabled
    }

    fn build_payload(msg: &OutboundMessage) -> Result<Value> {
        let channel = msg.chat_id.trim();
        if channel.is_empty() {
            return Err(ZeptoError::Channel(
                "Slack channel ID cannot be empty".to_string(),
            ));
        }

        let mut payload = json!({
            "channel": channel,
            "text": msg.content,
        });

        if let Some(ref reply_to) = msg.reply_to {
            if let Some(map) = payload.as_object_mut() {
                map.insert("thread_ts".to_string(), Value::String(reply_to.clone()));
            }
        }

        Ok(payload)
    }

    async fn open_socket_mode_url(client: &reqwest::Client, app_token: &str) -> Result<String> {
        let response = client
            .post(SLACK_SOCKET_OPEN_URL)
            .bearer_auth(app_token)
            .send()
            .await
            .map_err(|e| {
                ZeptoError::Channel(format!(
                    "Failed to open Slack Socket Mode connection: {}",
                    e
                ))
            })?;

        let status = response.status();
        let body = response.text().await.map_err(|e| {
            ZeptoError::Channel(format!("Failed to read Slack Socket Mode response: {}", e))
        })?;

        if !status.is_success() {
            return Err(ZeptoError::Channel(format!(
                "Slack Socket Mode HTTP {}: {}",
                status, body
            )));
        }

        let parsed: SlackSocketOpenResponse = serde_json::from_str(&body).map_err(|e| {
            ZeptoError::Channel(format!("Invalid Slack Socket Mode open response: {}", e))
        })?;

        if !parsed.ok {
            return Err(ZeptoError::Channel(format!(
                "Slack Socket Mode open failed: {}",
                parsed.error.unwrap_or_else(|| "unknown_error".to_string())
            )));
        }

        parsed.url.filter(|u| !u.trim().is_empty()).ok_or_else(|| {
            ZeptoError::Channel("Slack Socket Mode response missing URL".to_string())
        })
    }

    fn parse_socket_message(
        raw: &str,
        allowlist: &[String],
        deny_by_default: bool,
    ) -> Result<ParsedSocketMessage> {
        let envelope: SlackSocketEnvelope = serde_json::from_str(raw)
            .map_err(|e| ZeptoError::Channel(format!("Invalid Slack socket payload: {}", e)))?;

        let ack_message = envelope
            .envelope_id
            .as_deref()
            .map(|envelope_id| json!({ "envelope_id": envelope_id }).to_string());

        // Collect image files from the event payload before passing envelope by ref.
        let files: Vec<SlackFile> = envelope
            .payload
            .as_ref()
            .and_then(|p| p.event.as_ref())
            .map(|e| {
                e.files
                    .iter()
                    .filter(|f| {
                        f.mimetype
                            .as_deref()
                            .is_some_and(|m| m.starts_with("image/"))
                    })
                    .map(|f| SlackFile {
                        url_private_download: f.url_private_download.clone(),
                        mimetype: f.mimetype.clone(),
                        name: f.name.clone(),
                        size: f.size,
                    })
                    .collect()
            })
            .unwrap_or_default();

        let inbound_message = Self::extract_inbound_message(&envelope, allowlist, deny_by_default);

        Ok(ParsedSocketMessage {
            ack_message,
            inbound_message,
            files,
        })
    }

    fn extract_inbound_message(
        envelope: &SlackSocketEnvelope,
        allowlist: &[String],
        deny_by_default: bool,
    ) -> Option<InboundMessage> {
        if envelope.envelope_type != "events_api" {
            return None;
        }

        let payload = envelope.payload.as_ref()?;
        let event = payload.event.as_ref()?;

        if event.event_type != "message" {
            return None;
        }
        if event.subtype.is_some() || event.bot_id.is_some() {
            return None;
        }

        let sender_id = event.user.as_deref()?.trim().to_string();
        let chat_id = event.channel.as_deref()?.trim().to_string();
        let content = event.text.as_deref()?.trim().to_string();
        if sender_id.is_empty() || chat_id.is_empty() || content.is_empty() {
            return None;
        }

        let allowed = if allowlist.is_empty() {
            !deny_by_default
        } else {
            allowlist.contains(&sender_id)
        };
        if !allowed {
            info!(
                "Slack: user {} not in allowlist, ignoring inbound message",
                sender_id
            );
            return None;
        }

        let mut inbound = InboundMessage::new("slack", &sender_id, &chat_id, &content);
        if let Some(ts) = event.ts.as_deref() {
            if !ts.trim().is_empty() {
                inbound = inbound.with_metadata("slack_ts", ts);
            }
        }
        if let Some(thread_ts) = event.thread_ts.as_deref() {
            if !thread_ts.trim().is_empty() {
                inbound = inbound.with_metadata("slack_thread_ts", thread_ts);
            }
        }

        Some(inbound)
    }

    async fn wait_for_reconnect_or_shutdown(shutdown_rx: &mut mpsc::Receiver<()>) -> bool {
        tokio::select! {
            _ = shutdown_rx.recv() => true,
            _ = tokio::time::sleep(Duration::from_secs(SLACK_RECONNECT_DELAY_SECS)) => false,
        }
    }

    async fn run_socket_mode_loop(
        client: reqwest::Client,
        app_token: String,
        bot_token: String,
        bus: Arc<MessageBus>,
        allowlist: Vec<String>,
        deny_by_default: bool,
        mut shutdown_rx: mpsc::Receiver<()>,
    ) {
        loop {
            let socket_url = tokio::select! {
                _ = shutdown_rx.recv() => {
                    info!("Slack Socket Mode shutdown requested");
                    return;
                }
                opened = Self::open_socket_mode_url(&client, &app_token) => {
                    match opened {
                        Ok(url) => url,
                        Err(e) => {
                            warn!("Slack Socket Mode open failed: {}", e);
                            if Self::wait_for_reconnect_or_shutdown(&mut shutdown_rx).await {
                                return;
                            }
                            continue;
                        }
                    }
                }
            };

            let ws_stream = tokio::select! {
                _ = shutdown_rx.recv() => {
                    info!("Slack Socket Mode shutdown requested");
                    return;
                }
                connected = connect_async(&socket_url) => {
                    match connected {
                        Ok((stream, _)) => stream,
                        Err(e) => {
                            warn!("Failed to connect Slack Socket Mode websocket: {}", e);
                            if Self::wait_for_reconnect_or_shutdown(&mut shutdown_rx).await {
                                return;
                            }
                            continue;
                        }
                    }
                }
            };

            info!("Slack Socket Mode connected");
            let (mut ws_writer, mut ws_reader) = ws_stream.split();

            loop {
                let next = tokio::select! {
                    _ = shutdown_rx.recv() => {
                        info!("Slack Socket Mode shutdown requested");
                        return;
                    }
                    message = ws_reader.next() => message,
                };

                match next {
                    Some(Ok(WsMessage::Text(raw))) => {
                        match Self::parse_socket_message(&raw, &allowlist, deny_by_default) {
                            Ok(parsed) => {
                                if let Some(ack_message) = parsed.ack_message {
                                    if let Err(e) =
                                        ws_writer.send(WsMessage::Text(ack_message.into())).await
                                    {
                                        warn!("Slack Socket Mode ack send failed: {}", e);
                                        break;
                                    }
                                }

                                if let Some(mut inbound) = parsed.inbound_message {
                                    // Download image files attached to this message
                                    for file in &parsed.files {
                                        if let (Some(ref url), Some(ref mime)) =
                                            (&file.url_private_download, &file.mimetype)
                                        {
                                            if file.size.is_none_or(|s| s <= 20 * 1024 * 1024) {
                                                match client
                                                    .get(url)
                                                    .bearer_auth(&bot_token)
                                                    .send()
                                                    .await
                                                {
                                                    Ok(resp) => {
                                                        if let Ok(bytes) = resp.bytes().await {
                                                            let mut media = MediaAttachment::new(
                                                                MediaType::Image,
                                                            )
                                                            .with_data(bytes.to_vec())
                                                            .with_mime_type(mime);
                                                            if let Some(ref name) = file.name {
                                                                media = media.with_filename(name);
                                                            }
                                                            inbound = inbound.with_media(media);
                                                        }
                                                    }
                                                    Err(e) => warn!(
                                                        "Failed to download Slack file: {}",
                                                        e
                                                    ),
                                                }
                                            }
                                        }
                                    }
                                    if let Err(e) = bus.publish_inbound(inbound).await {
                                        error!("Failed to publish Slack inbound message: {}", e);
                                    }
                                }
                            }
                            Err(e) => {
                                debug!("Ignoring Slack socket payload: {}", e);
                            }
                        }
                    }
                    Some(Ok(WsMessage::Ping(payload))) => {
                        if let Err(e) = ws_writer.send(WsMessage::Pong(payload)).await {
                            warn!("Slack Socket Mode pong send failed: {}", e);
                            break;
                        }
                    }
                    Some(Ok(WsMessage::Close(frame))) => {
                        info!("Slack Socket Mode closed by server: {:?}", frame);
                        break;
                    }
                    Some(Ok(_)) => {}
                    Some(Err(e)) => {
                        warn!("Slack Socket Mode stream error: {}", e);
                        break;
                    }
                    None => {
                        warn!("Slack Socket Mode stream ended");
                        break;
                    }
                }
            }

            if Self::wait_for_reconnect_or_shutdown(&mut shutdown_rx).await {
                return;
            }
            info!("Reconnecting Slack Socket Mode");
        }
    }
}

#[async_trait]
impl Channel for SlackChannel {
    fn name(&self) -> &str {
        "slack"
    }

    async fn start(&mut self) -> Result<()> {
        if self.running.swap(true, Ordering::SeqCst) {
            info!("Slack channel already running");
            return Ok(());
        }

        if !self.config.enabled {
            warn!("Slack channel is disabled in configuration");
            self.running.store(false, Ordering::SeqCst);
            return Ok(());
        }

        if self.config.bot_token.trim().is_empty() {
            self.running.store(false, Ordering::SeqCst);
            return Err(ZeptoError::Config("Slack bot token is empty".to_string()));
        }

        let app_token = self.config.app_token.trim().to_string();
        if app_token.is_empty() {
            info!("Starting Slack channel (outbound only, app_token not configured)");
            return Ok(());
        }

        let (shutdown_tx, shutdown_rx) = mpsc::channel::<()>(1);
        self.shutdown_tx = Some(shutdown_tx);

        info!("Starting Slack channel with Socket Mode inbound");
        let running_clone = Arc::clone(&self.running);
        let client = self.client.clone();
        let bot_token = self.config.bot_token.clone();
        let bus = Arc::clone(&self.bus);
        let allow_from = self.config.allow_from.clone();
        let deny_by_default = self.config.deny_by_default;
        tokio::spawn(async move {
            let task_result = std::panic::AssertUnwindSafe(async move {
                Self::run_socket_mode_loop(
                    client,
                    app_token,
                    bot_token,
                    bus,
                    allow_from,
                    deny_by_default,
                    shutdown_rx,
                )
                .await;
            })
            .catch_unwind()
            .await;
            if task_result.is_err() {
                error!("Slack socket mode task panicked");
            }
            running_clone.store(false, Ordering::SeqCst);
        });

        Ok(())
    }

    async fn stop(&mut self) -> Result<()> {
        if !self.running.swap(false, Ordering::SeqCst) {
            info!("Slack channel already stopped");
            return Ok(());
        }

        if let Some(tx) = self.shutdown_tx.take() {
            let _ = tx.send(()).await;
        }

        info!("Slack channel stopped");
        Ok(())
    }

    async fn send(&self, msg: OutboundMessage) -> Result<()> {
        if !self.running.load(Ordering::SeqCst) {
            return Err(ZeptoError::Channel("Slack channel not running".to_string()));
        }

        if self.config.bot_token.trim().is_empty() {
            return Err(ZeptoError::Config("Slack bot token is empty".to_string()));
        }

        let payload = Self::build_payload(&msg)?;

        let response = self
            .client
            .post(SLACK_CHAT_POST_MESSAGE_URL)
            .bearer_auth(&self.config.bot_token)
            .json(&payload)
            .send()
            .await
            .map_err(|e| ZeptoError::Channel(format!("Failed to call Slack API: {}", e)))?;

        let status = response.status();
        let body = response.text().await.map_err(|e| {
            ZeptoError::Channel(format!("Failed to read Slack API response: {}", e))
        })?;

        if !status.is_success() {
            return Err(ZeptoError::Channel(format!(
                "Slack API returned HTTP {}: {}",
                status, body
            )));
        }

        let body_json: Value = serde_json::from_str(&body)
            .map_err(|e| ZeptoError::Channel(format!("Invalid Slack API response JSON: {}", e)))?;

        if !body_json
            .get("ok")
            .and_then(Value::as_bool)
            .unwrap_or(false)
        {
            let api_error = body_json
                .get("error")
                .and_then(Value::as_str)
                .unwrap_or("unknown_error");
            return Err(ZeptoError::Channel(format!(
                "Slack API returned error: {}",
                api_error
            )));
        }

        info!("Slack: Message sent successfully");
        Ok(())
    }

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

    fn is_allowed(&self, user_id: &str) -> bool {
        self.base_config.is_allowed(user_id)
    }
}

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

    fn test_bus() -> Arc<MessageBus> {
        Arc::new(MessageBus::new())
    }

    #[test]
    fn test_slack_channel_creation() {
        let config = SlackConfig {
            enabled: true,
            bot_token: "xoxb-test-token".to_string(),
            app_token: "xapp-test-token".to_string(),
            allow_from: vec!["U123".to_string()],
            ..Default::default()
        };
        let channel = SlackChannel::new(config, test_bus());

        assert_eq!(channel.name(), "slack");
        assert!(!channel.is_running());
        assert!(channel.is_allowed("U123"));
        assert!(!channel.is_allowed("U999"));
    }

    #[test]
    fn test_slack_empty_allowlist() {
        let config = SlackConfig {
            enabled: true,
            bot_token: "xoxb-test-token".to_string(),
            app_token: String::new(),
            allow_from: vec![],
            ..Default::default()
        };
        let channel = SlackChannel::new(config, test_bus());

        assert!(channel.is_allowed("anyone"));
    }

    #[test]
    fn test_slack_config_access() {
        let config = SlackConfig {
            enabled: true,
            bot_token: "xoxb-my-token".to_string(),
            app_token: "xapp-token".to_string(),
            allow_from: vec!["UADMIN".to_string()],
            ..Default::default()
        };
        let channel = SlackChannel::new(config, test_bus());

        assert!(channel.is_enabled());
        assert_eq!(channel.slack_config().bot_token, "xoxb-my-token");
        assert_eq!(channel.slack_config().allow_from, vec!["UADMIN"]);
    }

    #[tokio::test]
    async fn test_slack_start_without_token() {
        let config = SlackConfig {
            enabled: true,
            bot_token: String::new(),
            app_token: String::new(),
            allow_from: vec![],
            ..Default::default()
        };
        let mut channel = SlackChannel::new(config, test_bus());

        let result = channel.start().await;
        assert!(result.is_err());
        assert!(!channel.is_running());
    }

    #[tokio::test]
    async fn test_slack_start_disabled() {
        let config = SlackConfig {
            enabled: false,
            bot_token: "xoxb-test-token".to_string(),
            app_token: String::new(),
            allow_from: vec![],
            ..Default::default()
        };
        let mut channel = SlackChannel::new(config, test_bus());

        let result = channel.start().await;
        assert!(result.is_ok());
        assert!(!channel.is_running());
    }

    #[tokio::test]
    async fn test_slack_stop_not_running() {
        let config = SlackConfig {
            enabled: true,
            bot_token: "xoxb-test-token".to_string(),
            app_token: String::new(),
            allow_from: vec![],
            ..Default::default()
        };
        let mut channel = SlackChannel::new(config, test_bus());

        let result = channel.stop().await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_slack_send_not_running() {
        let config = SlackConfig {
            enabled: true,
            bot_token: "xoxb-test-token".to_string(),
            app_token: String::new(),
            allow_from: vec![],
            ..Default::default()
        };
        let channel = SlackChannel::new(config, test_bus());

        let msg = OutboundMessage::new("slack", "C123456", "Hello");
        let result = channel.send(msg).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_slack_send_empty_chat_id() {
        let config = SlackConfig {
            enabled: true,
            bot_token: "xoxb-test-token".to_string(),
            app_token: String::new(),
            allow_from: vec![],
            ..Default::default()
        };
        let channel = SlackChannel::new(config, test_bus());
        channel.running.store(true, Ordering::SeqCst);

        let msg = OutboundMessage::new("slack", "", "Hello");
        let result = channel.send(msg).await;
        assert!(result.is_err());
    }

    #[test]
    fn test_slack_payload_with_reply() {
        let msg = OutboundMessage::new("slack", "C123", "hello").with_reply("173401.000200");
        let payload = SlackChannel::build_payload(&msg).expect("payload should build");

        assert_eq!(payload["channel"], "C123");
        assert_eq!(payload["text"], "hello");
        assert_eq!(payload["thread_ts"], "173401.000200");
    }

    #[test]
    fn test_parse_socket_message_extracts_inbound_and_ack() {
        let raw = r#"{
            "envelope_id":"envelope-123",
            "type":"events_api",
            "payload":{
                "event":{
                    "type":"message",
                    "user":"U123",
                    "channel":"C999",
                    "text":"hello from slack",
                    "ts":"173401.000200",
                    "thread_ts":"173401.000100"
                }
            }
        }"#;

        let parsed =
            SlackChannel::parse_socket_message(raw, &[], false).expect("parse should succeed");
        assert_eq!(
            parsed.ack_message,
            Some(r#"{"envelope_id":"envelope-123"}"#.to_string())
        );
        let inbound = parsed.inbound_message.expect("inbound expected");
        assert_eq!(inbound.channel, "slack");
        assert_eq!(inbound.sender_id, "U123");
        assert_eq!(inbound.chat_id, "C999");
        assert_eq!(inbound.content, "hello from slack");
        assert_eq!(
            inbound.metadata.get("slack_ts"),
            Some(&"173401.000200".to_string())
        );
        assert_eq!(
            inbound.metadata.get("slack_thread_ts"),
            Some(&"173401.000100".to_string())
        );
    }

    #[test]
    fn test_parse_socket_message_ignores_non_message_event() {
        let raw = r#"{
            "envelope_id":"envelope-456",
            "type":"events_api",
            "payload":{"event":{"type":"reaction_added","user":"U123"}}
        }"#;

        let parsed =
            SlackChannel::parse_socket_message(raw, &[], false).expect("parse should succeed");
        assert!(parsed.ack_message.is_some());
        assert!(parsed.inbound_message.is_none());
    }

    #[test]
    fn test_parse_socket_message_ignores_disallowed_user() {
        let raw = r#"{
            "envelope_id":"envelope-789",
            "type":"events_api",
            "payload":{
                "event":{"type":"message","user":"U999","channel":"C123","text":"blocked"}
            }
        }"#;

        let parsed = SlackChannel::parse_socket_message(raw, &["U123".to_string()], false)
            .expect("parse should succeed");
        assert!(parsed.ack_message.is_some());
        assert!(parsed.inbound_message.is_none());
    }

    #[test]
    fn test_parse_socket_message_ignores_bot_or_subtype_messages() {
        let bot_message = r#"{
            "envelope_id":"e1",
            "type":"events_api",
            "payload":{"event":{"type":"message","bot_id":"B123","channel":"C1","text":"bot"}}
        }"#;
        let subtype_message = r#"{
            "envelope_id":"e2",
            "type":"events_api",
            "payload":{"event":{"type":"message","subtype":"message_changed","channel":"C1","text":"edit"}}
        }"#;

        let bot_parsed = SlackChannel::parse_socket_message(bot_message, &[], false)
            .expect("parse should succeed");
        let subtype_parsed = SlackChannel::parse_socket_message(subtype_message, &[], false)
            .expect("parse should succeed");

        assert!(bot_parsed.inbound_message.is_none());
        assert!(subtype_parsed.inbound_message.is_none());
    }
}