Files
InfiniteAPI/src/__tests__/Signal/lid-mapping.test.ts
T
João Lucas de Oliveira Lopes 7254917c53 test: add unit tests for LIDMappingStore and message processing utilities (#1949)
* test: add unit tests for LIDMappingStore and message processing utilities

* Update src/__tests__/Signal/lid-mapping.test.ts

Co-authored-by: Rajeh Taher <rajeh@reforward.dev>

* Update src/__tests__/Utils/process-message.test.ts

Co-authored-by: Rajeh Taher <rajeh@reforward.dev>

* Update src/__tests__/Utils/process-message.test.ts

Co-authored-by: Rajeh Taher <rajeh@reforward.dev>

---------

Co-authored-by: Rajeh Taher <rajeh@reforward.dev>
2025-11-19 15:26:34 +02:00

48 lines
1.7 KiB
TypeScript

import { jest } from '@jest/globals'
import P from 'pino'
import { LIDMappingStore } from '../../Signal/lid-mapping'
import type { LIDMapping, SignalDataTypeMap, SignalKeyStoreWithTransaction } from '../../Types'
const HOSTED_DEVICE_ID = 99
const mockKeys: jest.Mocked<SignalKeyStoreWithTransaction> = {
get: jest.fn<SignalKeyStoreWithTransaction['get']>() as any,
set: jest.fn<SignalKeyStoreWithTransaction['set']>(),
transaction: jest.fn<SignalKeyStoreWithTransaction['transaction']>(async (work: () => any) => await work()) as any,
isInTransaction: jest.fn<SignalKeyStoreWithTransaction['isInTransaction']>()
}
const logger = P({ level: 'silent' })
describe('LIDMappingStore', () => {
const mockPnToLIDFunc = jest.fn<(jids: string[]) => Promise<LIDMapping[] | undefined>>()
let lidMappingStore: LIDMappingStore
beforeEach(() => {
jest.clearAllMocks()
lidMappingStore = new LIDMappingStore(mockKeys, logger, mockPnToLIDFunc)
})
describe('getPNForLID', () => {
it('should correctly map a standard LID with a hosted device ID back to a standard PN with a hosted device', async () => {
const lidWithHostedDevice = `12345:${HOSTED_DEVICE_ID}@lid`
const pnUser = '54321'
// @ts-ignore
mockKeys.get.mockResolvedValue({ [`12345_reverse`]: pnUser } as SignalDataTypeMap['lid-mapping'])
const result = await lidMappingStore.getPNForLID(lidWithHostedDevice)
expect(result).toBe(`${pnUser}:${HOSTED_DEVICE_ID}@s.whatsapp.net`)
})
it('should return null if no reverse mapping is found', async () => {
const lid = 'nonexistent@lid'
// @ts-ignore
mockKeys.get.mockResolvedValue({} as SignalDataTypeMap['lid-mapping']) // Simulate not found in DB
const result = await lidMappingStore.getPNForLID(lid)
expect(result).toBeNull()
})
})
})