From 852b1b766c6d851896a2dcffb05b874aad3152ac Mon Sep 17 00:00:00 2001 From: Alina Sireneva Date: Mon, 18 Mar 2024 01:49:55 +0300 Subject: [PATCH] test: properly run tests on bun turned out bun sucks :D --- .config/vite-utils/fixup-bun-test.ts | 128 ++++++ .config/vite-utils/fixup-cjs.ts | 26 ++ .config/vite-utils/test-setup-plugin.ts | 21 + .config/vite-utils/test-setup.mts | 9 + .config/vite.browser.mts | 24 +- .config/vite.bun.mts | 130 +++++- .config/vite.mts | 2 +- .config/vitest.setup.mts | 9 - .github/workflows/test.yaml | 6 +- package.json | 1 + packages/node/src/utils/tcp.test.ts | 2 +- packages/sqlite/test/sqlite.test.ts | 2 +- packages/test/package.json | 1 + packages/test/src/crypto.ts | 22 +- .../src/__snapshots__/diff.test.ts.snap | 326 +++++++------- .../codegen/__snapshots__/errors.test.ts.snap | 2 +- .../codegen/__snapshots__/reader.test.ts.snap | 150 +++---- .../codegen/__snapshots__/types.test.ts.snap | 406 +++++++++--------- .../codegen/__snapshots__/writer.test.ts.snap | 90 ++-- packages/wasm/tests/init.ts | 2 +- pnpm-lock.yaml | 57 ++- 21 files changed, 859 insertions(+), 557 deletions(-) create mode 100644 .config/vite-utils/fixup-bun-test.ts create mode 100644 .config/vite-utils/fixup-cjs.ts create mode 100644 .config/vite-utils/test-setup-plugin.ts create mode 100644 .config/vite-utils/test-setup.mts delete mode 100644 .config/vitest.setup.mts diff --git a/.config/vite-utils/fixup-bun-test.ts b/.config/vite-utils/fixup-bun-test.ts new file mode 100644 index 00000000..28edfb2b --- /dev/null +++ b/.config/vite-utils/fixup-bun-test.ts @@ -0,0 +1,128 @@ +const bunTest = require('bun:test') +const console = require('console') // https://github.com/oven-sh/bun/issues/6044 +const chaiExpect = require('chai').expect +const bunExpect = bunTest.expect + +class BunTestUnsupportedError extends Error { + constructor(readonly feature) { + super() + } +} + +function _wrapBunExpect(args, bun, invert = false) { + let chai = chaiExpect(...args) + if (invert) chai = chai.not + + return new Proxy(bun, { + get: (target, prop, receiver) => { + if (prop === 'eq') return (...args) => chai.eq(...args) + if (prop === 'eql') return (...args) => chai.eql(...args) + if (prop === 'throws') return (...args) => chai.throws(...args) + if (prop === 'is') return chai.is + if (prop === 'to') return chai.to + if (prop === 'false') return chai.false + if (prop === 'true') return chai.true + if (prop === 'deep') return chai.deep + + if (prop === 'toMatchInlineSnapshot') { + return (expected, options) => { + let snapshot + if (typeof args[0] === 'string') { + const snapshot = '"' + args[0] + '"' + return chaiExpect(snapshot).eql(expected.trim()) + } else { + const obj = eval('(' + expected + ')') // idc lol + return chaiExpect(args[0]).eql(obj) + } + } + } + + if (prop === 'not') { + const not = bun.not + return _wrapBunExpect(args, not, !invert) + } + + if (prop === 'rejects') { + if (typeof args[0] === 'function') { + const newArgs = [args[0](), ...args.slice(1)] + return _wrapBunExpect(newArgs, bunExpect(...newArgs), invert).rejects + } + + return bun.rejects + } + if (prop === 'resolves') { + return bun.resolves + } + + if (prop === 'toHaveBeenCalledOnce' || prop === 'toHaveBeenCalledTimes' || prop === 'toMatchSnapshot') { + throw new BunTestUnsupportedError(prop) + } + + return Reflect.get(target, prop, receiver).bind(bun) + }, + }) +} + +export function expect(...args) { + return _wrapBunExpect(args, bunExpect(...args)) +} + +expect.any = bunExpect.any + +const stubbedGlobal = new Map() +function stubGlobal(name, value) { + stubbedGlobal.set(name, globalThis[name]) + globalThis[name] = value +} + +function unstubAllGlobals() { + for (const [name, value] of stubbedGlobal) { + globalThis[name] = value + } + stubbedGlobal.clear() +} + +const _wrapRunner = (name, fn) => { + const handleError = (err) => { + if (err instanceof BunTestUnsupportedError) { + console.warn(`skipping "${name}" - ${err.feature} is currently not supported in bun:test`) + return + } + throw err + } + return (...args) => { + try { + const res = fn(...args) + if (res instanceof Promise) { + return res.catch(handleError) + } + return res + } catch (e) { + return handleError(e) + } + } +} + +const it = (name, fn) => bunTest.it(name, _wrapRunner(name, fn)) +it.only = (name, fn) => bunTest.it.only(name, _wrapRunner(name, fn)) +it.skip = (name, fn) => bunTest.it.skip(name, _wrapRunner(name, fn)) +it.each = (table) => (name, fn) => bunTest.it.each(table)(name, _wrapRunner(name, fn)) + +export { it } + +export const vi = { + ...bunTest.jest, + ...bunTest.vi, + mocked: (fn) => fn, + stubGlobal, + unstubAllGlobals, + ...['setSystemTime', 'advanceTimersByTimeAsync', 'advanceTimersByTime', 'waitFor', 'doMock'].reduce( + (acc, name) => ({ + ...acc, + [name]: () => { + throw new BunTestUnsupportedError(name) + }, + }), + {}, + ), +} diff --git a/.config/vite-utils/fixup-cjs.ts b/.config/vite-utils/fixup-cjs.ts new file mode 100644 index 00000000..f43da923 --- /dev/null +++ b/.config/vite-utils/fixup-cjs.ts @@ -0,0 +1,26 @@ +import { Plugin } from 'vite' +import * as cjsLexer from 'cjs-module-lexer' +import esbuild from 'esbuild' + +await cjsLexer.init() + +export function fixupCjs(): Plugin { + return { + name: 'fixup-cjs', + async transform(code, id) { + if (!id.match(/\/packages\/tl\/.*\.js$/)) return code + + const lexed = cjsLexer.parse(code) + const r = await esbuild.transform(code, { format: 'esm' }) + code = r.code.replace(/export default require_stdin\(\);/, '') + + code += 'const __exp = require_stdin()\n' + + for (const exp of lexed.exports) { + code += `export const ${exp} = __exp.${exp}\n` + } + + return code + }, + } +} diff --git a/.config/vite-utils/test-setup-plugin.ts b/.config/vite-utils/test-setup-plugin.ts new file mode 100644 index 00000000..4e638511 --- /dev/null +++ b/.config/vite-utils/test-setup-plugin.ts @@ -0,0 +1,21 @@ +import { Plugin } from 'vite' +import { fileURLToPath } from 'url' + +const setupFile = fileURLToPath(new URL('./test-setup.mts', import.meta.url)) + +export function testSetup(params?: { additionalCode?: string }): Plugin { + const { additionalCode = '' } = params || {} + + return { + name: 'test-setup', + async transform(code, id) { + if (!id.match(/\.test\.m?[tj]s/)) return + + return { + code: `import '${setupFile}'\n` + additionalCode + code, + map: null, + } + }, + apply: 'build', + } +} diff --git a/.config/vite-utils/test-setup.mts b/.config/vite-utils/test-setup.mts new file mode 100644 index 00000000..7e112de8 --- /dev/null +++ b/.config/vite-utils/test-setup.mts @@ -0,0 +1,9 @@ +import { setPlatform } from '../../packages/core/src/platform.js' + +// @ts-expect-error no .env here +const TEST_ENV = import.meta.env.TEST_ENV +if (TEST_ENV === 'browser') { + setPlatform(new (await import('../../packages/web/src/platform.js')).WebPlatform()) +} else { + setPlatform(new (await import('../../packages/node/src/platform.js')).NodePlatform()) +} \ No newline at end of file diff --git a/.config/vite.browser.mts b/.config/vite.browser.mts index df0c4340..57c5f10e 100644 --- a/.config/vite.browser.mts +++ b/.config/vite.browser.mts @@ -1,11 +1,8 @@ /// import { defineConfig, mergeConfig } from 'vite' -import * as cjsLexer from 'cjs-module-lexer' -import esbuild from 'esbuild' import baseConfig from './vite.mjs' - -await cjsLexer.init() +import { fixupCjs } from './vite-utils/fixup-cjs' export default mergeConfig(baseConfig, defineConfig({ test: { @@ -26,24 +23,7 @@ export default mergeConfig(baseConfig, defineConfig({ // ], }, plugins: [ - { - name: 'fixup-cjs', - async transform(code, id) { - if (!id.match(/\/packages\/tl\/.*\.js$/)) return code - - const lexed = cjsLexer.parse(code) - const r = await esbuild.transform(code, { format: 'esm' }) - code = r.code.replace(/export default require_stdin\(\);/, '') - - code += 'const __exp = require_stdin()\n' - - for (const exp of lexed.exports) { - code += `export const ${exp} = __exp.${exp}\n` - } - - return code - } - } + fixupCjs(), ], define: { 'import.meta.env.TEST_ENV': '"browser"' diff --git a/.config/vite.bun.mts b/.config/vite.bun.mts index 26f77310..8552be38 100644 --- a/.config/vite.bun.mts +++ b/.config/vite.bun.mts @@ -1,10 +1,126 @@ -/// -import { defineConfig, mergeConfig } from 'vite' +import { defineConfig } from 'vite' +import { globSync } from 'glob' +import { resolve, join } from 'path' +import * as fs from 'fs' +import { fixupCjs } from './vite-utils/fixup-cjs' +import { testSetup } from './vite-utils/test-setup-plugin' -import baseConfig from './vite.mjs' +const SKIP_PACKAGES = ['create-bot', 'crypto-node'] -export default mergeConfig(baseConfig, defineConfig({ +// https://github.com/oven-sh/bun/issues/4145 prevents us from using vitest directly +// so we have to use bun's native test runner +const FIXUP_TEST = resolve(__dirname, 'vite-utils/fixup-bun-test.ts') + + +// bun:test doesn't support certain features of vitest, so we'll skip them for now +// https://github.com/oven-sh/bun/issues/1825 +const SKIP_TESTS = [ + // uses timers + 'core/src/network/config-manager.test.ts', + // incompatible spies + 'core/src/utils/crypto/mtproto.test.ts', + // snapshot format + 'tl-utils/src/codegen/errors.test.ts' +].map(path => resolve(__dirname, '../packages', path)) + +export default defineConfig({ + build: { + lib: { + entry: (() => { + const files: string[] = [] + + const packages = resolve(__dirname, '../packages') + + for (const dir of fs.readdirSync(packages)) { + if (dir.startsWith('.') || SKIP_PACKAGES.includes(dir)) continue + if (!fs.statSync(resolve(packages, dir)).isDirectory()) continue + + const fullDir = resolve(packages, dir) + + for (const file of globSync(join(fullDir, '**/*.test.ts'))) { + if (SKIP_TESTS.includes(file)) continue + files.push(file) + } + } + + return files + })(), + formats: ['es'], + }, + rollupOptions: { + external: [ + 'zlib', + 'vitest', + 'stream', + 'net', + 'crypto', + 'module', + 'fs', + 'fs/promises', + 'events', + 'path', + 'util', + 'os', + 'bun:test', + ], + output: { + chunkFileNames: 'chunk-[hash].js', + entryFileNames: '[name]-[hash].test.js', + minifyInternalExports: false, + }, + treeshake: false, + }, + commonjsOptions: { + ignoreDynamicRequires: true, + }, + outDir: 'dist/tests', + emptyOutDir: true, + target: 'esnext', + minify: false, + }, + plugins: [ + fixupCjs(), + { + name: 'fix-vitest', + transform(code) { + if (!code.includes('vitest')) return code + code = code.replace(/^import {(.+?)} from ['"]vitest['"]/gms, (_, names) => { + const namesParsed = names.split(',').map((name) => name.trim()) + + const namesFromFixup: string[] = [] + const newNames = namesParsed + .map((name) => { + if (['expect', 'vi', 'it'].includes(name)) { + namesFromFixup.push(name) + return '' + } + return name + }) + .filter(Boolean) + + let code = `import {${newNames.join(', ')}} from 'bun:test'` + + if (namesFromFixup.length) { + code += `\nimport { ${namesFromFixup.join(', ')} } from '${FIXUP_TEST}'` + } + return code + }) + return code + }, + }, + { + name: 'fix-wasm-load', + async transform(code, id) { + if (code.includes('@mtcute/wasm/mtcute.wasm')) { + return code.replace('@mtcute/wasm/mtcute.wasm', resolve(__dirname, '../packages/wasm/mtcute.wasm')) + } + + return code + } + }, + testSetup(), + ], define: { - 'import.meta.env.TEST_ENV': '"bun"' - } -})) + 'import.meta.env.TEST_ENV': '"bun"', + }, +}) diff --git a/.config/vite.mts b/.config/vite.mts index 52164131..c346cdd2 100644 --- a/.config/vite.mts +++ b/.config/vite.mts @@ -12,7 +12,7 @@ export default defineConfig({ ], }, setupFiles: [ - './.config/vitest.setup.mts' + './.config/vite-utils/test-setup.mts' ] }, define: { diff --git a/.config/vitest.setup.mts b/.config/vitest.setup.mts deleted file mode 100644 index ca0294fd..00000000 --- a/.config/vitest.setup.mts +++ /dev/null @@ -1,9 +0,0 @@ -import { setPlatform } from '../packages/core/src/platform.js' - -// @ts-expect-error no .env here -const TEST_ENV = import.meta.env.TEST_ENV -if (TEST_ENV === 'browser') { - setPlatform(new (await import('../packages/web/src/platform.js')).WebPlatform()) -} else { - setPlatform(new (await import('../packages/node/src/platform.js')).NodePlatform()) -} \ No newline at end of file diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 93cc765f..c23cd9e3 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -43,14 +43,18 @@ jobs: CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} with: files: ./coverage/coverage-final.json + test-bun: runs-on: ubuntu-latest needs: lint steps: - uses: actions/checkout@v4 + - uses: ./.github/actions/init - uses: ./.github/actions/init-bun + - name: 'Build tests' + run: pnpm exec vite build -c .config/vite.bun.mts - name: 'Run tests' - run: bun run test:ci + run: cd dist/tests && bun test test-web: runs-on: ubuntu-latest diff --git a/package.json b/package.json index 3fd58dba..76f42749 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,7 @@ "@vitest/browser": "1.4.0", "@vitest/coverage-v8": "1.4.0", "@vitest/ui": "1.4.0", + "chai": "^5.1.0", "cjs-module-lexer": "1.2.3", "dotenv-flow": "3.2.0", "dpdm": "3.14.0", diff --git a/packages/node/src/utils/tcp.test.ts b/packages/node/src/utils/tcp.test.ts index da70f7b9..684e3cf4 100644 --- a/packages/node/src/utils/tcp.test.ts +++ b/packages/node/src/utils/tcp.test.ts @@ -5,7 +5,7 @@ import { TransportState } from '@mtcute/core' import { getPlatform } from '@mtcute/core/platform.js' import { defaultProductionDc, LogManager } from '@mtcute/core/utils.js' -if (import.meta.env.TEST_ENV === 'node' || import.meta.env.TEST_ENV === 'bun') { +if (import.meta.env.TEST_ENV === 'node') { vi.doMock('net', () => ({ connect: vi.fn().mockImplementation((port: number, ip: string, cb: () => void) => { cb() diff --git a/packages/sqlite/test/sqlite.test.ts b/packages/sqlite/test/sqlite.test.ts index 3cc5e385..c3709f8e 100644 --- a/packages/sqlite/test/sqlite.test.ts +++ b/packages/sqlite/test/sqlite.test.ts @@ -10,7 +10,7 @@ import { import { SqliteStorage } from '../src/index.js' -if (import.meta.env.TEST_ENV === 'node' || import.meta.env.TEST_ENV === 'bun') { +if (import.meta.env.TEST_ENV === 'node') { describe('SqliteStorage', () => { const storage = new SqliteStorage(':memory:') diff --git a/packages/test/package.json b/packages/test/package.json index 3d1c3d71..b8c473f4 100644 --- a/packages/test/package.json +++ b/packages/test/package.json @@ -7,6 +7,7 @@ "license": "MIT", "main": "src/index.ts", "type": "module", + "sideEffects": false, "scripts": { "build": "pnpm run -w build-package test" }, diff --git a/packages/test/src/crypto.ts b/packages/test/src/crypto.ts index 7a2a5bb8..dab13e34 100644 --- a/packages/test/src/crypto.ts +++ b/packages/test/src/crypto.ts @@ -2,10 +2,7 @@ import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vite import { gzipSync, inflateSync } from 'zlib' import { getPlatform } from '@mtcute/core/platform.js' -import { - dataViewFromBuffer, - ICryptoProvider, -} from '@mtcute/core/utils.js' +import { dataViewFromBuffer, ICryptoProvider } from '@mtcute/core/utils.js' import { defaultCryptoProvider } from './platform.js' @@ -42,9 +39,14 @@ export function withFakeRandom(provider: ICryptoProvider, source = DEFAULT_ENTRO offset += buf.length } - provider.randomFill = getRandomValues + return new Proxy(provider, { + get(target, prop, receiver) { + if (prop === 'randomFill') return getRandomValues - return provider + // eslint-disable-next-line @typescript-eslint/no-unsafe-return + return Reflect.get(target, prop, receiver) + }, + }) } export function useFakeMathRandom(source = DEFAULT_ENTROPY): void { @@ -176,14 +178,10 @@ export function testCryptoProvider(c: ICryptoProvider): void { p.hexDecode('6D656E746174696F6E206F6620494745206D6F646520666F72204F70656E5353'), ) expect( - p.hexEncode( - aes.encrypt(p.hexDecode('99706487a1cde613bc6de0b6f24b1c7aa448c8b9c3403e3467a8cad89340f53b')), - ), + p.hexEncode(aes.encrypt(p.hexDecode('99706487a1cde613bc6de0b6f24b1c7aa448c8b9c3403e3467a8cad89340f53b'))), ).to.eq('792ea8ae577b1a66cb3bd92679b8030ca54ee631976bd3a04547fdcb4639fa69') expect( - p.hexEncode( - aes.decrypt(p.hexDecode('792ea8ae577b1a66cb3bd92679b8030ca54ee631976bd3a04547fdcb4639fa69')), - ), + p.hexEncode(aes.decrypt(p.hexDecode('792ea8ae577b1a66cb3bd92679b8030ca54ee631976bd3a04547fdcb4639fa69'))), ).to.eq('99706487a1cde613bc6de0b6f24b1c7aa448c8b9c3403e3467a8cad89340f53b') }) diff --git a/packages/tl-utils/src/__snapshots__/diff.test.ts.snap b/packages/tl-utils/src/__snapshots__/diff.test.ts.snap index b1f53b43..9296cb59 100644 --- a/packages/tl-utils/src/__snapshots__/diff.test.ts.snap +++ b/packages/tl-utils/src/__snapshots__/diff.test.ts.snap @@ -623,159 +623,6 @@ exports[`generateTlSchemasDifference shows added constructors 1`] = ` } `; -exports[`generateTlSchemasDifference shows removed constructors 1`] = ` -{ - "classes": { - "added": [], - "modified": [], - "removed": [ - { - "arguments": [], - "id": 3847402009, - "kind": "class", - "name": "test2", - "type": "Test", - }, - ], - }, - "methods": { - "added": [], - "modified": [], - "removed": [], - }, - "unions": { - "added": [], - "modified": [ - { - "classes": { - "added": [], - "modified": [], - "removed": [ - { - "arguments": [], - "id": 3847402009, - "kind": "class", - "name": "test2", - "type": "Test", - }, - ], - }, - "methods": { - "added": [], - "modified": [], - "removed": [], - }, - "name": "Test", - }, - ], - "removed": [], - }, -} -`; - -exports[`generateTlSchemasDifference shows modified constructors 1`] = ` -{ - "classes": { - "added": [], - "modified": [ - { - "arguments": { - "added": [], - "modified": [ - { - "name": "foo", - "type": { - "new": "Foo", - "old": "int", - }, - }, - ], - "removed": [], - }, - "id": { - "new": 3348640942, - "old": 1331975629, - }, - "name": "test", - }, - ], - "removed": [], - }, - "methods": { - "added": [], - "modified": [], - "removed": [], - }, - "unions": { - "added": [], - "modified": [], - "removed": [], - }, -} -`; - -exports[`generateTlSchemasDifference shows removed unions 1`] = ` -{ - "classes": { - "added": [], - "modified": [ - { - "arguments": { - "added": [], - "modified": [ - { - "name": "foo", - "type": { - "new": "Foo", - "old": "int", - }, - }, - ], - "removed": [], - }, - "id": { - "new": 3348640942, - "old": 1331975629, - }, - "name": "test", - }, - ], - "removed": [ - { - "arguments": [], - "id": 3739166976, - "kind": "class", - "name": "test1", - "type": "Test1", - }, - ], - }, - "methods": { - "added": [], - "modified": [], - "removed": [], - }, - "unions": { - "added": [], - "modified": [], - "removed": [ - { - "classes": [ - { - "arguments": [], - "id": 3739166976, - "kind": "class", - "name": "test1", - "type": "Test1", - }, - ], - "name": "Test1", - }, - ], - }, -} -`; - exports[`generateTlSchemasDifference shows added unions 1`] = ` { "classes": { @@ -838,6 +685,79 @@ exports[`generateTlSchemasDifference shows added unions 1`] = ` } `; +exports[`generateTlSchemasDifference shows modified constructors 1`] = ` +{ + "classes": { + "added": [], + "modified": [ + { + "arguments": { + "added": [], + "modified": [ + { + "name": "foo", + "type": { + "new": "Foo", + "old": "int", + }, + }, + ], + "removed": [], + }, + "id": { + "new": 3348640942, + "old": 1331975629, + }, + "name": "test", + }, + ], + "removed": [], + }, + "methods": { + "added": [], + "modified": [], + "removed": [], + }, + "unions": { + "added": [], + "modified": [], + "removed": [], + }, +} +`; + +exports[`generateTlSchemasDifference shows modified methods 1`] = ` +{ + "classes": { + "added": [], + "modified": [], + "removed": [], + }, + "methods": { + "added": [], + "modified": [ + { + "id": { + "new": 3994885231, + "old": 471282454, + }, + "name": "test", + }, + ], + "removed": [], + }, + "unions": { + "added": [ + undefined, + ], + "modified": [], + "removed": [ + undefined, + ], + }, +} +`; + exports[`generateTlSchemasDifference shows modified unions 1`] = ` { "classes": { @@ -1114,33 +1034,113 @@ exports[`generateTlSchemasDifference shows modified unions 3`] = ` } `; -exports[`generateTlSchemasDifference shows modified methods 1`] = ` +exports[`generateTlSchemasDifference shows removed constructors 1`] = ` { "classes": { "added": [], "modified": [], - "removed": [], + "removed": [ + { + "arguments": [], + "id": 3847402009, + "kind": "class", + "name": "test2", + "type": "Test", + }, + ], }, "methods": { + "added": [], + "modified": [], + "removed": [], + }, + "unions": { "added": [], "modified": [ { - "id": { - "new": 3994885231, - "old": 471282454, + "classes": { + "added": [], + "modified": [], + "removed": [ + { + "arguments": [], + "id": 3847402009, + "kind": "class", + "name": "test2", + "type": "Test", + }, + ], }, - "name": "test", + "methods": { + "added": [], + "modified": [], + "removed": [], + }, + "name": "Test", }, ], "removed": [], }, - "unions": { - "added": [ - undefined, +} +`; + +exports[`generateTlSchemasDifference shows removed unions 1`] = ` +{ + "classes": { + "added": [], + "modified": [ + { + "arguments": { + "added": [], + "modified": [ + { + "name": "foo", + "type": { + "new": "Foo", + "old": "int", + }, + }, + ], + "removed": [], + }, + "id": { + "new": 3348640942, + "old": 1331975629, + }, + "name": "test", + }, ], + "removed": [ + { + "arguments": [], + "id": 3739166976, + "kind": "class", + "name": "test1", + "type": "Test1", + }, + ], + }, + "methods": { + "added": [], + "modified": [], + "removed": [], + }, + "unions": { + "added": [], "modified": [], "removed": [ - undefined, + { + "classes": [ + { + "arguments": [], + "id": 3739166976, + "kind": "class", + "name": "test1", + "type": "Test1", + }, + ], + "name": "Test1", + }, ], }, } diff --git a/packages/tl-utils/src/codegen/__snapshots__/errors.test.ts.snap b/packages/tl-utils/src/codegen/__snapshots__/errors.test.ts.snap index ec018883..616c872a 100644 --- a/packages/tl-utils/src/codegen/__snapshots__/errors.test.ts.snap +++ b/packages/tl-utils/src/codegen/__snapshots__/errors.test.ts.snap @@ -125,7 +125,7 @@ RpcError.fromTl = function (obj) { var err = new RpcError(obj.errorCode, obj.errorMessage); var match, param; - if ((match=err.text.match(/^FLOOD_WAIT_(\d+)$/))!=null){ err.text = 'FLOOD_WAIT_%d'; param = err.duration = parseInt(match[1]) } + if ((match=err.text.match(/^FLOOD_WAIT_(d+)$/))!=null){ err.text = 'FLOOD_WAIT_%d'; param = err.duration = parseInt(match[1]) } else return err diff --git a/packages/tl-utils/src/codegen/__snapshots__/reader.test.ts.snap b/packages/tl-utils/src/codegen/__snapshots__/reader.test.ts.snap index e063b51c..d23976f1 100644 --- a/packages/tl-utils/src/codegen/__snapshots__/reader.test.ts.snap +++ b/packages/tl-utils/src/codegen/__snapshots__/reader.test.ts.snap @@ -59,6 +59,65 @@ exports[`generateReaderCodeForTlEntries > updates readers used in bare vectors 1 }" `; +exports[`generateReaderCodeForTlEntries doesn't generate code for methods by default 1`] = ` +"var m={ +471282454:function(r){return{_:'test'}}, +}" +`; + +exports[`generateReaderCodeForTlEntries generates code for methods if asked to 1`] = ` +"var m={ +471282454:function(r){return{_:'test'}}, +2119910527:function(r){return{_:'test2'}}, +}" +`; + +exports[`generateReaderCodeForTlEntries generates code for multiple entries 1`] = ` +"var m={ +471282454:function(r){return{_:'test'}}, +2119910527:function(r){return{_:'test2'}}, +}" +`; + +exports[`generateReaderCodeForTlEntries method return readers doesn't include Bool parsing 1`] = ` +"var m={ +_results:{ +}, +}" +`; + +exports[`generateReaderCodeForTlEntries method return readers includes primitive return type parsing info 1`] = ` +"var m={ +1809692154:function(r){return{_:'test1'}}, +_results:{ +'test':function(r){return r.int()}, +}, +}" +`; + +exports[`generateReaderCodeForTlEntries method return readers includes primitive vectors return type parsing info 1`] = ` +"var m={ +_results:{ +'test':function(r){return r.vector(r.int)}, +}, +}" +`; + +exports[`generateReaderCodeForTlEntries method return readers includes primitive vectors return type parsing info 2`] = ` +"var m={ +_results:{ +'test':function(r){return r.vector(r.int)}, +}, +}" +`; + +exports[`generateReaderCodeForTlEntries updates readers used in bare vectors 1`] = ` +"var m={ +471282454:function(r=this){return{_:'test'}}, +3562390222:function(r){return{_:'test2',a:r.vector(m[471282454],1),}}, +}" +`; + exports[`generateReaderCodeForTlEntry > generates code for bare types 1`] = `"1945237724:function(r){return{_:'msg_container',messages:r.vector(m[155834844],1),}},"`; exports[`generateReaderCodeForTlEntry > generates code for bare types 2`] = `"2924480661:function(r){return{_:'future_salts',salts:r.vector(m[155834844]),current:r.object(),}},"`; @@ -89,7 +148,21 @@ exports[`generateReaderCodeForTlEntry > generates code for constructors without exports[`generateReaderCodeForTlEntry > generates code with raw flags for constructors with flags 1`] = `"1554225816:function(r){var flags=r.uint(),flags2=r.uint();return{_:'test',flags:flags,flags2:flags2,}},"`; -exports[`generateReaderCodeForTlEntry generates code for constructors without arguments 1`] = `"2875595611:function(r){return{_:'topPeerCategoryBotsPM'}},"`; +exports[`generateReaderCodeForTlEntry generates code for bare types 1`] = `"1945237724:function(r){return{_:'msg_container',messages:r.vector(m[155834844],1),}},"`; + +exports[`generateReaderCodeForTlEntry generates code for bare types 2`] = `"2924480661:function(r){return{_:'future_salts',salts:r.vector(m[155834844]),current:r.object(),}},"`; + +exports[`generateReaderCodeForTlEntry generates code for bare types 3`] = `"2924480661:function(r){return{_:'future_salts',salts:r.vector(m[155834844],1),current:m[155834844](r),}},"`; + +exports[`generateReaderCodeForTlEntry generates code for constructors with arguments before flags field 1`] = `"2262925665:function(r){var id=r.long(),flags=r.uint();return{_:'poll',id:id,quiz:!!(flags&8),question:r.string(),}},"`; + +exports[`generateReaderCodeForTlEntry generates code for constructors with generics 1`] = `"3667594509:function(r){return{_:'invokeWithLayer',layer:r.int(),query:r.object(),}},"`; + +exports[`generateReaderCodeForTlEntry generates code for constructors with multiple flags fields 1`] = `"1041346555:function(r){var flags=r.uint(),pts=r.int(),timeout=flags&2?r.int():void 0,flags2=r.uint();return{_:'updates.channelDifferenceEmpty',final:!!(flags&1),pts:pts,timeout:timeout,canDeleteChannel:!!(flags2&1),}},"`; + +exports[`generateReaderCodeForTlEntry generates code for constructors with optional arguments 1`] = `"1041346555:function(r){var flags=r.uint();return{_:'updates.channelDifferenceEmpty',final:!!(flags&1),pts:r.int(),timeout:flags&2?r.int():void 0,}},"`; + +exports[`generateReaderCodeForTlEntry generates code for constructors with optional vector arguments 1`] = `"2338894028:function(r){var flags=r.uint();return{_:'messages.getWebPagePreview',message:r.string(),entities:flags&8?r.vector(r.object):void 0,}},"`; exports[`generateReaderCodeForTlEntry generates code for constructors with simple arguments 1`] = `"2299280777:function(r){return{_:'inputBotInlineMessageID',dcId:r.int(),id:r.long(),accessHash:r.long(),}},"`; @@ -99,81 +172,8 @@ exports[`generateReaderCodeForTlEntry generates code for constructors with simpl exports[`generateReaderCodeForTlEntry generates code for constructors with true flags 1`] = `"649453030:function(r){var flags=r.uint();return{_:'messages.messageEditData',caption:!!(flags&1),}},"`; -exports[`generateReaderCodeForTlEntry generates code for constructors with optional arguments 1`] = `"1041346555:function(r){var flags=r.uint();return{_:'updates.channelDifferenceEmpty',final:!!(flags&1),pts:r.int(),timeout:flags&2?r.int():void 0,}},"`; - -exports[`generateReaderCodeForTlEntry generates code for constructors with arguments before flags field 1`] = `"2262925665:function(r){var id=r.long(),flags=r.uint();return{_:'poll',id:id,quiz:!!(flags&8),question:r.string(),}},"`; - -exports[`generateReaderCodeForTlEntry generates code for constructors with multiple flags fields 1`] = `"1041346555:function(r){var flags=r.uint(),pts=r.int(),timeout=flags&2?r.int():void 0,flags2=r.uint();return{_:'updates.channelDifferenceEmpty',final:!!(flags&1),pts:pts,timeout:timeout,canDeleteChannel:!!(flags2&1),}},"`; - exports[`generateReaderCodeForTlEntry generates code for constructors with vector arguments 1`] = `"2131196633:function(r){return{_:'contacts.resolvedPeer',peer:r.object(),chats:r.vector(r.object),users:r.vector(r.object),}},"`; -exports[`generateReaderCodeForTlEntry generates code for constructors with optional vector arguments 1`] = `"2338894028:function(r){var flags=r.uint();return{_:'messages.getWebPagePreview',message:r.string(),entities:flags&8?r.vector(r.object):void 0,}},"`; - -exports[`generateReaderCodeForTlEntry generates code for constructors with generics 1`] = `"3667594509:function(r){return{_:'invokeWithLayer',layer:r.int(),query:r.object(),}},"`; - -exports[`generateReaderCodeForTlEntry generates code for bare types 1`] = `"1945237724:function(r){return{_:'msg_container',messages:r.vector(m[155834844],1),}},"`; - -exports[`generateReaderCodeForTlEntry generates code for bare types 2`] = `"2924480661:function(r){return{_:'future_salts',salts:r.vector(m[155834844]),current:r.object(),}},"`; - -exports[`generateReaderCodeForTlEntry generates code for bare types 3`] = `"2924480661:function(r){return{_:'future_salts',salts:r.vector(m[155834844],1),current:m[155834844](r),}},"`; +exports[`generateReaderCodeForTlEntry generates code for constructors without arguments 1`] = `"2875595611:function(r){return{_:'topPeerCategoryBotsPM'}},"`; exports[`generateReaderCodeForTlEntry generates code with raw flags for constructors with flags 1`] = `"1554225816:function(r){var flags=r.uint(),flags2=r.uint();return{_:'test',flags:flags,flags2:flags2,}},"`; - -exports[`generateReaderCodeForTlEntries method return readers includes primitive return type parsing info 1`] = ` -"var m={ -1809692154:function(r){return{_:'test1'}}, -_results:{ -'test':function(r){return r.int()}, -}, -}" -`; - -exports[`generateReaderCodeForTlEntries method return readers includes primitive vectors return type parsing info 1`] = ` -"var m={ -_results:{ -'test':function(r){return r.vector(r.int)}, -}, -}" -`; - -exports[`generateReaderCodeForTlEntries method return readers includes primitive vectors return type parsing info 2`] = ` -"var m={ -_results:{ -'test':function(r){return r.vector(r.int)}, -}, -}" -`; - -exports[`generateReaderCodeForTlEntries method return readers doesn't include Bool parsing 1`] = ` -"var m={ -_results:{ -}, -}" -`; - -exports[`generateReaderCodeForTlEntries generates code for multiple entries 1`] = ` -"var m={ -471282454:function(r){return{_:'test'}}, -2119910527:function(r){return{_:'test2'}}, -}" -`; - -exports[`generateReaderCodeForTlEntries doesn't generate code for methods by default 1`] = ` -"var m={ -471282454:function(r){return{_:'test'}}, -}" -`; - -exports[`generateReaderCodeForTlEntries generates code for methods if asked to 1`] = ` -"var m={ -471282454:function(r){return{_:'test'}}, -2119910527:function(r){return{_:'test2'}}, -}" -`; - -exports[`generateReaderCodeForTlEntries updates readers used in bare vectors 1`] = ` -"var m={ -471282454:function(r=this){return{_:'test'}}, -3562390222:function(r){return{_:'test2',a:r.vector(m[471282454],1),}}, -}" -`; diff --git a/packages/tl-utils/src/codegen/__snapshots__/types.test.ts.snap b/packages/tl-utils/src/codegen/__snapshots__/types.test.ts.snap index 1b571a40..71966bb9 100644 --- a/packages/tl-utils/src/codegen/__snapshots__/types.test.ts.snap +++ b/packages/tl-utils/src/codegen/__snapshots__/types.test.ts.snap @@ -163,6 +163,169 @@ exports[`generateTypescriptDefinitionsForTlEntry > writes generic types 1`] = ` }" `; +exports[`generateTypescriptDefinitionsForTlEntry comments adds return type comments 1`] = ` +"/** + * This is a test method + * + * RPC method returns {@link tl.TypeTest} + */ +interface RawTestRequest { + _: 'test'; +}" +`; + +exports[`generateTypescriptDefinitionsForTlEntry comments adds return type comments 2`] = ` +"/** + * This is a test method + * + * RPC method returns {@link tl.TypeTest} + */ +interface RawTestRequest { + _: 'test'; +}" +`; + +exports[`generateTypescriptDefinitionsForTlEntry comments adds return type comments 3`] = ` +"/** + * RPC method returns {@link tl.TypeTest} array + */ +interface RawTestRequest { + _: 'test'; +}" +`; + +exports[`generateTypescriptDefinitionsForTlEntry comments adds tdlib style comments 1`] = ` +"/** + * This is a test constructor + */ +interface RawTest { + _: 'test'; + /** + * Some field + */ + field: number; +}" +`; + +exports[`generateTypescriptDefinitionsForTlEntry comments adds tl style comments 1`] = ` +"/** + * This is a test constructor + */ +interface RawTest { + _: 'test'; +}" +`; + +exports[`generateTypescriptDefinitionsForTlEntry comments adds usage info comments 1`] = ` +"/** + * RPC method returns {@link tl.TypeTest} + * + * This method is **not** available for bots + * + * This method *may* throw one of these errors: FOO, BAR + */ +interface RawTestRequest { + _: 'test'; +}" +`; + +exports[`generateTypescriptDefinitionsForTlEntry comments adds usage info comments 2`] = ` +"/** + * RPC method returns {@link tl.TypeTest} + * + * This method is **not** available for normal users + */ +interface RawTestBotRequest { + _: 'testBot'; +}" +`; + +exports[`generateTypescriptDefinitionsForTlEntry comments should not break @link tags 1`] = ` +"/** + * This is a test constructor with a very long comment + * {@link whatever} more text + */ +interface RawTest { + _: 'test'; +}" +`; + +exports[`generateTypescriptDefinitionsForTlEntry comments wraps long comments 1`] = ` +"/** + * This is a test constructor with a very very very very very + * very very very long comment + */ +interface RawTest { + _: 'test'; +}" +`; + +exports[`generateTypescriptDefinitionsForTlEntry comments wraps long comments 2`] = ` +"/** + * This is a test method with a very very very very very very + * very very long comment + * + * RPC method returns {@link tl.TypeTest} + */ +interface RawTestRequest { + _: 'test'; +}" +`; + +exports[`generateTypescriptDefinitionsForTlEntry generates code with raw flags for constructors with flags 1`] = ` +"interface RawTest { + _: 'test'; + flags: number; + flags2: number; +}" +`; + +exports[`generateTypescriptDefinitionsForTlEntry ignores namespace for name 1`] = ` +"interface RawTest { + _: 'test.test'; +}" +`; + +exports[`generateTypescriptDefinitionsForTlEntry marks optional fields as optional 1`] = ` +"interface RawTest { + _: 'test'; + a?: boolean; + b?: string; + c?: tl.TypeFoo; + d?: tl.namespace.TypeFoo[]; +}" +`; + +exports[`generateTypescriptDefinitionsForTlEntry renames non-primitive types 1`] = ` +"interface RawTest { + _: 'test'; + foo: tl.TypeFoo; + bar: tl.TypeBar[]; + baz: tl.namespace.TypeBaz; + egg: tl.namespace.TypeEgg[]; +}" +`; + +exports[`generateTypescriptDefinitionsForTlEntry replaces primitive types 1`] = ` +"interface RawTest { + _: 'test'; + a: number; + b: Long; + c: Double; + d: string; + e: Uint8Array; + f: boolean; + g: number[]; +}" +`; + +exports[`generateTypescriptDefinitionsForTlEntry writes generic types 1`] = ` +"interface RawInvokeWithoutUpdatesRequest { + _: 'invokeWithoutUpdates'; + query: X; +}" +`; + exports[`generateTypescriptDefinitionsForTlSchema > writes schemas with methods 1`] = ` "interface RawTest { _: 'test'; @@ -291,209 +454,6 @@ exports[`generateTypescriptDefinitionsForTlSchema > writes simple schemas 2`] = _types = JSON.parse('{"test":"Test"}');" `; -exports[`generateTypescriptDefinitionsForTlEntry comments adds tl style comments 1`] = ` -"/** - * This is a test constructor - */ -interface RawTest { - _: 'test'; -}" -`; - -exports[`generateTypescriptDefinitionsForTlEntry comments adds return type comments 1`] = ` -"/** - * This is a test method - * - * RPC method returns {@link tl.TypeTest} - */ -interface RawTestRequest { - _: 'test'; -}" -`; - -exports[`generateTypescriptDefinitionsForTlEntry comments adds return type comments 2`] = ` -"/** - * This is a test method - * - * RPC method returns {@link tl.TypeTest} - */ -interface RawTestRequest { - _: 'test'; -}" -`; - -exports[`generateTypescriptDefinitionsForTlEntry comments adds return type comments 3`] = ` -"/** - * RPC method returns {@link tl.TypeTest} array - */ -interface RawTestRequest { - _: 'test'; -}" -`; - -exports[`generateTypescriptDefinitionsForTlEntry comments adds usage info comments 1`] = ` -"/** - * RPC method returns {@link tl.TypeTest} - * - * This method is **not** available for bots - * - * This method *may* throw one of these errors: FOO, BAR - */ -interface RawTestRequest { - _: 'test'; -}" -`; - -exports[`generateTypescriptDefinitionsForTlEntry comments adds usage info comments 2`] = ` -"/** - * RPC method returns {@link tl.TypeTest} - * - * This method is **not** available for normal users - */ -interface RawTestBotRequest { - _: 'testBot'; -}" -`; - -exports[`generateTypescriptDefinitionsForTlEntry comments adds tdlib style comments 1`] = ` -"/** - * This is a test constructor - */ -interface RawTest { - _: 'test'; - /** - * Some field - */ - field: number; -}" -`; - -exports[`generateTypescriptDefinitionsForTlEntry comments wraps long comments 1`] = ` -"/** - * This is a test constructor with a very very very very very - * very very very long comment - */ -interface RawTest { - _: 'test'; -}" -`; - -exports[`generateTypescriptDefinitionsForTlEntry comments wraps long comments 2`] = ` -"/** - * This is a test method with a very very very very very very - * very very long comment - * - * RPC method returns {@link tl.TypeTest} - */ -interface RawTestRequest { - _: 'test'; -}" -`; - -exports[`generateTypescriptDefinitionsForTlEntry comments should not break @link tags 1`] = ` -"/** - * This is a test constructor with a very long comment - * {@link whatever} more text - */ -interface RawTest { - _: 'test'; -}" -`; - -exports[`generateTypescriptDefinitionsForTlEntry replaces primitive types 1`] = ` -"interface RawTest { - _: 'test'; - a: number; - b: Long; - c: Double; - d: string; - e: Uint8Array; - f: boolean; - g: number[]; -}" -`; - -exports[`generateTypescriptDefinitionsForTlEntry ignores namespace for name 1`] = ` -"interface RawTest { - _: 'test.test'; -}" -`; - -exports[`generateTypescriptDefinitionsForTlEntry renames non-primitive types 1`] = ` -"interface RawTest { - _: 'test'; - foo: tl.TypeFoo; - bar: tl.TypeBar[]; - baz: tl.namespace.TypeBaz; - egg: tl.namespace.TypeEgg[]; -}" -`; - -exports[`generateTypescriptDefinitionsForTlEntry marks optional fields as optional 1`] = ` -"interface RawTest { - _: 'test'; - a?: boolean; - b?: string; - c?: tl.TypeFoo; - d?: tl.namespace.TypeFoo[]; -}" -`; - -exports[`generateTypescriptDefinitionsForTlEntry writes generic types 1`] = ` -"interface RawInvokeWithoutUpdatesRequest { - _: 'invokeWithoutUpdates'; - query: X; -}" -`; - -exports[`generateTypescriptDefinitionsForTlEntry generates code with raw flags for constructors with flags 1`] = ` -"interface RawTest { - _: 'test'; - flags: number; - flags2: number; -}" -`; - -exports[`generateTypescriptDefinitionsForTlSchema writes simple schemas 1`] = ` -"interface RawTest { - _: 'test'; -} -interface RpcCallReturn { -} -type TypeTest = tl.RawTest -function isAnyTest(o: object): o is TypeTest - -type TlObject = - | tl.RawTest" -`; - -exports[`generateTypescriptDefinitionsForTlSchema writes simple schemas 2`] = ` -"ns.isAnyTest = _isAny('Test'); -_types = JSON.parse('{"test":"Test"}');" -`; - -exports[`generateTypescriptDefinitionsForTlSchema writes schemas with multi-unions 1`] = ` -"interface RawTest { - _: 'test'; -} -interface RawTest2 { - _: 'test2'; -} -interface RpcCallReturn { -} -type TypeTest = tl.RawTest | tl.RawTest2 -function isAnyTest(o: object): o is TypeTest - -type TlObject = - | tl.RawTest - | tl.RawTest2" -`; - -exports[`generateTypescriptDefinitionsForTlSchema writes schemas with multi-unions 2`] = ` -"ns.isAnyTest = _isAny('Test'); -_types = JSON.parse('{"test":"Test","test2":"Test"}');" -`; - exports[`generateTypescriptDefinitionsForTlSchema writes schemas with methods 1`] = ` "interface RawTest { _: 'test'; @@ -522,6 +482,28 @@ exports[`generateTypescriptDefinitionsForTlSchema writes schemas with methods 2` _types = JSON.parse('{"test":"Test"}');" `; +exports[`generateTypescriptDefinitionsForTlSchema writes schemas with multi-unions 1`] = ` +"interface RawTest { + _: 'test'; +} +interface RawTest2 { + _: 'test2'; +} +interface RpcCallReturn { +} +type TypeTest = tl.RawTest | tl.RawTest2 +function isAnyTest(o: object): o is TypeTest + +type TlObject = + | tl.RawTest + | tl.RawTest2" +`; + +exports[`generateTypescriptDefinitionsForTlSchema writes schemas with multi-unions 2`] = ` +"ns.isAnyTest = _isAny('Test'); +_types = JSON.parse('{"test":"Test","test2":"Test"}');" +`; + exports[`generateTypescriptDefinitionsForTlSchema writes schemas with namespaces 1`] = ` "interface RawTest { _: 'test'; @@ -581,3 +563,21 @@ ns.isAnyTest = _isAny('test.Test'); })(ns.test); _types = JSON.parse('{"test":"Test","test2":"Test","test.test":"test.Test","test.test2":"test.Test"}');" `; + +exports[`generateTypescriptDefinitionsForTlSchema writes simple schemas 1`] = ` +"interface RawTest { + _: 'test'; +} +interface RpcCallReturn { +} +type TypeTest = tl.RawTest +function isAnyTest(o: object): o is TypeTest + +type TlObject = + | tl.RawTest" +`; + +exports[`generateTypescriptDefinitionsForTlSchema writes simple schemas 2`] = ` +"ns.isAnyTest = _isAny('Test'); +_types = JSON.parse('{"test":"Test"}');" +`; diff --git a/packages/tl-utils/src/codegen/__snapshots__/writer.test.ts.snap b/packages/tl-utils/src/codegen/__snapshots__/writer.test.ts.snap index d7bbc466..ff9a4152 100644 --- a/packages/tl-utils/src/codegen/__snapshots__/writer.test.ts.snap +++ b/packages/tl-utils/src/codegen/__snapshots__/writer.test.ts.snap @@ -29,6 +29,35 @@ _staticSize:{ }" `; +exports[`generateWriterCodeForTlEntries generates code for bare types 1`] = ` +"var m={ +'future_salt':function(w,v){w.uint(155834844);w.bytes(h(v,'salt'));}, +'future_salts':function(w,v){w.uint(2924480661);w.vector(m._bare[155834844],h(v,'salts'),1);m._bare[155834844](w,h(v,'current'));}, +_bare:{ +155834844:function(w=this,v){w.bytes(h(v,'salt'));}, +}, +}" +`; + +exports[`generateWriterCodeForTlEntries should include prelude by default 1`] = ` +"function h(o,p){var q=o[p];if(q===void 0)throw Error('Object '+o._+' is missing required property '+p);return q} +var m={ +}" +`; + +exports[`generateWriterCodeForTlEntries should include static sizes calculations 1`] = ` +"function h(o,p){var q=o[p];if(q===void 0)throw Error('Object '+o._+' is missing required property '+p);return q} +var m={ +'test1':function(w,v){w.uint(102026291);w.int(h(v,'foo'));w.int(h(v,'bar'));}, +'test2':function(w,v){w.uint(2926357645);w.int(h(v,'foo'));w.double(h(v,'bar'));}, +'test3':function(w,v){w.uint(3373702963);w.int(h(v,'foo'));w.bytes(h(v,'bar'));}, +_staticSize:{ +'test1':12, +'test2':16, +}, +}" +`; + exports[`generateWriterCodeForTlEntry > automatically computes constructor ID if needed 1`] = `"'topPeerCategoryBotsPM':function(w){w.uint(2875595611);},"`; exports[`generateWriterCodeForTlEntry > generates code for bare vectors 1`] = `"'msg_container':function(w,v){w.uint(1945237724);w.vector(m._bare[155834844],h(v,'messages'),1);},"`; @@ -59,7 +88,21 @@ exports[`generateWriterCodeForTlEntry > generates code for constructors without exports[`generateWriterCodeForTlEntry > generates code with raw flags for constructors with flags 1`] = `"'test':function(w,v){w.uint(1554225816);var flags=v.flags;w.uint(flags);var flags2=v.flags2;w.uint(flags2);},"`; -exports[`generateWriterCodeForTlEntry generates code for constructors without arguments 1`] = `"'topPeerCategoryBotsPM':function(w){w.uint(2875595611);},"`; +exports[`generateWriterCodeForTlEntry automatically computes constructor ID if needed 1`] = `"'topPeerCategoryBotsPM':function(w){w.uint(2875595611);},"`; + +exports[`generateWriterCodeForTlEntry generates code for bare vectors 1`] = `"'msg_container':function(w,v){w.uint(1945237724);w.vector(m._bare[155834844],h(v,'messages'),1);},"`; + +exports[`generateWriterCodeForTlEntry generates code for bare vectors 2`] = `"'future_salts':function(w,v){w.uint(2924480661);w.vector(m._bare[155834844],h(v,'salts'));w.object(h(v,'current'));},"`; + +exports[`generateWriterCodeForTlEntry generates code for constructors with generics 1`] = `"'invokeWithLayer':function(w,v){w.uint(3667594509);w.int(h(v,'layer'));w.object(h(v,'query'));},"`; + +exports[`generateWriterCodeForTlEntry generates code for constructors with multiple fields using the same flag 1`] = `"'inputMediaPoll':function(w,v){w.uint(261416433);var flags=0;var _solution=v.solution!==undefined;var _solutionEntities=v.solutionEntities&&v.solutionEntities.length;var _flags_1=_solution||_solutionEntities;if(_flags_1)flags|=2;w.uint(flags);if(_flags_1)w.string(v.solution);if(_flags_1)w.vector(w.object,v.solutionEntities);},"`; + +exports[`generateWriterCodeForTlEntry generates code for constructors with multiple flags fields 1`] = `"'updates.channelDifferenceEmpty':function(w,v){w.uint(1041346555);var flags=0;if(v.final===true)flags|=1;var _timeout=v.timeout!==undefined;if(_timeout)flags|=2;w.uint(flags);w.int(h(v,'pts'));if(_timeout)w.int(v.timeout);var flags2=0;if(v.canDeleteChannel===true)flags2|=1;w.uint(flags2);},"`; + +exports[`generateWriterCodeForTlEntry generates code for constructors with optional arguments 1`] = `"'updates.channelDifferenceEmpty':function(w,v){w.uint(1041346555);var flags=0;if(v.final===true)flags|=1;var _timeout=v.timeout!==undefined;if(_timeout)flags|=2;w.uint(flags);w.int(h(v,'pts'));if(_timeout)w.int(v.timeout);},"`; + +exports[`generateWriterCodeForTlEntry generates code for constructors with optional vector arguments 1`] = `"'messages.getWebPagePreview':function(w,v){w.uint(2338894028);var flags=0;var _entities=v.entities&&v.entities.length;if(_entities)flags|=8;w.uint(flags);w.string(h(v,'message'));if(_entities)w.vector(w.object,v.entities);},"`; exports[`generateWriterCodeForTlEntry generates code for constructors with simple arguments 1`] = `"'inputBotInlineMessageID':function(w,v){w.uint(2299280777);w.int(h(v,'dcId'));w.long(h(v,'id'));w.long(h(v,'accessHash'));},"`; @@ -69,51 +112,8 @@ exports[`generateWriterCodeForTlEntry generates code for constructors with simpl exports[`generateWriterCodeForTlEntry generates code for constructors with true flags 1`] = `"'messages.messageEditData':function(w,v){w.uint(649453030);var flags=0;if(v.caption===true)flags|=1;w.uint(flags);},"`; -exports[`generateWriterCodeForTlEntry generates code for constructors with optional arguments 1`] = `"'updates.channelDifferenceEmpty':function(w,v){w.uint(1041346555);var flags=0;if(v.final===true)flags|=1;var _timeout=v.timeout!==undefined;if(_timeout)flags|=2;w.uint(flags);w.int(h(v,'pts'));if(_timeout)w.int(v.timeout);},"`; - -exports[`generateWriterCodeForTlEntry generates code for constructors with multiple flags fields 1`] = `"'updates.channelDifferenceEmpty':function(w,v){w.uint(1041346555);var flags=0;if(v.final===true)flags|=1;var _timeout=v.timeout!==undefined;if(_timeout)flags|=2;w.uint(flags);w.int(h(v,'pts'));if(_timeout)w.int(v.timeout);var flags2=0;if(v.canDeleteChannel===true)flags2|=1;w.uint(flags2);},"`; - -exports[`generateWriterCodeForTlEntry generates code for constructors with multiple fields using the same flag 1`] = `"'inputMediaPoll':function(w,v){w.uint(261416433);var flags=0;var _solution=v.solution!==undefined;var _solutionEntities=v.solutionEntities&&v.solutionEntities.length;var _flags_1=_solution||_solutionEntities;if(_flags_1)flags|=2;w.uint(flags);if(_flags_1)w.string(v.solution);if(_flags_1)w.vector(w.object,v.solutionEntities);},"`; - exports[`generateWriterCodeForTlEntry generates code for constructors with vector arguments 1`] = `"'contacts.resolvedPeer':function(w,v){w.uint(2131196633);w.object(h(v,'peer'));w.vector(w.object,h(v,'chats'));w.vector(w.object,h(v,'users'));},"`; -exports[`generateWriterCodeForTlEntry generates code for constructors with optional vector arguments 1`] = `"'messages.getWebPagePreview':function(w,v){w.uint(2338894028);var flags=0;var _entities=v.entities&&v.entities.length;if(_entities)flags|=8;w.uint(flags);w.string(h(v,'message'));if(_entities)w.vector(w.object,v.entities);},"`; - -exports[`generateWriterCodeForTlEntry generates code for constructors with generics 1`] = `"'invokeWithLayer':function(w,v){w.uint(3667594509);w.int(h(v,'layer'));w.object(h(v,'query'));},"`; - -exports[`generateWriterCodeForTlEntry generates code for bare vectors 1`] = `"'msg_container':function(w,v){w.uint(1945237724);w.vector(m._bare[155834844],h(v,'messages'),1);},"`; - -exports[`generateWriterCodeForTlEntry generates code for bare vectors 2`] = `"'future_salts':function(w,v){w.uint(2924480661);w.vector(m._bare[155834844],h(v,'salts'));w.object(h(v,'current'));},"`; +exports[`generateWriterCodeForTlEntry generates code for constructors without arguments 1`] = `"'topPeerCategoryBotsPM':function(w){w.uint(2875595611);},"`; exports[`generateWriterCodeForTlEntry generates code with raw flags for constructors with flags 1`] = `"'test':function(w,v){w.uint(1554225816);var flags=v.flags;w.uint(flags);var flags2=v.flags2;w.uint(flags2);},"`; - -exports[`generateWriterCodeForTlEntry automatically computes constructor ID if needed 1`] = `"'topPeerCategoryBotsPM':function(w){w.uint(2875595611);},"`; - -exports[`generateWriterCodeForTlEntries generates code for bare types 1`] = ` -"var m={ -'future_salt':function(w,v){w.uint(155834844);w.bytes(h(v,'salt'));}, -'future_salts':function(w,v){w.uint(2924480661);w.vector(m._bare[155834844],h(v,'salts'),1);m._bare[155834844](w,h(v,'current'));}, -_bare:{ -155834844:function(w=this,v){w.bytes(h(v,'salt'));}, -}, -}" -`; - -exports[`generateWriterCodeForTlEntries should include prelude by default 1`] = ` -"function h(o,p){var q=o[p];if(q===void 0)throw Error('Object '+o._+' is missing required property '+p);return q} -var m={ -}" -`; - -exports[`generateWriterCodeForTlEntries should include static sizes calculations 1`] = ` -"function h(o,p){var q=o[p];if(q===void 0)throw Error('Object '+o._+' is missing required property '+p);return q} -var m={ -'test1':function(w,v){w.uint(102026291);w.int(h(v,'foo'));w.int(h(v,'bar'));}, -'test2':function(w,v){w.uint(2926357645);w.int(h(v,'foo'));w.double(h(v,'bar'));}, -'test3':function(w,v){w.uint(3373702963);w.int(h(v,'foo'));w.bytes(h(v,'bar'));}, -_staticSize:{ -'test1':12, -'test2':16, -}, -}" -`; diff --git a/packages/wasm/tests/init.ts b/packages/wasm/tests/init.ts index 2723bca5..a6cbd1c7 100644 --- a/packages/wasm/tests/init.ts +++ b/packages/wasm/tests/init.ts @@ -3,7 +3,7 @@ import { initSync } from '../src/index.js' export async function initWasm() { const url = new URL('../mtcute.wasm', import.meta.url) - if (import.meta.env.TEST_ENV === 'node' || import.meta.env.TEST_ENV === 'bun') { + if (import.meta.env.TEST_ENV === 'node') { const fs = await import('fs/promises') const blob = await fs.readFile(url) initSync(blob) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e10b32f0..1eed727d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -42,6 +42,9 @@ importers: '@vitest/ui': specifier: 1.4.0 version: 1.4.0(vitest@1.4.0) + chai: + specifier: ^5.1.0 + version: 5.1.0 cjs-module-lexer: specifier: 1.2.3 version: 1.2.3 @@ -1620,7 +1623,7 @@ packages: dependencies: '@vitest/spy': 0.34.6 '@vitest/utils': 0.34.6 - chai: 4.3.10 + chai: 4.4.1 dev: false /@vitest/expect@1.4.0: @@ -1909,6 +1912,11 @@ packages: /assertion-error@1.1.0: resolution: {integrity: sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==} + /assertion-error@2.0.1: + resolution: {integrity: sha512-Izi8RQcffqCeNVgFigKli1ssklIbpHnCYc6AknXGYoB6grJqyeby7jv12JUQgmTAnIDnbck1uxksT4dzN3PWBA==} + engines: {node: '>=12'} + dev: true + /astral-regex@2.0.0: resolution: {integrity: sha512-Z7tMw1ytTXt5jqMcOP+OQteU1VuNK9Y02uuJtKQ1Sv69jXQKKg5cibLwGJow8yzZP+eAc18EmLGPal0bp36rvQ==} engines: {node: '>=8'} @@ -2028,19 +2036,6 @@ packages: engines: {node: '>=6'} dev: true - /chai@4.3.10: - resolution: {integrity: sha512-0UXG04VuVbruMUYbJ6JctvH0YnC/4q3/AkT18q4NaITo91CUm0liMS9VqzT9vZhVQ/1eqPanMWjBM+Juhfb/9g==} - engines: {node: '>=4'} - dependencies: - assertion-error: 1.1.0 - check-error: 1.0.3 - deep-eql: 4.1.3 - get-func-name: 2.0.2 - loupe: 2.3.7 - pathval: 1.1.1 - type-detect: 4.0.8 - dev: false - /chai@4.4.1: resolution: {integrity: sha512-13sOfMv2+DWduEU+/xbun3LScLoqN17nBeTLUsmDfKdoiC1fr0n9PU4guu4AhRcOVFk/sW8LyZWHuhWtQZiF+g==} engines: {node: '>=4'} @@ -2053,6 +2048,17 @@ packages: pathval: 1.1.1 type-detect: 4.0.8 + /chai@5.1.0: + resolution: {integrity: sha512-kDZ7MZyM6Q1DhR9jy7dalKohXQ2yrlXkk59CR52aRKxJrobmlBNqnFQxX9xOX8w+4mz8SYlKJa/7D7ddltFXCw==} + engines: {node: '>=12'} + dependencies: + assertion-error: 2.0.1 + check-error: 2.0.0 + deep-eql: 5.0.1 + loupe: 3.1.0 + pathval: 2.0.0 + dev: true + /chalk@2.4.2: resolution: {integrity: sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==} engines: {node: '>=4'} @@ -2088,6 +2094,11 @@ packages: dependencies: get-func-name: 2.0.2 + /check-error@2.0.0: + resolution: {integrity: sha512-tjLAOBHKVxtPoHe/SA7kNOMvhCRdCJ3vETdeY0RuAc9popf+hyaSV6ZEg9hr4cpWF7jmo/JSWEnLDrnijS9Tog==} + engines: {node: '>= 16'} + dev: true + /cheerio-select@2.1.0: resolution: {integrity: sha512-9v9kG0LvzrlcungtnJtpGNxY+fzECQKhK4EGJX2vByejiMX84MFNQw4UxPJl3bFbTMw+Dfs37XaIkCwTZfLh4g==} dependencies: @@ -2371,6 +2382,11 @@ packages: dependencies: type-detect: 4.0.8 + /deep-eql@5.0.1: + resolution: {integrity: sha512-nwQCf6ne2gez3o1MxWifqkciwt0zhl0LO1/UwVu4uMBuPmflWM4oQ70XMqHqnBJA+nhzncaqL9HVL6KkHJ28lw==} + engines: {node: '>=6'} + dev: true + /deep-extend@0.6.0: resolution: {integrity: sha512-LOHxIOaPYdHlJRtCQfDIVZtfw/ufM8+rVj649RIHzcm/vGwQRXFt6OPqIFWsm2XEMrNIEtWR64sY1LEKD2vAOA==} engines: {node: '>=4.0.0'} @@ -4056,6 +4072,12 @@ packages: dependencies: get-func-name: 2.0.2 + /loupe@3.1.0: + resolution: {integrity: sha512-qKl+FrLXUhFuHUoDJG7f8P8gEMHq9NFS0c6ghXG1J0rldmZFQZoNVv/vyirE9qwCIhWZDsvEFd1sbFu3GvRQFg==} + dependencies: + get-func-name: 2.0.2 + dev: true + /lru-cache@6.0.0: resolution: {integrity: sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==} engines: {node: '>=10'} @@ -4677,6 +4699,11 @@ packages: /pathval@1.1.1: resolution: {integrity: sha512-Dp6zGqpTdETdR63lehJYPeIOqpiNBNtc7BpWSLrOje7UaIsE5aY92r/AunQA7rsXvet3lrJ3JnZX29UPTKXyKQ==} + /pathval@2.0.0: + resolution: {integrity: sha512-vE7JKRyES09KiunauX7nd2Q9/L7lhok4smP9RZTDeD4MVs72Dp2qNFVz39Nz5a0FVEW0BJR6C0DYrq6unoziZA==} + engines: {node: '>= 14.16'} + dev: true + /picocolors@1.0.0: resolution: {integrity: sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ==} @@ -5773,7 +5800,7 @@ packages: acorn: 8.10.0 acorn-walk: 8.2.0 cac: 6.7.14 - chai: 4.3.10 + chai: 4.4.1 debug: 4.3.4 local-pkg: 0.4.3 magic-string: 0.30.5