2023-12-23 22:00:20 +03:00
|
|
|
import { expect } from 'chai'
|
|
|
|
import { describe, it } from 'mocha'
|
|
|
|
|
2024-05-03 05:43:51 +03:00
|
|
|
import { MtcuteError, tl } from '@mtcute/core'
|
2024-03-04 01:39:18 +03:00
|
|
|
import { BaseTelegramClient, TelegramClient } from '@mtcute/core/client.js'
|
2023-12-23 22:00:20 +03:00
|
|
|
|
|
|
|
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 })
|
2023-12-23 22:00:20 +03:00
|
|
|
|
|
|
|
// reset storage just in case
|
2024-02-03 13:26:21 +03:00
|
|
|
await base.mt.storage.load()
|
|
|
|
await base.storage.clear(true)
|
2023-12-23 22:00:20 +03:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const phone = `999662${getAccountId()}`
|
|
|
|
let user
|
|
|
|
|
|
|
|
try {
|
|
|
|
user = await tg.start({
|
|
|
|
phone,
|
|
|
|
code: () => '22222',
|
|
|
|
})
|
|
|
|
} catch (e) {
|
2024-05-03 05:43:51 +03:00
|
|
|
if (
|
|
|
|
(e instanceof MtcuteError && e.message.match(/Signup is no longer supported|2FA is enabled/)) ||
|
|
|
|
tl.RpcError.is(e, 'SESSION_PASSWORD_NEEDED')
|
|
|
|
) {
|
2023-12-23 22:00:20 +03:00
|
|
|
// retry with another number
|
2024-05-09 02:13:55 +03:00
|
|
|
await tg.logOut().catch((err) => {
|
|
|
|
console.error('Failed to log out:', err)
|
|
|
|
})
|
2023-12-23 22:00:20 +03:00
|
|
|
continue
|
2024-04-23 23:03:00 +03:00
|
|
|
} else {
|
|
|
|
await tg.close()
|
|
|
|
throw e
|
|
|
|
}
|
2023-12-23 22:00:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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 })
|
2023-12-23 22:00:20 +03:00
|
|
|
|
|
|
|
// reset storage just in case
|
2024-02-03 13:26:21 +03:00
|
|
|
await base.mt.storage.load()
|
|
|
|
await base.mt.storage.clear(true)
|
2023-12-23 22:00:20 +03:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const phone = `999661${getAccountId()}`
|
|
|
|
let user
|
|
|
|
|
|
|
|
try {
|
|
|
|
user = await tg.start({
|
|
|
|
phone,
|
|
|
|
code: () => '11111',
|
|
|
|
})
|
|
|
|
} catch (e) {
|
2024-05-03 05:43:51 +03:00
|
|
|
if (
|
|
|
|
(e instanceof MtcuteError && e.message.match(/Signup is no longer supported|2FA is enabled/)) ||
|
|
|
|
tl.RpcError.is(e, 'SESSION_PASSWORD_NEEDED')
|
|
|
|
) {
|
2023-12-23 22:00:20 +03:00
|
|
|
// retry with another number
|
|
|
|
continue
|
2024-04-23 23:03:00 +03:00
|
|
|
} else {
|
|
|
|
await tg.close()
|
|
|
|
throw e
|
|
|
|
}
|
2023-12-23 22:00:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
await tg.close()
|
|
|
|
|
|
|
|
expect(user.isSelf).to.be.true
|
|
|
|
expect(user.phoneNumber).to.equal(phone)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|