76 lines
2.8 KiB
TypeScript
76 lines
2.8 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
import { queueNames } from "@toolctrl/queue";
|
|
import {
|
|
FORM_SUBMISSION_WEBHOOK_OUTBOX_QUEUE,
|
|
FormSubmissionWebhookDeliveryJobError,
|
|
parseFormSubmissionWebhookDeliveryJob,
|
|
runFormSubmissionWebhookDeliveryJob,
|
|
} from "./form-submission-webhook-delivery.js";
|
|
|
|
function buildJob(overrides: Record<string, unknown> = {}) {
|
|
return {
|
|
outboxEventId: "outbox_1",
|
|
tenantId: "tenant_1",
|
|
eventType: "webhook.form-submission.deliver",
|
|
dispatchKey: "form-submission-webhook:sub_1",
|
|
payload: {
|
|
submissionId: "sub_1",
|
|
formId: "form_1",
|
|
tenantId: "tenant_1",
|
|
webhookUrl: "https://example.com/hook",
|
|
targetType: "crm-contact",
|
|
targetId: "contact_1",
|
|
values: { email: "ada@example.com" },
|
|
...overrides,
|
|
},
|
|
};
|
|
}
|
|
|
|
describe("FORM_SUBMISSION_WEBHOOK_OUTBOX_QUEUE", () => {
|
|
it("matches the outbox webhook queue", () => {
|
|
expect(FORM_SUBMISSION_WEBHOOK_OUTBOX_QUEUE).toBe("outbox.webhook");
|
|
});
|
|
});
|
|
|
|
describe("parseFormSubmissionWebhookDeliveryJob", () => {
|
|
it("accepts the exact form-submission webhook event", () => {
|
|
const parsed = parseFormSubmissionWebhookDeliveryJob(buildJob());
|
|
expect(parsed.eventType).toBe("webhook.form-submission.deliver");
|
|
expect(parsed.payload.webhookUrl).toBe("https://example.com/hook");
|
|
});
|
|
|
|
it("rejects other webhook event types", () => {
|
|
expect(() => parseFormSubmissionWebhookDeliveryJob({ ...buildJob(), eventType: "webhook.other.deliver" })).toThrow(FormSubmissionWebhookDeliveryJobError);
|
|
});
|
|
|
|
it("rejects envelope and payload tenant mismatches", () => {
|
|
expect(() => parseFormSubmissionWebhookDeliveryJob(buildJob({ tenantId: "tenant_2" }))).toThrow(/tenantId mismatch/);
|
|
});
|
|
});
|
|
|
|
describe("runFormSubmissionWebhookDeliveryJob", () => {
|
|
it("enqueues a deliver-webhook job with outbox idempotency", async () => {
|
|
const enqueueWebhook = vi.fn(async () => undefined);
|
|
|
|
const result = await runFormSubmissionWebhookDeliveryJob(buildJob(), { enqueueWebhook });
|
|
|
|
expect(result).toEqual({ enqueued: true, queueName: queueNames.email, jobName: "deliver-webhook" });
|
|
expect(enqueueWebhook).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
queueName: queueNames.email,
|
|
jobName: "deliver-webhook",
|
|
payload: expect.objectContaining({
|
|
kind: "webhook.deliver",
|
|
url: "https://example.com/hook",
|
|
method: "POST",
|
|
eventName: "form.submission",
|
|
tenantId: "tenant_1",
|
|
body: expect.objectContaining({ submissionId: "sub_1", tenantId: "tenant_1" }),
|
|
metadata: expect.objectContaining({ source: "form-submission", outboxEventId: "outbox_1" }),
|
|
}),
|
|
}),
|
|
expect.objectContaining({ tenantId: "tenant_1", idempotencyKey: "outbox_1" }),
|
|
);
|
|
});
|
|
});
|