Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

test: bind three claims that no test was holding

Yudhi Armyndharis3d6f00accb09
diff --git a/src/modules/message/bulk-message.service.spec.ts b/src/modules/message/bulk-message.service.spec.ts
index b1dc1c1ce..fa0dc0d4c 100644
--- a/src/modules/message/bulk-message.service.spec.ts
+++ b/src/modules/message/bulk-message.service.spec.ts
@@ -348,6 +348,21 @@ describe('BulkMessageService.processBatch', () => {
expect(finalPartial.results[0].error?.message).toMatch(/message:sending/);
});
+ /**
+ * The ONLY reason this second copy of the gate exists is that it must flag a plugin refusal apart
+ * from a delivery failure, so the per-item `message:failed` hook is skipped. Nothing bound that:
+ * deleting the flag on the envelope-refusal branch left the whole suite green, which means the
+ * refusal would have been reported to every plugin as a failed delivery with no test noticing.
+ */
+ it('does not report an unusable envelope to plugins as a delivery failure', async () => {
+ repo.findOne.mockResolvedValue(makeBatch(1));
+ hookManager.execute.mockResolvedValue({ continue: true, data: { notInput: 1 } });
+
+ await runProcessBatch();
+
+ expect(hookManager.execute).not.toHaveBeenCalledWith('message:failed', expect.anything(), expect.anything());
+ });
+
// Negative twin: a chain that returns nothing is the ordinary no-plugin path.
it('keeps the original content when the hook chain returns no data', async () => {
repo.findOne.mockResolvedValue(makeBatch(1));
@@ -395,6 +410,35 @@ describe('BulkMessageService.processBatch', () => {
expect(finalPartial.results[0].error?.message).toMatch(/character limit/i);
});
+ /**
+ * The cap covers text AND caption — a caption is the other caller-supplied string a template can
+ * inflate without bound, and it lands in the same `messages.body` column. Only the text half was
+ * bound: narrowing the loop to `['text']` left the suite green, so the caption half was a claim
+ * with nothing behind it.
+ */
+ it('fails an item whose rendered CAPTION exceeds the cap', async () => {
+ const huge = 'x'.repeat(70 * 1024);
+ repo.findOne.mockResolvedValue({
+ ...makeBatch(1),
+ messages: [
+ {
+ chatId: '[email protected]',
+ type: 'image',
+ content: { image: { base64: 'AAAA', mimetype: 'image/jpeg' }, caption: '{{v}}' },
+ variables: { v: huge },
+ },
+ ],
+ });
+
+ await runProcessBatch();
+
+ const finalPartial = (
+ repo.update.mock.calls as Array<[unknown, { status: BatchStatus; results: BatchMessageResult[] }]>
+ ).at(-1)![1];
+ expect(finalPartial.results[0].status).toBe(BatchMessageStatus.FAILED);
+ expect(finalPartial.results[0].error?.message).toMatch(/caption/i);
+ });
+
it('still sends an item whose render stays under the cap', async () => {
repo.findOne.mockResolvedValue(batchWithVariables('hello {{v}}', { v: 'world' }));
diff --git a/src/modules/takeover/session-takeover.service.spec.ts b/src/modules/takeover/session-takeover.service.spec.ts
index a78fcfde1..f7d915234 100644
--- a/src/modules/takeover/session-takeover.service.spec.ts
+++ b/src/modules/takeover/session-takeover.service.spec.ts
@@ -188,4 +188,32 @@ describe('SessionTakeoverService', () => {
resolveFirst();
svc.onModuleDestroy();
});
+
+ /**
+ * There are TWO shutdown guards: one at sweep entry and one per adoption. Every existing case has
+ * at least one eligible session, so the per-adoption guard stops the work either way and the entry
+ * guard binds nothing — deleting it left the whole suite green.
+ *
+ * The ownership QUERY is what separates them: it runs before the per-adoption guard, so a sweep
+ * that never queries proves the entry guard fired. That also matters on its own — a sweep entered
+ * during shutdown otherwise still hits the database on every remaining tick.
+ */
+ it('a sweep entered during shutdown does not even query ownership', async () => {
+ const ownershipCalls = jest.fn().mockResolvedValue([lapsed({ name: 'any' })]);
+ const config = {
+ get: (key: string, def?: unknown) =>
+ ({ features: { autoStartSessions: true }, 'session.takeoverSweepMs': 1000 })[key as 'features'] ?? def,
+ } as unknown as ConfigService;
+ const svc = new SessionTakeoverService(
+ { start: jest.fn().mockResolvedValue(undefined) } as unknown as SessionService,
+ { lapsedHeldByOthers: ownershipCalls } as unknown as SessionOwnershipService,
+ { reapProcessingBatches: jest.fn().mockResolvedValue(0) } as unknown as BulkMessageService,
+ config,
+ );
+
+ svc.onModuleDestroy(); // flips the shutting-down signal
+ await svc.sweep();
+
+ expect(ownershipCalls).not.toHaveBeenCalled();
+ });
});