2021-04-08 12:19:38 +03:00
|
|
|
import { expect } from 'chai'
|
2023-06-05 03:30:48 +03:00
|
|
|
import { describe, it } from 'mocha'
|
2022-06-30 16:32:56 +03:00
|
|
|
|
2023-10-16 19:23:53 +03:00
|
|
|
import { hexDecodeToBuffer, hexEncode } from '@mtcute/tl-runtime'
|
|
|
|
|
|
|
|
import { IntermediatePacketCodec, TransportError } from '../../src/index.js'
|
2021-04-08 12:19:38 +03:00
|
|
|
|
|
|
|
describe('IntermediatePacketCodec', () => {
|
|
|
|
it('should return correct tag', () => {
|
2023-10-16 19:23:53 +03:00
|
|
|
expect(hexEncode(new IntermediatePacketCodec().tag())).eq('eeeeeeee')
|
2021-04-08 12:19:38 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should correctly parse immediate framing', (done) => {
|
|
|
|
const codec = new IntermediatePacketCodec()
|
2023-10-16 19:23:53 +03:00
|
|
|
codec.on('packet', (data: Uint8Array) => {
|
2021-04-08 12:19:38 +03:00
|
|
|
expect([...data]).eql([5, 1, 2, 3, 4])
|
|
|
|
done()
|
|
|
|
})
|
2023-10-16 19:23:53 +03:00
|
|
|
codec.feed(hexDecodeToBuffer('050000000501020304'))
|
2021-04-08 12:19:38 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should correctly parse incomplete framing', (done) => {
|
|
|
|
const codec = new IntermediatePacketCodec()
|
2023-10-16 19:23:53 +03:00
|
|
|
codec.on('packet', (data: Uint8Array) => {
|
2021-04-08 12:19:38 +03:00
|
|
|
expect([...data]).eql([5, 1, 2, 3, 4])
|
|
|
|
done()
|
|
|
|
})
|
2023-10-16 19:23:53 +03:00
|
|
|
codec.feed(hexDecodeToBuffer('050000000501'))
|
|
|
|
codec.feed(hexDecodeToBuffer('020304'))
|
2021-04-08 12:19:38 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should correctly parse multiple streamed packets', (done) => {
|
|
|
|
const codec = new IntermediatePacketCodec()
|
|
|
|
|
|
|
|
let number = 0
|
|
|
|
|
2023-10-16 19:23:53 +03:00
|
|
|
codec.on('packet', (data: Uint8Array) => {
|
2021-04-08 12:19:38 +03:00
|
|
|
if (number === 0) {
|
|
|
|
expect([...data]).eql([5, 1, 2, 3, 4])
|
|
|
|
number = 1
|
|
|
|
} else {
|
|
|
|
expect([...data]).eql([3, 1, 2, 3, 1])
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
})
|
2023-10-16 19:23:53 +03:00
|
|
|
codec.feed(hexDecodeToBuffer('050000000501'))
|
|
|
|
codec.feed(hexDecodeToBuffer('020304050000'))
|
|
|
|
codec.feed(hexDecodeToBuffer('000301020301'))
|
2021-04-08 12:19:38 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should correctly parse transport errors', (done) => {
|
|
|
|
const codec = new IntermediatePacketCodec()
|
|
|
|
|
2023-09-03 02:37:51 +03:00
|
|
|
codec.on('error', (err: TransportError) => {
|
2021-04-08 12:19:38 +03:00
|
|
|
expect(err).to.have.instanceOf(TransportError)
|
|
|
|
expect(err.code).eq(404)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
2023-10-16 19:23:53 +03:00
|
|
|
codec.feed(hexDecodeToBuffer('040000006cfeffff'))
|
2021-04-08 12:19:38 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should reset when called reset()', (done) => {
|
|
|
|
const codec = new IntermediatePacketCodec()
|
|
|
|
|
2023-10-16 19:23:53 +03:00
|
|
|
codec.on('packet', (data: Uint8Array) => {
|
2021-04-08 12:19:38 +03:00
|
|
|
expect([...data]).eql([1, 2, 3, 4, 5])
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
2023-10-16 19:23:53 +03:00
|
|
|
codec.feed(hexDecodeToBuffer('ff0000001234567812345678'))
|
2021-04-08 12:19:38 +03:00
|
|
|
codec.reset()
|
2023-10-16 19:23:53 +03:00
|
|
|
codec.feed(hexDecodeToBuffer('050000000102030405'))
|
2021-04-08 12:19:38 +03:00
|
|
|
})
|
|
|
|
})
|