use crate::channels::telegram::rich::api::{build_body, build_body_blocks, build_body_html};
use crate::channels::telegram::rich::{contains_task_list, has_rich_structure};
use teloxide::types::{MessageId, ThreadId};
#[test]
fn task_list_asterisk_prefix_detected() {
assert!(contains_task_list("* [ ] star task\n* [x] done"));
}
#[test]
fn task_list_plus_prefix_detected() {
assert!(contains_task_list("+ [ ] plus task"));
}
#[test]
fn task_list_mixed_prefixes() {
assert!(contains_task_list("- [ ] dash\n* [x] star\n+ [ ] plus"));
}
#[test]
fn task_list_indented_detected() {
assert!(contains_task_list(" - [ ] indented"));
}
#[test]
fn rich_structure_ordered_list_detected() {
assert!(has_rich_structure("1. first\n2. second\n3. third"));
}
#[test]
fn rich_structure_empty_text_false() {
assert!(!has_rich_structure(""));
}
#[test]
fn rich_structure_whitespace_only_false() {
assert!(!has_rich_structure(" \n \n "));
}
#[test]
fn rich_request_body_with_thread_id() {
let body = build_body(99, Some(ThreadId(MessageId(42))), "| A |\n| - |\n| 1 |");
assert_eq!(body["chat_id"], 99);
assert_eq!(body["message_thread_id"], 42);
assert_eq!(body["rich_message"]["markdown"], "| A |\n| - |\n| 1 |");
}
#[test]
fn rich_request_body_without_thread_id() {
let body = build_body(100, None, "hello");
assert_eq!(body["chat_id"], 100);
assert!(body.get("message_thread_id").is_none());
assert_eq!(body["rich_message"]["markdown"], "hello");
}
#[test]
fn rich_html_body_uses_html_key() {
let body = build_body_html(100, None, "<details><summary>x</summary>y</details>");
assert_eq!(body["chat_id"], 100);
assert!(body.get("message_thread_id").is_none());
assert!(body["rich_message"].get("markdown").is_none());
assert_eq!(
body["rich_message"]["html"],
"<details><summary>x</summary>y</details>"
);
}
#[test]
fn rich_html_body_with_thread_id() {
let body = build_body_html(99, Some(ThreadId(MessageId(42))), "<b>hi</b>");
assert_eq!(body["message_thread_id"], 42);
assert_eq!(body["rich_message"]["html"], "<b>hi</b>");
}
#[test]
fn rich_block_body_passes_rich_message_through() {
let rich = serde_json::json!({
"blocks": [
{"type": "table", "align": ["left"], "header": [[{"type": "text", "text": "A"}]], "rows": []},
{"type": "code", "language": "rust", "text": "fn main() {}"}
]
});
let body = build_body_blocks(100, None, &rich);
assert_eq!(body["chat_id"], 100);
assert!(body.get("message_thread_id").is_none());
assert!(body["rich_message"].get("markdown").is_none());
assert!(body["rich_message"].get("html").is_none());
assert_eq!(body["rich_message"]["blocks"][1]["type"], "code");
assert_eq!(body["rich_message"]["blocks"][1]["text"], "fn main() {}");
}
#[test]
fn rich_block_body_with_thread_id() {
let rich = serde_json::json!({ "blocks": [] });
let body = build_body_blocks(99, Some(ThreadId(MessageId(42))), &rich);
assert_eq!(body["message_thread_id"], 42);
}
#[test]
fn markdown_to_rich_blocks_serializes_table_and_fence() {
let v = crate::channels::telegram::rich::markdown_to_rich_blocks(
"| A | B |\n| - | - |\n| 1 | 2 |\n\n```rust\nfn x() {}\n```",
);
let blocks = v["blocks"].as_array().expect("blocks");
assert!(blocks.iter().any(|b| b["type"] == "table"));
assert!(
blocks
.iter()
.any(|b| b["type"] == "code" && b["text"] == "fn x() {}")
);
}