Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
test(engine): close mutation-survivable gaps in the extraction-guard specs
Yudhi Armyndharisaa2beb7a682c
diff --git a/src/engine/adapters/baileys-events.spec.ts b/src/engine/adapters/baileys-events.spec.tsindex a09b24fe6..0615a4c3b 100644--- a/src/engine/adapters/baileys-events.spec.ts+++ b/src/engine/adapters/baileys-events.spec.ts@@ -1,7 +1,7 @@import { BaileysEvents, type BaileysEventsHost } from './baileys-events';import { createLogger } from '../../common/services/logger.service';import { ConcurrencyLimiter } from '../../common/utils/concurrency-limiter';-import type { WASocket } from '@whiskeysockets/baileys';+import type { WAMessage, WASocket } from '@whiskeysockets/baileys';/*** `downloadMediaMessage` is a jest mock, not a silent no-op: the real-download branch@@ -115,7 +115,16 @@ describe('BaileysEvents.mapMessage', () => {{key: { id: 'wamid.3', remoteJid: '[email protected]', fromMe: false },messageTimestamp: 1_700_000_000,- message: { liveLocationMessage: { degreesLatitude: 1.5, degreesLongitude: 2.5 } },+ message: {+ liveLocationMessage: {+ degreesLatitude: 1.5,+ degreesLongitude: 2.5,+ // ILiveLocationMessage has neither field in the real proto; present here so a slip that+ // reads them off the live variant (instead of the static-only `staticLm`) is caught.+ name: 'SHOULD BE IGNORED',+ address: 'SHOULD BE IGNORED',+ },+ } as WAMessage['message'],},'liveLocationMessage',);@@ -162,6 +171,51 @@ describe('BaileysEvents.mapMessage', () => {expect(incoming.media?.omitted).toBe(true);});+ it('classifies a sticker as media (pins stickerMessage in the isMediaType disjunction)', async () => {+ const incoming = await events.mapMessage(+ {+ key: { id: 'wamid.8', remoteJid: '[email protected]', fromMe: true },+ messageTimestamp: 1_700_000_000,+ message: { stickerMessage: { mimetype: 'image/webp', fileLength: 555 } },+ },+ 'stickerMessage',+ { skipMediaDownload: true },+ );++ expect(incoming.media).toEqual({+ mimetype: 'image/webp',+ filename: undefined,+ omitted: true,+ sizeBytes: 555,+ });+ });++ it('classifies a documentWithCaptionMessage as media (pins it in the isMediaType disjunction)', async () => {+ const incoming = await events.mapMessage(+ {+ key: { id: 'wamid.9', remoteJid: '[email protected]', fromMe: true },+ messageTimestamp: 1_700_000_000,+ message: {+ documentMessage: {+ mimetype: 'application/pdf',+ fileLength: 321,+ fileName: 'contract.pdf',+ caption: 'signed',+ },+ },+ },+ 'documentWithCaptionMessage',+ { skipMediaDownload: true },+ );++ expect(incoming.media).toEqual({+ mimetype: 'application/pdf',+ filename: 'contract.pdf',+ omitted: true,+ sizeBytes: 321,+ });+ });+it('resolves a quoted message from contextInfo', async () => {const incoming = await events.mapMessage({@@ -197,4 +251,34 @@ describe('BaileysEvents.mapMessage', () => {expect(incoming.quotedMessage).toBeUndefined();expect(downloadMediaMessage).not.toHaveBeenCalled();});++ it('resolves a quoted reply, mention and disappearing timer riding on an imageMessage context (not extendedTextMessage)', async () => {+ const incoming = await events.mapMessage(+ {+ key: { id: 'wamid.10', remoteJid: '[email protected]', fromMe: false },+ messageTimestamp: 1_700_000_000,+ message: {+ imageMessage: {+ mimetype: 'image/jpeg',+ caption: 'look',+ contextInfo: {+ stanzaId: 'wamid.original2',+ expiration: 604800,+ mentionedJid: ['[email protected]'],+ // No `conversation` on the quoted sub-message — only `imageMessage.caption` — so this+ // also pins the qBody fallback chain beyond its first (`conversation`) arm.+ quotedMessage: { imageMessage: { caption: 'quoted image caption' } },+ },+ },+ },+ },+ 'imageMessage',+ { skipMediaDownload: true },+ );++ expect(incoming.quotedMessage?.id).toBe('wamid.original2');+ expect(incoming.quotedMessage?.body).toBe('quoted image caption');+ expect(incoming.ephemeralDuration).toBe(604800);+ expect(incoming.mentionedIds).toEqual(['[email protected]']);+ });});diff --git a/src/modules/session/message-projector.service.spec.ts b/src/modules/session/message-projector.service.spec.tsindex 93ba2c1b5..6fdc60eba 100644--- a/src/modules/session/message-projector.service.spec.ts+++ b/src/modules/session/message-projector.service.spec.ts@@ -196,12 +196,12 @@ describe('MessageProjector', () => {});});-// Separate top-level suite: handleInboundMessage's own stale-engine fences and early returns-// (message-projector.service.ts:114-326) were previously exercised only indirectly through-// session.service.spec.ts. Uses the TestingModule idiom (mirrors session.service.spec.ts) rather-// than the direct-construction style above, so the full DI surface (ConfigService, SessionLidResolver)-// is wired the same way production does.-describe('MessageProjector', () => {+// Separate top-level suite: handleInboundMessage's stale-engine fence and its isStatusBroadcast+// early return (message-projector.service.ts:114-119) were previously exercised only indirectly+// through session.service.spec.ts. Uses the TestingModule idiom (mirrors session.service.spec.ts)+// rather than the direct-construction style above, so the full DI surface (ConfigService,+// SessionLidResolver) is wired the same way production does.+describe('MessageProjector (inbound projection)', () => {let projector: MessageProjector;let engines: EngineRegistry;let messageRepository: { create: jest.Mock; insert: jest.Mock; findOne: jest.Mock; update: jest.Mock };@@ -332,6 +332,11 @@ describe('MessageProjector', () => {expect(eventsGateway.emitMessage).toHaveBeenCalledWith(SESSION_ID, expect.anything());expect(webhookService.dispatch).toHaveBeenCalledWith(SESSION_ID, 'message.received', expect.anything());expect(sessionRepository.update).toHaveBeenCalledTimes(1);+ // Pins the ternary at message-projector.service.ts:255: a genuinely inbound message (fromMe:+ // false) must be tagged INCOMING, not just "not OUTGOING".+ expect(messageRepository.create).toHaveBeenCalledWith(+ expect.objectContaining({ direction: MessageDirection.INCOMING }),+ );});it('routes a status broadcast to the status store instead of the message table', async () => {