bob-chat 0.3.1

Chat channel types and streaming abstractions for Bob Agent Framework
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
//! Integration tests for the `ChatBot` lifecycle.
//!
//! These tests verify the full event pipeline: register handlers, add a mock
//! adapter, inject events, and confirm the correct handlers are invoked with
//! the expected data.

use std::{
    collections::VecDeque,
    sync::{
        Arc, Mutex,
        atomic::{AtomicUsize, Ordering},
    },
};

use bob_chat::{
    bot::{ChatBot, ChatBotConfig},
    emoji::{EmojiValue, WellKnownEmoji},
    error::ChatError,
    event::{
        ActionEvent, ChatEvent, ModalCloseEvent, ModalSubmitEvent, ReactionEvent, SlashCommandEvent,
    },
    message::{AdapterPostableMessage, Author, EphemeralMessage, IncomingMessage, SentMessage},
};

// ---------------------------------------------------------------------------
// Lightweight mock adapter for integration tests
// ---------------------------------------------------------------------------

struct TestAdapter {
    name: &'static str,
    events: Mutex<VecDeque<ChatEvent>>,
}

impl TestAdapter {
    fn new(name: &'static str, events: Vec<ChatEvent>) -> Self {
        Self { name, events: Mutex::new(VecDeque::from(events)) }
    }
}

#[async_trait::async_trait]
impl bob_chat::ChatAdapter for TestAdapter {
    fn name(&self) -> &str {
        self.name
    }

    async fn post_message(
        &self,
        thread_id: &str,
        _message: &AdapterPostableMessage,
    ) -> Result<SentMessage, ChatError> {
        Ok(SentMessage {
            id: "msg-1".into(),
            thread_id: thread_id.into(),
            adapter_name: self.name.into(),
            raw: None,
        })
    }

    async fn edit_message(
        &self,
        thread_id: &str,
        message_id: &str,
        _message: &AdapterPostableMessage,
    ) -> Result<SentMessage, ChatError> {
        Ok(SentMessage {
            id: message_id.into(),
            thread_id: thread_id.into(),
            adapter_name: self.name.into(),
            raw: None,
        })
    }

    async fn delete_message(&self, _thread_id: &str, _message_id: &str) -> Result<(), ChatError> {
        Ok(())
    }

    fn render_card(&self, _card: &bob_chat::CardElement) -> String {
        String::new()
    }

    fn render_message(&self, _message: &AdapterPostableMessage) -> String {
        String::new()
    }

    async fn recv_event(&mut self) -> Option<ChatEvent> {
        let Ok(mut q) = self.events.lock() else {
            return None;
        };
        q.pop_front()
    }

    async fn post_ephemeral(
        &self,
        _thread_id: &str,
        _user_id: &str,
        _message: &AdapterPostableMessage,
    ) -> Result<EphemeralMessage, ChatError> {
        Err(ChatError::NotSupported("ephemeral".into()))
    }

    async fn open_dm(&self, user_id: &str) -> Result<String, ChatError> {
        Ok(format!("dm-{user_id}"))
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn sample_author() -> Author {
    Author {
        user_id: "u1".into(),
        user_name: "alice".into(),
        full_name: "Alice".into(),
        is_bot: false,
    }
}

fn sample_message(text: &str) -> IncomingMessage {
    IncomingMessage {
        id: "m1".into(),
        text: text.into(),
        author: sample_author(),
        attachments: vec![],
        is_mention: false,
        thread_id: "t1".into(),
        timestamp: None,
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

/// Register `on_mention` handler → inject Mention event → verify handler
/// called with correct thread and message data.
#[tokio::test]
async fn mention_handler_receives_correct_data() {
    let received_text = Arc::new(Mutex::new(String::new()));
    let received_thread = Arc::new(Mutex::new(String::new()));
    let rt = Arc::clone(&received_text);
    let rth = Arc::clone(&received_thread);

    let mut bot = ChatBot::new(ChatBotConfig::default());
    bot.on_mention(move |thread, msg| {
        let rt = Arc::clone(&rt);
        let rth = Arc::clone(&rth);
        async move {
            if let Ok(mut t) = rt.lock() {
                *t = msg.text.clone();
            }
            if let Ok(mut th) = rth.lock() {
                *th = thread.thread_id().to_owned();
            }
        }
    });

    bot.add_adapter(TestAdapter::new(
        "test",
        vec![ChatEvent::Mention {
            thread_id: "channel-42".into(),
            message: sample_message("hey @bot help me"),
        }],
    ));

    bot.run().await.expect("run should succeed");

    assert_eq!(*received_text.lock().expect("lock"), "hey @bot help me");
    assert_eq!(*received_thread.lock().expect("lock"), "channel-42");
}

/// Register `on_slash_command("/test")` → inject matching and non-matching
/// commands → verify only the matching command triggers the handler.
#[tokio::test]
async fn slash_command_filter_matches_correctly() {
    let counter = Arc::new(AtomicUsize::new(0));
    let received_text = Arc::new(Mutex::new(String::new()));
    let c = Arc::clone(&counter);
    let rt = Arc::clone(&received_text);

    let mut bot = ChatBot::new(ChatBotConfig::default());
    bot.on_slash_command(Some(vec!["/test".into()]), move |cmd| {
        let c = Arc::clone(&c);
        let rt = Arc::clone(&rt);
        async move {
            c.fetch_add(1, Ordering::SeqCst);
            if let Ok(mut t) = rt.lock() {
                *t = cmd.text.clone();
            }
        }
    });

    bot.add_adapter(TestAdapter::new(
        "test",
        vec![
            ChatEvent::SlashCommand(SlashCommandEvent {
                command: "/test".into(),
                text: "arg1".into(),
                channel_id: "c1".into(),
                user: sample_author(),
                trigger_id: None,
                adapter_name: "test".into(),
            }),
            ChatEvent::SlashCommand(SlashCommandEvent {
                command: "/other".into(),
                text: "arg2".into(),
                channel_id: "c1".into(),
                user: sample_author(),
                trigger_id: None,
                adapter_name: "test".into(),
            }),
        ],
    ));

    bot.run().await.expect("run should succeed");

    assert_eq!(counter.load(Ordering::SeqCst), 1);
    assert_eq!(*received_text.lock().expect("lock"), "arg1");
}

/// Register `on_action("approve")` → inject matching and non-matching actions
/// → verify only the matching one triggers.
#[tokio::test]
async fn action_filter_matches_correctly() {
    let counter = Arc::new(AtomicUsize::new(0));
    let c = Arc::clone(&counter);

    let mut bot = ChatBot::new(ChatBotConfig::default());
    bot.on_action(Some(vec!["approve".into()]), move |_action, _thread| {
        let c = Arc::clone(&c);
        async move {
            c.fetch_add(1, Ordering::SeqCst);
        }
    });

    bot.add_adapter(TestAdapter::new(
        "test",
        vec![
            ChatEvent::Action(ActionEvent {
                action_id: "approve".into(),
                thread_id: "t1".into(),
                message_id: "m1".into(),
                user: sample_author(),
                value: Some("yes".into()),
                trigger_id: None,
                adapter_name: "test".into(),
            }),
            ChatEvent::Action(ActionEvent {
                action_id: "reject".into(),
                thread_id: "t1".into(),
                message_id: "m1".into(),
                user: sample_author(),
                value: Some("no".into()),
                trigger_id: None,
                adapter_name: "test".into(),
            }),
        ],
    ));

    bot.run().await.expect("run should succeed");
    assert_eq!(counter.load(Ordering::SeqCst), 1);
}

/// Register `on_reaction([ThumbsUp])` → inject ThumbsUp and ThumbsDown →
/// verify only ThumbsUp triggers handler.
#[tokio::test]
async fn reaction_emoji_filter() {
    let counter = Arc::new(AtomicUsize::new(0));
    let c = Arc::clone(&counter);

    let thumbs_up = EmojiValue::from_well_known(WellKnownEmoji::ThumbsUp);
    let thumbs_down = EmojiValue::from_well_known(WellKnownEmoji::ThumbsDown);

    let mut bot = ChatBot::new(ChatBotConfig::default());
    bot.on_reaction(Some(vec![thumbs_up.clone()]), move |_reaction| {
        let c = Arc::clone(&c);
        async move {
            c.fetch_add(1, Ordering::SeqCst);
        }
    });

    bot.add_adapter(TestAdapter::new(
        "test",
        vec![
            ChatEvent::Reaction(ReactionEvent {
                thread_id: "t1".into(),
                message_id: "m1".into(),
                user: sample_author(),
                emoji: thumbs_up,
                added: true,
                adapter_name: "test".into(),
            }),
            ChatEvent::Reaction(ReactionEvent {
                thread_id: "t1".into(),
                message_id: "m1".into(),
                user: sample_author(),
                emoji: thumbs_down,
                added: true,
                adapter_name: "test".into(),
            }),
        ],
    ));

    bot.run().await.expect("run should succeed");
    assert_eq!(counter.load(Ordering::SeqCst), 1);
}

/// Via `on_mention` handler → `ThreadHandle::subscribe()` → then a later
/// Message in that same thread fires the `on_subscribed_message` handler
/// instead of the generic `on_message` handler.
#[tokio::test]
async fn subscribed_thread_dispatches_correctly() {
    let sub_counter = Arc::new(AtomicUsize::new(0));
    let msg_counter = Arc::new(AtomicUsize::new(0));
    let sc = Arc::clone(&sub_counter);
    let mc = Arc::clone(&msg_counter);

    let mut bot = ChatBot::new(ChatBotConfig::default());

    // The mention handler subscribes to its thread.
    bot.on_mention(move |thread, _msg| async move {
        thread.subscribe().await;
    });

    bot.on_subscribed_message(move |_thread, _msg| {
        let sc = Arc::clone(&sc);
        async move {
            sc.fetch_add(1, Ordering::SeqCst);
        }
    });
    bot.on_message(None, move |_thread, _msg| {
        let mc = Arc::clone(&mc);
        async move {
            mc.fetch_add(1, Ordering::SeqCst);
        }
    });

    // Events: first a Mention (triggers subscribe), then a Message in the
    // same thread (should route to subscribed handler), then a Message in a
    // different thread (should route to normal handler).
    bot.add_adapter(TestAdapter::new(
        "test",
        vec![
            ChatEvent::Mention {
                thread_id: "t-sub".into(),
                message: sample_message("@bot subscribe me"),
            },
            ChatEvent::Message {
                thread_id: "t-sub".into(),
                message: sample_message("subscribed thread msg"),
            },
            ChatEvent::Message {
                thread_id: "t-other".into(),
                message: sample_message("regular thread msg"),
            },
        ],
    ));

    bot.run().await.expect("run should succeed");
    assert_eq!(sub_counter.load(Ordering::SeqCst), 1);
    assert_eq!(msg_counter.load(Ordering::SeqCst), 1);
}

/// Verify `ThreadHandle::post_ephemeral` with DM fallback on an adapter
/// that doesn't support ephemeral → `open_dm` + `post_message` are called.
#[tokio::test]
async fn ephemeral_dm_fallback_via_thread_handle() {
    let post_counter = Arc::new(AtomicUsize::new(0));
    let pc = Arc::clone(&post_counter);

    let mut bot = ChatBot::new(ChatBotConfig::default());

    bot.on_mention(move |thread, _msg| {
        let pc = Arc::clone(&pc);
        async move {
            // The TestAdapter returns NotSupported for ephemeral, supports DM.
            let result = thread
                .post_ephemeral("u1", AdapterPostableMessage::Text("secret".into()), true)
                .await;
            // The NullAdapter in the ThreadHandle will fail, but the test
            // verifies the control flow compiles and runs without panic.
            // A full integration with a real adapter is tested via
            // thread::tests::post_ephemeral_fallback_to_dm.
            drop(result);
            pc.fetch_add(1, Ordering::SeqCst);
        }
    });

    bot.add_adapter(TestAdapter::new(
        "test",
        vec![ChatEvent::Mention { thread_id: "t1".into(), message: sample_message("@bot secret") }],
    ));

    bot.run().await.expect("run should succeed");
    assert_eq!(post_counter.load(Ordering::SeqCst), 1);
}

/// Full lifecycle: multiple event types processed in one run.
#[tokio::test]
async fn full_lifecycle_multiple_event_types() {
    let mention_count = Arc::new(AtomicUsize::new(0));
    let msg_count = Arc::new(AtomicUsize::new(0));
    let action_count = Arc::new(AtomicUsize::new(0));
    let reaction_count = Arc::new(AtomicUsize::new(0));
    let cmd_count = Arc::new(AtomicUsize::new(0));
    let submit_count = Arc::new(AtomicUsize::new(0));
    let close_count = Arc::new(AtomicUsize::new(0));

    let mn = Arc::clone(&mention_count);
    let ms = Arc::clone(&msg_count);
    let ac = Arc::clone(&action_count);
    let rc = Arc::clone(&reaction_count);
    let cc = Arc::clone(&cmd_count);
    let sc = Arc::clone(&submit_count);
    let cl = Arc::clone(&close_count);

    let mut bot = ChatBot::new(ChatBotConfig::default());

    bot.on_mention(move |_t, _m| {
        let mn = Arc::clone(&mn);
        async move {
            mn.fetch_add(1, Ordering::SeqCst);
        }
    });
    bot.on_message(None, move |_t, _m| {
        let ms = Arc::clone(&ms);
        async move {
            ms.fetch_add(1, Ordering::SeqCst);
        }
    });
    bot.on_action(None, move |_a, _t| {
        let ac = Arc::clone(&ac);
        async move {
            ac.fetch_add(1, Ordering::SeqCst);
        }
    });
    bot.on_reaction(None, move |_r| {
        let rc = Arc::clone(&rc);
        async move {
            rc.fetch_add(1, Ordering::SeqCst);
        }
    });
    bot.on_slash_command(None, move |_c| {
        let cc = Arc::clone(&cc);
        async move {
            cc.fetch_add(1, Ordering::SeqCst);
        }
    });
    bot.on_modal_submit(None, move |_s| {
        let sc = Arc::clone(&sc);
        async move {
            sc.fetch_add(1, Ordering::SeqCst);
        }
    });
    bot.on_modal_close(move |_c| {
        let cl = Arc::clone(&cl);
        async move {
            cl.fetch_add(1, Ordering::SeqCst);
        }
    });

    let thumbs_up = EmojiValue::from_well_known(WellKnownEmoji::ThumbsUp);

    bot.add_adapter(TestAdapter::new(
        "test",
        vec![
            ChatEvent::Mention { thread_id: "t1".into(), message: sample_message("@bot hello") },
            ChatEvent::Message { thread_id: "t1".into(), message: sample_message("plain message") },
            ChatEvent::Action(ActionEvent {
                action_id: "btn".into(),
                thread_id: "t1".into(),
                message_id: "m1".into(),
                user: sample_author(),
                value: None,
                trigger_id: None,
                adapter_name: "test".into(),
            }),
            ChatEvent::Reaction(ReactionEvent {
                thread_id: "t1".into(),
                message_id: "m1".into(),
                user: sample_author(),
                emoji: thumbs_up,
                added: true,
                adapter_name: "test".into(),
            }),
            ChatEvent::SlashCommand(SlashCommandEvent {
                command: "/test".into(),
                text: "".into(),
                channel_id: "c1".into(),
                user: sample_author(),
                trigger_id: None,
                adapter_name: "test".into(),
            }),
            ChatEvent::ModalSubmit(ModalSubmitEvent {
                callback_id: "fb".into(),
                view_id: "v1".into(),
                user: sample_author(),
                values: std::collections::HashMap::new(),
                private_metadata: None,
                adapter_name: "test".into(),
            }),
            ChatEvent::ModalClose(ModalCloseEvent {
                callback_id: "fb".into(),
                view_id: "v1".into(),
                user: sample_author(),
                adapter_name: "test".into(),
            }),
        ],
    ));

    bot.run().await.expect("run should succeed");

    assert_eq!(mention_count.load(Ordering::SeqCst), 1, "mention");
    assert_eq!(msg_count.load(Ordering::SeqCst), 1, "message");
    assert_eq!(action_count.load(Ordering::SeqCst), 1, "action");
    assert_eq!(reaction_count.load(Ordering::SeqCst), 1, "reaction");
    assert_eq!(cmd_count.load(Ordering::SeqCst), 1, "slash_command");
    assert_eq!(submit_count.load(Ordering::SeqCst), 1, "modal_submit");
    assert_eq!(close_count.load(Ordering::SeqCst), 1, "modal_close");
}