diff --git a/.config/vite-utils/polyfills-deno.ts b/.config/vite-utils/polyfills-deno.ts index 7021a58c..8d045ead 100644 --- a/.config/vite-utils/polyfills-deno.ts +++ b/.config/vite-utils/polyfills-deno.ts @@ -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' diff --git a/.config/vite.bun.ts b/.config/vite.bun.ts index 5d1b6327..98c77079 100644 --- a/.config/vite.bun.ts +++ b/.config/vite.bun.ts @@ -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'], diff --git a/.config/vite.deno.ts b/.config/vite.deno.ts index 47d42caa..34872846 100644 --- a/.config/vite.deno.ts +++ b/.config/vite.deno.ts @@ -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'], diff --git a/packages/convert/src/tdesktop/tdata.test.ts b/packages/convert/src/tdesktop/tdata.test.ts index ec30e7ea..88821121 100644 --- a/packages/convert/src/tdesktop/tdata.test.ts +++ b/packages/convert/src/tdesktop/tdata.test.ts @@ -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() @@ -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', () => {}) +} diff --git a/packages/core/src/network/config-manager.test.ts b/packages/core/src/network/config-manager.test.ts index 1b397339..a58a39b5 100644 --- a/packages/core/src/network/config-manager.test.ts +++ b/packages/core/src/network/config-manager.test.ts @@ -95,15 +95,20 @@ describe('ConfigManager', () => { cm.onUpdated.add(listener) await cm.update() - const call = structuredClone(listener.mock.calls[0][0]) as AsyncResourceContext + const call = listener.mock.calls[0][0] as AsyncResourceContext + 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,