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'
// @ts-expect-error no typings
import { describe as _describe, afterAll, afterEach, beforeAll, beforeEach, it } from 'jsr:@std/testing/bdd'
// @ts-expect-error no typings
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
// https://github.com/oven-sh/bun/issues/1825
skipTests: [
// uses timers
// uses timers
'core/src/network/config-manager.test.ts',
'core/src/network/persistent-connection.test.ts',
// use fixtures
'convert/src/tdesktop/tdata.test.ts',
],
}),
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
skipPackages: ['create-bot', 'crypto-node', 'bun', 'node'],
skipTests: [
// uses timers
// uses timers
'core/src/network/config-manager.test.ts',
'core/src/network/persistent-connection.test.ts',
// https://github.com/denoland/deno/issues/22470
'wasm/tests/gunzip.test.ts',
'wasm/tests/zlib.test.ts',
// use fixtures
'convert/src/tdesktop/tdata.test.ts',
],
}),
formats: ['es'],

View file

@ -1,12 +1,7 @@
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vitest'
import { Long } from '@mtcute/core'
import type { INodeFsLike } from '../utils/fs.js'
import { getDefaultCryptoProvider } from '../utils/crypto.js'
import { Tdata } from './tdata.js'
class FakeFs implements INodeFsLike {
readonly files = new Map<string, Uint8Array>()
@ -31,76 +26,84 @@ class FakeFs implements INodeFsLike {
}
}
describe('tdata', () => {
it('should read simple tdata', async () => {
const tdata = await Tdata.open({
path: fileURLToPath(new URL('./__fixtures__/simple', import.meta.url)),
if (import.meta.env.TEST_ENV === 'node') {
describe('tdata', async () => {
const { getDefaultCryptoProvider } = await import('../utils/crypto.js')
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 () => {
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()
expect({ auth, key: tdata.keyData }).toMatchSnapshot()
})
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)),
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?')
})
const auth0 = await tdata.readMtpAuthorization(0)
const auth1 = await tdata.readMtpAuthorization(1)
it('should read multi-account tdata', async () => {
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 () => {
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,
},
expect({ auth0, auth1, key: tdata.keyData }).toMatchSnapshot()
})
const key = new Uint8Array(256)
key.fill(1)
await tdata.writeMtpAuthorization({
userId: Long.fromNumber(12345678),
mainDcId: 2,
authKeys: [
{
dcId: 2,
key,
it('should write simple tdata', async () => {
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,
},
],
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)
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)
cm.onUpdated.remove(listener)
await cm.update()
expect(listener).toHaveBeenCalledOnce()
expect(call).toEqual({
abort: {},
expect(callCopy).toEqual({
current: config,
currentExpiresAt: 300_000,
currentFetchedAt: 0,