import { describe, expect, it } from "vitest"; import { EMAIL_SUPPRESSION_QUERY_BATCH_SIZE, isEmailSuppressed, listSuppressedEmailAddresses, normalizeSuppressionAddress, recordEmailSuppression, type EmailSuppressionBatchClient, type EmailSuppressionClient, type EmailSuppressionRow, } from "./email-suppression.server.js"; /** Minimal in-memory stand-in for the EmailSuppressionList Prisma delegate. */ function createFakeClient(): EmailSuppressionClient & EmailSuppressionBatchClient & { rows: EmailSuppressionRow[]; findManyCalls: number } { const rows: EmailSuppressionRow[] = []; let seq = 0; const client: EmailSuppressionClient & EmailSuppressionBatchClient & { rows: EmailSuppressionRow[]; findManyCalls: number } = { rows, findManyCalls: 0, emailSuppressionList: { async upsert(args) { const existing = rows.find((r) => r.tenantId === args.where.tenantId_emailAddress.tenantId && r.emailAddress === args.where.tenantId_emailAddress.emailAddress); if (existing) { existing.reason = args.update.reason; existing.detail = args.update.detail; return existing; } const created: EmailSuppressionRow = { id: `s${++seq}`, ...args.create }; rows.push(created); return created; }, async findUnique(args) { return rows.find((r) => r.tenantId === args.where.tenantId_emailAddress.tenantId && r.emailAddress === args.where.tenantId_emailAddress.emailAddress) ?? null; }, async findMany(args) { client.findManyCalls += 1; return rows .filter((row) => row.tenantId === args.where.tenantId && args.where.emailAddress.in.includes(row.emailAddress)) .map(({ emailAddress }) => ({ emailAddress })); }, }, }; return client; } describe("normalizeSuppressionAddress", () => { it("trims and lower-cases", () => { expect(normalizeSuppressionAddress(" Foo@Bar.TEST ")).toBe("foo@bar.test"); }); }); describe("recordEmailSuppression", () => { it("creates a normalized entry", async () => { const client = createFakeClient(); await recordEmailSuppression({ tenantId: "t1", emailAddress: "Hard@Bounce.test", reason: "bounce", detail: "550 mailbox unavailable" }, client); expect(client.rows).toEqual([{ id: "s1", tenantId: "t1", emailAddress: "hard@bounce.test", reason: "bounce", detail: "550 mailbox unavailable" }]); }); it("is idempotent on (tenantId, emailAddress) and updates the reason/detail", async () => { const client = createFakeClient(); await recordEmailSuppression({ tenantId: "t1", emailAddress: "a@x.test", reason: "manual" }, client); await recordEmailSuppression({ tenantId: "t1", emailAddress: "A@X.test", reason: "complaint", detail: "spam report" }, client); expect(client.rows).toHaveLength(1); expect(client.rows[0]).toMatchObject({ reason: "complaint", detail: "spam report" }); }); it("scopes entries per tenant", async () => { const client = createFakeClient(); await recordEmailSuppression({ tenantId: "t1", emailAddress: "a@x.test", reason: "bounce" }, client); await recordEmailSuppression({ tenantId: "t2", emailAddress: "a@x.test", reason: "bounce" }, client); expect(client.rows).toHaveLength(2); }); }); describe("isEmailSuppressed", () => { it("returns true only for a suppressed address in the same tenant", async () => { const client = createFakeClient(); await recordEmailSuppression({ tenantId: "t1", emailAddress: "blocked@x.test", reason: "bounce" }, client); await expect(isEmailSuppressed({ tenantId: "t1", emailAddress: "Blocked@X.test" }, client)).resolves.toBe(true); await expect(isEmailSuppressed({ tenantId: "t1", emailAddress: "other@x.test" }, client)).resolves.toBe(false); await expect(isEmailSuppressed({ tenantId: "t2", emailAddress: "blocked@x.test" }, client)).resolves.toBe(false); }); }); describe("listSuppressedEmailAddresses", () => { it("normalizes, deduplicates, tenant-scopes, and resolves a batch with one query", async () => { const client = createFakeClient(); await recordEmailSuppression({ tenantId: "t1", emailAddress: "blocked@x.test", reason: "bounce" }, client); await recordEmailSuppression({ tenantId: "t2", emailAddress: "other@x.test", reason: "bounce" }, client); await expect( listSuppressedEmailAddresses( { tenantId: "t1", emailAddresses: [" Blocked@X.test ", "blocked@x.test", "other@x.test"] }, client, ), ).resolves.toEqual(new Set(["blocked@x.test"])); expect(client.findManyCalls).toBe(1); }); it("does not query for an empty batch", async () => { const client = createFakeClient(); await expect(listSuppressedEmailAddresses({ tenantId: "t1", emailAddresses: [] }, client)).resolves.toEqual(new Set()); expect(client.findManyCalls).toBe(0); }); it("chunks recipient sets before they can approach the database parameter limit", async () => { const client = createFakeClient(); const addresses = Array.from({ length: EMAIL_SUPPRESSION_QUERY_BATCH_SIZE + 1 }, (_, index) => `recipient-${index}@x.test`); await expect(listSuppressedEmailAddresses({ tenantId: "t1", emailAddresses: addresses }, client)).resolves.toEqual(new Set()); expect(client.findManyCalls).toBe(2); }); });