1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
//! Mock bot that sends requests to the fake server
use std::{
env,
fmt::Debug,
hash::Hash,
mem::discriminant,
panic,
sync::{atomic::AtomicI32, Arc, Mutex, MutexGuard, PoisonError},
};
use gag::Gag;
use lazy_static::lazy_static;
use teloxide::{
dispatching::{
dialogue::{ErasedStorage, GetChatId, InMemStorage, Storage},
UpdateHandler,
},
error_handlers::ErrorHandler,
prelude::*,
types::{MaybeInaccessibleMessage, Me, UpdateKind},
};
// Needed for trait bound stuff
pub use crate::utils::DistributionKey;
use crate::{
dataset::{IntoUpdate, MockMe},
listener::InsertingListener,
server,
server::ServerManager,
state::State,
utils::{assert_eqn, default_distribution_function, find_chat_id},
};
lazy_static! {
static ref BOT_LOCK: Mutex<()> = Mutex::new(());
}
const DEFAULT_STACK_SIZE: usize = 8 * 1024 * 1024;
/// A mocked bot that sends requests to the fake server
/// Please check the [`new`] function docs and [github examples](https://github.com/LasterAlex/teloxide_tests/tree/master/examples) for more information.
///
/// If you are having troubles with generics while trying to store `MockBot`, just do this:
///
/// `MockBot<Box<dyn std::error::Error + Send + Sync>, teloxide_tests::mock_bot::DistributionKey>`
///
/// [`new`]: crate::MockBot::new
pub struct MockBot<Err, Key> {
/// The bot with a fake server url
pub bot: Bot,
/// The thing that dptree::entry() returns
pub handler_tree: UpdateHandler<Err>,
/// Updates to send as user
pub updates: Vec<Update>,
/// Bot parameters are here
pub me: Me,
/// If you have something like a state, you should add the storage here using .dependencies()
pub dependencies: DependencyMap,
/// The stack size of the runtime for running updates
pub stack_size: usize,
distribution_f: fn(&Update) -> Option<Key>,
error_handler: Arc<dyn ErrorHandler<Err> + Send + Sync>,
current_update_id: AtomicI32,
state: Arc<Mutex<State>>,
_bot_lock: Option<MutexGuard<'static, ()>>,
}
impl<Err> MockBot<Err, DistributionKey>
where
Err: Debug + Send + Sync + 'static,
{
/// Creates a new MockBot, using something that can be turned into Updates, and a handler tree.
/// You can't create a new bot while you have another bot in scope. Otherwise you will have a
/// lot of race conditions. If you still somehow manage to create two bots at the same time
/// (idk how),
/// please look into [this crate for serial testing](https://crates.io/crates/serial_test)
///
/// The `update` is just any Mock type, like `MockMessageText` or `MockCallbackQuery` or
/// `vec![MockMessagePhoto]` if you want! All updates will be sent consecutively and asynchronously.
/// The `handler_tree` is the same as in `dptree::entry()`, you will need to make your handler
/// tree into a separate function, like this:
/// ```no_run
/// use teloxide::dispatching::UpdateHandler;
/// fn handler_tree() -> UpdateHandler<Box<dyn std::error::Error + Send + Sync + 'static>> {
/// teloxide::dptree::entry() /* your handlers go here */
/// }
/// ```
///
/// # Full example
///
/// ```no_run
/// use teloxide::dispatching::UpdateHandler;
/// use teloxide::types::Update;
/// use teloxide_tests::{MockBot, MockMessageText};
/// use teloxide::dispatching::dialogue::GetChatId;
/// use teloxide::prelude::*;
///
/// fn handler_tree() -> UpdateHandler<Box<dyn std::error::Error + Send + Sync + 'static>> {
/// teloxide::dptree::entry().endpoint(|update: Update, bot: Bot| async move {
/// bot.send_message(update.chat_id().unwrap(), "Hello!").await?;
/// Ok(())
/// })
/// }
///
/// #[tokio::main] // Change for tokio::test in your implementation
/// async fn main() {
/// let mut bot = MockBot::new(MockMessageText::new().text("Hi!"), handler_tree());
/// bot.dispatch().await;
/// let responses = bot.get_responses();
/// let message = responses
/// .sent_messages
/// .last()
/// .expect("No sent messages were detected!");
/// assert_eq!(message.text(), Some("Hello!"));
/// }
/// ```
///
pub fn new<T>(
update: T, // This 'T' is just anything that can be turned into an Update, like a
// MockMessageText or MockCallbackQuery, or a vec[MockMessagePhoto] if you want!
handler_tree: UpdateHandler<Err>,
) -> Self
where
T: IntoUpdate, // And that code just "proves" that it can be turned into an update
Err: Debug,
{
let _ = pretty_env_logger::try_init();
let token = "1234567890:QWERTYUIOPASDFGHJKLZXCVBNMQWERTYUIO";
let bot = Bot::new(token);
let current_update_id = AtomicI32::new(42);
let state = Arc::new(Mutex::new(State::default()));
// If the lock is poisoned, we don't care, some other bot panicked and can't do anything
let lock = Some(BOT_LOCK.lock().unwrap_or_else(PoisonError::into_inner));
Self {
bot,
me: MockMe::new().build(),
updates: update.into_update(¤t_update_id),
handler_tree,
dependencies: DependencyMap::new(),
stack_size: DEFAULT_STACK_SIZE,
error_handler: LoggingErrorHandler::new(),
distribution_f: default_distribution_function,
_bot_lock: lock,
current_update_id,
state,
}
}
}
// Trait bound things.
impl<Err, Key> MockBot<Err, Key>
where
Err: Debug + Send + Sync + 'static,
Key: Hash + Eq + Clone + Send + 'static,
{
/// Same as [`new`], but it inserts a distribution_function into the dispatcher
///
/// [`new`]: crate::MockBot::new
// It is its own function instead of `.distribution_function` setter because of the Key
// generic. If `new` sets the Key to DefaultKey, it's impossible to swich back to a different
// one, even if it fits all the trait bounds.
pub fn new_with_distribution_function<T>(
update: T,
handler_tree: UpdateHandler<Err>,
f: fn(&Update) -> Option<Key>,
) -> Self
where
T: IntoUpdate,
Err: Debug,
{
// Again, trait bounds stuff, the generic Key is hard to work around
let MockBot {
bot,
me,
updates,
handler_tree,
dependencies,
stack_size,
error_handler,
distribution_f: _,
_bot_lock,
current_update_id,
state,
} = MockBot::new(update, handler_tree);
Self {
bot,
me,
updates,
handler_tree,
dependencies,
stack_size,
error_handler,
distribution_f: f,
_bot_lock,
current_update_id,
state,
}
}
/// Sets the dependencies of the dptree. The same as deps![] in bot dispatching.
/// Just like in this teloxide example: <https://github.com/teloxide/teloxide/blob/master/crates/teloxide/examples/dialogue.rs>
/// You can use it to add dependencies to your handler tree.
/// For more examples - look into `get_state` method documentation
pub fn dependencies(&mut self, deps: DependencyMap) {
self.dependencies = deps;
}
/// Sets the bot parameters, like supports_inline_queries, first_name, etc.
pub fn me(&mut self, me: MockMe) {
self.me = me.build();
}
/// Sets the updates. Useful for reusing the same mocked bot instance in different tests
/// Reminder: You can pass in `vec![MockMessagePhoto]` or something else!
pub fn update<T: IntoUpdate>(&mut self, update: T) {
self.updates = update.into_update(&self.current_update_id);
}
/// Sets the error_handler for Dispather
pub fn error_handler(&mut self, handler: Arc<dyn ErrorHandler<Err> + Send + Sync>) {
self.error_handler = handler;
}
/// Just inserts the updates into the state, returning them
fn insert_updates(&self, updates: &mut [Update]) {
for update in updates.iter_mut() {
match update.kind.clone() {
UpdateKind::Message(mut message) => {
// Add the message to the list of messages, so the bot can interact with it
self.state.lock().unwrap().add_message(&mut message);
update.kind = UpdateKind::Message(message.clone());
}
UpdateKind::EditedMessage(mut message) => {
self.state.lock().unwrap().edit_message(&mut message);
update.kind = UpdateKind::EditedMessage(message.clone());
}
UpdateKind::CallbackQuery(mut callback) => {
if let Some(MaybeInaccessibleMessage::Regular(ref mut message)) =
callback.message
{
self.state.lock().unwrap().add_message(message);
}
update.kind = UpdateKind::CallbackQuery(callback.clone());
}
_ => {}
}
}
}
async fn run_updates(&self, bot: Bot, updates: Vec<Update>) {
let handler_tree = self.handler_tree.clone();
let deps = self.dependencies.clone();
let stack_size = self.stack_size;
let distribution_f = self.distribution_f.clone();
let error_handler = self.error_handler.clone();
tokio::task::spawn_blocking(move || {
let runtime = tokio::runtime::Builder::new_multi_thread()
.thread_stack_size(stack_size) // Not needed, but just in case
.enable_all()
.build()
.unwrap();
runtime.block_on(async {
Dispatcher::builder(bot.clone(), handler_tree.clone())
.dependencies(deps)
.distribution_function(distribution_f)
.error_handler(error_handler)
.build()
.dispatch_with_listener(
InsertingListener { updates },
LoggingErrorHandler::new(),
)
.await;
});
})
.await
.expect("Dispatcher panicked!");
}
/// Actually dispatches the bot, calling the update through the handler tree.
/// All the requests made through the bot will be stored in `responses`, and can be retrieved
/// with `get_responses`. All the responses are unique to that dispatch, and will be erased for
/// every new dispatch.
///
/// This method overrides env variables `TELOXIDE_TOKEN` and `TELOXIDE_API_URL`, so anyone can
/// call `Bot::from_env()` and get an actual bot that is connected to the fake server
pub async fn dispatch(&mut self) {
self.state.lock().unwrap().reset();
let server = ServerManager::start(self.me.clone(), self.state.clone())
.await
.unwrap();
let mut updates = self.updates.clone();
self.insert_updates(&mut updates);
let api_url = reqwest::Url::parse(&format!("http://127.0.0.1:{}", server.port)).unwrap();
let bot = self.bot.clone().set_api_url(api_url.clone());
env::set_var("TELOXIDE_TOKEN", bot.token());
env::set_var("TELOXIDE_API_URL", api_url.to_string());
self.run_updates(bot, updates).await;
server.stop().await.unwrap();
}
/// Returns the responses stored in `responses`
/// Should be treated as a variable, because it kinda is
pub fn get_responses(&self) -> server::Responses {
self.state.lock().unwrap().responses.clone()
}
async fn get_potential_storages<S>(
&self,
) -> (
Option<Arc<Arc<InMemStorage<S>>>>,
Option<Arc<Arc<ErasedStorage<S>>>>,
)
where
S: Send + 'static + Clone,
{
let default_panic = panic::take_hook();
let in_mem_storage: Option<Arc<Arc<InMemStorage<S>>>>;
let erased_storage: Option<Arc<Arc<ErasedStorage<S>>>>;
// No trace storage cuz who uses it
let dependencies = Arc::new(self.dependencies.clone());
// Get dependencies into Arc cuz otherwise it complaints about &self being moved
panic::set_hook(Box::new(|_| {
// Do nothing to ignore the panic
}));
let print_gag = Gag::stderr().unwrap(); // Otherwise the panic will be printed
in_mem_storage = std::thread::spawn(move || {
// Try to convert one of dptrees fields into an InMemStorage
dependencies.get()
})
.join()
.ok();
let dependencies = Arc::new(self.dependencies.clone());
// Dependencies were moved to a prev. thread, so create a new one
erased_storage = std::thread::spawn(move || {
// The same for ErasedStorage
dependencies.get()
})
.join()
.ok();
panic::set_hook(default_panic); // Restore the default panic hook
drop(print_gag);
(in_mem_storage, erased_storage)
}
/// Sets the state of the dialogue, if the storage exists in dependencies
/// Panics if no storage was found
///
/// The only supported storages are `InMemStorage` and `ErasedStorage`,
/// using raw storages without `.erase()` is not supported.
///
/// For example on how to make `ErasedStorage` from `RedisStorage` or `SqliteStorage` go to [this teloxide example](https://github.com/teloxide/teloxide/blob/master/crates/teloxide/examples/db_remember.rs#L41)
///
/// # Example
/// ```no_run
/// use teloxide::dispatching::UpdateHandler;
/// use teloxide::types::Update;
/// use teloxide_tests::{MockBot, MockMessageText};
/// use teloxide::dispatching::dialogue::GetChatId;
/// use teloxide::prelude::*;
/// use teloxide::{
/// dispatching::{
/// dialogue::{self, InMemStorage},
/// UpdateFilterExt,
/// }
/// };
/// use dptree::deps;
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
/// enum State {
/// #[default]
/// Start,
/// NotStart
/// }
///
/// type MyDialogue = Dialogue<State, InMemStorage<State>>;
///
/// fn handler_tree() -> UpdateHandler<Box<dyn std::error::Error + Send + Sync + 'static>> {
/// dialogue::enter::<Update, InMemStorage<State>, State, _>().endpoint(|update: Update, bot: Bot, dialogue: MyDialogue| async move {
/// let message = bot.send_message(update.chat_id().unwrap(), "Hello!").await?;
/// dialogue.update(State::NotStart).await?;
/// Ok(())
/// })
/// }
///
/// #[tokio::main]
/// async fn main() {
/// let mut bot = MockBot::new(MockMessageText::new().text("Hi!"), handler_tree());
/// bot.dependencies(deps![InMemStorage::<State>::new()]);
/// bot.set_state(State::Start).await;
/// // Yes, Start is the default state, but this just shows how it works
///
/// bot.dispatch().await;
///
/// let state: State = bot.get_state().await;
/// // The `: State` type annotation is nessessary! Otherwise the compiler wont't know, what to return
/// assert_eq!(state, State::NotStart);
///
/// let responses = bot.get_responses();
/// let message = responses
/// .sent_messages
/// .last()
/// .expect("No sent messages were detected!");
/// assert_eq!(message.text(), Some("Hello!"));
/// }
/// ```
///
pub async fn set_state<S>(&self, state: S)
where
S: Send + 'static + Clone,
{
let (in_mem_storage, erased_storage) = self.get_potential_storages().await;
let first_update = self.updates.first().expect("No updates were detected!");
let chat_id = match first_update.chat_id() {
Some(chat_id) => chat_id,
None => match find_chat_id(serde_json::to_value(first_update).unwrap()) {
Some(id) => ChatId(id),
None => {
log::error!("No chat id was detected in the update! Did you send an update without a chat identifier? Like MockCallbackQuery without an attached message?");
panic!("No chat id was detected!");
}
},
};
if let Some(storage) = in_mem_storage {
// If memory storage exists
(*storage)
.clone()
.update_dialogue(chat_id, state)
.await
.expect("Failed to update dialogue");
} else if let Some(storage) = erased_storage {
// If erased storage exists
(*storage)
.clone()
.update_dialogue(chat_id, state)
.await
.expect("Failed to update dialogue");
} else {
log::error!("No storage was detected! Did you add it to bot.dependencies(deps![get_bot_storage().await]); ? Did you specify the type ::<State> ?");
panic!("No storage was detected! Did you add it to bot.dependencies(deps![get_bot_storage().await]); ? Did you specify the type ::<State> ?");
}
}
/// Helper function to fetch the state of the dialogue and assert its value
pub async fn assert_state<S>(&self, state: S)
where
S: Send + Default + 'static + Clone + Debug + PartialEq,
{
assert_eqn!(self.get_state::<S>().await, state, "States are not equal!")
}
/// Gets the state of the dialogue, if the storage exists in dependencies
/// Panics if no storage was found
/// You need to use type annotation to get the state, please refer to the [`set_state`]
/// documentation example
///
/// [`set_state`]: crate::MockBot::set_state
pub async fn get_state<S>(&self) -> S
where
S: Send + Default + 'static + Clone,
{
self.try_get_state().await.unwrap_or(S::default())
}
/// Same as [`get_state`], but returns None if the state is None, instead of the default
///
/// [`get_state`]: crate::MockBot::get_state
pub async fn try_get_state<S>(&self) -> Option<S>
where
S: Send + 'static + Clone,
{
let (in_mem_storage, erased_storage) = self.get_potential_storages().await;
let first_update = self.updates.first().expect("No updates were detected!");
let chat_id = match first_update.chat_id() {
Some(chat_id) => chat_id,
None => match find_chat_id(serde_json::to_value(first_update).unwrap()) {
Some(id) => ChatId(id),
None => {
panic!("No chat id was detected!");
}
},
};
if let Some(storage) = in_mem_storage {
// If memory storage exists
(*storage)
.clone()
.get_dialogue(chat_id)
.await
.ok()
.flatten()
} else if let Some(storage) = erased_storage {
// If erased storage exists
(*storage)
.clone()
.get_dialogue(chat_id)
.await
.ok()
.flatten()
} else {
log::error!("No storage was detected! Did you add it to bot.dependencies(deps![get_bot_storage().await]); ? Did you specify the type ::<State> ?");
panic!("No storage was detected! Did you add it to bot.dependencies(deps![get_bot_storage().await]); ? Did you specify the type ::<State> ?");
}
}
//
// Syntactic sugar
//
/// Dispatches and checks the last sent message text or caption. Pass in an empty string if you
/// want the text or caption to be None
pub async fn dispatch_and_check_last_text(&mut self, text_or_caption: &str) {
self.dispatch().await;
let responses = self.get_responses();
let message = responses
.sent_messages
.last()
.expect("No sent messages were detected!");
if let Some(text) = message.text() {
assert_eqn!(text, text_or_caption, "Texts are not equal!");
} else if let Some(caption) = message.caption() {
assert_eqn!(caption, text_or_caption, "Captions are not equal!");
} else if !text_or_caption.is_empty() {
panic!("Message has no text or caption!");
}
}
/// Same as `dispatch_and_check_last_text`, but also checks the state. You need to derive
/// PartialEq, Clone and Debug for the state like in `set_state` example
pub async fn dispatch_and_check_last_text_and_state<S>(
&mut self,
text_or_caption: &str,
state: S,
) where
S: Send + Default + 'static + Clone + std::fmt::Debug + PartialEq,
{
self.dispatch().await;
let responses = self.get_responses();
let message = responses
.sent_messages
.last()
.expect("No sent messages were detected!");
if let Some(text) = message.text() {
assert_eqn!(text, text_or_caption, "Texts are not equal!");
} else if let Some(caption) = message.caption() {
assert_eqn!(caption, text_or_caption, "Captions are not equal!");
} else if !text_or_caption.is_empty() {
panic!("Message has no text or caption!");
}
self.assert_state(state).await;
}
/// Same as `dispatch_and_check_last_text`, but also checks, if the variants of the state are the same
///
/// For example, `State::Start { some_field: "value" }` and `State::Start { some_field: "other value" }` are the same in this function
pub async fn dispatch_and_check_last_text_and_state_discriminant<S>(
&mut self,
text_or_caption: &str,
state: S,
) where
S: Send + PartialEq + Debug + Default + 'static + Clone,
{
self.dispatch().await;
let responses = self.get_responses();
let message = responses
.sent_messages
.last()
.expect("No sent messages were detected!");
if let Some(text) = message.text() {
assert_eqn!(text, text_or_caption, "Texts are not equal!");
} else if let Some(caption) = message.caption() {
assert_eqn!(caption, text_or_caption, "Captions are not equal!");
} else if !text_or_caption.is_empty() {
panic!("Message has no text or caption!");
}
let got_state: S = self.get_state().await;
if discriminant(&got_state) != discriminant(&state) {
assert_eqn!(got_state, state, "State variants are not equal!")
}
}
/// Just checks the state after dispathing the update, like `dispatch_and_check_last_text_and_state`
pub async fn dispatch_and_check_state<S>(&mut self, state: S)
where
S: Send + Default + 'static + Clone + std::fmt::Debug + PartialEq,
{
self.dispatch().await;
self.assert_state(state).await;
}
/// Just checks the state discriminant after dispathing the update, like `dispatch_and_check_last_text_and_state_discriminant`
pub async fn dispatch_and_check_state_discriminant<S>(&mut self, state: S)
where
S: Send + Debug + PartialEq + Default + 'static + Clone,
{
self.dispatch().await;
let got_state: S = self.get_state().await;
if discriminant(&got_state) != discriminant(&state) {
assert_eqn!(got_state, state, "State variants are not equal!")
}
}
}