71 lines
2.2 KiB
Rust
71 lines
2.2 KiB
Rust
use telefind_bot::{handle, Reply};
|
|
use telefind_core::{Document, DocumentId, MemoryIndex, PublicName, SearchIndex};
|
|
|
|
#[test]
|
|
fn plain_and_addressed_search_return_public_post_link() {
|
|
let mut index = MemoryIndex::default();
|
|
index
|
|
.upsert(Document {
|
|
id: DocumentId::Post {
|
|
chat: 9,
|
|
message: 4,
|
|
},
|
|
username: PublicName::new("techfeed").unwrap(),
|
|
title: "".into(),
|
|
body: "Rust 教程".into(),
|
|
media: None,
|
|
})
|
|
.unwrap();
|
|
for text in ["rust", "/search@MyBot RUST", "/search 教程"] {
|
|
let Reply::Results { total, links } = handle(&index, "mybot", text).unwrap() else {
|
|
panic!("expected results")
|
|
};
|
|
assert_eq!(total, 1);
|
|
assert_eq!(links[0].url, "https://t.me/techfeed/4");
|
|
assert_eq!(links[0].label, "帖子 · Rust 教程");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn missing_queries_and_foreign_commands_are_distinct() {
|
|
let index = MemoryIndex::default();
|
|
for text in ["/start", "/search@me ", "/help"] {
|
|
assert_eq!(handle(&index, "me", text).unwrap(), Reply::Help);
|
|
}
|
|
for text in [" ", "/search@another x", "/status"] {
|
|
assert_eq!(handle(&index, "me", text).unwrap(), Reply::Ignored);
|
|
}
|
|
assert_eq!(
|
|
handle(&index, "me", "中文").unwrap(),
|
|
Reply::Results {
|
|
total: 0,
|
|
links: vec![]
|
|
}
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn paging_is_stateless_and_rejects_invalid_numbers() {
|
|
let mut index = MemoryIndex::default();
|
|
for message in 1..=11 {
|
|
index
|
|
.upsert(Document {
|
|
id: DocumentId::Post { chat: 1, message },
|
|
username: PublicName::new("books").unwrap(),
|
|
title: "book".into(),
|
|
body: String::new(),
|
|
media: None,
|
|
})
|
|
.unwrap();
|
|
}
|
|
let Reply::Results { total, links } = handle(&index, "me", "/page@me 2 book").unwrap() else {
|
|
panic!("expected page")
|
|
};
|
|
assert_eq!(total, 11);
|
|
assert_eq!(links.len(), 1);
|
|
assert_eq!(links[0].url, "https://t.me/books/11");
|
|
for text in ["/page 0 book", "/page x book", "/page 2"] {
|
|
assert_eq!(handle(&index, "me", text).unwrap(), Reply::Help);
|
|
}
|
|
}
|