- Create packages/emails/ with EmailClient interface abstraction - Wrap Postmark ServerClient in adapter for clean typing - Add createLoggingEmailClient for dev mode (logs to console) - Split email templates into individual files with full test coverage - Update api-server to use new package via context injection - Remove EMAIL_DEV_MODE - now uses POSTMARK_API_KEY presence - Delete apps/api-server/src/utils/email.ts Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
import { describe, expect, it, mock } from "bun:test";
|
|
import { createPostmarkClient } from "./client.js";
|
|
|
|
const mockSendEmail = mock(() =>
|
|
Promise.resolve({ MessageID: "test-message-id-123" }),
|
|
);
|
|
|
|
void mock.module("postmark", () => ({
|
|
ServerClient: class MockServerClient {
|
|
sendEmail = mockSendEmail;
|
|
},
|
|
}));
|
|
|
|
describe("createPostmarkClient", () => {
|
|
it("should create an EmailClient with sendEmail method", () => {
|
|
const client = createPostmarkClient("test-api-key");
|
|
expect(typeof client.sendEmail).toBe("function");
|
|
});
|
|
|
|
it("should throw an error if API key is empty", () => {
|
|
expect(() => createPostmarkClient("")).toThrow(
|
|
"Postmark API key is required",
|
|
);
|
|
});
|
|
|
|
it("should convert our interface to Postmark format and return converted result", async () => {
|
|
const client = createPostmarkClient("test-api-key");
|
|
|
|
const result = await client.sendEmail({
|
|
from: "sender@example.com",
|
|
to: "recipient@example.com",
|
|
subject: "Test Subject",
|
|
htmlBody: "<p>HTML</p>",
|
|
textBody: "Text",
|
|
});
|
|
|
|
expect(mockSendEmail).toHaveBeenCalledWith({
|
|
From: "sender@example.com",
|
|
To: "recipient@example.com",
|
|
Subject: "Test Subject",
|
|
HtmlBody: "<p>HTML</p>",
|
|
TextBody: "Text",
|
|
});
|
|
expect(result.messageId).toBe("test-message-id-123");
|
|
});
|
|
});
|