#![allow(dead_code)]
use rustigram_api::{BotClient, ClientConfig};
use rustigram_bot::context::Context;
use rustigram_bot::error::BotError;
use rustigram_bot::handler::{handler_fn, BoxHandler};
use rustigram_types::chat::{Chat, ChatType};
use rustigram_types::message::Message;
use rustigram_types::update::{Update, UpdateKind};
use rustigram_types::user::User;
use std::time::Duration;
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
pub fn client_for(base_url: &str) -> BotClient {
let config = ClientConfig::new("123456:test-token-for-dispatch-tests")
.expect("the test token is well-formed")
.api_base_url(base_url.to_owned());
BotClient::new(config).expect("client builds")
}
pub fn client_without_retries(base_url: &str) -> BotClient {
let config = ClientConfig::new("123456:test-token-for-dispatch-tests")
.expect("the test token is well-formed")
.api_base_url(base_url.to_owned())
.max_retries(0);
BotClient::new(config).expect("client builds")
}
pub fn client() -> BotClient {
client_for("http://127.0.0.1:1")
}
pub fn message_update(update_id: i64, text: &str) -> Update {
Update {
update_id,
kind: UpdateKind::Message(message(text)),
}
}
pub fn user() -> User {
let mut user = User::default();
user.id = 7;
user.first_name = "Test".into();
user
}
pub fn chat(kind: ChatType) -> Chat {
let mut chat = Chat::default();
chat.id = 42;
chat.kind = kind;
chat
}
pub fn message(text: &str) -> Message {
let mut message = Message::default();
message.message_id = 1;
message.date = 1_700_000_000;
message.chat = chat(ChatType::Private);
message.from = Some(user());
message.text = Some(text.to_owned());
message
}
pub fn command_update(command: &str) -> Update {
use rustigram_types::message::{MessageEntity, MessageEntityKind};
let text = format!("/{command}");
let length = text.len() as u32;
let mut update = text_update(&text);
if let UpdateKind::Message(ref mut msg) = update.kind {
msg.entities = Some(vec![MessageEntity {
kind: MessageEntityKind::BotCommand,
offset: 0,
length,
url: None,
user: None,
language: None,
custom_emoji_id: None,
unix_time: None,
date_time_format: None,
}]);
}
update
}
pub fn text_update(text: &str) -> Update {
Update {
update_id: 1,
kind: UpdateKind::Message(message(text)),
}
}
pub type Reports = (
UnboundedSender<&'static str>,
UnboundedReceiver<&'static str>,
);
pub fn reports() -> Reports {
unbounded_channel()
}
pub fn reporting(tx: &UnboundedSender<&'static str>, name: &'static str) -> BoxHandler {
let tx = tx.clone();
handler_fn(move |_ctx: Context| {
let tx = tx.clone();
async move {
let _ = tx.send(name);
Ok(())
}
})
}
pub fn failing(tx: &UnboundedSender<&'static str>, name: &'static str) -> BoxHandler {
let tx = tx.clone();
handler_fn(move |_ctx: Context| {
let tx = tx.clone();
async move {
let _ = tx.send(name);
Err(BotError::Handler(anyhow::anyhow!(
"handler failed on purpose"
)))
}
})
}
pub async fn next_report(rx: &mut UnboundedReceiver<&'static str>) -> &'static str {
tokio::time::timeout(Duration::from_secs(5), rx.recv())
.await
.expect("a handler should have reported within five seconds")
.expect("the report channel is still open")
}
pub async fn assert_no_further_reports(rx: &mut UnboundedReceiver<&'static str>) {
tokio::time::sleep(Duration::from_millis(50)).await;
if let Ok(extra) = rx.try_recv() {
panic!("`{extra}` also ran, but only one handler should have");
}
}