mtcute/e2e/node/ts/tests/01.auth.ts

87 lines
2.4 KiB
TypeScript
Raw Normal View History

import { expect } from 'chai'
import { describe, it } from 'mocha'
2024-04-23 23:03:00 +03:00
import { MtcuteError } from '@mtcute/core'
2024-03-04 01:39:18 +03:00
import { BaseTelegramClient, TelegramClient } from '@mtcute/core/client.js'
import { getApiParams } from '../utils.js'
const getAccountId = () =>
Math.floor(Math.random() * 10000)
.toString()
.padStart(4, '0')
describe('1. authorization', function () {
this.timeout(300_000)
it('should authorize in default dc', async () => {
2024-02-03 13:26:21 +03:00
const base = new BaseTelegramClient(getApiParams('dc2.session'))
const tg = new TelegramClient({ client: base })
// reset storage just in case
2024-02-03 13:26:21 +03:00
await base.mt.storage.load()
await base.storage.clear(true)
while (true) {
const phone = `999662${getAccountId()}`
let user
try {
user = await tg.start({
phone,
code: () => '22222',
})
} catch (e) {
2024-04-23 23:03:00 +03:00
if (e instanceof MtcuteError && e.message.match(/Signup is no longer supported|2FA is enabled/)) {
// retry with another number
continue
2024-04-23 23:03:00 +03:00
} else {
await tg.close()
throw e
}
}
await tg.close()
expect(user.isSelf).to.be.true
expect(user.phoneNumber).to.equal(phone)
break
}
})
it('should authorize in dc 1', async () => {
2024-02-03 13:26:21 +03:00
const base = new BaseTelegramClient(getApiParams('dc1.session'))
const tg = new TelegramClient({ client: base })
// reset storage just in case
2024-02-03 13:26:21 +03:00
await base.mt.storage.load()
await base.mt.storage.clear(true)
while (true) {
const phone = `999661${getAccountId()}`
let user
try {
user = await tg.start({
phone,
code: () => '11111',
})
} catch (e) {
2024-04-23 23:03:00 +03:00
if (e instanceof MtcuteError && e.message.match(/Signup is no longer supported|2FA is enabled/)) {
// retry with another number
continue
2024-04-23 23:03:00 +03:00
} else {
await tg.close()
throw e
}
}
await tg.close()
expect(user.isSelf).to.be.true
expect(user.phoneNumber).to.equal(phone)
break
}
})
})