mirror of
https://github.com/zhigang1992/mtcute.git
synced 2026-01-12 22:49:02 +08:00
chore: use vitest directly for bun and deno
This commit is contained in:
24
.config/deno-import-map.json
Normal file
24
.config/deno-import-map.json
Normal file
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"imports": {
|
||||
"@fuman/utils": "jsr:@fuman/utils@0.0.10",
|
||||
"chai": "https://esm.sh/chai@5.1.2?pin=v135",
|
||||
"node:test": "./deno-shims/node-test.js",
|
||||
"mtcute": "../packages/deno/src/index.ts",
|
||||
"mtcute/utils.js": "../packages/deno/src/utils.ts",
|
||||
"@mtcute/core": "../packages/core/src/index.ts",
|
||||
"@mtcute/core/client.js": "../packages/core/src/highlevel/client.ts",
|
||||
"@mtcute/core/methods.js": "../packages/core/src/highlevel/methods.ts",
|
||||
"@mtcute/core/utils.js": "../packages/core/src/utils/index.ts",
|
||||
"@mtcute/core/worker.js": "../packages/core/src/highlevel/worker/index.ts",
|
||||
"@mtcute/markdown-parser": "../packages/markdown-parser/src/index.ts",
|
||||
"@mtcute/html-parser": "../packages/html-parser/src/index.ts",
|
||||
"@mtcute/file-id": "../packages/file-id/src/index.ts",
|
||||
"@mtcute/tl-runtime": "../packages/tl-runtime/src/index.ts",
|
||||
"@mtcute/wasm": "../packages/wasm/src/index.ts",
|
||||
"@mtcute/tl": "./deno-shims/tl.js",
|
||||
"@mtcute/tl/binary/rsa-keys.js": "./deno-shims/tl-rsa.js",
|
||||
"@mtcute/tl/binary/reader.js": "./deno-shims/tl-reader.js",
|
||||
"@mtcute/tl/compat/reader.js": "./deno-shims/tl-reader-compat.js",
|
||||
"@mtcute/tl/binary/writer.js": "./deno-shims/tl-writer.js"
|
||||
}
|
||||
}
|
||||
5
.config/deno-shims/tl-reader-compat.js
Normal file
5
.config/deno-shims/tl-reader-compat.js
Normal file
@@ -0,0 +1,5 @@
|
||||
import { createRequire } from 'node:module'
|
||||
|
||||
const require = createRequire(import.meta.url)
|
||||
|
||||
export const { __tlReaderMapCompat } = require('../../packages/tl/compat/reader.js')
|
||||
@@ -2,4 +2,4 @@ import { createRequire } from 'node:module'
|
||||
|
||||
const require = createRequire(import.meta.url)
|
||||
|
||||
export const { __tlReaderMap } = require('../../../packages/tl/binary/reader.js')
|
||||
export const { __tlReaderMap } = require('../../packages/tl/binary/reader.js')
|
||||
@@ -2,4 +2,4 @@ import { createRequire } from 'node:module'
|
||||
|
||||
const require = createRequire(import.meta.url)
|
||||
|
||||
export const { __publicKeyIndex } = require('../../../packages/tl/binary/rsa-keys.js')
|
||||
export const { __publicKeyIndex } = require('../../packages/tl/binary/rsa-keys.js')
|
||||
@@ -2,4 +2,4 @@ import { createRequire } from 'node:module'
|
||||
|
||||
const require = createRequire(import.meta.url)
|
||||
|
||||
export const { __tlWriterMap } = require('../../../packages/tl/binary/writer.js')
|
||||
export const { __tlWriterMap } = require('../../packages/tl/binary/writer.js')
|
||||
@@ -2,4 +2,4 @@ import { createRequire } from 'node:module'
|
||||
|
||||
const require = createRequire(import.meta.url)
|
||||
|
||||
export const { tl, mtp } = require('../../../packages/tl/index.js')
|
||||
export const { tl, mtp } = require('../../packages/tl/index.js')
|
||||
@@ -1,27 +0,0 @@
|
||||
import * as fs from 'node:fs'
|
||||
import { join, resolve } from 'node:path'
|
||||
|
||||
import { globSync } from 'glob'
|
||||
|
||||
export function collectTestEntrypoints(params: { skipPackages: string[], skipTests: string[] }) {
|
||||
const files: string[] = []
|
||||
|
||||
const packages = resolve(__dirname, '../../packages')
|
||||
|
||||
const skipTests = params.skipTests.map(path => resolve(packages, path))
|
||||
|
||||
for (const dir of fs.readdirSync(packages)) {
|
||||
if (dir.startsWith('.') || params.skipPackages.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 (skipTests.includes(file)) continue
|
||||
if (file.match(/\/(node_modules|dist)\//)) continue
|
||||
files.push(file)
|
||||
}
|
||||
}
|
||||
|
||||
return files
|
||||
}
|
||||
@@ -1,172 +0,0 @@
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { resolve } from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
|
||||
const rootPackageJson = JSON.parse(readFileSync(new URL('../../package.json', import.meta.url), 'utf-8'))
|
||||
const packagesDir = fileURLToPath(new URL('../../packages', import.meta.url))
|
||||
const IS_JSR = process.env.JSR === '1'
|
||||
|
||||
export function getPackageVersion(name) {
|
||||
const json = JSON.parse(readFileSync(resolve(packagesDir, name, 'package.json'), 'utf-8'))
|
||||
return json.version
|
||||
}
|
||||
|
||||
export function processPackageJson(packageDir) {
|
||||
const packageJsonOrig = JSON.parse(readFileSync(resolve(packageDir, 'package.json'), 'utf-8'))
|
||||
const packageJson = structuredClone(packageJsonOrig)
|
||||
const entrypoints = {}
|
||||
|
||||
// copy common fields from root
|
||||
for (const field of ['license', 'author', 'contributors', 'homepage', 'repository', 'bugs']) {
|
||||
if (rootPackageJson[field]) {
|
||||
packageJson[field] = rootPackageJson[field]
|
||||
}
|
||||
}
|
||||
|
||||
const newScripts = {}
|
||||
|
||||
if (packageJson.keepScripts) {
|
||||
for (const script of packageJson.keepScripts) {
|
||||
newScripts[script] = packageJson.scripts[script]
|
||||
}
|
||||
delete packageJson.keepScripts
|
||||
}
|
||||
packageJson.scripts = newScripts
|
||||
delete packageJson.devDependencies
|
||||
delete packageJson.private
|
||||
|
||||
if (packageJson.distOnlyFields) {
|
||||
Object.assign(packageJson, packageJson.distOnlyFields)
|
||||
delete packageJson.distOnlyFields
|
||||
}
|
||||
|
||||
if (packageJson.jsrOnlyFields) {
|
||||
if (IS_JSR) {
|
||||
Object.assign(packageJson, packageJson.jsrOnlyFields)
|
||||
}
|
||||
delete packageJson.jsrOnlyFields
|
||||
}
|
||||
|
||||
function replaceWorkspaceDependencies(field) {
|
||||
if (!packageJson[field]) return
|
||||
|
||||
const dependencies = packageJson[field]
|
||||
|
||||
for (const name of Object.keys(dependencies)) {
|
||||
if (name.startsWith('@fuman/')) {
|
||||
delete dependencies[name] // fuman is bundled with vite for now
|
||||
continue
|
||||
}
|
||||
|
||||
const value = dependencies[name]
|
||||
|
||||
if (value.startsWith('workspace:')) {
|
||||
if (value !== 'workspace:^' && value !== 'workspace:*') {
|
||||
throw new Error(
|
||||
`Cannot replace workspace dependency ${name} with ${value} - only workspace:^ and * are supported`,
|
||||
)
|
||||
}
|
||||
if (!name.startsWith('@mtcute/')) {
|
||||
throw new Error(`Cannot replace workspace dependency ${name} - only @mtcute/* is supported`)
|
||||
}
|
||||
|
||||
// note: pnpm replaces workspace:* with the current version, unlike this script
|
||||
const depVersion = value === 'workspace:*' ? '*' : `^${getPackageVersion(name.slice(8))}`
|
||||
dependencies[name] = depVersion
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
replaceWorkspaceDependencies('dependencies')
|
||||
replaceWorkspaceDependencies('devDependencies')
|
||||
replaceWorkspaceDependencies('peerDependencies')
|
||||
replaceWorkspaceDependencies('optionalDependencies')
|
||||
|
||||
delete packageJson.typedoc
|
||||
|
||||
if (packageJson.browser) {
|
||||
function maybeFixPath(p, repl) {
|
||||
if (!p) return p
|
||||
|
||||
if (p.startsWith('./src/')) {
|
||||
return repl + p.slice(6)
|
||||
}
|
||||
|
||||
if (p.startsWith('./')) {
|
||||
return repl + p.slice(2)
|
||||
}
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
for (const key of Object.keys(packageJson.browser)) {
|
||||
if (!key.startsWith('./src/')) continue
|
||||
|
||||
const path = key.slice(6)
|
||||
packageJson.browser[`./esm/${path}`] = maybeFixPath(packageJson.browser[key], './esm/')
|
||||
|
||||
delete packageJson.browser[key]
|
||||
}
|
||||
}
|
||||
|
||||
if (packageJson.exports) {
|
||||
let exports = packageJson.exports
|
||||
if (typeof exports === 'string') {
|
||||
exports = { '.': exports }
|
||||
}
|
||||
if (typeof exports !== 'object') {
|
||||
throw new TypeError('package.json exports must be an object')
|
||||
}
|
||||
|
||||
const newExports = {}
|
||||
for (const [key, value] of Object.entries(exports)) {
|
||||
if (typeof value !== 'string') {
|
||||
throw new TypeError(`package.json exports value must be a string: ${key}`)
|
||||
}
|
||||
if (value.endsWith('.wasm')) {
|
||||
newExports[key] = value
|
||||
continue
|
||||
}
|
||||
|
||||
let entrypointName = key.replace(/^\.(\/|$)/, '').replace(/\.js$/, '')
|
||||
if (entrypointName === '') entrypointName = 'index'
|
||||
|
||||
entrypoints[entrypointName] = value
|
||||
newExports[key] = {
|
||||
import: {
|
||||
types: `./${entrypointName}.d.ts`,
|
||||
default: `./${entrypointName}.js`,
|
||||
},
|
||||
require: {
|
||||
types: `./${entrypointName}.d.cts`,
|
||||
default: `./${entrypointName}.cjs`,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
packageJson.exports = newExports
|
||||
}
|
||||
|
||||
if (typeof packageJson.bin === 'object') {
|
||||
const newBin = {}
|
||||
for (const [key, value] of Object.entries(packageJson.bin)) {
|
||||
if (typeof value !== 'string') {
|
||||
throw new TypeError(`package.json bin value must be a string: ${key}`)
|
||||
}
|
||||
|
||||
let entrypointName = key.replace(/^\.(\/|$)/, '').replace(/\.js$/, '')
|
||||
if (entrypointName === '') entrypointName = 'index'
|
||||
|
||||
entrypoints[entrypointName] = value
|
||||
newBin[key] = `./${entrypointName}.js`
|
||||
}
|
||||
|
||||
packageJson.bin = newBin
|
||||
}
|
||||
|
||||
return {
|
||||
packageJsonOrig,
|
||||
packageJson,
|
||||
entrypoints,
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
import * as vitestExpect from '@vitest/expect'
|
||||
import * as vitestSpy from '@vitest/spy'
|
||||
import { afterAll, afterEach, beforeAll, beforeEach, vi as bunVi, it, jest } from 'bun:test'
|
||||
import * as chai from 'chai'
|
||||
|
||||
import { setupChai, stubGlobal, unstubAllGlobals, waitFor } from './polyfills'
|
||||
|
||||
setupChai(chai, vitestExpect)
|
||||
|
||||
export { afterAll, afterEach, beforeAll, beforeEach, it }
|
||||
export const expect = chai.expect
|
||||
|
||||
export const vi = {
|
||||
...jest,
|
||||
...bunVi,
|
||||
...vitestSpy,
|
||||
mocked: fn => fn,
|
||||
stubGlobal,
|
||||
unstubAllGlobals,
|
||||
waitFor,
|
||||
...['setSystemTime', 'advanceTimersByTimeAsync', 'advanceTimersByTime', 'doMock'].reduce(
|
||||
(acc, name) => ({
|
||||
...acc,
|
||||
[name]: () => {
|
||||
throw new Error(name)
|
||||
},
|
||||
}),
|
||||
{},
|
||||
),
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
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 vitestExpect from 'npm:@vitest/expect@1.4.0'
|
||||
// @ts-expect-error no typings
|
||||
import * as vitestSpy from 'npm:@vitest/spy@1.4.0'
|
||||
// @ts-expect-error no typings
|
||||
import * as chai from 'npm:chai'
|
||||
|
||||
import { setupChai, stubGlobal, unstubAllGlobals, waitFor } from './polyfills'
|
||||
|
||||
export { afterAll, afterEach, beforeAll, beforeEach, it }
|
||||
|
||||
setupChai(chai, vitestExpect)
|
||||
|
||||
// https://github.com/denoland/deno_std/issues/2213
|
||||
Object.defineProperty(it, 'each', {
|
||||
// eslint-disable-next-line ts/no-unsafe-function-type
|
||||
value: (items: any[][]) => (name: string, fn: Function) => {
|
||||
return items.map((item) => {
|
||||
return it(`${util.format(name, ...item)}`, () => fn(...item))
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
// https://github.com/denoland/deno_std/issues/4634
|
||||
export function describe(...args) {
|
||||
const fn = args.find(arg => typeof arg === 'function')
|
||||
if (fn.toString().startsWith('async')) {
|
||||
return
|
||||
}
|
||||
|
||||
return _describe(...args)
|
||||
}
|
||||
describe.skip = _describe.skip
|
||||
describe.only = _describe.only
|
||||
describe.ignore = _describe.ignore
|
||||
|
||||
export const expect = chai.expect
|
||||
|
||||
export const vi = {
|
||||
...vitestSpy,
|
||||
mocked: (fn: any) => fn,
|
||||
stubGlobal,
|
||||
unstubAllGlobals,
|
||||
waitFor,
|
||||
// todo use @sinonjs/fake-timers (see https://github.com/vitest-dev/vitest/blob/main/packages/vitest/src/integrations/mock/timers.ts)
|
||||
...['setSystemTime', 'advanceTimersByTimeAsync', 'advanceTimersByTime', 'doMock'].reduce(
|
||||
(acc, name) => ({
|
||||
...acc,
|
||||
[name]: () => {
|
||||
throw new Error(name)
|
||||
},
|
||||
}),
|
||||
{},
|
||||
),
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
export function setupChai(chai: any, vitestExpect: any) {
|
||||
chai.use(vitestExpect.JestExtend)
|
||||
chai.use(vitestExpect.JestChaiExpect)
|
||||
chai.use(vitestExpect.JestAsymmetricMatchers)
|
||||
chai.use((chai: any, utils: any) => {
|
||||
utils.addMethod(
|
||||
chai.Assertion.prototype,
|
||||
'toMatchInlineSnapshot',
|
||||
function (properties?: object, inlineSnapshot?: string) {
|
||||
// based on https://github.com/vitest-dev/vitest/blob/main/packages/vitest/src/integrations/snapshot/chai.ts
|
||||
|
||||
const received = utils.flag(this, 'object')
|
||||
if (typeof properties === 'string') {
|
||||
inlineSnapshot = properties
|
||||
properties = undefined
|
||||
}
|
||||
|
||||
if (typeof inlineSnapshot !== 'string') {
|
||||
throw new TypeError('toMatchInlineSnapshot requires a string argument')
|
||||
}
|
||||
|
||||
// todo use @vitest/snapshot
|
||||
if (typeof received === 'string') {
|
||||
const snapshot = `"${received}"`
|
||||
return chai.expect(snapshot).eql(inlineSnapshot.trim())
|
||||
} else {
|
||||
// eslint-disable-next-line no-eval
|
||||
const obj = eval(`(${inlineSnapshot})`) // idc lol
|
||||
return chai.expect(received).eql(obj)
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
utils.addMethod(chai.Assertion.prototype, 'toMatchSnapshot', () => {
|
||||
// todo use @vitest/snapshot
|
||||
})
|
||||
})
|
||||
|
||||
vitestExpect.setState(
|
||||
{
|
||||
assertionCalls: 0,
|
||||
isExpectingAssertions: false,
|
||||
isExpectingAssertionsError: null,
|
||||
expectedAssertionsNumber: null,
|
||||
expectedAssertionsNumberErrorGen: null,
|
||||
environment: 'deno',
|
||||
testPath: 'deno-test.ts',
|
||||
currentTestName: 'deno-test',
|
||||
},
|
||||
chai.expect,
|
||||
)
|
||||
Object.defineProperty(globalThis, vitestExpect.GLOBAL_EXPECT, {
|
||||
value: chai.expect,
|
||||
writable: true,
|
||||
configurable: true,
|
||||
})
|
||||
|
||||
chai.expect.addEqualityTesters = customTesters => vitestExpect.addCustomEqualityTesters(customTesters)
|
||||
}
|
||||
|
||||
const stubbedGlobal = new Map()
|
||||
export function stubGlobal(name: string, value: any) {
|
||||
stubbedGlobal.set(name, globalThis[name])
|
||||
globalThis[name] = value
|
||||
}
|
||||
|
||||
export function unstubAllGlobals() {
|
||||
for (const [name, value] of stubbedGlobal) {
|
||||
globalThis[name] = value
|
||||
}
|
||||
stubbedGlobal.clear()
|
||||
}
|
||||
|
||||
// eslint-disable-next-line ts/no-unsafe-function-type
|
||||
export async function waitFor(fn: Function) {
|
||||
// less customizations than vi.waitFor but it's good enough for now
|
||||
const timeout = Date.now() + 5000
|
||||
|
||||
let lastError: unknown
|
||||
while (Date.now() < timeout) {
|
||||
try {
|
||||
return await fn()
|
||||
} catch (e) {
|
||||
lastError = e
|
||||
await new Promise(resolve => setTimeout(resolve, 10))
|
||||
}
|
||||
}
|
||||
|
||||
throw lastError
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
import type { Plugin } from 'vite'
|
||||
|
||||
import { fileURLToPath } from 'node:url'
|
||||
|
||||
const setupFile = fileURLToPath(new URL('./test-setup.ts', 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',
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
/// <reference types="vitest" />
|
||||
import { mergeConfig } from 'vite'
|
||||
import { nodePolyfills } from 'vite-plugin-node-polyfills'
|
||||
/// <reference types="@vitest/browser/providers/playwright" />
|
||||
|
||||
import { nodePolyfills } from 'vite-plugin-node-polyfills'
|
||||
import { mergeConfig } from 'vitest/config'
|
||||
import { fixupCjs } from './vite-utils/fixup-cjs'
|
||||
import baseConfig from './vite.js'
|
||||
|
||||
@@ -9,9 +9,14 @@ export default mergeConfig(baseConfig, {
|
||||
test: {
|
||||
browser: {
|
||||
enabled: true,
|
||||
name: 'chromium',
|
||||
provider: 'playwright',
|
||||
slowHijackESM: false,
|
||||
headless: Boolean(process.env.CI),
|
||||
instances: [
|
||||
{ browser: 'chromium' },
|
||||
{ browser: 'firefox' },
|
||||
{ browser: 'webkit' },
|
||||
],
|
||||
},
|
||||
fakeTimers: {
|
||||
toFake: ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval', 'Date'],
|
||||
@@ -30,8 +35,9 @@ export default mergeConfig(baseConfig, {
|
||||
include: ['stream', 'path', 'zlib', 'util', 'events'],
|
||||
globals: {
|
||||
Buffer: false,
|
||||
global: false,
|
||||
process: false,
|
||||
// for WHATEVER REASON browserify-zlib uses `global` and `process` and it dies in browser lol
|
||||
global: true,
|
||||
process: true,
|
||||
},
|
||||
}),
|
||||
],
|
||||
@@ -45,10 +51,7 @@ export default mergeConfig(baseConfig, {
|
||||
},
|
||||
optimizeDeps: {
|
||||
esbuildOptions: {
|
||||
// for WHATEVER REASON browserify-zlib uses `global` and it dies in browser lol
|
||||
define: {
|
||||
global: 'globalThis',
|
||||
},
|
||||
target: 'esnext',
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
@@ -1,101 +0,0 @@
|
||||
import { resolve } from 'node:path'
|
||||
|
||||
import { defineConfig } from 'vite'
|
||||
|
||||
import { collectTestEntrypoints } from './vite-utils/collect-test-entrypoints'
|
||||
import { fixupCjs } from './vite-utils/fixup-cjs'
|
||||
import { testSetup } from './vite-utils/test-setup-plugin'
|
||||
|
||||
const POLYFILLS = resolve(__dirname, 'vite-utils/polyfills-bun.ts')
|
||||
|
||||
export default defineConfig({
|
||||
build: {
|
||||
lib: {
|
||||
entry: process.env.ENTRYPOINT
|
||||
? [process.env.ENTRYPOINT]
|
||||
: collectTestEntrypoints({
|
||||
// https://github.com/oven-sh/bun/issues/4145 prevents us from using vitest directly
|
||||
// so we have to use bun's native test runner
|
||||
skipPackages: ['create-bot'],
|
||||
// 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
|
||||
'core/src/network/config-manager.test.ts',
|
||||
'core/src/network/persistent-connection.test.ts',
|
||||
// use fixtures
|
||||
'convert/src/tdesktop/tdata.test.ts',
|
||||
'convert/src/gramjs/store-session.test.ts',
|
||||
],
|
||||
}),
|
||||
formats: ['es'],
|
||||
},
|
||||
rollupOptions: {
|
||||
external: [
|
||||
/^(node|bun):.*/,
|
||||
'vitest',
|
||||
],
|
||||
output: {
|
||||
chunkFileNames: 'chunk-[hash].js',
|
||||
entryFileNames: '[name]-[hash].test.js',
|
||||
minifyInternalExports: false,
|
||||
},
|
||||
treeshake: false,
|
||||
},
|
||||
commonjsOptions: {
|
||||
ignoreDynamicRequires: true,
|
||||
},
|
||||
outDir: process.env.OUT_DIR || 'dist/tests',
|
||||
emptyOutDir: true,
|
||||
target: 'esnext',
|
||||
minify: false,
|
||||
},
|
||||
plugins: [
|
||||
fixupCjs(),
|
||||
{
|
||||
name: 'polyfills',
|
||||
transform(code) {
|
||||
if (!code.includes('vitest')) return code
|
||||
code = code.replace(/^import \{([^}]+)\} from ['"]vitest['"];?$/gm, (_, 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 '${POLYFILLS}'`
|
||||
}
|
||||
return code
|
||||
})
|
||||
return code
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'fix-wasm-load',
|
||||
async transform(code) {
|
||||
if (code.includes('@mtcute/wasm/mtcute.wasm')) {
|
||||
return code.replace('@mtcute/wasm/mtcute.wasm', resolve(__dirname, '../packages/wasm/src/mtcute.wasm'))
|
||||
}
|
||||
if (code.includes('./mtcute.wasm')) {
|
||||
return code.replace(/\.?\.\/mtcute\.wasm/, resolve(__dirname, '../packages/wasm/src/mtcute.wasm'))
|
||||
}
|
||||
|
||||
return code
|
||||
},
|
||||
},
|
||||
testSetup(),
|
||||
],
|
||||
define: {
|
||||
'import.meta.env.TEST_ENV': '"bun"',
|
||||
},
|
||||
})
|
||||
@@ -1,81 +0,0 @@
|
||||
import { resolve } from 'node:path'
|
||||
|
||||
import { defineConfig } from 'vite'
|
||||
|
||||
import { collectTestEntrypoints } from './vite-utils/collect-test-entrypoints'
|
||||
import { fixupCjs } from './vite-utils/fixup-cjs'
|
||||
import { testSetup } from './vite-utils/test-setup-plugin'
|
||||
|
||||
const POLYFILLS = resolve(__dirname, 'vite-utils/polyfills-deno.ts')
|
||||
|
||||
export default defineConfig({
|
||||
build: {
|
||||
lib: {
|
||||
entry: process.env.ENTRYPOINT
|
||||
? [process.env.ENTRYPOINT]
|
||||
: collectTestEntrypoints({
|
||||
// these packages rely on node apis and are not meant to be run under deno
|
||||
skipPackages: ['create-bot', 'bun', 'node'],
|
||||
skipTests: [
|
||||
// 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',
|
||||
'convert/src/gramjs/store-session.test.ts',
|
||||
],
|
||||
}),
|
||||
formats: ['es'],
|
||||
},
|
||||
rollupOptions: {
|
||||
external: [
|
||||
/^(jsr|npm|node|https?):/,
|
||||
],
|
||||
output: {
|
||||
chunkFileNames: 'chunk-[hash].js',
|
||||
entryFileNames: '[name]-[hash].test.js',
|
||||
minifyInternalExports: false,
|
||||
},
|
||||
treeshake: false,
|
||||
},
|
||||
commonjsOptions: {
|
||||
ignoreDynamicRequires: true,
|
||||
},
|
||||
outDir: process.env.OUT_DIR || 'dist/tests',
|
||||
emptyOutDir: true,
|
||||
target: 'esnext',
|
||||
minify: false,
|
||||
},
|
||||
plugins: [
|
||||
fixupCjs(),
|
||||
{
|
||||
name: 'polyfills',
|
||||
transform(code) {
|
||||
if (!code.includes('vitest')) return code
|
||||
code = code.replace(/^import \{([^}]+)\} from ['"]vitest['"];?$/gm, (_, names) => {
|
||||
const namesParsed = names.split(',').map(name => name.trim())
|
||||
|
||||
return `import {${namesParsed.join(', ')}} from '${POLYFILLS}'`
|
||||
})
|
||||
return code
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'fix-wasm-load',
|
||||
async transform(code) {
|
||||
if (code.includes('./mtcute.wasm')) {
|
||||
return code.replace(/\.?\.\/mtcute\.wasm/, resolve(__dirname, '../packages/wasm/src/mtcute.wasm'))
|
||||
}
|
||||
|
||||
return code
|
||||
},
|
||||
},
|
||||
testSetup(),
|
||||
],
|
||||
define: {
|
||||
'import.meta.env.TEST_ENV': '"deno"',
|
||||
},
|
||||
})
|
||||
@@ -1,5 +1,29 @@
|
||||
/// <reference types="vitest" />
|
||||
import { defineConfig } from 'vite'
|
||||
import type { ViteUserConfig } from 'vitest/config'
|
||||
import { defineConfig } from 'vitest/config'
|
||||
|
||||
// https://github.com/oven-sh/bun/issues/4145#issuecomment-2551246135
|
||||
let runtime = 'node'
|
||||
if ('bun' in process.versions) runtime = 'bun'
|
||||
if ('deno' in process.versions) runtime = 'deno'
|
||||
|
||||
const poolOptions: ViteUserConfig['test'] = runtime === 'node'
|
||||
? {
|
||||
pool: 'threads',
|
||||
poolOptions: {
|
||||
threads: {
|
||||
singleThread: true,
|
||||
minThreads: 2,
|
||||
maxThreads: 10,
|
||||
},
|
||||
},
|
||||
}
|
||||
: {
|
||||
pool: 'vitest-in-process-pool',
|
||||
coverage: {
|
||||
enabled: false,
|
||||
},
|
||||
reporters: [['default', { summary: false }]],
|
||||
}
|
||||
|
||||
export default defineConfig({
|
||||
test: {
|
||||
@@ -22,8 +46,12 @@ export default defineConfig({
|
||||
setupFiles: [
|
||||
'./.config/vite-utils/test-setup.ts',
|
||||
],
|
||||
...poolOptions,
|
||||
},
|
||||
esbuild: {
|
||||
target: 'esnext',
|
||||
},
|
||||
define: {
|
||||
'import.meta.env.TEST_ENV': '"node"',
|
||||
'import.meta.env.TEST_ENV': JSON.stringify(runtime),
|
||||
},
|
||||
})
|
||||
|
||||
@@ -43,13 +43,11 @@ jobs:
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: ./.forgejo/actions/init
|
||||
- uses: oven-sh/setup-bun@v1
|
||||
- uses: oven-sh/setup-bun@v2
|
||||
with:
|
||||
bun-version: '1.1.4'
|
||||
- name: 'Build tests'
|
||||
run: pnpm exec vite build -c .config/vite.bun.ts
|
||||
bun-version: '1.2.4'
|
||||
- name: 'Run tests'
|
||||
run: cd dist/tests && bun test
|
||||
run: pnpm run test:bun
|
||||
|
||||
test-deno:
|
||||
runs-on: node22
|
||||
@@ -58,19 +56,14 @@ jobs:
|
||||
- uses: ./.forgejo/actions/init
|
||||
- uses: https://github.com/denoland/setup-deno@v1
|
||||
with:
|
||||
deno-version: '2.0'
|
||||
- name: 'Build tests'
|
||||
run: pnpm exec vite build -c .config/vite.deno.ts
|
||||
deno-version: '2.2.2'
|
||||
- name: 'Run tests'
|
||||
run: cd dist/tests && deno test -A --unstable-ffi --node-modules-dir=false
|
||||
run: pnpm run test:deno
|
||||
|
||||
test-web:
|
||||
runs-on: docker
|
||||
container: mcr.microsoft.com/playwright:v1.42.1
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
browser: [chromium, firefox]
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- run: apt-get update && apt-get install -y make gcc g++
|
||||
@@ -82,7 +75,7 @@ jobs:
|
||||
with:
|
||||
max_attempts: 3
|
||||
timeout_minutes: 30
|
||||
command: pnpm run test:browser --browser.name=${{ matrix.browser }}
|
||||
command: pnpm run test:browser
|
||||
|
||||
e2e:
|
||||
runs-on: node22
|
||||
|
||||
@@ -1,28 +1,6 @@
|
||||
{
|
||||
"imports": {
|
||||
"@fuman/utils": "jsr:@fuman/utils@0.0.1",
|
||||
"chai": "https://esm.sh/chai@5.1.2?pin=v135",
|
||||
"node:test": "./tests/deno-shims/node-test.js",
|
||||
"mtcute": "../packages/deno/src/index.ts",
|
||||
"mtcute/utils.js": "../packages/deno/src/utils.ts",
|
||||
"@mtcute/core": "../packages/core/src/index.ts",
|
||||
"@mtcute/core/client.js": "../packages/core/src/highlevel/client.ts",
|
||||
"@mtcute/core/methods.js": "../packages/core/src/highlevel/methods.ts",
|
||||
"@mtcute/core/utils.js": "../packages/core/src/utils/index.ts",
|
||||
"@mtcute/core/worker.js": "../packages/core/src/highlevel/worker/index.ts",
|
||||
"@mtcute/markdown-parser": "../packages/markdown-parser/src/index.ts",
|
||||
"@mtcute/html-parser": "../packages/html-parser/src/index.ts",
|
||||
"@mtcute/file-id": "../packages/file-id/src/index.ts",
|
||||
"@mtcute/tl-runtime": "../packages/tl-runtime/src/index.ts",
|
||||
"@mtcute/wasm": "../packages/wasm/src/index.ts",
|
||||
"@mtcute/tl": "./tests/deno-shims/tl.js",
|
||||
"@mtcute/tl/binary/rsa-keys.js": "./tests/deno-shims/tl-rsa.js",
|
||||
"@mtcute/tl/binary/reader.js": "./tests/deno-shims/tl-reader.js",
|
||||
"@mtcute/tl/compat/reader.js": "./tests/deno-shims/tl-reader-compat.js",
|
||||
"@mtcute/tl/binary/writer.js": "./tests/deno-shims/tl-writer.js"
|
||||
},
|
||||
"tasks": {
|
||||
"test": "deno test -A --unstable-ffi --trace-leaks --unstable-sloppy-imports --no-check --env-file=.env --unstable-detect-cjs",
|
||||
"test": "deno test -A --unstable-ffi --trace-leaks --unstable-sloppy-imports --no-check --env-file=.env --unstable-detect-cjs --import-map=../.config/deno-import-map.json",
|
||||
"test:all": "deno task test tests/*.e2e.ts"
|
||||
}
|
||||
}
|
||||
3
e2e/deno.lock
generated
3
e2e/deno.lock
generated
@@ -156,9 +156,6 @@
|
||||
"https://esm.sh/v135/chai@5.1.2/denonext/chai.mjs": "05cc6071c804cf39d4325a2c93807727623019abedca6ed1cb6534137bd4f65e"
|
||||
},
|
||||
"workspace": {
|
||||
"dependencies": [
|
||||
"jsr:@fuman/utils@0.0.1"
|
||||
],
|
||||
"packageJson": {
|
||||
"dependencies": [
|
||||
"npm:@fuman/utils@0.0.1",
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
import { createRequire } from 'node:module'
|
||||
|
||||
const require = createRequire(import.meta.url)
|
||||
|
||||
export const { __tlReaderMapCompat } = require('../../../packages/tl/compat/reader.js')
|
||||
19
package.json
19
package.json
@@ -34,6 +34,8 @@
|
||||
"test:ci": "vitest --config .config/vite.ts run --coverage.enabled --coverage.reporter=json",
|
||||
"test:browser": "vitest --config .config/vite.browser.ts run",
|
||||
"test:browser:dev": "vitest --config .config/vite.browser.ts watch",
|
||||
"test:bun": "bun --bun run test",
|
||||
"test:deno": "deno run -A --unstable-ffi --unstable-sloppy-imports --no-check --unstable-detect-cjs --import-map=.config/deno-import-map.json npm:vitest@3.0.7 --config .config/vite.ts",
|
||||
"lint": "eslint",
|
||||
"lint:ci": "CI=1 NODE_OPTIONS=\\\"--max_old_space_size=8192\\\" eslint",
|
||||
"lint:tsc": "pnpm -r --workspace-concurrency=4 exec tsc",
|
||||
@@ -50,11 +52,11 @@
|
||||
"@types/deno": "npm:@teidesu/deno-types@1.46.3",
|
||||
"@types/node": "22.13.1",
|
||||
"@types/ws": "8.5.4",
|
||||
"@vitest/browser": "2.0.5",
|
||||
"@vitest/coverage-v8": "2.0.5",
|
||||
"@vitest/expect": "2.0.5",
|
||||
"@vitest/spy": "2.0.5",
|
||||
"@vitest/ui": "2.0.5",
|
||||
"@vitest/browser": "3.0.7",
|
||||
"@vitest/coverage-v8": "3.0.7",
|
||||
"@vitest/expect": "3.0.7",
|
||||
"@vitest/spy": "3.0.7",
|
||||
"@vitest/ui": "3.0.7",
|
||||
"chai": "5.1.0",
|
||||
"cjs-module-lexer": "1.2.3",
|
||||
"dotenv-flow": "4.1.0",
|
||||
@@ -70,10 +72,11 @@
|
||||
"tsx": "4.17.0",
|
||||
"typedoc": "0.27.6",
|
||||
"typescript": "5.5.4",
|
||||
"vite": "5.4.2",
|
||||
"vite": "6.2.0",
|
||||
"vite-plugin-dts": "4.0.3",
|
||||
"vite-plugin-node-polyfills": "0.22.0",
|
||||
"vitest": "2.0.5"
|
||||
"vite-plugin-node-polyfills": "0.23.0",
|
||||
"vitest": "3.0.7",
|
||||
"vitest-in-process-pool": "^2.0.0"
|
||||
},
|
||||
"pnpm": {
|
||||
"overrides": {
|
||||
|
||||
@@ -5,6 +5,8 @@ import { millerRabin } from './miller-rabin.js'
|
||||
|
||||
describe(
|
||||
'miller-rabin test',
|
||||
// since miller-rabin factorization relies on RNG, it may take a while (or may not!)
|
||||
{ timeout: 10000 },
|
||||
() => {
|
||||
// miller-rabin factorization relies on RNG, so we should use a real random number generator
|
||||
const c = defaultCryptoProvider
|
||||
@@ -139,5 +141,4 @@ describe(
|
||||
testMillerRabin(BigInt(`0x${telegramDhPrime.replace(/ /g, '')}`), true)
|
||||
})
|
||||
},
|
||||
{ timeout: 10000 },
|
||||
) // since miller-rabin factorization relies on RNG, it may take a while (or may not!)
|
||||
)
|
||||
|
||||
@@ -188,6 +188,8 @@ export function testCryptoProvider(c: ICryptoProvider): void {
|
||||
|
||||
it(
|
||||
'should decompose PQ to prime factors P and Q',
|
||||
// since PQ factorization relies on RNG, it may take a while (or may not!)
|
||||
{ timeout: 10000 },
|
||||
async () => {
|
||||
const testFactorization = async (pq: string, p_: string, q: string) => {
|
||||
const [p1, q1] = await c.factorizePQ(hex.decode(pq))
|
||||
@@ -200,8 +202,6 @@ export function testCryptoProvider(c: ICryptoProvider): void {
|
||||
// random example
|
||||
await testFactorization('14fcab4dfc861f45', '494c5c99', '494c778d')
|
||||
},
|
||||
// since PQ factorization relies on RNG, it may take a while (or may not!)
|
||||
{ timeout: 10000 },
|
||||
)
|
||||
|
||||
it('should correctly gzip', () => {
|
||||
|
||||
1028
pnpm-lock.yaml
generated
1028
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user