135 lines
4.1 KiB
Rust
135 lines
4.1 KiB
Rust
use bot::{handle, Reply};
|
|
use search_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);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn type_filter_is_available_in_search_and_page_commands() {
|
|
let mut index = MemoryIndex::default();
|
|
for (id, media) in [(1, None), (2, Some(search_core::Media::Video))] {
|
|
index
|
|
.upsert(Document {
|
|
id: DocumentId::Post {
|
|
chat: 1,
|
|
message: id,
|
|
},
|
|
username: PublicName::new("demo_feed").unwrap(),
|
|
title: "教程".into(),
|
|
body: String::new(),
|
|
media,
|
|
})
|
|
.unwrap();
|
|
}
|
|
for query in [
|
|
"type:video 教程",
|
|
"/search type:video 教程",
|
|
"/page 1 type:video 教程",
|
|
] {
|
|
let Reply::Results { links, total } = handle(&index, "me", query).unwrap() else {
|
|
panic!("expected results")
|
|
};
|
|
assert_eq!(total, 1);
|
|
assert_eq!(links[0].url, "https://t.me/demo_feed/2");
|
|
}
|
|
assert_eq!(
|
|
handle(&index, "me", "/search type:unknown 教程").unwrap(),
|
|
Reply::Help
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn scoped_media_search_keeps_quoted_phrase_and_original_link() {
|
|
let mut index = MemoryIndex::default();
|
|
for (message, body) in [
|
|
(1, "Rust search"),
|
|
(2, "Rust search SALE"),
|
|
(3, "search Rust"),
|
|
] {
|
|
index
|
|
.upsert(Document {
|
|
id: DocumentId::Post { chat: 3, message },
|
|
username: PublicName::new("my_feed").unwrap(),
|
|
title: String::new(),
|
|
body: body.into(),
|
|
media: Some(search_core::Media::Video),
|
|
})
|
|
.unwrap();
|
|
}
|
|
let Reply::Results { total, links } = handle(
|
|
&index,
|
|
"me",
|
|
r#"/page@me 1 type:video from:@My_Feed exclude:sale "rust search""#,
|
|
)
|
|
.unwrap() else {
|
|
panic!("expected a search result")
|
|
};
|
|
assert_eq!(total, 1);
|
|
assert_eq!(links[0].url, "https://t.me/my_feed/1");
|
|
}
|