test: define Gmail mailer behavior

Co-Authored-By: Claude <noreply@anthropic.com>

Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
SJCoetz 2026-08-03 00:48:32 +02:00
parent d3d7ce01b8
commit e964993619

79
lib/email.test.ts Normal file
View file

@ -0,0 +1,79 @@
import assert from "node:assert/strict";
import test from "node:test";
import { getGmailCredentials, sendEmail, type MailTransport } from "./email";
const env = {
GMAIL_SMTP_USER: "rjbulk01@gmail.com",
GMAIL_SMTP_APP_PASSWORD: "test-app-password",
};
test("getGmailCredentials rejects missing Gmail configuration", () => {
assert.throws(
() => getGmailCredentials({}),
{ message: "Gmail email credentials are not configured" },
);
});
test("sendEmail uses the authenticated Gmail user and forwards the message", async () => {
const sent: unknown[] = [];
const transport: MailTransport = {
async sendMail(message) {
sent.push(message);
return {};
},
};
await sendEmail(
{
to: "customer@example.com",
subject: "Delivery statement",
html: "<p>Statement attached.</p>",
attachments: [
{
filename: "statement.pdf",
content: Buffer.from("pdf-bytes"),
},
],
},
{ env, transport },
);
assert.deepEqual(sent, [
{
from: "rjbulk01@gmail.com",
to: "customer@example.com",
subject: "Delivery statement",
html: "<p>Statement attached.</p>",
attachments: [
{
filename: "statement.pdf",
content: Buffer.from("pdf-bytes"),
},
],
},
]);
});
test("sendEmail hides provider error details and credentials", async () => {
const secret = "test-app-password";
const transport: MailTransport = {
async sendMail() {
throw new Error(`authentication failed for ${secret}`);
},
};
await assert.rejects(
sendEmail(
{
to: "customer@example.com",
subject: "Delivery statement",
html: "<p>Statement attached.</p>",
},
{ env, transport },
),
(error: unknown) =>
error instanceof Error &&
error.message === "Gmail email send failed" &&
!error.message.includes(secret),
);
});