From 9622d2c0a2f66a26897c55ed380691bb9334156d Mon Sep 17 00:00:00 2001 From: alina sireneva Date: Tue, 3 Dec 2024 05:29:25 +0300 Subject: [PATCH] test(core): fixed resolvePeer tests --- .../methods/users/resolve-peer.test.ts | 78 ++++++++++++++++++- 1 file changed, 76 insertions(+), 2 deletions(-) diff --git a/packages/core/src/highlevel/methods/users/resolve-peer.test.ts b/packages/core/src/highlevel/methods/users/resolve-peer.test.ts index 5a6f2d74..86b26bc0 100644 --- a/packages/core/src/highlevel/methods/users/resolve-peer.test.ts +++ b/packages/core/src/highlevel/methods/users/resolve-peer.test.ts @@ -140,9 +140,44 @@ describe('resolvePeer', () => { }) }) - it('should throw if not in storage', async () => { + it('should return with zero hash for bots if not in storage', async () => { + const client = StubTelegramClient.offline() + + await client.storage.self.storeFrom(createStub('user', { + bot: true, + })) + + expect(await resolvePeer(client, 123)).toEqual({ + _: 'inputPeerUser', + userId: 123, + accessHash: Long.ZERO, + }) + }) + + it('should try fetching with zero hash for users if not in storage', async () => { const client = new StubTelegramClient() + const handler = vi.fn().mockResolvedValue([ + createStub('user', { + id: 123, + accessHash: Long.fromBits(456, 789), + }), + ]) + client.respondWith('users.getUsers', handler) + + expect(await resolvePeer(client, 123)).toEqual({ + _: 'inputPeerUser', + userId: 123, + accessHash: Long.fromBits(456, 789), + }) + }) + + it('should throw if zero hash fetch failed', async () => { + const client = new StubTelegramClient() + + const handler = vi.fn().mockResolvedValue([]) + client.respondWith('users.getUsers', handler) + await expect(resolvePeer(client, 123)).rejects.toThrow(MtPeerNotFoundError) }) }) @@ -192,9 +227,48 @@ describe('resolvePeer', () => { }) }) - it('should throw if not in storage', async () => { + it('should return with zero hash for bots if not in storage', async () => { + const client = StubTelegramClient.offline() + + await client.storage.self.storeFrom(createStub('user', { + bot: true, + })) + + expect(await resolvePeer(client, -1000000000123)).toEqual({ + _: 'inputPeerChannel', + channelId: 123, + accessHash: Long.ZERO, + }) + }) + + it('should try fetching with zero hash for users if not in storage', async () => { const client = new StubTelegramClient() + const handler = vi.fn().mockResolvedValue({ + chats: [ + createStub('channel', { + id: 123, + accessHash: Long.fromBits(456, 789), + }), + ], + }) + client.respondWith('channels.getChannels', handler) + + expect(await resolvePeer(client, -1000000000123)).toEqual({ + _: 'inputPeerChannel', + channelId: 123, + accessHash: Long.fromBits(456, 789), + }) + }) + + it('should throw if zero hash fetch failed', async () => { + const client = new StubTelegramClient() + + const handler = vi.fn().mockResolvedValue({ + chats: [], + }) + client.respondWith('channels.getChannels', handler) + await expect(resolvePeer(client, -1000000000123)).rejects.toThrow(MtPeerNotFoundError) }) })