2023-11-09 00:20:43 +03:00
|
|
|
import { describe, expect, it } from 'vitest'
|
2023-11-08 17:28:45 +03:00
|
|
|
|
2023-11-29 20:31:18 +03:00
|
|
|
import { hexEncode } from '@mtcute/core/utils.js'
|
|
|
|
|
2023-11-09 00:20:43 +03:00
|
|
|
import { StubTelegramClient } from './client.js'
|
|
|
|
import { createStub } from './stub.js'
|
2023-11-08 17:28:45 +03:00
|
|
|
|
|
|
|
describe('client stub', () => {
|
|
|
|
it('should correctly intercept rpc calls', async () => {
|
|
|
|
const client = new StubTelegramClient()
|
|
|
|
|
|
|
|
const stubConfig = createStub('config')
|
2023-11-17 00:17:03 +03:00
|
|
|
client.respondWith('help.getConfig', () => stubConfig)
|
2023-11-08 17:28:45 +03:00
|
|
|
|
|
|
|
await client.with(async () => {
|
|
|
|
const result = await client.call({ _: 'help.getConfig' })
|
2023-11-09 00:20:43 +03:00
|
|
|
expect(result).toEqual(stubConfig)
|
2023-11-08 17:28:45 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should correctly decrypt intercepted raw messages', async () => {
|
|
|
|
const log: string[] = []
|
|
|
|
|
|
|
|
const client = new StubTelegramClient()
|
|
|
|
|
|
|
|
client.onRawMessage((msg) => {
|
2023-11-29 20:31:18 +03:00
|
|
|
log.push(`message ctor=${hexEncode(msg.subarray(0, 4))}`)
|
2023-11-08 17:28:45 +03:00
|
|
|
client.close().catch(() => {})
|
|
|
|
})
|
|
|
|
|
|
|
|
await client.with(async () => {
|
|
|
|
await client.call({ _: 'help.getConfig' }).catch(() => {}) // ignore "client closed" error
|
|
|
|
|
2023-11-09 00:20:43 +03:00
|
|
|
expect(log).toEqual([
|
2023-11-08 17:28:45 +03:00
|
|
|
'message ctor=dcf8f173', // msg_container
|
|
|
|
])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|