test: fixed some tests

This commit is contained in:
alina 🌸 2024-09-07 19:17:48 +03:00
parent f3c0daa835
commit 2ac7cbd35b
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
13 changed files with 115 additions and 128 deletions

View file

@ -23,15 +23,16 @@ describe('gramjs/convert', () => {
ipAddress: '149.154.167.40', ipAddress: '149.154.167.40',
ipv6: false, ipv6: false,
port: 443, port: 443,
testMode: true,
}, },
media: { media: {
id: 2, id: 2,
ipAddress: '149.154.167.40', ipAddress: '149.154.167.40',
ipv6: false, ipv6: false,
port: 443, port: 443,
},
},
testMode: true, testMode: true,
},
},
version: 3, version: 3,
}) })
}) })

View file

@ -22,14 +22,15 @@ describe('mtkruto/convert', () => {
id: 2, id: 2,
ipAddress: '149.154.167.40', ipAddress: '149.154.167.40',
port: 443, port: 443,
testMode: true,
}, },
media: { media: {
id: 2, id: 2,
ipAddress: '149.154.167.40', ipAddress: '149.154.167.40',
port: 443, port: 443,
},
},
testMode: true, testMode: true,
},
},
version: 3, version: 3,
}) })
}) })

View file

@ -22,11 +22,13 @@ describe('pyrogram/convert', () => {
id: 2, id: 2,
ipAddress: '149.154.167.40', ipAddress: '149.154.167.40',
port: 443, port: 443,
testMode: true,
}, },
media: { media: {
id: 2, id: 2,
ipAddress: '149.154.167.40', ipAddress: '149.154.167.40',
port: 443, port: 443,
testMode: true,
}, },
}, },
self: { self: {
@ -35,7 +37,6 @@ describe('pyrogram/convert', () => {
userId: 5000801609, userId: 5000801609,
usernames: [], usernames: [],
}, },
testMode: true,
version: 3, version: 3,
}) })
}) })

View file

@ -23,15 +23,16 @@ describe('telethon/convert', () => {
ipAddress: '149.154.167.40', ipAddress: '149.154.167.40',
ipv6: false, ipv6: false,
port: 80, port: 80,
testMode: true,
}, },
media: { media: {
id: 2, id: 2,
ipAddress: '149.154.167.40', ipAddress: '149.154.167.40',
ipv6: false, ipv6: false,
port: 80, port: 80,
},
},
testMode: true, testMode: true,
},
},
version: 3, version: 3,
}) })
}) })

View file

@ -28,7 +28,7 @@ describe('telethon/parse', () => {
it('should correctly parse ipv6 sessions', () => { it('should correctly parse ipv6 sessions', () => {
expect(parseTelethonSession(TELETHON_TEST_SESSION_V6)).toEqual({ expect(parseTelethonSession(TELETHON_TEST_SESSION_V6)).toEqual({
dcId: 1, dcId: 1,
ipAddress: '2001:0b28:f23d:f001:0000:0000:0000:000e', ipAddress: '2001:b28:f23d:f001::e',
port: 443, port: 443,
ipv6: true, ipv6: true,
authKey: hex.decode( authKey: hex.decode(

View file

@ -1,7 +1,7 @@
import type { tl } from '@mtcute/tl' import type { tl } from '@mtcute/tl'
import { TlBinaryReader, TlBinaryWriter } from '@mtcute/tl-runtime' import { TlBinaryReader, TlBinaryWriter } from '@mtcute/tl-runtime'
import { base64 } from '@fuman/utils'
import { getPlatform } from '../../platform.js'
import { assertNever } from '../../types/utils.js' import { assertNever } from '../../types/utils.js'
/** /**
@ -10,7 +10,7 @@ import { assertNever } from '../../types/utils.js'
* @param id Inline message ID * @param id Inline message ID
*/ */
export function parseInlineMessageId(id: string): tl.TypeInputBotInlineMessageID { export function parseInlineMessageId(id: string): tl.TypeInputBotInlineMessageID {
const buf = getPlatform().base64Decode(id, true) const buf = base64.decode(id, true)
const reader = TlBinaryReader.manual(buf) const reader = TlBinaryReader.manual(buf)
if (buf.length === 20) { if (buf.length === 20) {
@ -57,7 +57,7 @@ export function encodeInlineMessageId(id: tl.TypeInputBotInlineMessageID): strin
assertNever(id) assertNever(id)
} }
return getPlatform().base64Encode(writer.result(), true) return base64.encode(writer.result(), true)
} }
export function normalizeInlineId(id: string | tl.TypeInputBotInlineMessageID): tl.TypeInputBotInlineMessageID { export function normalizeInlineId(id: string | tl.TypeInputBotInlineMessageID): tl.TypeInputBotInlineMessageID {

View file

@ -2,8 +2,7 @@
/* eslint-disable ts/no-unsafe-call,ts/no-unsafe-argument */ /* eslint-disable ts/no-unsafe-call,ts/no-unsafe-argument */
import Long from 'long' import Long from 'long'
import { base64 } from '@fuman/utils'
import { getPlatform } from '../../platform.js'
const customInspectSymbol = Symbol.for('nodejs.util.inspect.custom') const customInspectSymbol = Symbol.for('nodejs.util.inspect.custom')
@ -57,7 +56,7 @@ export function makeInspectable<T>(
if (val && typeof val === 'object') { if (val && typeof val === 'object') {
if (val instanceof Uint8Array) { if (val instanceof Uint8Array) {
val = getPlatform().base64Encode(val) val = base64.encode(val)
} else if (Long.isLong(val)) { } else if (Long.isLong(val)) {
val = val.toString() val = val.toString()
} else if (typeof val.toJSON === 'function') { } else if (typeof val.toJSON === 'function') {

View file

@ -17,6 +17,16 @@ const stubDcs = {
mediaOnly: true, mediaOnly: true,
}, },
} }
const stubDcsTest = {
main: {
...stubDcs.main,
testMode: true,
},
media: {
...stubDcs.media,
testMode: true,
},
}
const stubDcsBasic = { const stubDcsBasic = {
main: { main: {
id: 2, id: 2,
@ -24,6 +34,7 @@ const stubDcsBasic = {
ipv6: false, ipv6: false,
mediaOnly: false, mediaOnly: false,
port: 443, port: 443,
testMode: false,
}, },
media: { media: {
id: 2, id: 2,
@ -31,6 +42,17 @@ const stubDcsBasic = {
ipv6: false, ipv6: false,
mediaOnly: true, mediaOnly: true,
port: 443, port: 443,
testMode: false,
},
}
const stubDcsBasicTest = {
main: {
...stubDcsBasic.main,
testMode: true,
},
media: {
...stubDcsBasic.media,
testMode: true,
}, },
} }
const stubDcsSameMedia = { const stubDcsSameMedia = {
@ -47,24 +69,22 @@ describe('writeStringSession', () => {
expect( expect(
writeStringSession({ writeStringSession({
version: 3, version: 3,
testMode: false,
primaryDcs: stubDcsBasic, primaryDcs: stubDcsBasic,
authKey: stubAuthKey, authKey: stubAuthKey,
}), }),
).toMatchInlineSnapshot( ).toMatchInlineSnapshot(
'"AwQAAAAXAQIADjE0OS4xNTQuMTY3LjUwALsBAAAXAQICDzE0OS4xNTQuMTY3LjIyMrsBAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"', '"AwQAAAAXAgIADjE0OS4xNTQuMTY3LjUwALsBAAAXAgICDzE0OS4xNTQuMTY3LjIyMrsBAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"',
) )
}) })
it('should write production string session without user with same dc for media', () => { it('should write production string session without user with same dc for media', () => {
expect( expect(
writeStringSession({ writeStringSession({
version: 3, version: 3,
testMode: false,
primaryDcs: stubDcsBasicSameMedia, primaryDcs: stubDcsBasicSameMedia,
authKey: stubAuthKey, authKey: stubAuthKey,
}), }),
).toMatchInlineSnapshot( ).toMatchInlineSnapshot(
'"AwAAAAAXAQIADjE0OS4xNTQuMTY3LjUwALsBAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"', '"AwAAAAAXAgIADjE0OS4xNTQuMTY3LjUwALsBAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"',
) )
}) })
@ -72,7 +92,6 @@ describe('writeStringSession', () => {
expect( expect(
writeStringSession({ writeStringSession({
version: 3, version: 3,
testMode: false,
primaryDcs: stubDcsBasic, primaryDcs: stubDcsBasic,
authKey: stubAuthKey, authKey: stubAuthKey,
self: { self: {
@ -83,7 +102,7 @@ describe('writeStringSession', () => {
}, },
}), }),
).toMatchInlineSnapshot( ).toMatchInlineSnapshot(
'"AwUAAAAXAQIADjE0OS4xNTQuMTY3LjUwALsBAAAXAQICDzE0OS4xNTQuMTY3LjIyMrsBAAA5MAAAAAAAADeXebwgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"', '"AwUAAAAXAgIADjE0OS4xNTQuMTY3LjUwALsBAAAXAgICDzE0OS4xNTQuMTY3LjIyMrsBAAA5MAAAAAAAADeXebwgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"',
) )
}) })
@ -91,8 +110,7 @@ describe('writeStringSession', () => {
expect( expect(
writeStringSession({ writeStringSession({
version: 3, version: 3,
testMode: true, primaryDcs: stubDcsBasicTest,
primaryDcs: stubDcsBasic,
authKey: stubAuthKey, authKey: stubAuthKey,
self: { self: {
userId: 12345, userId: 12345,
@ -102,7 +120,7 @@ describe('writeStringSession', () => {
}, },
}), }),
).toMatchInlineSnapshot( ).toMatchInlineSnapshot(
'"AwcAAAAXAQIADjE0OS4xNTQuMTY3LjUwALsBAAAXAQICDzE0OS4xNTQuMTY3LjIyMrsBAAA5MAAAAAAAADeXebwgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"', '"AwUAAAAXAgIEDjE0OS4xNTQuMTY3LjUwALsBAAAXAgIGDzE0OS4xNTQuMTY3LjIyMrsBAAA5MAAAAAAAADeXebwgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"',
) )
}) })
}) })
@ -116,7 +134,6 @@ describe('readStringSession', () => {
), ),
).toEqual({ ).toEqual({
version: 3, version: 3,
testMode: false,
primaryDcs: stubDcsBasic, primaryDcs: stubDcsBasic,
authKey: stubAuthKey, authKey: stubAuthKey,
self: null, self: null,
@ -130,7 +147,6 @@ describe('readStringSession', () => {
), ),
).toEqual({ ).toEqual({
version: 3, version: 3,
testMode: false,
primaryDcs: stubDcsBasicSameMedia, primaryDcs: stubDcsBasicSameMedia,
authKey: stubAuthKey, authKey: stubAuthKey,
self: null, self: null,
@ -144,7 +160,6 @@ describe('readStringSession', () => {
), ),
).toEqual({ ).toEqual({
version: 3, version: 3,
testMode: false,
primaryDcs: stubDcsBasic, primaryDcs: stubDcsBasic,
authKey: stubAuthKey, authKey: stubAuthKey,
self: { self: {
@ -163,8 +178,7 @@ describe('readStringSession', () => {
), ),
).toEqual({ ).toEqual({
version: 3, version: 3,
testMode: true, primaryDcs: stubDcsBasicTest,
primaryDcs: stubDcsBasic,
authKey: stubAuthKey, authKey: stubAuthKey,
self: { self: {
userId: 12345, userId: 12345,
@ -184,7 +198,6 @@ describe('readStringSession', () => {
), ),
).toEqual({ ).toEqual({
version: 2, version: 2,
testMode: false,
primaryDcs: stubDcs, primaryDcs: stubDcs,
authKey: stubAuthKey, authKey: stubAuthKey,
self: null, self: null,
@ -198,7 +211,6 @@ describe('readStringSession', () => {
), ),
).toEqual({ ).toEqual({
version: 2, version: 2,
testMode: false,
primaryDcs: stubDcsSameMedia, primaryDcs: stubDcsSameMedia,
authKey: stubAuthKey, authKey: stubAuthKey,
self: null, self: null,
@ -212,7 +224,6 @@ describe('readStringSession', () => {
), ),
).toEqual({ ).toEqual({
version: 2, version: 2,
testMode: false,
primaryDcs: stubDcs, primaryDcs: stubDcs,
authKey: stubAuthKey, authKey: stubAuthKey,
self: { self: {
@ -231,8 +242,7 @@ describe('readStringSession', () => {
), ),
).toEqual({ ).toEqual({
version: 2, version: 2,
testMode: true, primaryDcs: stubDcsTest,
primaryDcs: stubDcs,
authKey: stubAuthKey, authKey: stubAuthKey,
self: { self: {
userId: 12345, userId: 12345,
@ -252,7 +262,6 @@ describe('readStringSession', () => {
), ),
).toEqual({ ).toEqual({
version: 1, version: 1,
testMode: false,
// v1 didn't have separate media dc // v1 didn't have separate media dc
primaryDcs: stubDcsSameMedia, primaryDcs: stubDcsSameMedia,
authKey: stubAuthKey, authKey: stubAuthKey,

View file

@ -1,6 +1,6 @@
import { TlBinaryReader, TlBinaryWriter } from '@mtcute/tl-runtime' import { TlBinaryReader, TlBinaryWriter } from '@mtcute/tl-runtime'
import { base64 } from '@fuman/utils'
import { getPlatform } from '../../platform.js'
import { MtArgumentError } from '../../types/index.js' import { MtArgumentError } from '../../types/index.js'
import type { BasicDcOption, DcOptions } from '../../utils/dcs.js' import type { BasicDcOption, DcOptions } from '../../utils/dcs.js'
import { parseBasicDcOption, serializeBasicDcOption } from '../../utils/dcs.js' import { parseBasicDcOption, serializeBasicDcOption } from '../../utils/dcs.js'
@ -74,11 +74,11 @@ export function writeStringSession(data: StringSessionData): string {
writer.bytes(data.authKey) writer.bytes(data.authKey)
return getPlatform().base64Encode(writer.result(), true) return base64.encode(writer.result(), true)
} }
export function readStringSession(data: string): StringSessionData { export function readStringSession(data: string): StringSessionData {
const buf = getPlatform().base64Decode(data, true) const buf = base64.decode(data, true)
const version = buf[0] const version = buf[0]

View file

@ -1,109 +1,87 @@
// todo: fix test // todo: fix test
// import { describe, expect, it } from 'vitest' import { describe, expect, it } from 'vitest'
// import { defaultTestCryptoProvider, useFakeMathRandom } from '@mtcute/test' import { defaultTestCryptoProvider, useFakeMathRandom } from '@mtcute/test'
import { hex } from '@fuman/utils'
import { Bytes, write } from '@fuman/io'
// import { IntermediatePacketCodec, PaddedIntermediatePacketCodec, TransportError } from '../../index.js' import { IntermediatePacketCodec, PaddedIntermediatePacketCodec } from './intermediate'
// import { getPlatform } from '../../platform.js' import { TransportError } from './abstract'
// const p = getPlatform() describe('IntermediatePacketCodec', () => {
it('should return correct tag', () => {
expect(hex.encode(new IntermediatePacketCodec().tag())).eq('eeeeeeee')
})
// describe('IntermediatePacketCodec', () => { it('should correctly parse immediate framing', async () => {
// it('should return correct tag', () => { const codec = new IntermediatePacketCodec()
// expect(p.hexEncode(new IntermediatePacketCodec().tag())).eq('eeeeeeee') expect(codec.decode(Bytes.from(hex.decode('050000000501020304')), false)).eql(new Uint8Array([5, 1, 2, 3, 4]))
// }) })
// it('should correctly parse immediate framing', () => it('should correctly parse incomplete framing', () => {
// new Promise<void>((done) => { const codec = new IntermediatePacketCodec()
// const codec = new IntermediatePacketCodec() const buf = Bytes.alloc()
// codec.on('packet', (data: Uint8Array) => {
// expect([...data]).eql([5, 1, 2, 3, 4])
// done()
// })
// codec.feed(p.hexDecode('050000000501020304'))
// }))
// it('should correctly parse incomplete framing', () => write.bytes(buf, hex.decode('050000000501'))
// new Promise<void>((done) => { expect(codec.decode(buf, false)).toEqual(null)
// const codec = new IntermediatePacketCodec()
// codec.on('packet', (data: Uint8Array) => {
// expect([...data]).eql([5, 1, 2, 3, 4])
// done()
// })
// codec.feed(p.hexDecode('050000000501'))
// codec.feed(p.hexDecode('020304'))
// }))
// it('should correctly parse multiple streamed packets', () => write.bytes(buf, hex.decode('020304'))
// new Promise<void>((done) => { expect(codec.decode(buf, false)).eql(new Uint8Array([5, 1, 2, 3, 4]))
// const codec = new IntermediatePacketCodec() expect(codec.decode(buf, false)).toEqual(null)
})
// let number = 0 it('should correctly parse multiple streamed packets', () => {
const codec = new IntermediatePacketCodec()
const buf = Bytes.alloc()
// codec.on('packet', (data: Uint8Array) => { write.bytes(buf, hex.decode('050000000501'))
// if (number === 0) { expect(codec.decode(buf, false)).toEqual(null)
// expect([...data]).eql([5, 1, 2, 3, 4]) write.bytes(buf, hex.decode('020304050000'))
// number = 1 expect(codec.decode(buf, false)).eql(new Uint8Array([5, 1, 2, 3, 4]))
// } else { expect(codec.decode(buf, false)).eql(null)
// expect([...data]).eql([3, 1, 2, 3, 1])
// done()
// }
// })
// codec.feed(p.hexDecode('050000000501'))
// codec.feed(p.hexDecode('020304050000'))
// codec.feed(p.hexDecode('000301020301'))
// }))
// it('should correctly parse transport errors', () => write.bytes(buf, hex.decode('000301020301'))
// new Promise<void>((done) => { expect(codec.decode(buf, false)).eql(new Uint8Array([3, 1, 2, 3, 1]))
// const codec = new IntermediatePacketCodec() expect(codec.decode(buf, false)).toEqual(null)
})
// codec.on('error', (err: TransportError) => { it('should correctly parse transport errors', () => {
// expect(err).to.have.instanceOf(TransportError) const codec = new IntermediatePacketCodec()
// expect(err.code).eq(404) const buf = Bytes.alloc()
// done()
// })
// codec.feed(p.hexDecode('040000006cfeffff')) write.bytes(buf, hex.decode('040000006cfeffff'))
// })) expect(() => codec.decode(buf, false)).toThrow(new TransportError(404))
})
// it('should reset when called reset()', () => it('should correctly frame packets', () => {
// new Promise<void>((done) => { const data = hex.decode('6cfeffff')
// const codec = new IntermediatePacketCodec() const buf = Bytes.alloc()
// codec.on('packet', (data: Uint8Array) => { new IntermediatePacketCodec().encode(data, buf)
// expect([...data]).eql([1, 2, 3, 4, 5])
// done()
// })
// codec.feed(p.hexDecode('ff0000001234567812345678')) expect(hex.encode(buf.result())).toEqual('040000006cfeffff')
// codec.reset() })
// codec.feed(p.hexDecode('050000000102030405')) })
// }))
// it('should correctly frame packets', () => { describe('PaddedIntermediatePacketCodec', () => {
// const data = p.hexDecode('6cfeffff') useFakeMathRandom()
// expect(p.hexEncode(new IntermediatePacketCodec().encode(data))).toEqual('040000006cfeffff') const create = async () => {
// }) const codec = new PaddedIntermediatePacketCodec()
// }) codec.setup!(await defaultTestCryptoProvider())
// describe('PaddedIntermediatePacketCodec', () => { return codec
// useFakeMathRandom() }
// const create = async () => { it('should return correct tag', async () => {
// const codec = new PaddedIntermediatePacketCodec() expect(hex.encode((await create()).tag())).eq('dddddddd')
// codec.setup!(await defaultTestCryptoProvider()) })
// return codec it('should correctly frame packets', async () => {
// } const data = hex.decode('6cfeffff')
const buf = Bytes.alloc()
// it('should return correct tag', async () => { ;(await create()).encode(data, buf)
// expect(p.hexEncode((await create()).tag())).eq('dddddddd')
// })
// it('should correctly frame packets', async () => { expect(hex.encode(buf.result())).toEqual('0a0000006cfeffff29afd26df40f')
// const data = p.hexDecode('6cfeffff') })
})
// expect(p.hexEncode((await create()).encode(data))).toEqual('0a0000006cfeffff29afd26df40f')
// })
// })

View file

@ -29,8 +29,8 @@ export class IntermediatePacketCodec implements IPacketCodec {
if (length === 4) { if (length === 4) {
// error // error
const code = read.uint32le(reader) const code = read.int32le(reader)
throw new TransportError(code) throw new TransportError(-code)
} }
if (reader.available < length) { if (reader.available < length) {

View file

@ -1,15 +1,13 @@
// all available libraries either suck or are extremely large for the use case, so i made my own~ // all available libraries either suck or are extremely large for the use case, so i made my own~
import { hex } from '@fuman/utils' import { base64, hex } from '@fuman/utils'
import { getPlatform } from '../../platform.js'
/** /**
* Parses a single PEM block to buffer. * Parses a single PEM block to buffer.
* In fact just strips begin/end tags and parses the rest as Base64 * In fact just strips begin/end tags and parses the rest as Base64
*/ */
export function parsePemContents(pem: string): Uint8Array { export function parsePemContents(pem: string): Uint8Array {
return getPlatform().base64Decode(pem.replace(/^-----(?:BEGIN|END)(?: RSA)? PUBLIC KEY-----$|\n/gm, '')) return base64.decode(pem.replace(/^-----(?:BEGIN|END)(?: RSA)? PUBLIC KEY-----$|\n/gm, ''))
} }
// based on https://git.coolaj86.com/coolaj86/asn1-parser.js/src/branch/master/asn1-parser.js // based on https://git.coolaj86.com/coolaj86/asn1-parser.js/src/branch/master/asn1-parser.js

View file

@ -1,3 +1,2 @@
export * from './utils/crypto.js' export * from './utils/crypto.js'
export * from './utils/stream-utils.js'
export * from '@mtcute/core/utils.js' export * from '@mtcute/core/utils.js'