Add Playwright configuration and update codebase

The commit introduces Playwright configuration for End-to-End testing and modifies several files to optimize the project's structure. It also modifies the middleware to interact with Next.js and fix URL creation. Changes in database types were made to refine their structure.
This commit is contained in:
giancarlo
2024-04-11 15:59:08 +08:00
parent 2c7478abff
commit bf716b5dd6
13 changed files with 2249 additions and 1873 deletions

View File

@@ -0,0 +1,68 @@
import { Page } from '@playwright/test';
import { parse } from 'node-html-parser';
export class Mailbox {
constructor(
private readonly page: Page
) {
}
async visitMailbox(email: string) {
const mailbox = email.split('@')[0];
console.log(`Visiting mailbox ${mailbox} ...`)
if (!mailbox) {
throw new Error('Invalid email');
}
const json = await this.getInviteEmail(mailbox);
const html = (json.body as { html: string }).html;
const el = parse(html);
const linkHref = el.querySelector('a')?.getAttribute('href');
if (!linkHref) {
throw new Error('No link found in email');
}
console.log(`Visiting ${linkHref} ...`);
return this.page.goto(linkHref);
}
async getInviteEmail(
mailbox: string,
params = {
deleteAfter: true
}
) {
const url = `http://localhost:54324/api/v1/mailbox/${mailbox}`;
const response = await fetch(url);
const json = (await response.json()) as Array<{ id: string }>;
if (!json || !json.length) {
return;
}
const messageId = json[0]?.id;
const messageUrl = `${url}/${messageId}`;
const messageResponse = await fetch(messageUrl);
if (!messageResponse.ok) {
throw new Error(`Failed to fetch email: ${messageResponse.statusText}`);
}
// delete message
if (params.deleteAfter) {
await fetch(messageUrl, {
method: 'DELETE'
});
}
return await messageResponse.json();
}
}