use crate::brain::tools::telegram_send::{landing_echo, resolve_thread_id};
use crate::channels::telegram::TelegramState;
use serde_json::json;
use teloxide::types::{MessageId, ThreadId};
use uuid::Uuid;
#[tokio::test]
async fn session_origin_topic_applies_to_same_origin_chat() {
let state = TelegramState::new();
let session_id = Uuid::from_u128(0xABCD);
state
.register_session_chat(session_id, 12345, Some(42))
.await;
let input = json!({});
let result = resolve_thread_id(&input, 12345, session_id, &state).await;
assert_eq!(result, Some(ThreadId(MessageId(42))));
}
#[tokio::test]
async fn session_origin_topic_does_not_leak_into_other_chat() {
let state = TelegramState::new();
let session_id = Uuid::from_u128(0xABCD);
state
.register_session_chat(session_id, 12345, Some(42))
.await;
let input = json!({});
let result = resolve_thread_id(&input, -100999, session_id, &state).await;
assert_eq!(
result, None,
"origin topic 42 must not leak into chat -100999"
);
}
#[tokio::test]
async fn cross_chat_send_with_explicit_thread_id_still_honours_override() {
let state = TelegramState::new();
let session_id = Uuid::from_u128(0xABCD);
state
.register_session_chat(session_id, 12345, Some(42))
.await;
let input = json!({ "thread_id": 7 });
let result = resolve_thread_id(&input, -100999, session_id, &state).await;
assert_eq!(result, Some(ThreadId(MessageId(7))));
}
#[tokio::test]
async fn session_bound_to_general_other_chat_falls_through_to_lookup() {
let state = TelegramState::new();
let session_id = Uuid::from_u128(0xABCD);
state.register_session_chat(session_id, 12345, None).await;
let input = json!({});
let result = resolve_thread_id(&input, -100999, session_id, &state).await;
assert_eq!(result, None);
}
#[tokio::test]
async fn session_bound_to_general_same_chat_is_known_no_thread() {
let state = TelegramState::new();
let session_id = Uuid::from_u128(0xABCD);
state.register_session_chat(session_id, 12345, None).await;
let input = json!({});
let result = resolve_thread_id(&input, 12345, session_id, &state).await;
assert_eq!(result, None);
}
#[tokio::test]
async fn landing_echo_names_chat_and_no_topic_without_thread() {
let echo = landing_echo(-100123, None).await;
assert!(
echo.contains("chat -100123"),
"echo must name the landing chat: {echo}"
);
assert!(
echo.contains("no topic"),
"unthreaded landing must say so explicitly: {echo}"
);
}
#[tokio::test]
async fn landing_echo_degrades_to_numeric_topic_without_db() {
let echo = landing_echo(-100123, Some(ThreadId(MessageId(42)))).await;
assert!(
echo.contains("chat -100123"),
"echo must name the landing chat: {echo}"
);
assert!(
echo.contains("topic 42"),
"echo must carry the numeric topic: {echo}"
);
}