mtcute/e2e/tests/_utils.ts
alina sireneva 66b0ad82e9
Some checks failed
Tests / test-deno (push) Successful in 1m46s
Tests / test-node (node18) (push) Successful in 2m1s
Tests / test-node (node22) (push) Successful in 1m58s
Tests / test-node (node20) (push) Successful in 2m1s
Tests / test-bun (push) Successful in 2m3s
Tests / test-web (chromium) (push) Successful in 1m59s
Tests / test-web (firefox) (push) Successful in 1m13s
Docs / build (push) Successful in 6m20s
Tests / lint (push) Successful in 7m1s
Tests / e2e-deno (push) Successful in 49s
Tests / e2e (push) Failing after 1m4s
test(e2e): increased default waitFor timeout
2025-01-10 05:43:34 +03:00

44 lines
1.3 KiB
TypeScript

import type { BaseTelegramClientOptions, MaybePromise } from 'mtcute'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { sleep } from '@fuman/utils'
import { MemoryStorage, SqliteStorage } from 'mtcute'
import { LogManager } from 'mtcute/utils.js'
export const RUNTIME_DIR: string = fileURLToPath(new URL('../_runtime', import.meta.url))
export const IS_DENO = 'Deno' in globalThis
export const IS_BUN = 'Bun' in globalThis
export const IS_NODE = !IS_DENO && !IS_BUN
export function getApiParams(storage?: string): BaseTelegramClientOptions {
if (!process.env.API_ID || !process.env.API_HASH) {
throw new Error('API_ID and API_HASH env variables must be set')
}
return {
apiId: Number.parseInt(process.env.API_ID),
apiHash: process.env.API_HASH,
testMode: true,
storage: storage ? new SqliteStorage(join(RUNTIME_DIR, storage)) : new MemoryStorage(),
logLevel: LogManager.VERBOSE,
}
}
export async function waitFor(condition: () => MaybePromise<void>, timeout = 15000): Promise<void> {
const start = Date.now()
let lastError
while (Date.now() - start < timeout) {
try {
await condition()
return
} catch (e) {
lastError = e
await sleep(100)
}
}
throw lastError
}