test(e2e): added worker tests

This commit is contained in:
alina 🌸 2024-03-04 01:39:18 +03:00
parent cbc2002781
commit 14549020aa
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
10 changed files with 117 additions and 6 deletions

View file

@ -1,4 +1,4 @@
const { BaseTelegramClient } = require('@mtcute/core') const { BaseTelegramClient } = require('@mtcute/core/client.js')
const { describe, it } = require('mocha') const { describe, it } = require('mocha')
const { expect } = require('chai') const { expect } = require('chai')

View file

@ -11,6 +11,8 @@ module.exports = {
getFiles: () => 'tests/**/*.ts', getFiles: () => 'tests/**/*.ts',
beforeAll: () => ['tsc', 'node build-esm.cjs'], beforeAll: () => ['tsc', 'node build-esm.cjs'],
runFile: (file) => { runFile: (file) => {
if (require('path').basename(file)[0] === '_') return null
if (file.startsWith('tests/packaging/')) { if (file.startsWith('tests/packaging/')) {
// packaging tests - we need to make sure everything imports and works // packaging tests - we need to make sure everything imports and works
return [ return [

View file

@ -1,7 +1,7 @@
import { expect } from 'chai' import { expect } from 'chai'
import { describe, it } from 'mocha' import { describe, it } from 'mocha'
import { BaseTelegramClient } from '@mtcute/core' import { BaseTelegramClient } from '@mtcute/core/client.js'
import { getApiParams } from '../utils.js' import { getApiParams } from '../utils.js'

View file

@ -18,6 +18,7 @@
"@mtcute/tl-runtime": "*", "@mtcute/tl-runtime": "*",
"@mtcute/tl-utils": "*", "@mtcute/tl-utils": "*",
"@mtcute/wasm": "*", "@mtcute/wasm": "*",
"@mtcute/web": "*",
"@types/chai": "^4.3.8", "@types/chai": "^4.3.8",
"@types/mocha": "^10.0.2", "@types/mocha": "^10.0.2",
"chai": "^4.3.10", "chai": "^4.3.10",

View file

@ -21,6 +21,10 @@ function runForFile(dir, file, single = true) {
let cmds = runFile(file) let cmds = runFile(file)
if (!cmds) {
return
}
const options = { const options = {
env: { env: {
...env, ...env,

View file

@ -1,8 +1,8 @@
import { expect } from 'chai' import { expect } from 'chai'
import { describe, it } from 'mocha' import { describe, it } from 'mocha'
import { BaseTelegramClient, MtUnsupportedError } from '@mtcute/core' import { MtUnsupportedError } from '@mtcute/core'
import { TelegramClient } from '@mtcute/core/client.js' import { BaseTelegramClient, TelegramClient } from '@mtcute/core/client.js'
import { getApiParams } from '../utils.js' import { getApiParams } from '../utils.js'

85
e2e/ts/tests/05.worker.ts Normal file
View file

@ -0,0 +1,85 @@
/* eslint-disable no-restricted-imports */
import { expect } from 'chai'
import { describe, it } from 'mocha'
import path from 'path'
import { Worker } from 'worker_threads'
import { TelegramClient } from '@mtcute/core/client.js'
import { Message, TelegramWorkerPort, tl } from '@mtcute/node'
import { getApiParams, waitFor } from '../utils.js'
import type { CustomMethods } from './_worker.js'
describe('5. worker', async function () {
this.timeout(300_000)
const worker = new Worker(path.resolve(__dirname, '_worker.js'))
const port = new TelegramWorkerPort<CustomMethods>({
worker,
})
const portClient = new TelegramClient({ client: port })
it('should make api calls', async function () {
const res = await port.call({ _: 'help.getConfig' })
expect(res._).to.equal('config')
})
it('should call custom methods', async function () {
const hello = await port.invokeCustom('hello')
expect(hello).to.equal('world')
const sum = await port.invokeCustom('sum', 2, 3)
expect(sum).to.equal(5)
})
it('should throw errors', async function () {
try {
await port.call({ _: 'test.useError' })
throw new Error('should have thrown')
} catch (e) {
expect(e).to.be.an.instanceOf(tl.RpcError)
}
})
it('should receive updates', async function () {
const client2 = new TelegramClient(getApiParams('dc2.session'))
try {
await client2.connect()
await port.startUpdatesLoop()
const me = await portClient.getMe()
let username = me.username
if (!username) {
username = `mtcute_e2e_${Math.random().toString(36).slice(2, 8)}`
await portClient.setMyUsername(username)
}
const msgs: Message[] = []
portClient.on('new_message', (msg) => {
msgs.push(msg)
})
const testText = `test ${Math.random()}`
await client2.sendText(username, testText)
await waitFor(() => {
expect(msgs.length).to.be.greaterThan(0)
expect(msgs[0].text).to.equal(testText)
})
} catch (e) {
await client2.close()
throw e
}
await client2.close()
})
this.afterAll(async () => {
await port.close()
worker.terminate()
})
})

18
e2e/ts/tests/_worker.ts Normal file
View file

@ -0,0 +1,18 @@
import { WorkerCustomMethods } from '@mtcute/core/worker.js'
import { BaseTelegramClient, TelegramWorker } from '@mtcute/node'
import { getApiParams } from '../utils.js'
const customMethods = {
hello: async () => 'world',
sum: async (a: number, b: number) => a + b,
} as const satisfies WorkerCustomMethods
export type CustomMethods = typeof customMethods
const client = new BaseTelegramClient(getApiParams('dc1.session'))
// eslint-disable-next-line no-new
new TelegramWorker({
client,
customMethods,
})

View file

@ -1,7 +1,7 @@
import { expect } from 'chai' import { expect } from 'chai'
import { describe, it } from 'mocha' import { describe, it } from 'mocha'
import { BaseTelegramClient } from '@mtcute/core' import { BaseTelegramClient } from '@mtcute/core/client.js'
// @fix-import // @fix-import
import { getApiParams } from '../../utils' import { getApiParams } from '../../utils'

View file

@ -46,4 +46,5 @@ export type ClientMessageHandler = (message: WorkerOutboundMessage) => void
export type RespondFn = (message: WorkerOutboundMessage) => void export type RespondFn = (message: WorkerOutboundMessage) => void
export type WorkerMessageHandler = (message: WorkerInboundMessage, respond: RespondFn) => void export type WorkerMessageHandler = (message: WorkerInboundMessage, respond: RespondFn) => void
export type WorkerCustomMethods = Record<string, (...args: unknown[]) => Promise<unknown>> // eslint-disable-next-line @typescript-eslint/no-explicit-any
export type WorkerCustomMethods = Record<string, (...args: any[]) => Promise<any>>