Skip to content

Commit

Permalink
fix: format
Browse files Browse the repository at this point in the history
  • Loading branch information
arein committed Jan 1, 2024
1 parent 4f5a23c commit 473ff27
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 21 deletions.
18 changes: 15 additions & 3 deletions apps/laboratory/tests/email.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,34 @@ testMEmail.beforeEach(async ({ modalPage, context, modalValidator }) => {
const tempEmail = `web3modal${Math.floor(
Math.random() * AVAILABLE_MAILSAC_ADDRESSES
)}@mailsac.com`
const email = new Email(process.env['MAILSAC_API_KEY']!)
const mailsacApiKey = process.env['MAILSAC_API_KEY']
if (!mailsacApiKey) {
throw new Error('MAILSAC_API_KEY is not set')
}
const email = new Email(mailsacApiKey)
await email.deleteAllMessages(tempEmail)
await modalPage.loginWithEmail(tempEmail)

let latestMessage: any = await email.getNewMessage(tempEmail)
let latestMessage = await email.getNewMessage(tempEmail)
let messageId = latestMessage._id

if (!messageId) {
throw new Error('No messageId found')
}

let otp = await email.getCodeFromEmail(tempEmail, messageId)

if (otp.length !== 6) {
// device registration
// We got a device registration link so let's register first
const drp = new DeviceRegistrationPage(await context.newPage(), otp)
drp.load()
await drp.approveDevice()

latestMessage = await email.getNewMessage(tempEmail)
messageId = latestMessage._id
if (!messageId) {
throw new Error('No messageId found')
}
otp = await email.getCodeFromEmail(tempEmail, messageId)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { testM as base, testMSiwe as siwe, testMEmail as email } from './w3m-fixture'
import { testM as base, testMSiwe as siwe } from './w3m-fixture'
import { WalletPage } from '../pages/WalletPage'
import { WalletValidator } from '../validators/WalletValidator'

Expand Down
16 changes: 11 additions & 5 deletions apps/laboratory/tests/shared/pages/ModalPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ export class ModalPage {
) {
this.connectButton = this.page.getByTestId('connect-button')
this.url =
flavor !== 'default'
? `${this.baseURL}library/${this.library}-${this.flavor}/`
: `${this.baseURL}library/${this.library}/`
flavor === 'default'
? `${this.baseURL}library/${this.library}/`
: `${this.baseURL}library/${this.library}-${this.flavor}/`
}

async load() {
Expand All @@ -44,9 +44,15 @@ export class ModalPage {

async enterOTP(otp: string) {
const splitted = otp.split('')
for (let i = 0; i < splitted.length; i++) {
for (let i = 0; i < splitted.length; i = +1) {
const digit = splitted[i]
if (!digit) {
throw new Error('Invalid OTP')
}
/* eslint-disable no-await-in-loop */
await this.page.getByTestId('wui-otp-input').locator('input').nth(i).focus()
await this.page.getByTestId('wui-otp-input').locator('input').nth(i).fill(splitted[i]!)
/* eslint-disable no-await-in-loop */
await this.page.getByTestId('wui-otp-input').locator('input').nth(i).fill(digit)
}

await expect(this.page.getByText('Confirm Email')).not.toBeVisible()
Expand Down
26 changes: 14 additions & 12 deletions apps/laboratory/tests/shared/utils/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,33 @@ import { Mailsac, type EmailMessage } from '@mailsac/api'

export class Email {
private readonly mailsac: Mailsac<any>
private messageCount: any
private messageCount: number
constructor(public readonly apiKey: string) {
this.mailsac = new Mailsac({ headers: { 'Mailsac-Key': apiKey } })
this.messageCount = undefined
this.messageCount = 0
}

async deleteAllMessages(email: string) {
this.messageCount = 0

return await this.mailsac.messages.deleteAllMessages(email)
}

async getNewMessage(email: string) {
const timeout = new Promise((_, reject) => {
const timeout: Promise<EmailMessage> = new Promise((_, reject) => {
setTimeout(() => {
reject('timeout')
return reject(new Error('Timeout waiting for email'))
}, 15000)
})

const messagePoll = new Promise(resolve => {
const messagePoll: Promise<EmailMessage> = new Promise(resolve => {
const interval = setInterval(async () => {
const messages = await this.mailsac.messages.listMessages(email)
if (messages.data.length > this.messageCount) {
clearInterval(interval)
this.messageCount = messages.data.length
const message = messages.data[0] as EmailMessage

return resolve(message)
}
}, 500)
Expand All @@ -40,21 +42,21 @@ export class Email {

if (result.data.includes('Approve this login')) {
// Get the register.web3modal.com device registration URL
const regex = /https:\/\/register.*/
const regex = /https:\/\/register.*/u
const match = result.data.match(regex)
if (match) {
return match[0]
} else {
throw new Error('No url found in email: ' + result.data)
}

throw new Error(`No url found in email: ${result.data}`)
}

const otpRegex = /\d{3}\s?\d{3}/
const otpRegex = /\d{3}\s?\d{3}/u
const match = result.data.match(otpRegex)
if (match) {
return match[0].replace(/\s/g, '')
} else {
throw new Error('No code found in email: ' + result.data)
return match[0].replace(/\s/gu, '')
}

throw new Error(`No code found in email: ${result.data}`)
}
}

0 comments on commit 473ff27

Please sign in to comment.