36 lines · 1.3 KB
import { describe, expect, mock, test } from "bun:test";

const handled: string[] = [];
mock.module("./abuse-email", () => ({
  handleAbuseEmail: async (message: { to: string }) => handled.push(`abuse:${message.to}`),
}));
mock.module("./email-reply", () => ({
  isReplyAddress: (address: string) => address.toLowerCase().startsWith("reply+"),
  handleReplyEmail: async (message: { to: string }) => handled.push(`reply:${message.to}`),
}));

const { routeInboundEmail } = await import("./inbound-email");

function message(to: string) {
  const rejected: string[] = [];
  return { to, rejected, setReject: (reason: string) => rejected.push(reason) };
}

describe("routeInboundEmail", () => {
  test("routes abuse@, its subaddresses and thread replies, and rejects anything else", async () => {
    for (const to of ["abuse@gitfor.dev", "Abuse+dmca@gitfor.dev", "reply+abc@mail.gitfor.dev"]) {
      await routeInboundEmail(message(to) as unknown as ForwardableEmailMessage);
    }
    expect(handled).toEqual([
      "abuse:abuse@gitfor.dev",
      "abuse:Abuse+dmca@gitfor.dev",
      "reply:reply+abc@mail.gitfor.dev",
    ]);
    for (const to of ["abusers@gitfor.dev", "support@gitfor.dev"]) {
      const stray = message(to);
      await routeInboundEmail(stray as unknown as ForwardableEmailMessage);
      expect(stray.rejected).toEqual(["Unknown address"]);
    }
  });
});