tinychannels 0.1.0

Pluggable channel and messaging primitives for OpenHuman harness communication.
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
use super::*;
use crate::config::{
    ChannelsConfig, DingTalkConfig, DiscordConfig, EmailConfig, IMessageConfig, IrcConfig,
    LarkConfig, MatrixConfig, MattermostConfig, QQConfig, SignalConfig, SlackConfig,
    TelegramConfig, WhatsAppConfig, YuanbaoConfig,
};

#[test]
fn all_definitions_have_unique_ids() {
    let defs = all_channel_definitions();
    let mut ids: Vec<&str> = defs.iter().map(|d| d.id).collect();
    let len = ids.len();
    ids.sort();
    ids.dedup();
    assert_eq!(ids.len(), len, "duplicate channel definition ids found");
}

#[test]
fn every_definition_has_at_least_one_auth_mode() {
    for def in all_channel_definitions() {
        assert!(
            !def.auth_modes.is_empty(),
            "channel '{}' has no auth modes",
            def.id
        );
    }
}

#[test]
fn required_fields_have_non_empty_key_and_label() {
    for def in all_channel_definitions() {
        for spec in &def.auth_modes {
            for field in &spec.fields {
                if field.required {
                    assert!(
                        !field.key.is_empty(),
                        "empty key in {}.{:?}",
                        def.id,
                        spec.mode
                    );
                    assert!(
                        !field.label.is_empty(),
                        "empty label in {}.{:?}",
                        def.id,
                        spec.mode
                    );
                }
            }
        }
    }
}

#[test]
fn telegram_has_bot_token_and_managed_dm() {
    let def = find_channel_definition("telegram").expect("telegram not found");
    assert!(def.auth_mode_spec(ChannelAuthMode::BotToken).is_some());
    assert!(def.auth_mode_spec(ChannelAuthMode::ManagedDm).is_some());

    let bot = def.auth_mode_spec(ChannelAuthMode::BotToken).unwrap();
    assert!(
        bot.fields
            .iter()
            .any(|f| f.key == "bot_token" && f.required)
    );
    assert!(bot.auth_action.is_none());

    let managed = def.auth_mode_spec(ChannelAuthMode::ManagedDm).unwrap();
    assert_eq!(managed.auth_action, Some("telegram_managed_dm"));
    assert!(managed.fields.is_empty());
}

#[test]
fn channel_config_connected_covers_config_backed_modes() {
    let parsed_linq: ChannelsConfig = toml::from_str(
        r#"
[linq]
api_token = "linq-token"
from_phone = "+15550101"
"#,
    )
    .expect("linq channel config should parse");
    let config = ChannelsConfig {
        telegram: Some(TelegramConfig {
            bot_token: "telegram-token".into(),
            chat_id: None,
            allowed_users: vec![],
            stream_mode: Default::default(),
            draft_update_interval_ms: 1000,
            silent_streaming: true,
            mention_only: false,
        }),
        discord: Some(DiscordConfig {
            bot_token: "discord-token".into(),
            guild_id: None,
            channel_id: None,
            allowed_users: vec![],
            listen_to_bots: false,
            mention_only: false,
        }),
        slack: Some(SlackConfig {
            bot_token: "slack-token".into(),
            app_token: None,
            channel_id: None,
            allowed_users: vec![],
        }),
        mattermost: Some(MattermostConfig {
            url: "https://mattermost.example".into(),
            bot_token: "mattermost-token".into(),
            channel_id: None,
            allowed_users: vec![],
            thread_replies: None,
            mention_only: None,
        }),
        imessage: Some(IMessageConfig {
            allowed_contacts: vec![],
        }),
        matrix: Some(MatrixConfig {
            homeserver: "https://matrix.example".into(),
            access_token: "matrix-token".into(),
            user_id: None,
            device_id: None,
            room_id: "!room:matrix.example".into(),
            allowed_users: vec![],
        }),
        signal: Some(SignalConfig {
            http_url: "http://127.0.0.1:8080".into(),
            account: "+15550100".into(),
            group_id: None,
            allowed_from: vec![],
            ignore_attachments: false,
            ignore_stories: false,
        }),
        whatsapp: Some(WhatsAppConfig {
            access_token: Some("whatsapp-token".into()),
            phone_number_id: Some("phone-id".into()),
            verify_token: Some("verify".into()),
            app_secret: None,
            session_path: None,
            pair_phone: None,
            pair_code: None,
            allowed_numbers: vec![],
        }),
        linq: parsed_linq.linq,
        email: Some(EmailConfig {
            imap_host: "imap.example".into(),
            imap_port: 993,
            imap_folder: "INBOX".into(),
            smtp_host: "smtp.example".into(),
            smtp_port: 465,
            smtp_tls: true,
            username: "bot@example.com".into(),
            password: "email-password".into(),
            from_address: "bot@example.com".into(),
            idle_timeout_secs: 1740,
            allowed_senders: vec![],
        }),
        irc: Some(IrcConfig {
            server: "irc.example".into(),
            port: 6697,
            nickname: "openhuman".into(),
            username: None,
            channels: vec!["#ops".into()],
            allowed_users: vec![],
            server_password: None,
            nickserv_password: None,
            sasl_password: None,
            verify_tls: None,
        }),
        lark: Some(LarkConfig {
            app_id: "lark-app".into(),
            app_secret: "lark-secret".into(),
            encrypt_key: None,
            verification_token: None,
            allowed_users: vec![],
            use_feishu: false,
            receive_mode: Default::default(),
            port: None,
        }),
        dingtalk: Some(DingTalkConfig {
            client_id: "dingtalk-client".into(),
            client_secret: "dingtalk-secret".into(),
            allowed_users: vec![],
        }),
        qq: Some(QQConfig {
            app_id: "qq-app".into(),
            app_secret: "qq-secret".into(),
            allowed_users: vec![],
        }),
        yuanbao: Some(YuanbaoConfig::default()),
        ..Default::default()
    };

    for (channel, mode) in [
        ("telegram", ChannelAuthMode::BotToken),
        ("discord", ChannelAuthMode::BotToken),
        ("slack", ChannelAuthMode::BotToken),
        ("mattermost", ChannelAuthMode::BotToken),
        ("imessage", ChannelAuthMode::ManagedDm),
        ("matrix", ChannelAuthMode::ApiKey),
        ("signal", ChannelAuthMode::ApiKey),
        ("whatsapp", ChannelAuthMode::ApiKey),
        ("linq", ChannelAuthMode::ApiKey),
        ("email", ChannelAuthMode::ApiKey),
        ("irc", ChannelAuthMode::ApiKey),
        ("lark", ChannelAuthMode::ApiKey),
        ("dingtalk", ChannelAuthMode::ApiKey),
        ("qq", ChannelAuthMode::ApiKey),
        ("yuanbao", ChannelAuthMode::ApiKey),
    ] {
        assert!(
            channel_config_connected(&config, channel, mode),
            "{channel}/{mode:?} should be connected from config"
        );
    }

    assert!(!channel_config_connected(
        &config,
        "unknown",
        ChannelAuthMode::ApiKey
    ));
    assert!(!channel_config_connected(
        &config,
        "telegram",
        ChannelAuthMode::ManagedDm
    ));
}

#[test]
fn discord_has_bot_token_and_oauth() {
    let def = find_channel_definition("discord").expect("discord not found");
    assert!(def.auth_mode_spec(ChannelAuthMode::BotToken).is_some());
    assert!(def.auth_mode_spec(ChannelAuthMode::OAuth).is_some());

    let oauth = def.auth_mode_spec(ChannelAuthMode::OAuth).unwrap();
    assert_eq!(oauth.auth_action, Some("discord_oauth"));

    let managed = def.auth_mode_spec(ChannelAuthMode::ManagedDm);
    assert!(managed.is_some());
    assert_eq!(managed.unwrap().auth_action, Some("discord_managed_link"));
}

#[test]
fn discord_bot_token_exposes_allowed_users_field() {
    // Issue #3763: the Discord bot_token connect UI must offer an allowed_users
    // field (like Telegram) so a self-hosted bot's allowlist is set in-app
    // instead of by hand-editing config.toml.
    let def = find_channel_definition("discord").expect("discord not found");
    let bot_token = def
        .auth_mode_spec(ChannelAuthMode::BotToken)
        .expect("discord bot_token mode");
    let allowed = bot_token
        .fields
        .iter()
        .find(|f| f.key == "allowed_users")
        .expect("discord bot_token must expose an allowed_users field");
    assert!(
        !allowed.required,
        "allowed_users must be optional (blank = allow everyone)"
    );
}

#[test]
fn find_unknown_channel_returns_none() {
    assert!(find_channel_definition("nonexistent").is_none());
}

#[test]
fn validate_credentials_rejects_missing_required() {
    let def = find_channel_definition("telegram").unwrap();
    let empty = serde_json::Map::new();
    let result = def.validate_credentials(ChannelAuthMode::BotToken, &empty);
    assert!(result.is_err());
    assert!(result.unwrap_err().contains("bot_token"));
}

#[test]
fn validate_credentials_accepts_complete() {
    let def = find_channel_definition("telegram").unwrap();
    let mut creds = serde_json::Map::new();
    creds.insert(
        "bot_token".to_string(),
        serde_json::Value::String("123:abc".to_string()),
    );
    assert!(
        def.validate_credentials(ChannelAuthMode::BotToken, &creds)
            .is_ok()
    );
}

#[test]
fn validate_credentials_rejects_unsupported_mode() {
    let def = find_channel_definition("telegram").unwrap();
    let empty = serde_json::Map::new();
    let result = def.validate_credentials(ChannelAuthMode::OAuth, &empty);
    assert!(result.is_err());
    assert!(result.unwrap_err().contains("does not support"));
}

#[test]
fn serialization_produces_expected_structure() {
    let def = telegram_definition();
    let v = serde_json::to_value(&def).expect("serialize");
    let obj = v.as_object().expect("top-level object");
    assert_eq!(obj.get("id").and_then(|v| v.as_str()), Some("telegram"));
    assert_eq!(
        obj.get("display_name").and_then(|v| v.as_str()),
        Some("Telegram")
    );
    let modes = obj
        .get("auth_modes")
        .and_then(|v| v.as_array())
        .expect("auth_modes");
    assert_eq!(modes.len(), def.auth_modes.len());
    let caps = obj
        .get("capabilities")
        .and_then(|v| v.as_array())
        .expect("capabilities");
    assert_eq!(caps.len(), def.capabilities.len());
}

// -- #2048: Lark / Feishu + DingTalk channel definitions ------------------

#[test]
fn lark_definition_is_registered() {
    let def = find_channel_definition("lark").expect("lark definition not registered");
    assert_eq!(def.display_name, "Lark / Feishu");
    assert_eq!(def.icon, "lark");
}

#[test]
fn lark_uses_api_key_auth_with_app_id_and_secret_required() {
    let def = find_channel_definition("lark").expect("lark not found");
    let spec = def
        .auth_mode_spec(ChannelAuthMode::ApiKey)
        .expect("lark must support ApiKey auth mode");

    // The two non-negotiable fields are app_id + app_secret — every
    // Lark/Feishu open-platform app gets these from the developer console.
    let app_id = spec
        .fields
        .iter()
        .find(|f| f.key == "app_id")
        .expect("missing app_id field");
    assert!(app_id.required, "app_id must be required");
    assert_eq!(app_id.field_type, "string");

    let app_secret = spec
        .fields
        .iter()
        .find(|f| f.key == "app_secret")
        .expect("missing app_secret field");
    assert!(app_secret.required, "app_secret must be required");
    assert_eq!(
        app_secret.field_type, "secret",
        "app_secret must be a secret-typed field"
    );

    // Optional but supported: encrypt_key, verification_token, use_feishu,
    // receive_mode, port, allowed_users. Field names map 1:1 to `LarkConfig`
    // in `src/config.rs` — any rename on the
    // backend side will fail this assertion before the UI silently breaks.
    for key in [
        "encrypt_key",
        "verification_token",
        "use_feishu",
        "receive_mode",
        "port",
        "allowed_users",
    ] {
        let field = spec
            .fields
            .iter()
            .find(|f| f.key == key)
            .unwrap_or_else(|| panic!("lark spec missing optional field: {}", key));
        assert!(
            !field.required,
            "lark optional field {} must not be required",
            key
        );
    }

    let use_feishu = spec.fields.iter().find(|f| f.key == "use_feishu").unwrap();
    assert_eq!(use_feishu.field_type, "boolean");
}

#[test]
fn lark_validate_credentials_rejects_missing_app_secret() {
    let def = find_channel_definition("lark").expect("lark not found");
    let mut creds = serde_json::Map::new();
    creds.insert("app_id".into(), serde_json::Value::String("cli_xx".into()));
    // app_secret intentionally omitted.
    let err = def
        .validate_credentials(ChannelAuthMode::ApiKey, &creds)
        .expect_err("must reject when app_secret is missing");
    assert!(err.contains("app_secret"), "{err}");
}

#[test]
fn dingtalk_definition_is_registered() {
    let def = find_channel_definition("dingtalk").expect("dingtalk definition not registered");
    assert_eq!(def.display_name, "DingTalk (钉钉)");
    assert_eq!(def.icon, "dingtalk");
}

#[test]
fn dingtalk_requires_client_id_and_client_secret() {
    let def = find_channel_definition("dingtalk").expect("dingtalk not found");
    let spec = def
        .auth_mode_spec(ChannelAuthMode::ApiKey)
        .expect("dingtalk must support ApiKey auth mode");

    let client_id = spec
        .fields
        .iter()
        .find(|f| f.key == "client_id")
        .expect("missing client_id field");
    assert!(client_id.required);
    assert_eq!(client_id.field_type, "string");

    let client_secret = spec
        .fields
        .iter()
        .find(|f| f.key == "client_secret")
        .expect("missing client_secret field");
    assert!(client_secret.required);
    assert_eq!(
        client_secret.field_type, "secret",
        "client_secret must be a secret-typed field"
    );

    // DingTalkConfig in `src/config.rs` also
    // accepts `allowed_users` (Vec<String>, defaults to empty). Pin it
    // as an optional field here for the same reason we pin Lark's
    // optional set — schema renames blow up at test time, not in
    // production UI.
    let allowed_users = spec
        .fields
        .iter()
        .find(|f| f.key == "allowed_users")
        .expect("dingtalk spec missing optional allowed_users field");
    assert!(
        !allowed_users.required,
        "dingtalk allowed_users must not be required"
    );
    assert_eq!(allowed_users.field_type, "string");
}

#[test]
fn dingtalk_validate_credentials_rejects_missing_client_secret() {
    let def = find_channel_definition("dingtalk").expect("dingtalk not found");
    let mut creds = serde_json::Map::new();
    creds.insert(
        "client_id".into(),
        serde_json::Value::String("ding_xx".into()),
    );
    let err = def
        .validate_credentials(ChannelAuthMode::ApiKey, &creds)
        .expect_err("must reject when client_secret is missing");
    assert!(err.contains("client_secret"), "{err}");
}

#[test]
fn all_definitions_include_lark_and_dingtalk() {
    let ids: Vec<&str> = all_channel_definitions().iter().map(|d| d.id).collect();
    assert!(
        ids.contains(&"lark"),
        "lark missing from all_channel_definitions"
    );
    assert!(
        ids.contains(&"dingtalk"),
        "dingtalk missing from all_channel_definitions"
    );
}

#[test]
fn auth_mode_display_and_parse() {
    for mode in [
        ChannelAuthMode::ApiKey,
        ChannelAuthMode::BotToken,
        ChannelAuthMode::OAuth,
        ChannelAuthMode::ManagedDm,
    ] {
        let s = mode.to_string();
        let parsed: ChannelAuthMode = s.parse().expect("parse failed");
        assert_eq!(parsed, mode);
    }
}

#[test]
fn auth_mode_serializes_to_expected_wire_values() {
    assert_eq!(
        serde_json::to_value(ChannelAuthMode::ApiKey).expect("serialize"),
        serde_json::Value::String("api_key".to_string())
    );
    assert_eq!(
        serde_json::from_value::<ChannelAuthMode>(serde_json::Value::String("api_key".to_string()))
            .expect("deserialize"),
        ChannelAuthMode::ApiKey
    );
    assert_eq!(
        serde_json::to_value(ChannelAuthMode::BotToken).expect("serialize"),
        serde_json::Value::String("bot_token".to_string())
    );
    assert_eq!(
        serde_json::from_value::<ChannelAuthMode>(serde_json::Value::String(
            "bot_token".to_string()
        ))
        .expect("deserialize"),
        ChannelAuthMode::BotToken
    );
    assert_eq!(
        serde_json::to_value(ChannelAuthMode::OAuth).expect("serialize"),
        serde_json::Value::String("oauth".to_string())
    );
    assert_eq!(
        serde_json::from_value::<ChannelAuthMode>(serde_json::Value::String("oauth".to_string()))
            .expect("deserialize"),
        ChannelAuthMode::OAuth
    );
    assert_eq!(
        serde_json::to_value(ChannelAuthMode::ManagedDm).expect("serialize"),
        serde_json::Value::String("managed_dm".to_string())
    );
    assert_eq!(
        serde_json::from_value::<ChannelAuthMode>(serde_json::Value::String(
            "managed_dm".to_string()
        ))
        .expect("deserialize"),
        ChannelAuthMode::ManagedDm
    );
}

#[test]
fn email_definition_is_registered() {
    let def = find_channel_definition("email").expect("email channel not registered");
    assert_eq!(def.display_name, "Email (IMAP/SMTP)");
    assert!(def.capabilities.contains(&ChannelCapability::SendText));
    assert!(def.capabilities.contains(&ChannelCapability::ReceiveText));
}

#[test]
fn email_definition_field_shape_matches_email_config() {
    let def = find_channel_definition("email").expect("email not found");
    let spec = def
        .auth_mode_spec(ChannelAuthMode::ApiKey)
        .expect("email must expose an api_key auth mode");

    // Required fields — these gate a usable IMAP/SMTP connection.
    for key in ["imap_host", "username", "password", "smtp_host"] {
        let field = spec
            .fields
            .iter()
            .find(|f| f.key == key)
            .unwrap_or_else(|| panic!("email spec missing required field: {key}"));
        assert!(field.required, "email field {key} must be required");
    }
    // Password must be secret-typed so the UI masks it.
    let password = spec.fields.iter().find(|f| f.key == "password").unwrap();
    assert_eq!(password.field_type, "secret");

    // Optional fields — keys map 1:1 to `EmailConfig` in
    // the Email provider implementation. A rename there must
    // fail this assertion before the UI silently stops persisting the field.
    for key in [
        "imap_port",
        "smtp_port",
        "smtp_tls",
        "from_address",
        "imap_folder",
        "allowed_senders",
    ] {
        let field = spec
            .fields
            .iter()
            .find(|f| f.key == key)
            .unwrap_or_else(|| panic!("email spec missing optional field: {key}"));
        assert!(
            !field.required,
            "email optional field {key} must not be required"
        );
    }
    let smtp_tls = spec.fields.iter().find(|f| f.key == "smtp_tls").unwrap();
    assert_eq!(smtp_tls.field_type, "boolean");
    // Default-on so the UI checkbox is pre-checked and a fresh connect keeps TLS
    // rather than silently reverting the default when the box is left untouched.
    assert_eq!(smtp_tls.default_bool, Some(true));
}

#[test]
fn email_validate_credentials_rejects_missing_password() {
    let def = find_channel_definition("email").expect("email not found");
    let mut creds = serde_json::Map::new();
    creds.insert(
        "imap_host".into(),
        serde_json::Value::String("imap.x.com".into()),
    );
    creds.insert(
        "username".into(),
        serde_json::Value::String("u@x.com".into()),
    );
    creds.insert(
        "smtp_host".into(),
        serde_json::Value::String("smtp.x.com".into()),
    );
    // password intentionally omitted.
    let err = def
        .validate_credentials(ChannelAuthMode::ApiKey, &creds)
        .expect_err("must reject when password missing");
    assert!(err.contains("password"), "{err}");
}