Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
test(baileys): pin the send to the id the number check reported
Yudhi Armyndharis7a985adec17f
diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.tsindex 43feab62f..0e65cecc7 100644--- a/src/engine/adapters/baileys.adapter.spec.ts+++ b/src/engine/adapters/baileys.adapter.spec.ts@@ -1998,6 +1998,25 @@ describe('BaileysAdapter messaging', () => {);});+ // Brazil, Mexico and Argentina numbers exist on WhatsApp under another digit string than the one typed (the+ // ninth digit). The number check reports the id WhatsApp knows, and the send goes there, not to the typed one.+ it.each([+ ['the phone jid', null, '[email protected]'],+ ['the lid known for it', '484848@lid', '484848@lid'],+ ])('sendTextMessage addresses a first-contact send to the id the number check reported: %s', async (_, lid, to) => {+ fakeSock.sendMessage.mockResolvedValue({ key: { id: 'OUT1' }, messageTimestamp: 1700000001 });+ fakeSock.onWhatsApp.mockResolvedValue([{ jid: '[email protected]', exists: true }]);+ const getLIDForPN = jest.fn((pn: string) => Promise.resolve(pn === '[email protected]' ? lid : null));+ fakeSock.signalRepository = { lidMapping: { getLIDForPN } };+ const adapter = await readyAdapter();++ await adapter.sendTextMessage('[email protected]', 'hello');++ expect(fakeSock.onWhatsApp).toHaveBeenCalledWith('[email protected]');+ expect(getLIDForPN).toHaveBeenLastCalledWith('[email protected]');+ expect(fakeSock.sendMessage).toHaveBeenCalledWith(to, { text: 'hello', linkPreview: null }, safeSendOptions());+ });+it('sendTextMessage honors the chat disappearing timer when one is cached (#473)', async () => {fakeSock.sendMessage.mockResolvedValue({ key: { id: 'OUT1' }, messageTimestamp: 1700000001 });const adapter = await readyAdapter();diff --git a/test/engine-real/baileys-outbound.spec.mjs b/test/engine-real/baileys-outbound.spec.mjsindex 9a3af9f4b..3c1689ec7 100644--- a/test/engine-real/baileys-outbound.spec.mjs+++ b/test/engine-real/baileys-outbound.spec.mjs@@ -63,6 +63,22 @@ test('an unmapped neutral phone destination uses the Baileys domain', async t =>assert.equal(sock.onWhatsApp.mock.callCount(), 1);});+test('a first-contact send goes to the id the number check reported, not the one typed', async t => {+ // The ninth digit of a Brazilian number: WhatsApp knows it under another digit string than people type.+ const ASKED = '[email protected]';+ const CANON = '[email protected]';+ for (const lid of [null, LID]) {+ const { messaging, sock } = build(t);+ sock.onWhatsApp = t.mock.fn(() => Promise.resolve([{ jid: CANON, exists: true }]));+ const getLIDForPN = t.mock.fn(pn => Promise.resolve(pn === CANON ? lid : null));+ sock.signalRepository.lidMapping.getLIDForPN = getLIDForPN;+ await messaging.sendTextMessage(ASKED.replace('@s.whatsapp.net', '@c.us'), 'hello');+ assert.equal(sock.onWhatsApp.mock.calls[0].arguments[0], ASKED);+ assert.equal(getLIDForPN.mock.calls.at(-1).arguments[0], CANON);+ assert.equal(sock.sendMessage.mock.calls[0].arguments[0], lid ?? CANON);+ }+});+test('cold sends resolve through the real Baileys LID store', async t => {for (const pairs of [[], [{ pn: PN, lid: LID }]]) {const data = {};