use crate::channels::commands::handle_respond_to;
use crate::config::profile::{home_for_profile, with_profile_home_async};
use crate::config::{Config, RespondTo};
fn write_profile_home(home: &std::path::Path, config_toml: &str) {
std::fs::create_dir_all(home).expect("create profile home");
std::fs::write(home.join("config.toml"), config_toml).expect("write config");
}
#[cfg(unix)]
#[tokio::test]
async fn respond_to_in_group_persists_and_survives_reload() {
let profile = format!("test_respond_group_{}", uuid::Uuid::new_v4());
let home = home_for_profile(Some(&profile));
write_profile_home(
&home,
"[channels.telegram]\nenabled = true\nrespond_to = \"mention\"\n",
);
with_profile_home_async(Some(&profile), async {
let chat_id = "-1001234567890";
let reply = handle_respond_to("all", Some(chat_id)).await;
assert!(
reply.starts_with("✅"),
"expected a success reply, got: {reply}"
);
let raw = std::fs::read_to_string(home.join("config.toml")).unwrap();
assert!(
raw.contains("groups") && raw.contains(chat_id),
"config.toml is missing the groups override:\n{raw}"
);
let cfg = Config::load().expect("config must reload after the group write");
let group = cfg
.channels
.telegram
.groups
.get(chat_id)
.expect("groups entry must persist across reload");
assert!(matches!(group.respond_to, Some(RespondTo::All)));
assert!(matches!(
cfg.channels.telegram.respond_to,
RespondTo::Mention
));
})
.await;
}
#[cfg(unix)]
#[tokio::test]
async fn respond_to_in_dm_writes_channel_level() {
let profile = format!("test_respond_dm_{}", uuid::Uuid::new_v4());
let home = home_for_profile(Some(&profile));
write_profile_home(
&home,
"[channels.telegram]\nenabled = true\nrespond_to = \"mention\"\n",
);
with_profile_home_async(Some(&profile), async {
let reply = handle_respond_to("all", None).await;
assert!(
reply.starts_with("✅"),
"expected a success reply, got: {reply}"
);
let cfg = Config::load().expect("config must reload");
assert!(matches!(cfg.channels.telegram.respond_to, RespondTo::All));
assert!(cfg.channels.telegram.groups.is_empty());
})
.await;
}
#[cfg(unix)]
#[tokio::test]
async fn respond_to_in_group_same_as_global_still_creates_override() {
let profile = format!("test_respond_same_{}", uuid::Uuid::new_v4());
let home = home_for_profile(Some(&profile));
write_profile_home(
&home,
"[channels.telegram]\nenabled = true\nrespond_to = \"mention\"\n",
);
with_profile_home_async(Some(&profile), async {
let chat_id = "-5324478558";
let reply = handle_respond_to("mention", Some(chat_id)).await;
assert!(
reply.starts_with("✅"),
"expected success (not 'already in'), got: {reply}"
);
let raw = std::fs::read_to_string(home.join("config.toml")).unwrap();
assert!(
raw.contains("groups") && raw.contains(chat_id),
"config.toml is missing the groups override when value == global:\n{raw}"
);
let cfg = Config::load().expect("config must reload after group write");
let group = cfg
.channels
.telegram
.groups
.get(chat_id)
.expect("groups entry must exist even when value matches global");
assert!(matches!(group.respond_to, Some(RespondTo::Mention)));
assert!(matches!(
cfg.channels.telegram.respond_to,
RespondTo::Mention
));
})
.await;
}