mtcute/packages/tl-utils/tests/calculator.spec.ts

67 lines
2.3 KiB
TypeScript
Raw Normal View History

import { expect } from 'chai'
import { describe, it } from 'mocha'
import { calculateStaticSizes } from '../src/calculator.js'
import { parseTlToEntries } from '../src/parse.js'
describe('calculateStaticSizes', () => {
const test = (tl: string, expected: object) => {
expect(calculateStaticSizes(parseTlToEntries(tl))).eql(expected)
}
it('computes for constructors without parameters', () => {
test('auth.logOut = Bool;', { 'auth.logOut': 4 })
})
it('computes for constructors with static parameters', () => {
2023-09-24 01:32:22 +03:00
test('auth.exportAuthorization#e5bfffcd dc_id:int = auth.ExportedAuthorization;', {
'auth.exportAuthorization': 8,
})
})
it('correctly skips true fields', () => {
2023-09-24 01:32:22 +03:00
test('help.promoData#8c39793f flags:# proxy:flags.0?true expires:int = help.PromoData;', {
'help.promoData': 12,
})
})
it('correctly skips constructors with predicated fields', () => {
test(
'help.promoData#8c39793f flags:# proxy:flags.0?true expires:int peer:Peer psa_type:flags.1?string psa_message:flags.2?string = help.PromoData;',
{},
)
})
it('correctly skips constructors with non-static fields', () => {
2023-09-24 01:32:22 +03:00
test('help.promoData#8c39793f psa_type:string psa_message:string = help.PromoData;', {})
})
it('correctly skips constructors with vector fields', () => {
2023-09-24 01:32:22 +03:00
test('help.promoData#8c39793f psa_type:Vector<int> = help.PromoData;', {})
})
it('correctly handles static-sized children', () => {
test(
'peerUser#9db1bc6d user_id:int53 = Peer;\n' +
'help.promoData#8c39793f flags:# proxy:flags.0?true expires:int peer:peerUser = help.PromoData;',
{
peerUser: 12,
'help.promoData': 20,
},
)
})
it('correctly handles static-sized union children', () => {
test(
'peerUser#9db1bc6d user_id:int53 = Peer;\n' +
'peerChannel#9db1bc6d channel_id:int53 = Peer;\n' +
'help.promoData#8c39793f flags:# proxy:flags.0?true expires:int peer:Peer = help.PromoData;',
{
peerUser: 12,
peerChannel: 12,
'help.promoData': 24,
},
)
})
})