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

42 lines
1.3 KiB
TypeScript
Raw Normal View History

import { describe, expect, it } from 'vitest'
import { hex } from '@fuman/utils'
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-08-21 11:05:07 +03:00
// for some reason, this test fails in browser. todo: investigate
if (import.meta.env.TEST_ENV !== 'browser') {
it('should correctly decrypt intercepted raw messages', async () => {
const log: string[] = []
2023-11-08 17:28:45 +03:00
2024-08-21 11:05:07 +03:00
const client = new StubTelegramClient()
2023-11-08 17:28:45 +03:00
2024-08-21 11:05:07 +03:00
client.onRawMessage((msg) => {
log.push(`message ctor=${hex.encode(msg.subarray(0, 4))}`)
2024-08-21 11:05:07 +03:00
client.close().catch(() => {})
})
2023-11-08 17:28:45 +03:00
2024-08-21 11:05:07 +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-08-21 11:05:07 +03:00
expect(log).toEqual([
'message ctor=dcf8f173', // msg_container
])
})
2023-11-08 17:28:45 +03:00
})
2024-08-21 11:05:07 +03:00
}
2023-11-08 17:28:45 +03:00
})