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
use std::collections::HashMap;
use std::time::{SystemTime, UNIX_EPOCH};

use crate::Client;
use crate::dispatcher::Entities;
use crate::errors::{Error, Result};
use crate::raw::{base, functions, types};
use crate::storage::{USERNAME_TTL, input_peer};
use crate::types::PeerRef;
use crate::utils::{CHANNEL_OFFSET, chats_of, input_user};

pub(crate) enum ChatPeer {
    Group(i64),
    Channel(base::InputChannel),
}

impl Client {
    /// Returns the TL peer for a chat id, username or invite link, with its access hash, for the raw requests sent
    /// with [`invoke`](Client::invoke).
    ///
    /// Together with `invoke` this is the raw escape hatch: the friendly API never needs it.
    pub async fn resolve_peer(&self, chat_id: impl Into<PeerRef>) -> Result<base::InputPeer> {
        Ok(self.resolve(&chat_id.into()).await?.1)
    }

    pub(crate) async fn resolve(&self, chat: &PeerRef) -> Result<(i64, base::InputPeer)> {
        let chat_id = match chat {
            PeerRef::Id(id) => *id,
            PeerRef::Me => return Ok((self.0.me.lock().unwrap().id, base::InputPeer::InputPeerSelf)),
            PeerRef::Username(name) => self.username_id(name).await?,
            PeerRef::Invite(hash) => {
                let request = functions::messages::CheckChatInvite { hash: String::from(&**hash) };
                let chat = match self.invoke(&request).await? {
                    base::ChatInvite::ChatInviteAlready(c) => c.chat,
                    base::ChatInvite::ChatInvitePeek(c) => c.chat,
                    base::ChatInvite::ChatInvite(_) => return Err(Error::ChatNotJoined),
                };
                let entities = Entities::new(self, Vec::new(), vec![chat]);
                entities.chats.into_keys().next().ok_or(Error::Unexpected("messages.checkChatInvite sent no chat"))?
            }
        };
        Ok((chat_id, self.resolve_id(chat_id).await?))
    }

    /// A username's peer: from memory or the store while the name is fresh, else from Telegram.
    async fn username_id(&self, name: &str) -> Result<i64> {
        let name = name.trim_start_matches('@').to_ascii_lowercase();
        let now = SystemTime::now().duration_since(UNIX_EPOCH).map_or(0, |d| d.as_secs() as i64);
        let fresh = |updated: i64| now.saturating_sub(updated) <= USERNAME_TTL.as_secs() as i64;
        let cached = self.0.peers.lock().unwrap().by_username(&name);
        if let Some((id, _)) = cached.filter(|&(_, updated)| fresh(updated)) {
            return Ok(id);
        }
        match self.0.store.get_peer_by_username(name.clone()).await {
            Ok(Some(record)) => {
                if fresh(record.updated) && input_peer(record.id, record.access_hash).is_some() {
                    let id = record.id;
                    self.0.peers.lock().unwrap().load([record]);
                    return Ok(id);
                }
            }
            Ok(None) => {}
            Err(e) => self
                .0
                .events
                .storage_failed(e.kind(), format_args!("couldn't look @{name} up in storage, asking Telegram: {e}")),
        }
        self.resolve_username(&name).await
    }

    async fn resolve_id(&self, chat_id: i64) -> Result<base::InputPeer> {
        if let Some(peer) = self.known_peer(chat_id).await {
            return Ok(peer);
        }

        let peer = if chat_id > 0 {
            let input = types::InputUser { user_id: chat_id, access_hash: 0 };
            let users = self
                .invoke(&functions::users::GetUsers { id: vec![base::InputUser::InputUser(Box::new(input))] })
                .await?;
            let access_hash = users
                .iter()
                .find_map(|u| match u {
                    base::User::User(u) if u.id == chat_id => u.access_hash,
                    _ => None,
                })
                .ok_or(Error::PeerNotFound(chat_id))?;
            base::InputPeer::InputPeerUser(Box::new(types::InputPeerUser { user_id: chat_id, access_hash }))
        } else if chat_id <= -CHANNEL_OFFSET {
            let channel_id = -chat_id - CHANNEL_OFFSET;
            let input = types::InputChannel { channel_id, access_hash: 0 };
            let chats = self
                .invoke(&functions::channels::GetChannels {
                    id: vec![base::InputChannel::InputChannel(Box::new(input))],
                })
                .await?;
            let access_hash = chats_of(chats)
                .iter()
                .find_map(|c| match c {
                    base::Chat::Channel(c) if c.id == channel_id => c.access_hash,
                    base::Chat::ChannelForbidden(c) if c.id == channel_id => Some(c.access_hash),
                    base::Chat::Community(c) if c.id == channel_id => c.access_hash,
                    base::Chat::CommunityForbidden(c) if c.id == channel_id => c.access_hash,
                    _ => None,
                })
                .ok_or(Error::PeerNotFound(chat_id))?;
            base::InputPeer::InputPeerChannel(Box::new(types::InputPeerChannel { channel_id, access_hash }))
        } else {
            base::InputPeer::InputPeerChat(types::InputPeerChat { chat_id: -chat_id })
        };

        self.0.peers.lock().unwrap().insert(chat_id, peer.clone());
        Ok(peer)
    }

    /// The peer of a user or channel this account can address without a request: from memory, or saved in the
    /// store by an earlier run (one hop to the store thread). A failing engine is reported and counts as unknown.
    /// A peer only known from a message it was seen in is checked against the store first: a hash saved in an
    /// earlier run doesn't depend on that message.
    pub(crate) async fn known_peer(&self, chat_id: i64) -> Option<base::InputPeer> {
        let seen_in_message = match self.0.peers.lock().unwrap().get(&chat_id) {
            Some(
                peer @ (base::InputPeer::InputPeerUserFromMessage(_) | base::InputPeer::InputPeerChannelFromMessage(_)),
            ) => Some(peer.clone()),
            Some(peer) => return Some(peer.clone()),
            None => None,
        };
        if chat_id == self.0.me.lock().unwrap().id {
            return Some(base::InputPeer::InputPeerSelf);
        }
        // Basic groups need no hash.
        if chat_id > 0 || chat_id <= -CHANNEL_OFFSET {
            match self.0.store.get_peer(chat_id).await {
                Ok(Some(record)) => {
                    if let Some(peer) = input_peer(record.id, record.access_hash) {
                        self.0.peers.lock().unwrap().load([record]);
                        return Some(peer);
                    }
                }
                Ok(None) => {}
                Err(e) => self.0.events.storage_failed(
                    e.kind(),
                    format_args!("couldn't look chat {chat_id} up in storage, asking Telegram: {e}"),
                ),
            }
        }
        seen_in_message
    }

    pub(crate) async fn peer_id(&self, chat: PeerRef) -> Result<i64> {
        match chat {
            PeerRef::Id(id) => Ok(id),
            PeerRef::Me => Ok(self.0.me.lock().unwrap().id),
            chat => Ok(self.resolve(&chat).await?.0),
        }
    }

    /// Resolves one at a time, so it's meant for cached ids: each unknown username costs a request.
    pub(crate) async fn resolve_peers(
        &self,
        chats: impl IntoIterator<Item = impl Into<PeerRef>>,
    ) -> Result<Vec<base::InputPeer>> {
        let mut peers = Vec::new();
        for chat in chats {
            peers.push(self.resolve_peer(chat).await?);
        }
        Ok(peers)
    }

    pub(crate) async fn resolve_users(
        &self,
        users: impl IntoIterator<Item = impl Into<PeerRef>>,
    ) -> Result<Vec<base::InputUser>> {
        let mut input = Vec::new();
        for user in users {
            input.push(self.resolve_user(&user.into()).await?);
        }
        Ok(input)
    }

    pub(crate) async fn resolve_user(&self, user: &PeerRef) -> Result<base::InputUser> {
        let (id, peer) = self.resolve(user).await?;
        input_user(peer).ok_or(Error::PeerNotFound(id))
    }

    /// Gives each mention the input user Telegram wants in place of its bare user id. A mention of a user this
    /// account has never met is dropped and its text kept, rather than the whole message failing (a helper bot
    /// captioning a file with the requester's `tg://user` link). Each user is looked up once per call.
    pub(crate) async fn resolve_mentions(
        &self,
        entities: Vec<base::MessageEntity>,
    ) -> Result<Vec<base::MessageEntity>> {
        use base::MessageEntity as E;
        let mut known: HashMap<i64, Option<base::InputUser>> = HashMap::new();
        let mut resolved = Vec::with_capacity(entities.len());
        for entity in entities {
            let (offset, length, id) = match &entity {
                E::MessageEntityMentionName(m) => (m.offset, m.length, m.user_id),
                E::InputMessageEntityMentionName(m) => match &m.user_id {
                    base::InputUser::InputUser(u) => (m.offset, m.length, u.user_id),
                    _ => {
                        resolved.push(entity);
                        continue;
                    }
                },
                _ => {
                    resolved.push(entity);
                    continue;
                }
            };
            let user_id = match known.get(&id) {
                Some(user) => user.clone(),
                None => {
                    let user = self.resolve_mentioned(id).await?;
                    known.insert(id, user.clone());
                    user
                }
            };
            if let Some(user_id) = user_id {
                let mention = types::InputMessageEntityMentionName { offset, length, user_id };
                resolved.push(E::InputMessageEntityMentionName(Box::new(mention)));
            }
        }
        Ok(resolved)
    }

    /// The input user for a mention of `user_id`, or `None` when this account has never met the user: a mention is
    /// dropped and its text kept rather than failing the whole message.
    pub(crate) async fn resolve_mentioned(&self, user_id: i64) -> Result<Option<base::InputUser>> {
        match self.resolve_user(&user_id.into()).await {
            Ok(user) => Ok(Some(user)),
            Err(Error::PeerNotFound(_)) => Ok(None),
            Err(e) if e.is("USER_ID_INVALID") || e.is("PEER_ID_INVALID") => Ok(None),
            Err(e) => Err(e),
        }
    }

    pub(crate) async fn resolve_chat(&self, chat: &PeerRef) -> Result<ChatPeer> {
        match self.resolve(chat).await?.1 {
            base::InputPeer::InputPeerChat(p) => Ok(ChatPeer::Group(p.chat_id)),
            base::InputPeer::InputPeerChannel(p) => {
                let channel = types::InputChannel { channel_id: p.channel_id, access_hash: p.access_hash };
                Ok(ChatPeer::Channel(base::InputChannel::InputChannel(Box::new(channel))))
            }
            // A channel only seen as a min sender: reached through the message it was seen in.
            base::InputPeer::InputPeerChannelFromMessage(p) => {
                let channel =
                    types::InputChannelFromMessage { peer: p.peer, msg_id: p.msg_id, channel_id: p.channel_id };
                Ok(ChatPeer::Channel(base::InputChannel::InputChannelFromMessage(Box::new(channel))))
            }
            _ => Err(Error::InvalidArgument("chat_id is a user, not a group or channel")),
        }
    }

    pub(crate) async fn resolve_channel(&self, chat: &PeerRef) -> Result<base::InputChannel> {
        match self.resolve_chat(chat).await? {
            ChatPeer::Channel(channel) => Ok(channel),
            ChatPeer::Group(_) => Err(Error::InvalidArgument("only supergroups and channels have that")),
        }
    }

    pub(crate) async fn full_chat(&self, chat: ChatPeer) -> Result<(base::ChatFull, Entities)> {
        let base::messages::ChatFull::ChatFull(full) = match chat {
            ChatPeer::Group(chat_id) => self.invoke(&functions::messages::GetFullChat { chat_id }).await?,
            ChatPeer::Channel(channel) => self.invoke(&functions::channels::GetFullChannel { channel }).await?,
        };
        let types::messages::ChatFull { full_chat, chats, users } = *full;
        Ok((full_chat, Entities::new(self, users, chats)))
    }
}

#[cfg(test)]
mod tests {
    use crate::Client;
    use crate::errors::Error;
    use crate::raw::functions::{contacts, messages, users};
    use crate::raw::{Identifiable, Serializable, base, types};
    use crate::types::PeerRef;
    use crate::utils::get_channel_id;

    #[tokio::test]
    async fn resolves_every_kind_of_name() {
        let (client, mut rx) = Client::recording();
        client.0.me.lock().unwrap().id = 1;
        let channel = types::InputPeerChannel { channel_id: 9, access_hash: 1 };
        let channel = base::InputPeer::InputPeerChannel(Box::new(channel));
        client.0.peers.lock().unwrap().insert(get_channel_id(9), channel.clone());
        let group = base::InputPeer::InputPeerChat(types::InputPeerChat { chat_id: 5 });

        let offline = [
            (PeerRef::from("me"), (1, base::InputPeer::InputPeerSelf)),
            (PeerRef::from(1), (1, base::InputPeer::InputPeerSelf)),
            (PeerRef::from(-5), (-5, group)),
            (PeerRef::from("https://t.me/c/9/20"), (get_channel_id(9), channel.clone())),
            (PeerRef::from("-1000000000009"), (get_channel_id(9), channel)),
        ];
        for (chat, expected) in offline {
            assert_eq!(client.resolve(&chat).await.unwrap(), expected, "{chat:?}");
        }
        assert!(rx.try_recv().is_err());

        let online = [
            ("@durov", contacts::ResolveUsername::CONSTRUCTOR_ID),
            ("t.me/durov/5", contacts::ResolveUsername::CONSTRUCTOR_ID),
            ("t.me/+AbC", messages::CheckChatInvite::CONSTRUCTOR_ID),
            ("t.me/joinchat/AbC", messages::CheckChatInvite::CONSTRUCTOR_ID),
        ];
        for (chat, constructor) in online {
            assert!(client.resolve_peer(chat).await.is_err());
            let body = rx.try_recv().expect("no request sent");
            assert_eq!(u32::from_le_bytes(body[..4].try_into().unwrap()), constructor, "{chat}");
        }
    }

    #[tokio::test]
    async fn an_unjoined_invite_link_is_its_own_error() {
        let invite = base::ChatInvite::ChatInvite(Box::new(types::ChatInvite {
            channel: true,
            broadcast: false,
            public: false,
            megagroup: true,
            request_needed: false,
            verified: false,
            scam: false,
            fake: false,
            can_refulfill_subscription: false,
            title: "Private".into(),
            about: None,
            photo: base::Photo::PhotoEmpty(types::PhotoEmpty { id: 0 }),
            participants_count: 3,
            participants: None,
            color: 0,
            subscription_pricing: None,
            subscription_form_id: None,
            bot_verification: None,
        }));
        let (client, _sent) = Client::scripted(vec![Ok(invite.to_bytes())]);
        let result = client.resolve_peer("t.me/+AbC").await;
        assert!(matches!(result, Err(Error::ChatNotJoined)), "{result:?}");
    }

    #[tokio::test]
    async fn a_failing_store_falls_back_to_asking_telegram() {
        let (mut client, mut sent) = Client::scripted(vec![Err(Error::Unexpected("offline"))]);
        let engine = std::sync::Arc::new(crate::storage::Recording::failing());
        std::sync::Arc::get_mut(&mut client.0).unwrap().set_store(engine.clone());
        let result = client.resolve_peer(777).await;
        let body = sent.try_recv().expect("no request sent");
        assert_eq!(u32::from_le_bytes(body[..4].try_into().unwrap()), users::GetUsers::CONSTRUCTOR_ID);
        assert!(matches!(result, Err(Error::Unexpected("offline"))), "{result:?}");
        assert_eq!(engine.calls(), ["get_peer_by_id"]);
    }

    #[tokio::test]
    async fn a_failing_store_sends_names_and_numbers_to_telegram() {
        let offline = || Err(Error::Unexpected("offline"));
        let (mut client, mut sent) = Client::scripted(vec![offline(), offline()]);
        let engine = std::sync::Arc::new(crate::storage::Recording::failing());
        std::sync::Arc::get_mut(&mut client.0).unwrap().set_store(engine.clone());
        let events = client.0.events.record();
        let by_name = client.resolve_peer("@alice").await;
        let by_phone = client.resolve_phone("+1 555 0100").await;
        assert!(matches!(by_name, Err(Error::Unexpected("offline"))), "{by_name:?}");
        assert!(matches!(by_phone, Err(Error::Unexpected("offline"))), "{by_phone:?}");
        let constructor = |body: Vec<u8>| u32::from_le_bytes(body[..4].try_into().unwrap());
        assert_eq!(constructor(sent.try_recv().unwrap()), contacts::ResolveUsername::CONSTRUCTOR_ID);
        assert_eq!(constructor(sent.try_recv().unwrap()), contacts::ResolvePhone::CONSTRUCTOR_ID);
        assert_eq!(engine.calls(), ["get_peer_by_username", "get_peer_by_phone_number"]);
        let reported: Vec<String> = events
            .try_iter()
            .map(|e| match e {
                crate::enums::ClientEvent::StorageFailed { error } => error.to_string(),
                other => panic!("{other:?}"),
            })
            .collect();
        assert_eq!(
            reported,
            [
                "couldn't look @alice up in storage, asking Telegram: store is down",
                "couldn't look a phone number up in storage, asking Telegram: store is down",
            ]
        );
    }

    fn user_named(id: i64, username: &str) -> base::User {
        let mut buf = Vec::new();
        // flags: access_hash (bit 0) and username (bit 3)
        0b1001u32.serialize(&mut buf);
        0u32.serialize(&mut buf);
        id.serialize(&mut buf);
        7i64.serialize(&mut buf);
        username.to_owned().serialize(&mut buf);
        base::User::User(Box::new(types::User::deserialize_bare(&mut &buf[..]).unwrap()))
    }

    #[tokio::test]
    async fn a_fresh_username_is_resolved_without_a_request() {
        let (client, mut sent) = Client::scripted(Vec::new());
        crate::dispatcher::Entities::new(&client, vec![user_named(100, "Alice")], Vec::new());
        let (id, _) = client.resolve(&PeerRef::Username("@alice".into())).await.unwrap();
        assert_eq!(id, 100);
        assert!(sent.try_recv().is_err(), "a request went out");
    }

    #[tokio::test]
    async fn a_stale_username_is_looked_up_again() {
        let (client, mut sent) = Client::scripted(vec![Err(Error::Unexpected("offline"))]);
        let mut old = crate::storage::PeerRecord::new(100, 7);
        (old.usernames, old.updated) = (vec!["alice".into()], 1);
        client.0.peers.lock().unwrap().load([old]);
        let _ = client.resolve(&PeerRef::Username("alice".into())).await;
        assert_eq!(
            sent.try_recv().map(|b| u32::from_le_bytes(b[..4].try_into().unwrap())).ok(),
            Some(contacts::ResolveUsername::CONSTRUCTOR_ID)
        );
    }

    #[tokio::test]
    async fn a_stale_name_confirmed_by_telegram_is_fresh_again() {
        let peer = base::Peer::PeerUser(types::PeerUser { user_id: 100 });
        let resolved = types::contacts::ResolvedPeer { peer, chats: Vec::new(), users: vec![user_named(100, "alice")] };
        let reply = base::contacts::ResolvedPeer::ResolvedPeer(Box::new(resolved)).to_bytes();
        let (client, mut sent) = Client::scripted(vec![Ok(reply)]);
        let mut old = crate::storage::PeerRecord::new(100, 7);
        let nine_hours_ago = std::time::SystemTime::now() - std::time::Duration::from_secs(9 * 60 * 60);
        let then = nine_hours_ago.duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() as i64;
        (old.usernames, old.updated) = (vec!["alice".into()], then);
        client.0.peers.lock().unwrap().load([old]);

        for _ in 0..2 {
            let (id, _) = client.resolve(&PeerRef::Username("alice".into())).await.unwrap();
            assert_eq!(id, 100);
        }
        assert!(sent.try_recv().is_ok(), "the stale name was looked up");
        assert!(sent.try_recv().is_err(), "the confirmed name was looked up again");
    }

    #[tokio::test]
    async fn a_min_channel_is_reached_through_its_message() {
        let (client, mut sent) = Client::scripted(vec![Err(Error::Unexpected("offline"))]);
        let group = types::InputPeerChannel { channel_id: 9, access_hash: 1 };
        let group = base::InputPeer::InputPeerChannel(Box::new(group));
        let seen = types::InputPeerChannelFromMessage { peer: group.clone(), msg_id: 42, channel_id: 55 };
        let seen = base::InputPeer::InputPeerChannelFromMessage(Box::new(seen));
        client.0.peers.lock().unwrap().insert(get_channel_id(55), seen);

        let result = client.get_chat(get_channel_id(55)).await;
        assert!(matches!(result, Err(Error::Unexpected("offline"))), "{result:?}");
        let channel = types::InputChannelFromMessage { peer: group, msg_id: 42, channel_id: 55 };
        let channel = base::InputChannel::InputChannelFromMessage(Box::new(channel));
        assert_eq!(sent.try_recv().unwrap(), crate::raw::functions::channels::GetFullChannel { channel }.to_bytes());
    }

    fn community(id: i64, access_hash: i64) -> base::Chat {
        base::Chat::Community(Box::new(types::Community {
            creator: false,
            left: false,
            min: false,
            collapsed_in_dialogs: false,
            id,
            access_hash: Some(access_hash),
            title: "Hub".into(),
            photo: base::ChatPhoto::ChatPhotoEmpty,
            date: 0,
            admin_rights: None,
            default_banned_rights: None,
        }))
    }

    #[tokio::test]
    async fn a_community_keeps_its_access_hash() {
        let chats = types::messages::Chats { chats: vec![community(71, 4)] };
        let (client, mut sent) = Client::scripted(vec![Ok(base::messages::Chats::Chats(Box::new(chats)).to_bytes())]);
        crate::dispatcher::Entities::new(&client, Vec::new(), vec![community(70, 3)]);
        let peer = |channel_id, access_hash| {
            base::InputPeer::InputPeerChannel(Box::new(types::InputPeerChannel { channel_id, access_hash }))
        };
        assert_eq!(client.resolve_peer(get_channel_id(70)).await.unwrap(), peer(70, 3));
        assert!(sent.try_recv().is_err(), "a request went out for a community already seen");
        assert_eq!(client.resolve_peer(get_channel_id(71)).await.unwrap(), peer(71, 4));
    }

    #[tokio::test]
    async fn a_stored_hash_wins_over_a_message_context() {
        use crate::storage::{MemoryStorage, PeerRecord, Storage};

        let engine = std::sync::Arc::new(MemoryStorage::new());
        engine.update_peers(&[PeerRecord::new(7, 5)]).await.unwrap();
        let (mut client, mut sent) = Client::scripted(Vec::new());
        std::sync::Arc::get_mut(&mut client.0).unwrap().set_store(engine);
        let group =
            base::InputPeer::InputPeerChannel(Box::new(types::InputPeerChannel { channel_id: 9, access_hash: 1 }));
        let seen = types::InputPeerUserFromMessage { peer: group, msg_id: 42, user_id: 7 };
        client.0.peers.lock().unwrap().insert(7, base::InputPeer::InputPeerUserFromMessage(Box::new(seen)));
        let full = base::InputPeer::InputPeerUser(Box::new(types::InputPeerUser { user_id: 7, access_hash: 5 }));
        assert_eq!(client.resolve_peer(7).await.unwrap(), full);
        assert_eq!(client.0.peers.lock().unwrap().get(&7), Some(&full), "the stored hash replaced the context");
        assert!(sent.try_recv().is_err());
    }
}