diff --git a/tests/backend-fixture.ts b/tests/backend-fixture.ts index 497803a..4a96fed 100644 --- a/tests/backend-fixture.ts +++ b/tests/backend-fixture.ts @@ -7,11 +7,64 @@ import * as dgram from "dgram"; import * as http from "http"; import * as fs from "fs/promises"; import { MongoClient } from "mongodb"; -import { MONGO_PORT, MONGO_HOST } from "../src/back/config.js"; +import { + MONGO_PORT, + MONGO_HOST, + MAILCATCHER_API_PORT, + MAILCATCHER_SMTP_PORT, +} from "../src/back/config.js"; + +export class MailcatcherMessage { + id: number; + sender: string; + recipients: string[]; + subject: string; + size: string; + created_at: string; + + constructor({ + id, + sender, + recipients, + subject, + size, + created_at, + }: { + id: number; + sender: string; + recipients: string[]; + subject: string; + size: string; + created_at: string; + }) { + this.id = id; + this.sender = sender; + this.recipients = recipients; + this.subject = subject; + this.size = size; + this.created_at = created_at; + } + + async getBody(format: "html" | "plain" | "source") { + return (await fetch(`${mailcatcher_url}/messages/${this.id}.${format}`)).text(); + } + + async getURLs(): Promise { + const url_regex = + /https?:\/\/[a-z0-9.]+(:[0-9]+)\/[a-zA-Z0-9.\/!@#$%^&*()?\-=]+/g; + const body = await this.getBody("plain"); + return Array.from(body.matchAll(url_regex)).map((e) => e[0]); + } +} + +export type MailcatcherAPI = { + getLatestMessage: () => Promise; +}; type TestFixtures = { backend: { url: string; env: Record }; setMarkdownValue: (field_name: string, value: string) => Promise; + email: MailcatcherAPI; }; const c8 = "./node_modules/c8/bin/c8.js"; @@ -19,6 +72,8 @@ const app_host = process.env.TESTS_RUN_IN_DOCKER ? "test" // the docker name : "172.17.0.1"; // ip to access localhost from within docker (playwright always runs in docker) +const mailcatcher_url = `http://${app_host}:${MAILCATCHER_API_PORT}`; + export async function waitForHttpPort({ port, host = app_host, @@ -100,7 +155,10 @@ export const test = base.extend({ ...(process.env.TESTS_RUN_IN_DOCKER ? { MEILISEARCH_HOST: `http://meilisearch:7700` } : {}), + MAILCATCHER_HOST: app_host, + MAILCATCHER_SMTP_PORT: String(MAILCATCHER_SMTP_PORT), SEALIOUS_KILLSWITCH_PORT: `${killswitch_port}`, + SEALIOUS_BASE_URL: `http://${app_host}:${port}`, }; const backendProcess = spawn( @@ -167,6 +225,19 @@ export const test = base.extend({ } }); }, + + email: ({}, use) => { + async function getLatestMessage() { + const all_messages = (await ( + await fetch(`${mailcatcher_url}/messages`) + ).json()) as Record[]; + return new MailcatcherMessage(all_messages.at(-1) as any); + } + async function getURLsFromLatestMessage() {} + return use({ + getLatestMessage, + }); + }, }); export { expect } from "@playwright/test";