2023-11-09 00:20:43 +03:00
|
|
|
import { describe, expect, it } from 'vitest'
|
2023-11-08 17:28:45 +03:00
|
|
|
|
|
|
|
import { BaseTelegramClient } from '@mtcute/core'
|
2023-11-30 23:39:53 +03:00
|
|
|
import { MemoryStorage } from '@mtcute/core/src/storage/memory.js'
|
2023-11-08 17:28:45 +03:00
|
|
|
|
2023-11-09 00:20:43 +03:00
|
|
|
import { createStub } from './stub.js'
|
|
|
|
import { StubTelegramTransport } from './transport.js'
|
2023-11-08 17:28:45 +03:00
|
|
|
|
|
|
|
describe('transport stub', () => {
|
|
|
|
it('should correctly intercept calls', async () => {
|
|
|
|
const log: string[] = []
|
|
|
|
|
|
|
|
const client = new BaseTelegramClient({
|
|
|
|
apiId: 0,
|
|
|
|
apiHash: '',
|
|
|
|
logLevel: 0,
|
|
|
|
defaultDcs: {
|
|
|
|
main: createStub('dcOption', { ipAddress: '1.2.3.4', port: 1234 }),
|
|
|
|
media: createStub('dcOption', { ipAddress: '1.2.3.4', port: 5678 }),
|
|
|
|
},
|
2023-11-30 23:39:53 +03:00
|
|
|
storage: new MemoryStorage(),
|
2023-11-08 17:28:45 +03:00
|
|
|
transport: () =>
|
|
|
|
new StubTelegramTransport({
|
|
|
|
onConnect: (dc, testMode) => {
|
|
|
|
log.push(`connect ${dc.ipAddress}:${dc.port} test=${testMode}`)
|
2023-11-09 00:20:43 +03:00
|
|
|
client.close().catch(() => {})
|
2023-11-08 17:28:45 +03:00
|
|
|
},
|
|
|
|
onMessage(msg) {
|
|
|
|
log.push(`message size=${msg.length}`)
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
|
2023-11-09 00:20:43 +03:00
|
|
|
await client.connect().catch(() => {}) // ignore "client closed" error
|
2023-11-08 17:28:45 +03:00
|
|
|
|
2023-11-09 00:20:43 +03:00
|
|
|
expect(log).toEqual([
|
2023-11-08 17:28:45 +03:00
|
|
|
'message size=40', // req_pq_multi
|
|
|
|
'connect 1.2.3.4:1234 test=false',
|
|
|
|
])
|
|
|
|
})
|
|
|
|
})
|