Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
test(engine): guard against an isMediaType false positive in BaileysEvents spec
Yudhi Armyndharis41d70861e501
diff --git a/src/engine/adapters/baileys-events.spec.ts b/src/engine/adapters/baileys-events.spec.tsindex e8a3a5095..a09b24fe6 100644--- a/src/engine/adapters/baileys-events.spec.ts+++ b/src/engine/adapters/baileys-events.spec.ts@@ -3,6 +3,18 @@ import { createLogger } from '../../common/services/logger.service';import { ConcurrencyLimiter } from '../../common/utils/concurrency-limiter';import type { WASocket } from '@whiskeysockets/baileys';+/**+ * `downloadMediaMessage` is a jest mock, not a silent no-op: the real-download branch+ * (`baileys-events.ts:588-638`, reached whenever `isMediaType` is true and skipMediaDownload+ * isn't set) calls `b.downloadMediaMessage(...)`, and this stub implements nothing else that+ * branch needs — so if a non-media contentType were ever misclassified as media (the exact+ * slip the Plan 2 mapper extraction could introduce), the call throws and that throw is caught+ * by baileys-events.ts:632-637, leaving `media` unset. An `incoming.media` assertion alone+ * can't tell "correctly not classified as media" from "misclassified, then crashed and got+ * swallowed" — asserting `downloadMediaMessage` was never called can.+ */+const downloadMediaMessage = jest.fn();+/*** Minimal Baileys lib stub. `mapMessage` calls `normalizeMessageContent` to unwrap* ephemeral / viewOnce / documentWithCaption wrappers; identity is the correct@@ -10,7 +22,8 @@ import type { WASocket } from '@whiskeysockets/baileys';*/const libStub = {normalizeMessageContent: (content: unknown) => content,-} as Awaited<ReturnType<BaileysEventsHost['loadLib']>>;+ downloadMediaMessage,+} as unknown as Awaited<ReturnType<BaileysEventsHost['loadLib']>>;function makeHost(overrides: Partial<BaileysEventsHost> = {}): BaileysEventsHost {const noop = (): void => undefined;@@ -44,9 +57,16 @@ function makeHost(overrides: Partial<BaileysEventsHost> = {}): BaileysEventsHost}describe('BaileysEvents.mapMessage', () => {- it('maps a plain text message', async () => {- const events = new BaileysEvents(makeHost());+ // Hoisted: every case uses the same plain fake host; a test that needs a different one can+ // still call makeHost(overrides) directly and shadow this.+ let events: BaileysEvents;+ beforeEach(() => {+ downloadMediaMessage.mockClear();+ events = new BaileysEvents(makeHost());+ });++ it('maps a plain text message', async () => {const incoming = await events.mapMessage({key: { id: 'wamid.1', remoteJid: '[email protected]', fromMe: false },@@ -59,11 +79,12 @@ describe('BaileysEvents.mapMessage', () => {expect(incoming.body).toBe('hello there');expect(incoming.media).toBeUndefined();expect(incoming.location).toBeUndefined();+ // Guards against an isMediaType false positive on a non-media contentType (see the+ // downloadMediaMessage doc comment above) — not just that `media` happens to end up unset.+ expect(downloadMediaMessage).not.toHaveBeenCalled();});it('maps a static location, carrying name and address', async () => {- const events = new BaileysEvents(makeHost());-const incoming = await events.mapMessage({key: { id: 'wamid.2', remoteJid: '[email protected]', fromMe: false },@@ -86,11 +107,10 @@ describe('BaileysEvents.mapMessage', () => {description: 'Monas',address: 'Jakarta Pusat',});+ expect(downloadMediaMessage).not.toHaveBeenCalled();});it('maps a LIVE location without name/address (only the static variant carries them)', async () => {- const events = new BaileysEvents(makeHost());-const incoming = await events.mapMessage({key: { id: 'wamid.3', remoteJid: '[email protected]', fromMe: false },@@ -104,11 +124,10 @@ describe('BaileysEvents.mapMessage', () => {expect(incoming.location?.longitude).toBe(2.5);expect(incoming.location?.description).toBeUndefined();expect(incoming.location?.address).toBeUndefined();+ expect(downloadMediaMessage).not.toHaveBeenCalled();});it('emits an omitted media marker when the download is skipped, keeping mimetype and size', async () => {- const events = new BaileysEvents(makeHost());-const incoming = await events.mapMessage({key: { id: 'wamid.4', remoteJid: '[email protected]', fromMe: true },@@ -129,8 +148,6 @@ describe('BaileysEvents.mapMessage', () => {});it('carries the document filename onto the omitted marker', async () => {- const events = new BaileysEvents(makeHost());-const incoming = await events.mapMessage({key: { id: 'wamid.5', remoteJid: '[email protected]', fromMe: true },@@ -146,8 +163,6 @@ describe('BaileysEvents.mapMessage', () => {});it('resolves a quoted message from contextInfo', async () => {- const events = new BaileysEvents(makeHost());-const incoming = await events.mapMessage({key: { id: 'wamid.6', remoteJid: '[email protected]', fromMe: false },@@ -164,11 +179,10 @@ describe('BaileysEvents.mapMessage', () => {expect(incoming.quotedMessage?.id).toBe('wamid.original');expect(incoming.quotedMessage?.body).toBe('the original');+ expect(downloadMediaMessage).not.toHaveBeenCalled();});it('leaves quotedMessage undefined when contextInfo has no stanzaId', async () => {- const events = new BaileysEvents(makeHost());-const incoming = await events.mapMessage({key: { id: 'wamid.7', remoteJid: '[email protected]', fromMe: false },@@ -181,5 +195,6 @@ describe('BaileysEvents.mapMessage', () => {);expect(incoming.quotedMessage).toBeUndefined();+ expect(downloadMediaMessage).not.toHaveBeenCalled();});});