use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PrivacyMode {
#[default]
DmOnly,
AllowGroups,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TelegramConfig {
pub bot_username: String,
pub bot_token_keychain_account: String,
pub chat_id: i64,
#[serde(default)]
pub privacy_mode: PrivacyMode,
#[serde(default)]
pub allow_groups: Vec<i64>,
#[serde(default)]
pub e2e_disclosure_acked_at: Option<DateTime<Utc>>,
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::{TimeZone, Utc};
#[test]
fn round_trips_yaml() {
let cfg = TelegramConfig {
bot_username: "MyAgentBot".into(),
bot_token_keychain_account: "tg-bridge-1/telegram_bot_token".into(),
chat_id: 123456789,
privacy_mode: PrivacyMode::DmOnly,
allow_groups: vec![],
e2e_disclosure_acked_at: Some(Utc.with_ymd_and_hms(2026, 5, 4, 0, 0, 0).unwrap()),
};
let s = serde_yaml_ng::to_string(&cfg).unwrap();
let back: TelegramConfig = serde_yaml_ng::from_str(&s).unwrap();
assert_eq!(back, cfg);
}
#[test]
fn default_privacy_is_dm_only() {
let s = "bot_username: bot\nbot_token_keychain_account: a\nchat_id: 1\n";
let cfg: TelegramConfig = serde_yaml_ng::from_str(s).unwrap();
assert_eq!(cfg.privacy_mode, PrivacyMode::DmOnly);
}
#[test]
fn allow_groups_deserialize() {
let s = "bot_username: b\nbot_token_keychain_account: a\nchat_id: 1\nprivacy_mode: allow_groups\nallow_groups: [-1001, -1002]\n";
let cfg: TelegramConfig = serde_yaml_ng::from_str(s).unwrap();
assert_eq!(cfg.privacy_mode, PrivacyMode::AllowGroups);
assert_eq!(cfg.allow_groups, vec![-1001, -1002]);
}
#[test]
fn ack_chrono_parse() {
let s = "bot_username: b\nbot_token_keychain_account: a\nchat_id: 1\ne2e_disclosure_acked_at: 2026-05-04T00:00:00Z\n";
let cfg: TelegramConfig = serde_yaml_ng::from_str(s).unwrap();
assert!(cfg.e2e_disclosure_acked_at.is_some());
}
#[test]
fn missing_token_account_errors() {
let s = "bot_username: b\nchat_id: 1\n";
let r: Result<TelegramConfig, _> = serde_yaml_ng::from_str(s);
assert!(r.is_err());
}
}