vaegram 0.2.0

Telegram MTProto client library for users and bots
Documentation
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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
use std::sync::atomic::Ordering;

use crate::Client;
use crate::dispatcher::Dispatcher;
use crate::dispatcher::router::run_hook;
use crate::errors::{Error, Result};
use crate::handlers::UpdateKind;
use crate::raw::{base, functions};
use crate::session::internals::now;

// The server's clock is read in whole seconds, so the run's start, in Telegram's time, may read up to a second
// late.
const CLOCK_SLACK: i64 = 1;

impl Client {
    /// Takes updates and runs the handlers until `stop`, then returns `Ok(())`; it returns `Error::Disconnected`
    /// when the connection ends. Fails at once with `Error::InvalidArgument` while this client is already running,
    /// here or in [`compose`](Client::compose).
    ///
    /// Telegram sends updates to every connected session of an account, bots included, so several clients logged
    /// in as the same bot or user, in one process or several, each get every update and each run their handlers:
    /// a handler that replies then replies once per client. With [`catch_up`](crate::ClientBuilder::catch_up) on,
    /// the default, `run` first replays what came since the saved update state, so a client started from an old
    /// session, or from a copy of another client's, handles those updates again too.
    pub async fn run(&self) -> Result<()> {
        let running = self.claim_run()?;
        match self.prepare().await? {
            Some(dispatcher) => self.drive(running, dispatcher).await,
            None => Ok(()),
        }
    }

    /// The client's updates for one `run`, until the returned guard is dropped.
    pub(super) fn claim_run(&self) -> Result<Running> {
        if self.0.running.swap(true, Ordering::AcqRel) {
            return Err(Error::InvalidArgument("this client is already running"));
        }
        Ok(Running(self.clone()))
    }

    /// What anything that waits for updates checks first: a client built with `no_updates` never gets any.
    pub(crate) fn require_updates(&self) -> Result<()> {
        if self.0.options.no_updates {
            return Err(Error::InvalidArgument("updates are off for this client"));
        }
        Ok(())
    }

    /// Everything `run` does before it takes updates: who the account is, where the update state starts, and the
    /// start hook. `None` when `disconnect` cut it short: that stops the client before it ends the connection, so a
    /// start request failing with `Disconnected` then ends a stopped run, not a lost one. Untagged, like all of
    /// `run`: what it sends is vaegram's own doing, even inside a caller's tag.
    pub(super) async fn prepare(&self) -> Result<Option<Dispatcher>> {
        match crate::observe::untagged(self.start_dispatcher()).await {
            Err(Error::Disconnected) if *self.0.stop.borrow() => {
                // Cleared as `drive` clears it when a run ends.
                self.0.stop.send_replace(false);
                Ok(None)
            }
            started => started.map(Some),
        }
    }

    async fn start_dispatcher(&self) -> Result<Dispatcher> {
        self.require_updates()?;
        let local_start = now();
        self.set_me(&self.get_me_raw().await?);
        // When the run began, in Telegram's time, as the getMe reply just showed its clock: without catch-up, a bot
        // drops what Telegram dated before it.
        let started = local_start + self.session().seen_offset() - CLOCK_SLACK;
        let saved = self.0.state.lock().unwrap().take().filter(|_| self.0.catch_up);
        let dispatcher = match saved {
            Some(state) => Dispatcher::resume(self.clone(), state),
            None => {
                let base::updates::State::State(state) = self.invoke(&functions::updates::GetState {}).await?;
                if self.0.catch_up || !self.0.me.lock().unwrap().is_bot {
                    // With catch-up on and nothing saved, what Telegram still holds is delivered as it comes. A
                    // user's getState is current, so there is nothing from before to skip.
                    Dispatcher::new(self.clone(), &state)
                } else {
                    Dispatcher::from_now(self.clone(), &state, i32::try_from(started).unwrap_or(i32::MAX))
                }
            }
        };
        self.lifecycle_hook(UpdateKind::Start).await;
        Ok(dispatcher)
    }

    /// Takes updates until `stop` or a lost connection, then runs the stop hook; untagged, as [`prepare`] is.
    ///
    /// [`prepare`]: Self::prepare
    // `dispatcher` comes after `running`, so a cancelled `drive` drops it first: it hands its state back to the
    // client before `Running` saves it for a disconnected client.
    pub(super) async fn drive(&self, running: Running, mut dispatcher: Dispatcher) -> Result<()> {
        let stopped = crate::observe::untagged(dispatcher.run(&mut *self.0.updates.lock().await)).await;
        // Dropped first, so the update state is back in the client when the stop hook runs.
        drop(dispatcher);
        crate::observe::untagged(self.lifecycle_hook(UpdateKind::Stop)).await;
        // Cleared here, not on entry, so a stop() that lands before run gets going still counts.
        self.0.stop.send_replace(false);
        drop(running);
        if stopped { Ok(()) } else { Err(Error::Disconnected) }
    }

    /// Makes `run` stop taking updates, wait for the handlers already running, and return `Ok(())`.
    ///
    /// For Ctrl-C, use [`run_until_ctrl_c`](Self::run_until_ctrl_c) in place of `run`.
    pub fn stop(&self) {
        self.0.stop.send_replace(true);
    }

    async fn lifecycle_hook(&self, kind: UpdateKind) {
        let hook = self.0.handlers.current().hooks.get(kind);
        if let Some(hook) = hook {
            run_hook(self.clone(), kind, hook).await;
        }
    }
}

/// Held while a `run` owns the client's updates; dropping it, however `run` ends, lets the next one start, and
/// closes the storage of a client `disconnect` was called on meanwhile.
pub(super) struct Running(Client);

impl Drop for Running {
    fn drop(&mut self) {
        let inner = &self.0.0;
        inner.running.store(false, Ordering::SeqCst);
        if inner.disconnected.load(Ordering::SeqCst) {
            inner.finish_store();
        }
    }
}

#[cfg(test)]
pub(super) mod tests {
    use std::future::{Ready, ready};
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::sync::{Arc, Mutex};

    use tokio::sync::{Notify, mpsc, oneshot};

    use crate::Client;
    use crate::client::Me;
    use crate::errors::{Error, Result, RpcError};
    use crate::handlers::UpdateKind;
    use crate::raw::functions::{updates, users};
    use crate::raw::{Identifiable, Serializable, base, types};
    use crate::session::{Incoming, UpdatesRx};
    use crate::storage::{FileStorage, MemoryStorage, Storage, UpdateState};
    use crate::utils::get_peer;

    type Log = Arc<Mutex<Vec<&'static str>>>;

    fn log_hook(log: &Log, name: &'static str) -> impl Fn(Client) -> Ready<Result<()>> + Send + Sync + 'static {
        let log = log.clone();
        move |_| {
            log.lock().unwrap().push(name);
            ready(Ok(()))
        }
    }

    // Stopped before it runs, with a saved state and an update stream that stays open.
    pub(in crate::methods::utilities) async fn stopped_client() -> (Client, mpsc::Sender<Incoming>) {
        let me = vec![base::User::UserEmpty(types::UserEmpty { id: 1 })];
        let empty = base::updates::Difference::DifferenceEmpty(types::updates::DifferenceEmpty { date: 2, seq: 3 });
        let (client, _sent) = Client::scripted(vec![Ok(me.to_bytes()), Ok(empty.to_bytes())]);
        *client.0.state.lock().unwrap() = Some(UpdateState { pts: 10, date: 1, ..Default::default() });
        let (updates, rx) = UpdatesRx::channel();
        *client.0.updates.lock().await = rx;
        client.stop();
        (client, updates)
    }

    #[tokio::test]
    async fn run_refuses_without_updates() {
        let (mut client, mut sent) = Client::recording();
        client.set_options(crate::connection::Options::new().no_updates(true));
        let refused = client.run().await;
        assert!(matches!(refused, Err(Error::InvalidArgument(_))), "{refused:?}");
        let listened = client.listen::<crate::types::Message>().await;
        assert!(matches!(listened, Err(Error::InvalidArgument(_))), "{listened:?}");
        let asked = client.ask("me", "hi").await;
        assert!(matches!(asked, Err(Error::InvalidArgument(_))), "{asked:?}");
        assert!(sent.try_recv().is_err(), "something went out");
    }

    #[tokio::test]
    async fn stop_before_run_returns_at_once_and_saves_the_state() {
        let me = vec![base::User::UserEmpty(types::UserEmpty { id: 1 })];
        let empty = base::updates::Difference::DifferenceEmpty(types::updates::DifferenceEmpty { date: 2, seq: 3 });
        let (mut client, _sent) = Client::scripted(vec![Ok(me.to_bytes()), Ok(empty.to_bytes())]);
        let engine = Arc::new(MemoryStorage::new());
        Arc::get_mut(&mut client.0).unwrap().set_store(engine.clone());
        *client.0.state.lock().unwrap() = Some(UpdateState { pts: 10, date: 1, ..Default::default() });
        // Updates never arrive and the channel stays open, so only the stop can end run.
        let (_updates, rx) = UpdatesRx::channel();
        *client.0.updates.lock().await = rx;

        client.stop();
        let result = client.run().await;
        let owner = client.0.me.lock().unwrap().id;
        let saved = engine.load_state().await.unwrap().map(|s| (s.owner, s.update.map(|u| u.pts)));
        assert!(result.is_ok(), "{result:?}");
        assert_eq!(saved, Some((owner, Some(10))));
    }

    #[tokio::test]
    async fn the_state_stays_in_memory_after_stop() {
        let me = vec![base::User::UserEmpty(types::UserEmpty { id: 1 })];
        let empty = base::updates::Difference::DifferenceEmpty(types::updates::DifferenceEmpty { date: 2, seq: 3 });
        let (client, _sent) = Client::scripted(vec![Ok(me.to_bytes()), Ok(empty.to_bytes())]);
        *client.0.state.lock().unwrap() = Some(UpdateState { pts: 10, date: 1, ..Default::default() });
        let (_updates, rx) = UpdatesRx::channel();
        *client.0.updates.lock().await = rx;
        client.stop();
        assert!(client.run().await.is_ok());
        assert_eq!(client.0.state.lock().unwrap().as_ref().map(|s| s.pts), Some(10));
    }

    #[tokio::test]
    async fn stop_waits_for_running_handlers_then_returns_ok() {
        let me = vec![base::User::UserEmpty(types::UserEmpty { id: 1 })];
        let empty = base::updates::Difference::DifferenceEmpty(types::updates::DifferenceEmpty { date: 2, seq: 3 });
        let (client, _sent) = Client::scripted(vec![Ok(me.to_bytes()), Ok(empty.to_bytes())]);
        *client.0.state.lock().unwrap() = Some(UpdateState { pts: 10, date: 1, ..Default::default() });
        let (updates, rx) = UpdatesRx::channel();
        *client.0.updates.lock().await = rx;

        let (started, mut running) = oneshot::channel();
        let (started, gate) = (std::sync::Mutex::new(Some(started)), Arc::new(Notify::new()));
        let release = gate.clone();
        client.on_raw_update(move |_, _| {
            let _ = started.lock().unwrap().take().map(|s| s.send(()));
            let gate = gate.clone();
            async move {
                gate.notified().await;
                Ok(())
            }
        });
        let run = tokio::spawn({
            let client = client.clone();
            async move { client.run().await }
        });
        let status = types::UpdateUserStatus { user_id: 7, status: base::UserStatus::UserStatusEmpty };
        let update = types::UpdateShort { update: base::Update::UpdateUserStatus(Box::new(status)), date: 2 };
        let updates_short = base::Updates::UpdateShort(Box::new(update));
        updates.send(Incoming { updates: updates_short, from_request: false }).await.unwrap();
        while running.try_recv().is_err() {
            tokio::task::yield_now().await;
        }

        client.stop();
        for _ in 0..50 {
            tokio::task::yield_now().await;
        }
        assert!(!run.is_finished(), "run returned before the running handler finished");
        release.notify_one();
        assert!(run.await.unwrap().is_ok());
    }

    #[tokio::test]
    async fn a_second_run_while_one_is_running_is_refused() {
        let me = vec![base::User::UserEmpty(types::UserEmpty { id: 1 })];
        let empty = base::updates::Difference::DifferenceEmpty(types::updates::DifferenceEmpty { date: 2, seq: 3 });
        let (client, mut sent) = Client::scripted(vec![Ok(me.to_bytes()), Ok(empty.to_bytes())]);
        *client.0.state.lock().unwrap() = Some(UpdateState { pts: 10, date: 1, ..Default::default() });
        let (_updates, rx) = UpdatesRx::channel();
        *client.0.updates.lock().await = rx;
        let starts = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let counted = starts.clone();
        client.on_start(move |_| {
            counted.fetch_add(1, Ordering::Relaxed);
            ready(Ok(()))
        });
        let first = tokio::spawn({
            let client = client.clone();
            async move { client.run().await }
        });
        // users.getUsers, then updates.getDifference: the first run is taking updates.
        sent.recv().await;
        sent.recv().await;

        let second = client.run().await;
        assert!(matches!(second, Err(Error::InvalidArgument(_))), "{second:?}");
        assert!(sent.try_recv().is_err(), "the second run sent something");
        client.stop();
        assert!(first.await.unwrap().is_ok());
        assert_eq!(starts.load(Ordering::Relaxed), 1);
        assert!(!client.0.running.load(Ordering::Relaxed), "a later run would be refused too");
    }

    // The test kit's way to end: a handler holds a clone of its client, `disconnect`, then the runtime goes and
    // cancels `run`.
    #[tokio::test]
    async fn a_disconnected_run_saves_and_lets_go_of_the_session_file_as_it_ends() {
        let dir = std::env::temp_dir().join(format!("vaegram-run-disconnect-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).unwrap();
        let me = vec![base::User::UserEmpty(types::UserEmpty { id: 1 })];
        let empty = base::updates::Difference::DifferenceEmpty(types::updates::DifferenceEmpty { date: 2, seq: 3 });
        let (mut client, mut sent) = Client::scripted(vec![Ok(me.to_bytes()), Ok(empty.to_bytes())]);
        Arc::get_mut(&mut client.0).unwrap().set_store(Arc::new(FileStorage::new(dir.join("bot"))));
        client.save().await.unwrap();
        *client.0.state.lock().unwrap() = Some(UpdateState { pts: 10, date: 1, ..Default::default() });
        let (_updates, rx) = UpdatesRx::channel();
        *client.0.updates.lock().await = rx;
        let own = client.clone();
        client.on_raw_update(move |_, _| {
            let _own = &own;
            ready(Ok(()))
        });
        let run = tokio::spawn({
            let client = client.clone();
            async move { client.run().await }
        });
        // users.getUsers, then updates.getDifference: the run is taking updates.
        sent.recv().await;
        sent.recv().await;

        client.disconnect();
        run.abort();
        let _ = run.await;
        let second = FileStorage::new(dir.join("bot"));
        let reopened = second.open().await;
        let saved = second.load_state().await.ok().flatten().and_then(|s| s.update).map(|u| (u.pts, u.date));
        drop(second);
        let _ = std::fs::remove_dir_all(&dir);
        assert!(reopened.is_ok(), "{reopened:?}");
        assert_eq!(saved, Some((10, 2)), "the run's state is saved before the file is let go");
    }

    #[tokio::test]
    async fn run_inside_tagged_sends_nothing_tagged() {
        let me = vec![base::User::UserEmpty(types::UserEmpty { id: 1 })];
        let empty = base::updates::Difference::DifferenceEmpty(types::updates::DifferenceEmpty { date: 2, seq: 3 });
        let pong = || Ok(base::Pong::Pong(Box::new(types::Pong { msg_id: 0, ping_id: 1 })).to_bytes());
        let replies = vec![Ok(me.to_bytes()), pong(), Ok(empty.to_bytes()), pong()];
        let (client, mut sent) = Client::scripted_tags(replies);
        *client.0.state.lock().unwrap() = Some(UpdateState { pts: 10, date: 1, ..Default::default() });
        let ping = |client: Client| async move { client.invoke(&crate::raw::functions::Ping { ping_id: 1 }).await };
        client.on_start(ping);
        client.on_stop(ping);

        assert!(crate::observe::tagged(7, client.run()).await.is_err());
        let id = |body: &[u8]| u32::from_le_bytes(body[..4].try_into().unwrap());
        let sent: Vec<_> = std::iter::from_fn(|| sent.try_recv().ok()).map(|(body, tag)| (id(&body), tag)).collect();
        let ping = <crate::raw::functions::Ping as Identifiable>::CONSTRUCTOR_ID;
        let expected = [users::GetUsers::CONSTRUCTOR_ID, ping, updates::GetDifference::CONSTRUCTOR_ID, ping];
        assert_eq!(sent, expected.map(|id| (id, None)));
    }

    #[tokio::test]
    async fn restart_catches_up_from_saved_state() {
        let me = vec![base::User::UserEmpty(types::UserEmpty { id: 1 })];
        let empty = types::updates::DifferenceEmpty { date: 2, seq: 3 };
        let empty = base::updates::Difference::DifferenceEmpty(empty);
        let (client, mut sent) = Client::scripted(vec![Ok(me.to_bytes()), Ok(empty.to_bytes())]);
        *client.0.state.lock().unwrap() = Some(UpdateState { pts: 10, date: 1, ..Default::default() });

        assert!(client.run().await.is_err());

        let id = |body: &[u8]| u32::from_le_bytes(body[..4].try_into().unwrap());
        assert_eq!(id(&sent.try_recv().unwrap()), users::GetUsers::CONSTRUCTOR_ID);
        let difference = sent.try_recv().unwrap();
        assert_eq!(id(&difference), updates::GetDifference::CONSTRUCTOR_ID);
        assert_eq!(difference[8..12], 10i32.to_le_bytes());
        assert!(sent.try_recv().is_err(), "a saved state must not ask for updates.getState");
    }

    #[tokio::test]
    async fn without_catch_up_a_bot_starts_from_get_state() {
        let me = vec![base::User::UserEmpty(types::UserEmpty { id: 1 })];
        let now = types::updates::State { pts: 50, qts: 0, date: 2, seq: 0, unread_count: 0 };
        let now = base::updates::State::State(Box::new(now));
        let nothing_pending =
            base::updates::Difference::DifferenceEmpty(types::updates::DifferenceEmpty { date: 3, seq: 0 });
        let replies = vec![Ok(me.to_bytes()), Ok(now.to_bytes()), Ok(nothing_pending.to_bytes())];
        let (mut client, mut sent) = Client::scripted(replies);
        Arc::get_mut(&mut client.0).unwrap().catch_up = false;
        *client.0.state.lock().unwrap() = Some(UpdateState { pts: 10, date: 1, ..Default::default() });

        assert!(client.run().await.is_err());

        let bodies: Vec<Vec<u8>> = std::iter::from_fn(|| sent.try_recv().ok()).collect();
        let ids: Vec<u32> = bodies.iter().map(|b| u32::from_le_bytes(b[..4].try_into().unwrap())).collect();
        let expected = [
            users::GetUsers::CONSTRUCTOR_ID,
            updates::GetState::CONSTRUCTOR_ID,
            updates::GetDifference::CONSTRUCTOR_ID,
        ];
        assert_eq!(ids, expected);
        // The difference starts from getState's answer, not the saved state.
        assert_eq!(bodies[2][8..12], 50i32.to_le_bytes());
        assert_eq!(client.0.state.lock().unwrap().as_ref().map(|s| (s.pts, s.date)), Some((50, 3)));
    }

    // A user's getState is current, so a user's run starts from it as it is.
    #[tokio::test]
    async fn without_catch_up_a_user_starts_from_get_state_without_a_difference() {
        let me = vec![base::User::UserEmpty(types::UserEmpty { id: 1 })];
        let now = types::updates::State { pts: 50, qts: 0, date: 2, seq: 0, unread_count: 0 };
        let (mut client, mut sent) =
            Client::scripted(vec![Ok(me.to_bytes()), Ok(base::updates::State::State(Box::new(now)).to_bytes())]);
        Arc::get_mut(&mut client.0).unwrap().catch_up = false;
        // Test clients are bots; this one is a user.
        *client.0.me.lock().unwrap() = Me { id: 1, is_bot: false, ..Me::default() };

        assert!(client.run().await.is_err());

        let ids: Vec<u32> = std::iter::from_fn(|| sent.try_recv().ok())
            .map(|b| u32::from_le_bytes(b[..4].try_into().unwrap()))
            .collect();
        assert_eq!(ids, [users::GetUsers::CONSTRUCTOR_ID, updates::GetState::CONSTRUCTOR_ID]);
        assert_eq!(client.0.state.lock().unwrap().as_ref().map(|s| (s.pts, s.date)), Some((50, 2)));
    }

    fn connected(id: &str, qts: i32) -> base::Update {
        let connection = types::BotBusinessConnection {
            disabled: false,
            connection_id: id.into(),
            user_id: 7,
            dc_id: 2,
            date: 1,
            rights: None,
        };
        let connection = base::BotBusinessConnection::BotBusinessConnection(Box::new(connection));
        base::Update::UpdateBotBusinessConnect(Box::new(types::UpdateBotBusinessConnect { connection, qts }))
    }

    // A bot's updates.getState names the last update Telegram knows it confirmed, so updates it pushed in an earlier
    // run come again after it. Without catch-up they are skipped: the difference since then is read, and what it
    // brings from before the run is dropped.
    #[tokio::test]
    async fn without_catch_up_updates_from_before_are_skipped() {
        let me = vec![base::User::UserEmpty(types::UserEmpty { id: 1 })];
        let confirmed = types::updates::State { pts: 50, qts: 5, date: 2, seq: 0, unread_count: 0 };
        let now = types::updates::State { qts: 6, date: 3, ..confirmed.clone() };
        let pending = types::updates::Difference {
            new_messages: Vec::new(),
            new_encrypted_messages: Vec::new(),
            other_updates: vec![connected("old", 6)],
            chats: Vec::new(),
            users: Vec::new(),
            state: base::updates::State::State(Box::new(now)),
        };
        let pending = base::updates::Difference::Difference(Box::new(pending));
        let confirmed = base::updates::State::State(Box::new(confirmed));
        let replies = vec![Ok(me.to_bytes()), Ok(confirmed.to_bytes()), Ok(pending.to_bytes())];
        let (mut client, _sent) = Client::scripted(replies);
        Arc::get_mut(&mut client.0).unwrap().catch_up = false;
        let (updates, rx) = UpdatesRx::channel();
        *client.0.updates.lock().await = rx;
        let seen = Arc::new(Mutex::new(Vec::new()));
        let log = seen.clone();
        client.on_business_connection(crate::filters::all(), move |c| {
            log.lock().unwrap().push(c.id);
            ready(Ok(()))
        });
        // Telegram pushes the old one again, then a new one.
        for (id, qts) in [("old", 6), ("new", 7)] {
            let short = types::UpdateShort { update: connected(id, qts), date: 3 };
            let incoming = Incoming { updates: base::Updates::UpdateShort(Box::new(short)), from_request: false };
            updates.send(incoming).await.unwrap();
        }
        let run = tokio::spawn({
            let client = client.clone();
            async move { client.run().await }
        });
        while seen.lock().unwrap().is_empty() {
            tokio::task::yield_now().await;
        }
        for _ in 0..50 {
            tokio::task::yield_now().await;
        }
        client.stop();
        assert!(run.await.unwrap().is_ok());
        assert_eq!(*seen.lock().unwrap(), ["new"]);
    }

    fn new_message(id: i32, date: i32, pts: i32) -> base::Update {
        let message = base::Message::Message(Box::new(crate::types::Message::bare(id, get_peer(7), date)));
        base::Update::UpdateNewMessage(Box::new(types::UpdateNewMessage { message, pts, pts_count: 1 }))
    }

    /// The ids of the new messages a run without catch-up hands to its handlers, waiting until `expected` came,
    /// when getState says pts 50, the difference after it brings `held` and ends at pts `to`, `pushed` waits on
    /// the update stream, and the server's clock runs `server_ahead` seconds ahead of this one.
    async fn delivered_from_now(
        held: Vec<base::Update>,
        to: i32,
        pushed: Vec<base::Update>,
        expected: usize,
        server_ahead: i64,
    ) -> Vec<i32> {
        let me = vec![base::User::UserEmpty(types::UserEmpty { id: 1 })];
        let state = |pts| types::updates::State { pts, qts: 0, date: 2, seq: 0, unread_count: 0 };
        let held = held.into_iter().map(|u| match u {
            base::Update::UpdateNewMessage(u) => u.message,
            _ => unreachable!("only new messages are held here"),
        });
        let pending = types::updates::Difference {
            new_messages: held.collect(),
            new_encrypted_messages: Vec::new(),
            other_updates: Vec::new(),
            chats: Vec::new(),
            users: Vec::new(),
            state: base::updates::State::State(Box::new(state(to))),
        };
        let replies = vec![
            Ok(me.to_bytes()),
            Ok(base::updates::State::State(Box::new(state(50))).to_bytes()),
            Ok(base::updates::Difference::Difference(Box::new(pending)).to_bytes()),
        ];
        let (mut client, _sent) = Client::scripted(replies);
        Arc::get_mut(&mut client.0).unwrap().catch_up = false;
        client.session().set_seen_offset(server_ahead);
        let (updates, rx) = UpdatesRx::channel();
        *client.0.updates.lock().await = rx;
        let seen = Arc::new(Mutex::new(Vec::new()));
        let log = seen.clone();
        client.on_raw_update(move |_, raw| {
            if let base::Update::UpdateNewMessage(u) = &raw.update {
                if let base::Message::Message(m) = &u.message {
                    log.lock().unwrap().push(m.id);
                }
            }
            ready(Ok(()))
        });
        for update in pushed {
            let batch = types::Updates { updates: vec![update], users: Vec::new(), chats: Vec::new(), date: 0, seq: 0 };
            let incoming = Incoming { updates: base::Updates::Updates(Box::new(batch)), from_request: false };
            updates.send(incoming).await.unwrap();
        }
        let run = tokio::spawn({
            let client = client.clone();
            async move { client.run().await }
        });
        for _ in 0..1000 {
            if seen.lock().unwrap().len() >= expected {
                break;
            }
            tokio::task::yield_now().await;
        }
        for _ in 0..50 {
            tokio::task::yield_now().await;
        }
        client.stop();
        assert!(run.await.unwrap().is_ok());
        std::mem::take(&mut *seen.lock().unwrap())
    }

    fn now() -> i32 {
        crate::session::internals::now() as i32
    }

    // The test kit's case: the user's message reaches Telegram while the bot's run reads what Telegram held.
    #[tokio::test]
    async fn without_catch_up_what_came_since_the_run_began_is_delivered() {
        let held = vec![new_message(9, 1, 51), new_message(1, now(), 52)];
        assert_eq!(delivered_from_now(held, 52, Vec::new(), 1, 0).await, [1]);
    }

    // Telegram pushes a message it also puts in the difference, then one after it.
    #[tokio::test]
    async fn without_catch_up_a_message_both_held_and_pushed_runs_once() {
        let pushed = vec![new_message(1, now(), 51), new_message(2, now(), 52)];
        assert_eq!(delivered_from_now(vec![new_message(1, now(), 51)], 51, pushed, 2, 0).await, [1, 2]);
    }

    // The run's start is read off the clock the server's messages show, not the offset saved with the salt: here
    // the server runs an hour ahead, so a message dated by this clock's now came before the run began.
    #[tokio::test]
    async fn without_catch_up_the_run_begins_by_the_server_clock() {
        let held = vec![new_message(1, now(), 51), new_message(2, now() + 3600, 52)];
        assert_eq!(delivered_from_now(held, 52, Vec::new(), 1, 3600).await, [2]);
    }

    // Telegram pushes what it held again, and reading it fails at first: until a difference is read, only what
    // came since the run began is delivered from the stream too.
    #[tokio::test]
    async fn without_catch_up_a_held_update_pushed_before_the_first_difference_is_skipped() {
        let me = vec![base::User::UserEmpty(types::UserEmpty { id: 1 })];
        let confirmed = types::updates::State { pts: 50, qts: 5, date: 2, seq: 0, unread_count: 0 };
        let confirmed = base::updates::State::State(Box::new(confirmed));
        let flood = Err(Error::Rpc(RpcError::parse(420, "FLOOD_WAIT_1")));
        let (mut client, _sent) = Client::scripted(vec![Ok(me.to_bytes()), Ok(confirmed.to_bytes()), flood]);
        Arc::get_mut(&mut client.0).unwrap().catch_up = false;
        let (updates, rx) = UpdatesRx::channel();
        *client.0.updates.lock().await = rx;
        let seen = Arc::new(Mutex::new(Vec::new()));
        let log = seen.clone();
        client.on_raw_update(move |_, raw| {
            log.lock().unwrap().push(match &raw.update {
                base::Update::UpdateBotBusinessConnect(_) => "old connection",
                base::Update::UpdateNewMessage(_) => "new message",
                base::Update::UpdateBotCallbackQuery(_) => "callback query",
                _ => "other",
            });
            ready(Ok(()))
        });
        let query = types::UpdateBotCallbackQuery {
            query_id: 1,
            user_id: 7,
            peer: get_peer(7),
            msg_id: 1,
            chat_instance: 3,
            data: Some(b"tap".to_vec()),
            game_short_name: None,
        };
        // A callback query moves no counter, so Telegram never holds one: it passes.
        let query = base::Update::UpdateBotCallbackQuery(Box::new(query));
        for update in [connected("old", 6), new_message(1, now(), 51), query] {
            let short = types::UpdateShort { update, date: 3 };
            let incoming = Incoming { updates: base::Updates::UpdateShort(Box::new(short)), from_request: false };
            updates.send(incoming).await.unwrap();
        }
        let run = tokio::spawn({
            let client = client.clone();
            async move { client.run().await }
        });
        for _ in 0..1000 {
            if seen.lock().unwrap().len() >= 2 {
                break;
            }
            tokio::task::yield_now().await;
        }
        for _ in 0..50 {
            tokio::task::yield_now().await;
        }
        client.stop();
        assert!(run.await.unwrap().is_ok());
        assert_eq!(*seen.lock().unwrap(), ["new message", "callback query"]);
    }

    // Telegram refuses the first difference, so what it held won't be read: from then on nothing more is taken for
    // it, and an undated update pushed after reaches the handlers.
    #[tokio::test]
    async fn without_catch_up_a_refused_first_difference_ends_the_skipping() {
        let me = vec![base::User::UserEmpty(types::UserEmpty { id: 1 })];
        let confirmed = types::updates::State { pts: 50, qts: 0, date: 2, seq: 0, unread_count: 0 };
        let confirmed = base::updates::State::State(Box::new(confirmed));
        let refused = Err(Error::Rpc(RpcError::parse(400, "X_INVALID")));
        let (mut client, _sent) = Client::scripted(vec![Ok(me.to_bytes()), Ok(confirmed.to_bytes()), refused]);
        Arc::get_mut(&mut client.0).unwrap().catch_up = false;
        let (updates, rx) = UpdatesRx::channel();
        *client.0.updates.lock().await = rx;
        let seen = Arc::new(Mutex::new(Vec::new()));
        let log = seen.clone();
        client.on_raw_update(move |_, raw| {
            if let base::Update::UpdateDeleteMessages(u) = &raw.update {
                log.lock().unwrap().push(u.pts);
            }
            ready(Ok(()))
        });
        // The run reads the stream only after its first difference was refused.
        let deleted = types::UpdateDeleteMessages { messages: vec![1], pts: 51, pts_count: 1 };
        let short = types::UpdateShort { update: base::Update::UpdateDeleteMessages(Box::new(deleted)), date: 3 };
        let incoming = Incoming { updates: base::Updates::UpdateShort(Box::new(short)), from_request: false };
        updates.send(incoming).await.unwrap();
        let run = tokio::spawn({
            let client = client.clone();
            async move { client.run().await }
        });
        for _ in 0..1000 {
            if !seen.lock().unwrap().is_empty() {
                break;
            }
            tokio::task::yield_now().await;
        }
        client.stop();
        assert!(run.await.unwrap().is_ok());
        assert_eq!(*seen.lock().unwrap(), [51]);
    }

    #[tokio::test]
    async fn start_and_stop_hooks_bracket_run() {
        let (client, _updates) = stopped_client().await;
        let log = Log::default();
        client.on_stop(log_hook(&log, "stop"));
        client.on_start(log_hook(&log, "start"));
        assert!(client.run().await.is_ok());
        assert_eq!(*log.lock().unwrap(), ["start", "stop"]);
    }

    #[tokio::test]
    async fn the_stop_hook_runs_after_running_handlers() {
        let me = vec![base::User::UserEmpty(types::UserEmpty { id: 1 })];
        let empty = base::updates::Difference::DifferenceEmpty(types::updates::DifferenceEmpty { date: 2, seq: 3 });
        let (client, _sent) = Client::scripted(vec![Ok(me.to_bytes()), Ok(empty.to_bytes())]);
        *client.0.state.lock().unwrap() = Some(UpdateState { pts: 10, date: 1, ..Default::default() });
        let (updates, rx) = UpdatesRx::channel();
        *client.0.updates.lock().await = rx;

        let (started, mut running) = oneshot::channel();
        let (started, gate) = (Mutex::new(Some(started)), Arc::new(Notify::new()));
        let (finished, release) = (Arc::new(AtomicBool::new(false)), gate.clone());
        let done = finished.clone();
        client.on_raw_update(move |_, _| {
            let _ = started.lock().unwrap().take().map(|s| s.send(()));
            let (gate, done) = (gate.clone(), done.clone());
            async move {
                gate.notified().await;
                done.store(true, Ordering::SeqCst);
                Ok(())
            }
        });
        let seen_at_stop = Arc::new(Mutex::new(None));
        let seen = seen_at_stop.clone();
        client.on_stop(move |_| {
            *seen.lock().unwrap() = Some(finished.load(Ordering::SeqCst));
            ready(Ok(()))
        });
        let run = tokio::spawn({
            let client = client.clone();
            async move { client.run().await }
        });
        let status = types::UpdateUserStatus { user_id: 7, status: base::UserStatus::UserStatusEmpty };
        let update = types::UpdateShort { update: base::Update::UpdateUserStatus(Box::new(status)), date: 2 };
        updates
            .send(Incoming { updates: base::Updates::UpdateShort(Box::new(update)), from_request: false })
            .await
            .unwrap();
        while running.try_recv().is_err() {
            tokio::task::yield_now().await;
        }
        client.stop();
        release.notify_one();
        assert!(run.await.unwrap().is_ok());
        assert_eq!(*seen_at_stop.lock().unwrap(), Some(true));
    }

    #[tokio::test]
    async fn a_failed_start_hook_reaches_on_error_and_run_goes_on() {
        let (client, _updates) = stopped_client().await;
        client.on_start(|_| ready(Err::<(), _>(Error::Unexpected("boom"))));
        let (tx, mut failed) = mpsc::unbounded_channel();
        client.on_error(move |e| {
            let _ = tx.send((e.kind, e.chat_id));
            async {}
        });
        assert!(client.run().await.is_ok());
        assert_eq!(failed.try_recv().ok(), Some((UpdateKind::Start, None)));
    }

    #[tokio::test]
    async fn no_hooks_when_run_fails_before_starting() {
        let (client, _sent) = Client::scripted(vec![Err(Error::Rpc(RpcError::parse(401, "AUTH_KEY_UNREGISTERED")))]);
        let log = Log::default();
        client.on_start(log_hook(&log, "start"));
        client.on_stop(log_hook(&log, "stop"));
        assert!(client.run().await.is_err());
        assert!(log.lock().unwrap().is_empty());
    }

    #[tokio::test]
    async fn a_disconnect_during_the_start_ends_run_with_ok() {
        // `disconnect` stops the client, then fails the start's waiting request with `Disconnected`.
        let (client, _sent) = Client::scripted(vec![Err(Error::Disconnected)]);
        let log = Log::default();
        client.on_start(log_hook(&log, "start"));
        client.on_stop(log_hook(&log, "stop"));
        client.disconnect();
        let result = client.run().await;
        assert!(result.is_ok(), "{result:?}");
        assert!(log.lock().unwrap().is_empty(), "the start never finished, so neither hook runs");
        assert!(!*client.0.stop.borrow(), "a finished run clears the stop");

        // Without a stop, a connection that ends during the start is still an error.
        let (client, _sent) = Client::scripted(vec![Err(Error::Disconnected)]);
        assert!(matches!(client.run().await, Err(Error::Disconnected)));
    }

    #[tokio::test]
    async fn registering_again_replaces_the_hook() {
        let (client, _updates) = stopped_client().await;
        let log = Log::default();
        let first = client.on_start(log_hook(&log, "first")).id();
        client.on_start(log_hook(&log, "second"));
        assert!(!client.remove_handler(first), "a replaced hook is already gone");
        assert!(client.run().await.is_ok());
        assert_eq!(*log.lock().unwrap(), ["second"]);
    }

    #[tokio::test]
    async fn a_removed_start_hook_does_not_run() {
        let (client, _updates) = stopped_client().await;
        let log = Log::default();
        let start = client.on_start(log_hook(&log, "start")).id();
        client.on_stop(log_hook(&log, "stop"));
        assert!(client.remove_handler(start));
        assert!(client.run().await.is_ok());
        assert_eq!(*log.lock().unwrap(), ["stop"]);
    }
}