mtcute/packages/test/src/client.test.ts

39 lines
1.2 KiB
TypeScript
Raw Normal View History

import { hex } from '@fuman/utils'
import { describe, expect, it } from 'vitest'
2023-11-29 20:31:18 +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')
client.respondWith('help.getConfig', () => stubConfig)
2023-11-08 17:28:45 +03:00
await client.with(async () => {
const result = await client.call({ _: 'help.getConfig' })
expect(result).toEqual(stubConfig)
2023-11-08 17:28:45 +03:00
})
})
2024-09-19 01:31:42 +03:00
it('should correctly decrypt intercepted raw messages', async () => {
const log: string[] = []
2023-11-08 17:28:45 +03:00
2024-09-19 01:31:42 +03:00
const client = new StubTelegramClient()
2023-11-08 17:28:45 +03:00
2024-09-19 01:31:42 +03:00
client.onRawMessage((msg) => {
log.push(`message ctor=${hex.encode(msg.subarray(0, 4))}`)
client.close().catch(() => {})
})
2023-11-08 17:28:45 +03:00
2024-09-19 01:31:42 +03:00
await client.with(async () => {
await client.call({ _: 'help.getConfig' }).catch(() => {}) // ignore "client closed" error
2023-11-08 17:28:45 +03:00
2024-09-19 01:31:42 +03:00
expect(log).toEqual([
'message ctor=dcf8f173', // msg_container
])
2023-11-08 17:28:45 +03:00
})
2024-09-19 01:31:42 +03:00
})
2023-11-08 17:28:45 +03:00
})