Update hivegrid-cloud reference to latest main
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e98d38e716
commit
7b21524c86
3781 files changed
+373484
-225607
No files matched your search
@@ -0,0 +1,231 @@
|
||||
import { describe, expect, test, vi } from "vitest";
|
||||
import { createMailjetProvider, createMailjetProviderFromEnv, MailjetProvider } from "./mailjet.js";
|
||||
import { MailProviderError } from "./types.js";
|
||||
|
||||
type FetchMock = ReturnType<typeof vi.fn>;
|
||||
|
||||
function jsonResponse(body: unknown, init: ResponseInit = {}) {
|
||||
return new Response(JSON.stringify(body), {
|
||||
headers: { "content-type": "application/json", ...init.headers },
|
||||
...init,
|
||||
});
|
||||
}
|
||||
|
||||
function successBody(messageUuid = "uuid-123") {
|
||||
return {
|
||||
Messages: [
|
||||
{
|
||||
Status: "success",
|
||||
To: [{ Email: "to@example.com", MessageID: 456, MessageUUID: messageUuid }],
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
/** Parse the JSON body that was passed to the mocked fetch on its first call. */
|
||||
function sentPayload(fetchMock: FetchMock): {
|
||||
Messages: Array<Record<string, unknown>>;
|
||||
SandboxMode?: boolean;
|
||||
} {
|
||||
const init = fetchMock.mock.calls[0]?.[1] as RequestInit | undefined;
|
||||
if (!init || typeof init.body !== "string") {
|
||||
throw new Error("fetch was not called with a string body");
|
||||
}
|
||||
return JSON.parse(init.body);
|
||||
}
|
||||
|
||||
function provider(fetchMock: FetchMock, overrides: Partial<Parameters<typeof createMailjetProvider>[0]> = {}) {
|
||||
return new MailjetProvider({
|
||||
apiKey: "api_key",
|
||||
secretKey: "secret_key",
|
||||
fromEmail: "sender@example.com",
|
||||
fetch: fetchMock as unknown as typeof fetch,
|
||||
...overrides,
|
||||
});
|
||||
}
|
||||
|
||||
describe("createMailjetProvider factory", () => {
|
||||
test("returns a working provider that posts to the Mailjet send endpoint", async () => {
|
||||
const fetchMock = vi.fn(async () => jsonResponse(successBody("uuid-factory"), { status: 200 }));
|
||||
const mailer = createMailjetProvider({
|
||||
apiKey: "api_key",
|
||||
secretKey: "secret_key",
|
||||
fromEmail: "sender@example.com",
|
||||
fetch: fetchMock as unknown as typeof fetch,
|
||||
});
|
||||
|
||||
await expect(mailer.send({ to: ["user@example.com"], subject: "Hi", text: "Body" })).resolves.toMatchObject({
|
||||
provider: "mailjet",
|
||||
status: "queued",
|
||||
messageId: "uuid-factory",
|
||||
});
|
||||
expect(fetchMock).toHaveBeenCalledWith("https://api.mailjet.com/v3.1/send", expect.any(Object));
|
||||
});
|
||||
});
|
||||
|
||||
describe("MailjetProvider content validation", () => {
|
||||
test("rejects a send with neither text, html nor templateId before calling Mailjet", async () => {
|
||||
const fetchMock = vi.fn();
|
||||
await expect(provider(fetchMock).send({ to: ["user@example.com"], subject: "Empty" })).rejects.toMatchObject({
|
||||
provider: "mailjet",
|
||||
message: "Email text, html or templateId is required.",
|
||||
} satisfies Partial<MailProviderError>);
|
||||
expect(fetchMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("MailjetProvider per-recipient personalization", () => {
|
||||
test("splits into one message per recipient and copies Cc/Bcc only onto the first", async () => {
|
||||
const fetchMock = vi.fn(async () => jsonResponse(successBody(), { status: 200 }));
|
||||
await provider(fetchMock).send({
|
||||
to: ["first@example.com", "second@example.com"],
|
||||
cc: ["cc@example.com"],
|
||||
bcc: ["bcc@example.com"],
|
||||
subject: "Digest",
|
||||
text: "Body",
|
||||
personalization: [
|
||||
{ email: "first@example.com", data: { firstName: "Ada" } },
|
||||
{ email: "second@example.com", data: { firstName: "Grace" } },
|
||||
],
|
||||
});
|
||||
|
||||
const { Messages } = sentPayload(fetchMock);
|
||||
expect(Messages).toHaveLength(2);
|
||||
expect(Messages[0]).toMatchObject({
|
||||
To: [{ Email: "first@example.com" }],
|
||||
Variables: { firstName: "Ada" },
|
||||
Cc: [{ Email: "cc@example.com" }],
|
||||
Bcc: [{ Email: "bcc@example.com" }],
|
||||
});
|
||||
expect(Messages[1]).toMatchObject({
|
||||
To: [{ Email: "second@example.com" }],
|
||||
Variables: { firstName: "Grace" },
|
||||
});
|
||||
// Shared recipients must not be duplicated onto every personalized copy.
|
||||
expect(Messages[1]).not.toHaveProperty("Cc");
|
||||
expect(Messages[1]).not.toHaveProperty("Bcc");
|
||||
});
|
||||
});
|
||||
|
||||
describe("MailjetProvider attachment mapping", () => {
|
||||
test("routes inline attachments to InlinedAttachments with a ContentID and omits the regular bucket", async () => {
|
||||
const fetchMock = vi.fn(async () => jsonResponse(successBody(), { status: 200 }));
|
||||
await provider(fetchMock).send({
|
||||
to: ["user@example.com"],
|
||||
subject: "Logo",
|
||||
html: "<p>x</p>",
|
||||
attachments: [
|
||||
{ content: "YWJj", filename: "logo.png", disposition: "inline", id: "logo-cid" },
|
||||
{ content: "ZGVm", filename: "header.png", disposition: "inline" },
|
||||
],
|
||||
});
|
||||
|
||||
const message = sentPayload(fetchMock).Messages[0]!;
|
||||
expect(message).not.toHaveProperty("Attachments");
|
||||
expect(message.InlinedAttachments).toEqual([
|
||||
{ ContentType: "image/png", Filename: "logo.png", Base64Content: "YWJj", ContentID: "logo-cid" },
|
||||
// Falls back to the filename when no explicit Content-ID is provided.
|
||||
{ ContentType: "image/png", Filename: "header.png", Base64Content: "ZGVm", ContentID: "header.png" },
|
||||
]);
|
||||
});
|
||||
|
||||
test("keeps regular and inline attachments in separate buckets", async () => {
|
||||
const fetchMock = vi.fn(async () => jsonResponse(successBody(), { status: 200 }));
|
||||
await provider(fetchMock).send({
|
||||
to: ["user@example.com"],
|
||||
subject: "Mixed",
|
||||
html: "<p>x</p>",
|
||||
attachments: [
|
||||
{ content: "YWJj", filename: "report.pdf", disposition: "attachment" },
|
||||
{ content: "ZGVm", filename: "logo.png", disposition: "inline", id: "logo" },
|
||||
],
|
||||
});
|
||||
|
||||
const message = sentPayload(fetchMock).Messages[0]!;
|
||||
expect(message.Attachments).toEqual([
|
||||
{ ContentType: "application/pdf", Filename: "report.pdf", Base64Content: "YWJj" },
|
||||
]);
|
||||
expect(message.InlinedAttachments).toEqual([
|
||||
{ ContentType: "image/png", Filename: "logo.png", Base64Content: "ZGVm", ContentID: "logo" },
|
||||
]);
|
||||
});
|
||||
|
||||
test("falls back to application/octet-stream for missing and unknown extensions", async () => {
|
||||
const fetchMock = vi.fn(async () => jsonResponse(successBody(), { status: 200 }));
|
||||
await provider(fetchMock).send({
|
||||
to: ["user@example.com"],
|
||||
subject: "Files",
|
||||
html: "<p>x</p>",
|
||||
attachments: [
|
||||
{ content: "YWJj", filename: "noextension" },
|
||||
{ content: "ZGVm", filename: "archive.unknownext" },
|
||||
],
|
||||
});
|
||||
|
||||
const types = (sentPayload(fetchMock).Messages[0]!.Attachments as Array<{ ContentType: string }>).map(
|
||||
(a) => a.ContentType
|
||||
);
|
||||
expect(types).toEqual(["application/octet-stream", "application/octet-stream"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("MailjetProvider response parsing edge cases", () => {
|
||||
test("uses the numeric MessageID when no MessageUUID is present", async () => {
|
||||
const fetchMock = vi.fn(async () =>
|
||||
jsonResponse({ Messages: [{ Status: "success", To: [{ Email: "to@example.com", MessageID: 789 }] }] }, { status: 200 })
|
||||
);
|
||||
await expect(provider(fetchMock).send({ to: ["to@example.com"], subject: "x", text: "y" })).resolves.toMatchObject({
|
||||
messageId: "789",
|
||||
});
|
||||
});
|
||||
|
||||
test("returns an undefined messageId when the recipient list is empty", async () => {
|
||||
const fetchMock = vi.fn(async () => jsonResponse({ Messages: [{ Status: "success", To: [] }] }, { status: 200 }));
|
||||
const result = await provider(fetchMock).send({ to: ["to@example.com"], subject: "x", text: "y" });
|
||||
expect(result.messageId).toBeUndefined();
|
||||
expect(result.status).toBe("queued");
|
||||
});
|
||||
|
||||
test("returns an undefined messageId when the recipient carries neither UUID nor MessageID", async () => {
|
||||
const fetchMock = vi.fn(async () =>
|
||||
jsonResponse({ Messages: [{ Status: "success", To: [{ Email: "to@example.com" }] }] }, { status: 200 })
|
||||
);
|
||||
const result = await provider(fetchMock).send({ to: ["to@example.com"], subject: "x", text: "y" });
|
||||
expect(result.messageId).toBeUndefined();
|
||||
expect(result.status).toBe("queued");
|
||||
});
|
||||
|
||||
test("accepts a 2xx response with no body and reports a queued send", async () => {
|
||||
const fetchMock = vi.fn(async () => new Response(null, { status: 200 }));
|
||||
const result = await provider(fetchMock).send({ to: ["to@example.com"], subject: "x", text: "y" });
|
||||
expect(result).toEqual({ provider: "mailjet", status: "queued", messageId: undefined });
|
||||
});
|
||||
|
||||
test("tolerates a 2xx body whose Messages field is not an array", async () => {
|
||||
const fetchMock = vi.fn(async () => jsonResponse({ Messages: "not-an-array" }, { status: 200 }));
|
||||
const result = await provider(fetchMock).send({ to: ["to@example.com"], subject: "x", text: "y" });
|
||||
expect(result.messageId).toBeUndefined();
|
||||
expect(result.status).toBe("queued");
|
||||
});
|
||||
});
|
||||
|
||||
describe("createMailjetProviderFromEnv passthrough options", () => {
|
||||
test("wires onMetric and sandbox from the options bag", async () => {
|
||||
const fetchMock = vi.fn(async () => jsonResponse(successBody("uuid-env"), { status: 200 }));
|
||||
const samples: Array<{ provider: string; status: string }> = [];
|
||||
const mailer = createMailjetProviderFromEnv(
|
||||
{
|
||||
MAILJET_API_KEY: "k",
|
||||
MAILJET_SECRET_KEY: "s",
|
||||
MAILJET_FROM_EMAIL: "sender@example.com",
|
||||
},
|
||||
{ onMetric: (sample) => samples.push(sample), sandbox: true }
|
||||
) as MailjetProvider;
|
||||
Object.defineProperty(mailer, "fetchImpl", { value: fetchMock });
|
||||
|
||||
await mailer.send({ to: ["user@example.com"], subject: "x", text: "y" });
|
||||
|
||||
expect(samples).toEqual([{ provider: "mailjet", status: "delivered" }]);
|
||||
expect(sentPayload(fetchMock).SandboxMode).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user