All files / lib/email/providers mock.provider.ts

85.71% Statements 6/7
100% Branches 0/0
66.66% Functions 2/3
85.71% Lines 6/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39      28x                                           28x 28x   28x         28x   28x      
import type { EmailProvider } from "./base";
 
function htmlToText(html: string): string {
	return html
		.replace(/<br\s*\/?>/gi, "\n")
		.replace(/<\/p>/gi, "\n\n")
		.replace(/<\/div>/gi, "\n")
		.replace(/<\/tr>/gi, "\n")
		.replace(/<a[^>]+href="([^"]*)"[^>]*>(.*?)<\/a>/gi, "$2 ($1)")
		.replace(/<[^>]+>/g, "")
		.replace(/&nbsp;/g, " ")
		.replace(/&amp;/g, "&")
		.replace(/&lt;/g, "<")
		.replace(/&gt;/g, ">")
		.replace(/&#(\d+);/g, (_m, code) => String.fromCharCode(Number(code)))
		.replace(/\n{3,}/g, "\n\n")
		.trim();
}
 
export class MockProvider implements EmailProvider {
	async sendEmail(params: {
		to: string;
		subject: string;
		html: string;
	}): Promise<{ id: string }> {
		const { to, subject, html } = params;
		const textBody = htmlToText(html);
 
		console.log("[EMAIL-MOCK] Email would be sent:", {
			to,
			subject,
			htmlLength: html.length,
		});
		console.log(`[EMAIL-MOCK] Text body:\n${textBody}`);
 
		return { id: "mock-email-id" };
	}
}