mtcute/e2e/esm/tests/tl-runtime.js
Alina Tumanova f5976a2d74
ESM + end-to-end tests (#11)
* feat: moved tl-runtime to esm and native ArrayBuffers

* feat: migration to esm

* fix(core): web-related fixes

* test: finally, some good fucking e2e

* chore: fixed linters etc

* ci: added e2e to ci

* build(tl): fixed gen-code on node 20

* fix: codegen Uint8Array, not Buffer

never `git reset --hard` kids

* build: only do type-aware linting for `packages/*`

* build: ignore no-unresolved in ci for e2e

* fix: node 16 doesn't have subtle crypto apparently?

* fix(tests): use Uint8Array

for gods sake please can i just merge this already

* ci: don't parallel tasks in ci

because machines are utter garbage and it may just randomly break

* ci: pass secrets to e2e tests

* ci: separate cli command for ci

apparently im retarded

* fix: run codegen in e2e

im actually retarded

* ci: more fixes for e2e

* ci: debugging stuff

* ci: still debugging

* ci: hopefully fix ci???
2023-10-16 19:23:53 +03:00

122 lines
3.7 KiB
JavaScript

import { expect } from 'chai'
import Long from 'long'
import { describe, it } from 'mocha'
import {
hexDecode,
hexDecodeToBuffer,
hexEncode,
TlBinaryReader,
TlBinaryWriter,
TlSerializationCounter,
} from '@mtcute/tl-runtime'
// here we primarily want to check that everything imports properly,
// and that the code is actually executable. The actual correctness
// of the implementation is covered tested by unit tests
describe('encodings', () => {
it('works with Buffers', () => {
const buf = Buffer.alloc(5)
hexDecode(buf, '0102030405')
expect(hexEncode(Buffer.from('hello'))).to.equal('68656c6c6f')
expect(buf).eql(Buffer.from([1, 2, 3, 4, 5]))
})
it('works with Uint8Arrays', () => {
const buf = new Uint8Array(5)
hexDecode(buf, '0102030405')
expect(hexEncode(new Uint8Array([1, 2, 3, 4, 5]))).to.equal('0102030405')
expect(buf).eql(new Uint8Array([1, 2, 3, 4, 5]))
})
})
describe('TlBinaryReader', () => {
const map = {
'85337187': function (r) {
const ret = {}
ret._ = 'mt_resPQ'
ret.nonce = r.int128()
ret.serverNonce = r.int128()
ret.pq = r.bytes()
ret.serverPublicKeyFingerprints = r.vector(r.long)
return ret
},
}
const data =
'000000000000000001c8831ec97ae55140000000632416053e0549828cca27e966b301a48fece2fca5cf4d33f4a11ea877ba4aa5739073300817ed48941a08f98100000015c4b51c01000000216be86c022bb4c3'
it('should work with Buffers', () => {
const buf = Buffer.from(data, 'hex')
const r = new TlBinaryReader(map, buf, 8)
expect(r.long().toString(16)).to.equal('51e57ac91e83c801')
expect(r.uint()).to.equal(64)
const obj = r.object()
expect(obj._).equal('mt_resPQ')
})
it('should work with Uint8Arrays', () => {
const buf = hexDecodeToBuffer(data)
const r = new TlBinaryReader(map, buf, 8)
expect(r.long().toString(16)).to.equal('51e57ac91e83c801')
expect(r.uint()).to.equal(64)
const obj = r.object()
expect(obj._).equal('mt_resPQ')
})
})
describe('TlBinaryWriter', () => {
const map = {
mt_resPQ: function (w, obj) {
w.uint(85337187)
w.bytes(obj.pq)
w.vector(w.long, obj.serverPublicKeyFingerprints)
},
}
it('should work with Buffers', () => {
const obj = {
_: 'mt_resPQ',
pq: Buffer.from('17ED48941A08F981', 'hex'),
serverPublicKeyFingerprints: [Long.fromString('c3b42b026ce86b21', 16)],
}
expect(TlSerializationCounter.countNeededBytes(map, obj)).to.equal(32)
const w = TlBinaryWriter.alloc(map, 48)
w.long(Long.ZERO)
w.long(Long.fromString('51E57AC91E83C801', true, 16)) // messageId
w.object(obj)
expect(hexEncode(w.result())).eq(
'000000000000000001c8831ec97ae551632416050817ed48941a08f98100000015c4b51c01000000216be86c022bb4c3',
)
})
it('should work with Uint8Arrays', () => {
const obj = {
_: 'mt_resPQ',
pq: hexDecodeToBuffer('17ED48941A08F981'),
serverPublicKeyFingerprints: [Long.fromString('c3b42b026ce86b21', 16)],
}
expect(TlSerializationCounter.countNeededBytes(map, obj)).to.equal(32)
const w = TlBinaryWriter.alloc(map, 48)
w.long(Long.ZERO)
w.long(Long.fromString('51E57AC91E83C801', true, 16)) // messageId
w.object(obj)
expect(hexEncode(w.result())).eq(
'000000000000000001c8831ec97ae551632416050817ed48941a08f98100000015c4b51c01000000216be86c022bb4c3',
)
})
})