From 1c17ecf2ec7e3dbf6c46c98f3ffc7e261738e49c Mon Sep 17 00:00:00 2001 From: Renato Alcara Date: Wed, 21 Jan 2026 01:07:03 -0300 Subject: [PATCH] test: cover lid batch and jid normalization --- src/__tests__/Signal/lid-mapping.test.ts | 38 +++++++++++++++ src/__tests__/Utils/process-message.test.ts | 51 ++++++++++++++++++++- 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/src/__tests__/Signal/lid-mapping.test.ts b/src/__tests__/Signal/lid-mapping.test.ts index 68e49e9d..762829af 100644 --- a/src/__tests__/Signal/lid-mapping.test.ts +++ b/src/__tests__/Signal/lid-mapping.test.ts @@ -44,4 +44,42 @@ describe('LIDMappingStore', () => { expect(result).toBeNull() }) }) + + describe('getLIDsForPNs', () => { + it('should resolve multiple PNs in a single batch', async () => { + const pnOne = '11111@s.whatsapp.net' + const pnTwo = '22222:5@s.whatsapp.net' + + // @ts-ignore + mockKeys.get.mockResolvedValue({ '11111': 'aaaaa', '22222': 'bbbbb' } as SignalDataTypeMap['lid-mapping']) + + const result = await lidMappingStore.getLIDsForPNs([pnOne, pnTwo]) + + expect(result).toEqual( + expect.arrayContaining([ + { pn: pnOne, lid: 'aaaaa@lid' }, + { pn: pnTwo, lid: 'bbbbb:5@lid' } + ]) + ) + }) + }) + + describe('getPNsForLIDs', () => { + it('should resolve multiple LIDs in a single batch', async () => { + const lidOne = '33333@lid' + const lidTwo = '44444:99@hosted.lid' + + // @ts-ignore + mockKeys.get.mockResolvedValue({ '33333_reverse': '77777', '44444_reverse': '88888' } as SignalDataTypeMap['lid-mapping']) + + const result = await lidMappingStore.getPNsForLIDs([lidOne, lidTwo]) + + expect(result).toEqual( + expect.arrayContaining([ + { lid: lidOne, pn: '77777@s.whatsapp.net' }, + { lid: lidTwo, pn: '88888:99@hosted' } + ]) + ) + }) + }) }) diff --git a/src/__tests__/Utils/process-message.test.ts b/src/__tests__/Utils/process-message.test.ts index 12d5ea76..e4922dfb 100644 --- a/src/__tests__/Utils/process-message.test.ts +++ b/src/__tests__/Utils/process-message.test.ts @@ -1,5 +1,6 @@ import type { WAMessage } from '../../Types' -import { cleanMessage } from '../../Utils/process-message' +import type { SignalRepositoryWithLIDStore } from '../../Types' +import { cleanMessage, normalizeMessageJids } from '../../Utils/process-message' const createBaseMessage = (key: Partial, message?: Partial): WAMessage => { return { @@ -136,3 +137,51 @@ describe('cleanMessage', () => { }) }) }) + +describe('normalizeMessageJids', () => { + const signalRepository = { + lidMapping: { + getPNForLID: jest.fn() + } + } as unknown as SignalRepositoryWithLIDStore + + beforeEach(() => { + jest.clearAllMocks() + }) + + it('should resolve remoteJid LID to PN when mapping exists', async () => { + const message = createBaseMessage({ + remoteJid: '123456789@lid' + }) + + signalRepository.lidMapping.getPNForLID = jest.fn().mockResolvedValue('5511999999999@s.whatsapp.net') + + await normalizeMessageJids(message, signalRepository) + + expect(message.key.remoteJid).toBe('5511999999999@s.whatsapp.net') + }) + + it('should keep LID when no PN mapping exists', async () => { + const message = createBaseMessage({ + remoteJid: '123456789@lid' + }) + + signalRepository.lidMapping.getPNForLID = jest.fn().mockResolvedValue(null) + + await normalizeMessageJids(message, signalRepository) + + expect(message.key.remoteJid).toBe('123456789@lid') + }) + + it('should resolve participant LID to PN when mapping exists', async () => { + const message = createBaseMessage({ + participant: '999999999@lid' + }) + + signalRepository.lidMapping.getPNForLID = jest.fn().mockResolvedValue('5511888888888@s.whatsapp.net') + + await normalizeMessageJids(message, signalRepository) + + expect(message.key.participant).toBe('5511888888888@s.whatsapp.net') + }) +})