test(e2e): added worker tests
This commit is contained in:
parent
cbc2002781
commit
14549020aa
10 changed files with 117 additions and 6 deletions
|
@ -1,4 +1,4 @@
|
|||
const { BaseTelegramClient } = require('@mtcute/core')
|
||||
const { BaseTelegramClient } = require('@mtcute/core/client.js')
|
||||
const { describe, it } = require('mocha')
|
||||
const { expect } = require('chai')
|
||||
|
||||
|
|
|
@ -11,6 +11,8 @@ module.exports = {
|
|||
getFiles: () => 'tests/**/*.ts',
|
||||
beforeAll: () => ['tsc', 'node build-esm.cjs'],
|
||||
runFile: (file) => {
|
||||
if (require('path').basename(file)[0] === '_') return null
|
||||
|
||||
if (file.startsWith('tests/packaging/')) {
|
||||
// packaging tests - we need to make sure everything imports and works
|
||||
return [
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { expect } from 'chai'
|
||||
import { describe, it } from 'mocha'
|
||||
|
||||
import { BaseTelegramClient } from '@mtcute/core'
|
||||
import { BaseTelegramClient } from '@mtcute/core/client.js'
|
||||
|
||||
import { getApiParams } from '../utils.js'
|
||||
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
"@mtcute/tl-runtime": "*",
|
||||
"@mtcute/tl-utils": "*",
|
||||
"@mtcute/wasm": "*",
|
||||
"@mtcute/web": "*",
|
||||
"@types/chai": "^4.3.8",
|
||||
"@types/mocha": "^10.0.2",
|
||||
"chai": "^4.3.10",
|
||||
|
|
|
@ -21,6 +21,10 @@ function runForFile(dir, file, single = true) {
|
|||
|
||||
let cmds = runFile(file)
|
||||
|
||||
if (!cmds) {
|
||||
return
|
||||
}
|
||||
|
||||
const options = {
|
||||
env: {
|
||||
...env,
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import { expect } from 'chai'
|
||||
import { describe, it } from 'mocha'
|
||||
|
||||
import { BaseTelegramClient, MtUnsupportedError } from '@mtcute/core'
|
||||
import { TelegramClient } from '@mtcute/core/client.js'
|
||||
import { MtUnsupportedError } from '@mtcute/core'
|
||||
import { BaseTelegramClient, TelegramClient } from '@mtcute/core/client.js'
|
||||
|
||||
import { getApiParams } from '../utils.js'
|
||||
|
||||
|
|
85
e2e/ts/tests/05.worker.ts
Normal file
85
e2e/ts/tests/05.worker.ts
Normal 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
18
e2e/ts/tests/_worker.ts
Normal 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,
|
||||
})
|
|
@ -1,7 +1,7 @@
|
|||
import { expect } from 'chai'
|
||||
import { describe, it } from 'mocha'
|
||||
|
||||
import { BaseTelegramClient } from '@mtcute/core'
|
||||
import { BaseTelegramClient } from '@mtcute/core/client.js'
|
||||
|
||||
// @fix-import
|
||||
import { getApiParams } from '../../utils'
|
||||
|
|
|
@ -46,4 +46,5 @@ export type ClientMessageHandler = (message: WorkerOutboundMessage) => void
|
|||
export type RespondFn = (message: WorkerOutboundMessage) => 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>>
|
||||
|
|
Loading…
Reference in a new issue