import { describe, expect, it, vi } from "vitest"; import { TELEPHONY_WEBHOOK_JOB_NAME, TELEPHONY_WEBHOOK_QUEUE_NAME, buildTelephonyWebhookJob, enqueueTelephonyWebhookJob, recoverFailedTelephonyWebhookJob, type TelephonyWebhookQueue, type TelephonyWebhookRecoveryQueue, } from "./telephony.js"; function input() { return { tenantId: "tenant_1", connectionId: "connection_1", webhookEventId: "event_1", }; } describe("telephony webhook queue producer", () => { it("builds a typed minimal descriptor without provider payload data", () => { expect(buildTelephonyWebhookJob(input())).toEqual({ queueName: TELEPHONY_WEBHOOK_QUEUE_NAME, jobName: TELEPHONY_WEBHOOK_JOB_NAME, payload: { kind: "telephony.webhook.process", ...input(), }, }); }); it("uses the persisted event id as a stable BullMQ idempotency key", async () => { const add = vi.fn(async () => ({ id: "job_1" })); const queue = { add, close: vi.fn(async () => {}) } as TelephonyWebhookQueue; const descriptor = buildTelephonyWebhookJob(input()); await enqueueTelephonyWebhookJob(queue, descriptor); await enqueueTelephonyWebhookJob(queue, descriptor); expect(add).toHaveBeenCalledTimes(2); expect(add.mock.calls[0]?.[2]).toMatchObject({ attempts: 5, backoff: { type: "exponential", delay: 2_000 }, jobId: "telephony-webhook-event_1", removeOnComplete: 10_000, }); expect(add.mock.calls[1]?.[2]?.jobId).toBe(add.mock.calls[0]?.[2]?.jobId); }); it("manually retries a retained terminal failure with reset attempt counters", async () => { const retry = vi.fn(async () => undefined); const retainedJob = { id: "telephony-webhook-event_1", getState: vi.fn(async () => "failed"), retry, }; const add = vi.fn(async () => ({ id: "unexpected" })); const queue = { add, getJob: vi.fn(async () => retainedJob), close: vi.fn(async () => undefined), } as TelephonyWebhookRecoveryQueue; await expect(recoverFailedTelephonyWebhookJob(queue, buildTelephonyWebhookJob(input()))).resolves.toEqual({ id: "telephony-webhook-event_1", }); expect(queue.getJob).toHaveBeenCalledWith("telephony-webhook-event_1"); expect(retry).toHaveBeenCalledWith("failed", { resetAttemptsMade: true, resetAttemptsStarted: true, }); expect(add).not.toHaveBeenCalled(); }); it("adds a fresh stable job when no retained terminal failure exists", async () => { const add = vi.fn(async () => ({ id: "telephony-webhook-event_1" })); const queue = { add, getJob: vi.fn(async () => undefined), close: vi.fn(async () => undefined), } as TelephonyWebhookRecoveryQueue; await recoverFailedTelephonyWebhookJob(queue, buildTelephonyWebhookJob(input())); expect(add).toHaveBeenCalledWith( TELEPHONY_WEBHOOK_JOB_NAME, expect.objectContaining({ webhookEventId: "event_1" }), expect.objectContaining({ jobId: "telephony-webhook-event_1" }) ); }); it("rejects empty or oversized identifiers before touching BullMQ", () => { expect(() => buildTelephonyWebhookJob({ ...input(), tenantId: " " })).toThrow("valid tenantId"); expect(() => buildTelephonyWebhookJob({ ...input(), webhookEventId: "x".repeat(192) })).toThrow("valid webhookEventId"); }); });