Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

test(webhook): pin the outbox row re-pointed at a replay's queue job

Yudhi Armyndharis2324b3e8b870
diff --git a/src/modules/webhook/webhook-delivery.service.spec.ts b/src/modules/webhook/webhook-delivery.service.spec.ts
index 4bd8d483b..fb3f1202a 100644
--- a/src/modules/webhook/webhook-delivery.service.spec.ts
+++ b/src/modules/webhook/webhook-delivery.service.spec.ts
@@ -1697,6 +1697,24 @@ describe('WebhookDeliveryService', () => {
expect(jobData).not.toHaveProperty('replayData');
});
+ it('points the outbox row of a replay at the job it queued', async () => {
+ const queueService = await buildQueueService((key: string, def?: unknown) =>
+ key === 'queue.enabled' ? true : def,
+ );
+ (hookManager.execute as jest.Mock).mockResolvedValue({ continue: true, data: {} });
+ const webhook = createMockWebhook({ events: ['message.received'] });
+
+ await expect(queueService.redeliver(webhook, 'sess-1', 'message.received', 'stored-key', {})).resolves.toBe(
+ 'enqueued',
+ );
+
+ // A replay is queued under a new delivery id, and the sweep looks a queued row's job up by the id the
+ // row holds: left on the old one it finds no job and enqueues the event again.
+ const [, , opts] = (webhookQueue.add as jest.Mock).mock.calls[0] as [string, WebhookJobData, { jobId: string }];
+ expect(outboxService.markQueued).toHaveBeenCalledTimes(1);
+ expect(outboxService.markQueued).toHaveBeenCalledWith(webhook.id, 'stored-key', opts.jobId);
+ });
+
it('carries the pre-hook event data in the job only while payload retention is on', async () => {
const queueService = await buildQueueService((key: string, def?: unknown) => {
if (key === 'queue.enabled') return true;
diff --git a/src/modules/webhook/webhook-recovery-boundaries.spec.ts b/src/modules/webhook/webhook-recovery-boundaries.spec.ts
index 211e7c8bd..7e8be35d6 100644
--- a/src/modules/webhook/webhook-recovery-boundaries.spec.ts
+++ b/src/modules/webhook/webhook-recovery-boundaries.spec.ts
@@ -187,6 +187,55 @@ describe('webhook recovery across live ownership and configuration changes', ()
expect(post).toHaveBeenCalledTimes(2);
});
+ it('points a replayed row at its new queue job, so the next sweep enqueues nothing', async () => {
+ const config = new ConfigService({ queue: { enabled: true }, webhook: { timeout: 1000 } });
+ const repository = ds.getRepository(WebhookOutboxEvent);
+ const outbox = new WebhookOutboxService(repository);
+ const live = new Set<string>();
+ const queue = {
+ add: jest.fn((_name: string, _data: unknown, options: { jobId: string }) => {
+ live.add(options.jobId);
+ return Promise.resolve({});
+ }),
+ getJob: jest.fn((id: string) =>
+ Promise.resolve(live.has(id) ? { getState: () => Promise.resolve('waiting') } : undefined),
+ ),
+ waitUntilReady: jest.fn().mockResolvedValue(undefined),
+ };
+ const delivery = new WebhookDeliveryService(
+ webhooks,
+ failures,
+ config,
+ new HookManager(),
+ outbox,
+ undefined,
+ queue as never,
+ );
+ const reconciler = new WebhookReconcilerService(webhooks, outbox, delivery, config);
+ const options = { intervalMs: 1000, graceMs: 0, batchSize: 10, maxAttempts: 5 };
+ // A crash before the add left a row that names a job which was never created.
+ await outbox.open({
+ webhookId: primary.id,
+ sessionId: primary.sessionId,
+ event: 'message.received',
+ idempotencyKey: 'key-a',
+ deliveryId: 'job-never-added',
+ payload: { id: 1 },
+ });
+ await repository.update({ idempotencyKey: 'key-a' }, { createdAt: new Date(Date.now() - 600_000) });
+
+ expect(await reconciler.sweep(options)).toMatchObject({ replayed: 1, skipped: 0 });
+ const [[, , added]] = queue.add.mock.calls;
+ expect(await repository.findOneByOrFail({ idempotencyKey: 'key-a' })).toMatchObject({
+ state: 'queued',
+ deliveryId: added.jobId,
+ });
+
+ // The replacement job is alive: with the row pointing at it, the sweep leaves the row alone.
+ expect(await reconciler.sweep(options)).toMatchObject({ replayed: 0, skipped: 1 });
+ expect(queue.add).toHaveBeenCalledTimes(1);
+ });
+
it('rechecks the receiver after a held webhook hook before the first POST', async () => {
const config = new ConfigService({ webhook: { failurePayloadRetentionHours: 24 } });
const hooks = new HookManager();