import { describe, expect, it, vi } from "vitest"; import { bootstrapFormSubmissionWebhookDelivery } from "./form-submission-webhook-delivery-bootstrap.js"; import { FORM_SUBMISSION_WEBHOOK_OUTBOX_QUEUE } from "./form-submission-webhook-delivery.js"; function buildHandlerDeps() { return { enqueueWebhook: vi.fn(async () => undefined), }; } describe("bootstrapFormSubmissionWebhookDelivery", () => { it("disables itself outside the mail worker group", () => { const res = bootstrapFormSubmissionWebhookDelivery({ WORKER_QUEUE_GROUP: "general" } as Record); expect(res.status).toBe("disabled"); if (res.status === "disabled") expect(res.reason).toMatch(/non-mail/); }); it("disables itself when the env flag is false", () => { const res = bootstrapFormSubmissionWebhookDelivery({ WORKER_QUEUE_GROUP: "mail", FORM_SUBMISSION_WEBHOOK_DELIVERY_ENABLED: "false", } as Record); expect(res.status).toBe("disabled"); if (res.status === "disabled") expect(res.reason).toMatch(/FORM_SUBMISSION_WEBHOOK_DELIVERY_ENABLED/); }); it("subscribes to outbox.webhook on the mail worker group", () => { const create = vi.fn(() => ({ name: FORM_SUBMISSION_WEBHOOK_OUTBOX_QUEUE, close: async () => undefined })); const res = bootstrapFormSubmissionWebhookDelivery( { WORKER_QUEUE_GROUP: "mail" } as Record, { handlerDeps: buildHandlerDeps(), createWorkerImpl: create }, ); expect(res.status).toBe("started"); expect(create).toHaveBeenCalledWith( FORM_SUBMISSION_WEBHOOK_OUTBOX_QUEUE, expect.any(Function), expect.objectContaining({ concurrency: 5 }), ); }); it("honours FORM_SUBMISSION_WEBHOOK_DELIVERY_CONCURRENCY", () => { const create = vi.fn(() => ({ name: FORM_SUBMISSION_WEBHOOK_OUTBOX_QUEUE, close: async () => undefined })); bootstrapFormSubmissionWebhookDelivery( { WORKER_QUEUE_GROUP: "mail", FORM_SUBMISSION_WEBHOOK_DELIVERY_CONCURRENCY: "9", } as Record, { handlerDeps: buildHandlerDeps(), createWorkerImpl: create }, ); expect(create).toHaveBeenCalledWith( FORM_SUBMISSION_WEBHOOK_OUTBOX_QUEUE, expect.any(Function), expect.objectContaining({ concurrency: 9 }), ); }); });