test: fixed deno/bun/browser tests

This commit is contained in:
alina 🌸 2024-12-03 05:36:50 +03:00
parent 9622d2c0a2
commit a9ef0198e9
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
5 changed files with 81 additions and 69 deletions

View file

@ -1,6 +1,6 @@
// @ts-expect-error no typings
import util from 'node:util' import util from 'node:util'
// @ts-expect-error no typings
import { describe as _describe, afterAll, afterEach, beforeAll, beforeEach, it } from 'jsr:@std/testing/bdd' import { describe as _describe, afterAll, afterEach, beforeAll, beforeEach, it } from 'jsr:@std/testing/bdd'
// @ts-expect-error no typings // @ts-expect-error no typings
import * as vitestSpy from 'npm:@vitest/spy@1.4.0' import * as vitestSpy from 'npm:@vitest/spy@1.4.0'

View file

@ -20,9 +20,11 @@ export default defineConfig({
// bun:test doesn't support certain features of vitest, so we'll skip them for now // bun:test doesn't support certain features of vitest, so we'll skip them for now
// https://github.com/oven-sh/bun/issues/1825 // https://github.com/oven-sh/bun/issues/1825
skipTests: [ skipTests: [
// uses timers // uses timers
'core/src/network/config-manager.test.ts', 'core/src/network/config-manager.test.ts',
'core/src/network/persistent-connection.test.ts', 'core/src/network/persistent-connection.test.ts',
// use fixtures
'convert/src/tdesktop/tdata.test.ts',
], ],
}), }),
formats: ['es'], formats: ['es'],

View file

@ -17,12 +17,14 @@ export default defineConfig({
// these packages rely on node apis and are not meant to be run under deno // these packages rely on node apis and are not meant to be run under deno
skipPackages: ['create-bot', 'crypto-node', 'bun', 'node'], skipPackages: ['create-bot', 'crypto-node', 'bun', 'node'],
skipTests: [ skipTests: [
// uses timers // uses timers
'core/src/network/config-manager.test.ts', 'core/src/network/config-manager.test.ts',
'core/src/network/persistent-connection.test.ts', 'core/src/network/persistent-connection.test.ts',
// https://github.com/denoland/deno/issues/22470 // https://github.com/denoland/deno/issues/22470
'wasm/tests/gunzip.test.ts', 'wasm/tests/gunzip.test.ts',
'wasm/tests/zlib.test.ts', 'wasm/tests/zlib.test.ts',
// use fixtures
'convert/src/tdesktop/tdata.test.ts',
], ],
}), }),
formats: ['es'], formats: ['es'],

View file

@ -1,12 +1,7 @@
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vitest' import { describe, expect, it } from 'vitest'
import { Long } from '@mtcute/core' import { Long } from '@mtcute/core'
import type { INodeFsLike } from '../utils/fs.js' import type { INodeFsLike } from '../utils/fs.js'
import { getDefaultCryptoProvider } from '../utils/crypto.js'
import { Tdata } from './tdata.js'
class FakeFs implements INodeFsLike { class FakeFs implements INodeFsLike {
readonly files = new Map<string, Uint8Array>() readonly files = new Map<string, Uint8Array>()
@ -31,76 +26,84 @@ class FakeFs implements INodeFsLike {
} }
} }
describe('tdata', () => { if (import.meta.env.TEST_ENV === 'node') {
it('should read simple tdata', async () => { describe('tdata', async () => {
const tdata = await Tdata.open({ const { getDefaultCryptoProvider } = await import('../utils/crypto.js')
path: fileURLToPath(new URL('./__fixtures__/simple', import.meta.url)), const { fileURLToPath } = await import('node:url')
const { Tdata } = await import('./tdata.js')
it('should read simple tdata', async () => {
const tdata = await Tdata.open({
path: fileURLToPath(new URL('./__fixtures__/simple', import.meta.url)),
})
const auth = await tdata.readMtpAuthorization()
expect({ auth, key: tdata.keyData }).toMatchSnapshot()
}) })
const auth = await tdata.readMtpAuthorization() it('should read passcode-protected tdata', async () => {
const tdata = await Tdata.open({
path: fileURLToPath(new URL('./__fixtures__/passcode', import.meta.url)),
passcode: '123123',
})
expect({ auth, key: tdata.keyData }).toMatchSnapshot() const auth = await tdata.readMtpAuthorization()
})
it('should read passcode-protected tdata', async () => { expect({ auth, key: tdata.keyData }).toMatchSnapshot()
const tdata = await Tdata.open({
path: fileURLToPath(new URL('./__fixtures__/passcode', import.meta.url)),
passcode: '123123',
}) })
const auth = await tdata.readMtpAuthorization() it('should throw on invalid passcode', async () => {
await expect(Tdata.open({
expect({ auth, key: tdata.keyData }).toMatchSnapshot() path: fileURLToPath(new URL('./__fixtures__/passcode', import.meta.url)),
}) passcode: '123',
})).rejects.toThrow('Failed to decrypt, invalid password?')
it('should throw on invalid passcode', async () => {
await expect(Tdata.open({
path: fileURLToPath(new URL('./__fixtures__/passcode', import.meta.url)),
passcode: '123',
})).rejects.toThrow('Failed to decrypt, invalid password?')
})
it('should read multi-account tdata', async () => {
const tdata = await Tdata.open({
path: fileURLToPath(new URL('./__fixtures__/multiacc', import.meta.url)),
}) })
const auth0 = await tdata.readMtpAuthorization(0) it('should read multi-account tdata', async () => {
const auth1 = await tdata.readMtpAuthorization(1) const tdata = await Tdata.open({
path: fileURLToPath(new URL('./__fixtures__/multiacc', import.meta.url)),
})
expect({ auth0, auth1, key: tdata.keyData }).toMatchSnapshot() const auth0 = await tdata.readMtpAuthorization(0)
}) const auth1 = await tdata.readMtpAuthorization(1)
it('should write simple tdata', async () => { expect({ auth0, auth1, key: tdata.keyData }).toMatchSnapshot()
const fs = new FakeFs()
const crypto = await getDefaultCryptoProvider()
crypto.randomBytes = size => new Uint8Array(size)
const tdata = await Tdata.create({
path: '/',
fs,
crypto,
keyData: {
count: 1,
order: [0],
active: 0,
},
}) })
const key = new Uint8Array(256) it('should write simple tdata', async () => {
key.fill(1) const fs = new FakeFs()
await tdata.writeMtpAuthorization({ const crypto = await getDefaultCryptoProvider()
userId: Long.fromNumber(12345678), crypto.randomBytes = size => new Uint8Array(size)
mainDcId: 2,
authKeys: [ const tdata = await Tdata.create({
{ path: '/',
dcId: 2, fs,
key, crypto,
keyData: {
count: 1,
order: [0],
active: 0,
}, },
], })
authKeysToDestroy: [],
})
expect(fs.files).toMatchSnapshot() const key = new Uint8Array(256)
key.fill(1)
await tdata.writeMtpAuthorization({
userId: Long.fromNumber(12345678),
mainDcId: 2,
authKeys: [
{
dcId: 2,
key,
},
],
authKeysToDestroy: [],
})
expect(fs.files).toMatchSnapshot()
})
}) })
}) } else {
describe.skip('tdata', () => {})
}

View file

@ -95,15 +95,20 @@ describe('ConfigManager', () => {
cm.onUpdated.add(listener) cm.onUpdated.add(listener)
await cm.update() await cm.update()
const call = structuredClone(listener.mock.calls[0][0]) as AsyncResourceContext<tl.RawConfig> const call = listener.mock.calls[0][0] as AsyncResourceContext<tl.RawConfig>
const callCopy = structuredClone({
current: call.current,
currentFetchedAt: call.currentFetchedAt,
currentExpiresAt: call.currentExpiresAt,
isBackground: call.isBackground,
})
vi.setSystemTime(300_000) vi.setSystemTime(300_000)
cm.onUpdated.remove(listener) cm.onUpdated.remove(listener)
await cm.update() await cm.update()
expect(listener).toHaveBeenCalledOnce() expect(listener).toHaveBeenCalledOnce()
expect(call).toEqual({ expect(callCopy).toEqual({
abort: {},
current: config, current: config,
currentExpiresAt: 300_000, currentExpiresAt: 300_000,
currentFetchedAt: 0, currentFetchedAt: 0,