forked from fahrPool/fahrPool
81 lines
2.8 KiB
TypeScript
81 lines
2.8 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import type { WhatsAppConfig } from "./config";
|
|
import { buildTemplateComponents, sendWhatsAppTemplate } from "./whatsapp";
|
|
|
|
const CFG: WhatsAppConfig = {
|
|
accessToken: "tok",
|
|
phoneNumberId: "PN1",
|
|
recipient: "4915100000000",
|
|
templateName: "neuer_auftrag",
|
|
templateLang: "de",
|
|
graphVersion: "v21.0",
|
|
};
|
|
|
|
const PAYLOAD = {
|
|
auftragsnummer: "26-09-03-0001",
|
|
abholort_adresse: "Startstr. 1, Teststadt",
|
|
zielort_adresse: "Zielstr. 2, Teststadt",
|
|
kennzeichen: "S-WA 42",
|
|
};
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
describe("buildTemplateComponents", () => {
|
|
it("baut genau einen body-Component mit vier text-Parametern in fester Reihenfolge", () => {
|
|
expect(buildTemplateComponents(PAYLOAD)).toEqual([
|
|
{
|
|
type: "body",
|
|
parameters: [
|
|
{ type: "text", text: "26-09-03-0001" },
|
|
{ type: "text", text: "Startstr. 1, Teststadt" },
|
|
{ type: "text", text: "Zielstr. 2, Teststadt" },
|
|
{ type: "text", text: "S-WA 42" },
|
|
],
|
|
},
|
|
]);
|
|
});
|
|
});
|
|
|
|
describe("sendWhatsAppTemplate", () => {
|
|
it("POSTet an die Meta-Graph-URL und gibt bei 2xx die message id zurueck", async () => {
|
|
const fetchMock = vi.spyOn(globalThis, "fetch").mockResolvedValue(
|
|
new Response(JSON.stringify({ messages: [{ id: "wamid.ABC" }] }), { status: 200 })
|
|
);
|
|
|
|
const res = await sendWhatsAppTemplate(CFG, PAYLOAD);
|
|
|
|
expect(res).toEqual({ ok: true, messageId: "wamid.ABC" });
|
|
const [url, init] = fetchMock.mock.calls[0];
|
|
expect(url).toBe("https://graph.facebook.com/v21.0/PN1/messages");
|
|
expect(init?.method).toBe("POST");
|
|
expect((init?.headers as Record<string, string>).Authorization).toBe("Bearer tok");
|
|
const body = JSON.parse(init?.body as string);
|
|
expect(body).toMatchObject({
|
|
messaging_product: "whatsapp",
|
|
to: "4915100000000",
|
|
type: "template",
|
|
template: { name: "neuer_auftrag", language: { code: "de" } },
|
|
});
|
|
expect(body.template.components[0].parameters).toHaveLength(4);
|
|
});
|
|
|
|
it("gibt bei nicht-2xx ok:false mit Status und Body zurueck", async () => {
|
|
vi.spyOn(globalThis, "fetch").mockResolvedValue(
|
|
new Response(JSON.stringify({ error: { message: "Template not found" } }), { status: 404 })
|
|
);
|
|
const res = await sendWhatsAppTemplate(CFG, PAYLOAD);
|
|
expect(res.ok).toBe(false);
|
|
expect(res).toMatchObject({ ok: false });
|
|
if (!res.ok) expect(res.fehler).toContain("404");
|
|
if (!res.ok) expect(res.fehler).toContain("Template not found");
|
|
});
|
|
|
|
it("gibt bei einer fetch-Exception ok:false zurueck", async () => {
|
|
vi.spyOn(globalThis, "fetch").mockRejectedValue(new Error("ECONNRESET"));
|
|
const res = await sendWhatsAppTemplate(CFG, PAYLOAD);
|
|
expect(res).toEqual({ ok: false, fehler: "ECONNRESET" });
|
|
});
|
|
});
|