Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
test(identity): mark reported mapping fixtures
Yudhi Armyndharis56661bab6c56
diff --git a/src/modules/integration/conversation-mapping.service.spec.ts b/src/modules/integration/conversation-mapping.service.spec.tsindex 9548f9091..fc0fa798d 100644--- a/src/modules/integration/conversation-mapping.service.spec.ts+++ b/src/modules/integration/conversation-mapping.service.spec.ts@@ -97,7 +97,7 @@ describe('ConversationMappingService', () => {// lid 111 -> phone 628999. A 1:1 chat's neutral id flips from `111@lid` to `[email protected]` once the// mapping is learned, so a takeover recorded under either form has to govern the other. The store is// real but its cache is empty, as for a mapping past the preload cap or evicted: only the table has it.- const table = [{ lid: '111', phone: '628999' }];+ const table = [{ lid: '111', phone: '628999', isReported: true }];const lidStore = new LidMappingStoreService({find: ({ where }: { where: { lid?: FindOperator<string>; phone?: FindOperator<string> } }) =>Promise.resolve(table.filter(r => inList(r.lid, where.lid) && inList(r.phone, where.phone))),@@ -135,6 +135,20 @@ describe('ConversationMappingService', () => {expect(await service.findHandoverForChat('sess-1', '[email protected]')).toBeNull();});+ it('does not share takeover decisions through an unreported phone alias', async () => {+ table[0].isReported = false;+ try {+ await human('111@lid');+ expect(await service.findHandoverForChat('sess-1', '[email protected]')).toBeNull();+ await bot('[email protected]', 'convC');+ expect((await row('[email protected]'))!.handoverState).toBe('bot');+ await service.setHandover((await row('[email protected]'))!.id, 'bot');+ expect((await row('111@lid'))!.handoverState).toBe('human');+ } finally {+ table[0].isReported = true;+ }+ });+const row = (chatId: string) =>service.get({ sessionId: 'sess-1', chatId, pluginId: 'chatwoot-adapter', instanceId: 'i2' });@@ -174,7 +188,7 @@ describe('ConversationMappingService', () => {it('a hand-back reaches a row whose form was paired after the takeover', async () => {await human('111@lid');await bot('[email protected]', 'convC'); // inherits the takeover- table.push({ lid: '333', phone: '628999' }); // another lid for the number is learned meanwhile+ table.push({ lid: '333', phone: '628999', isReported: true }); // another lid for the number is learned meanwhiletry {await service.setHandover((await row('[email protected]'))!.id, 'bot');expect((await row('111@lid'))!.handoverState).toBe('bot');@@ -185,7 +199,7 @@ describe('ConversationMappingService', () => {});it('a decision on one lid of a number reaches its sibling lid and the phone-form row', async () => {- table.push({ lid: '222', phone: '628999' });+ table.push({ lid: '222', phone: '628999', isReported: true });try {await bot('111@lid', 'convB');await bot('222@lid', 'convD');diff --git a/src/modules/message/message.service.spec.ts b/src/modules/message/message.service.spec.tsindex 9b1f1139d..581517798 100644--- a/src/modules/message/message.service.spec.ts+++ b/src/modules/message/message.service.spec.ts@@ -393,33 +393,36 @@ describe('MessageService', () => {expect(messages.map(m => m.id).sort()).toEqual(['m-dm', 'm-lid']);});- it('finds the lid-authored message through a real store whose cache no longer holds the lid', async () => {- // The mapping is only in the table: past the preload cap or evicted by the LRU.- const table = [{ lid: '111', phone: '628999' }];- const store = new LidMappingStoreService({- find: ({ where }: { where: { lid?: FindOperator<string>; phone?: FindOperator<string> } }) =>- Promise.resolve(table.filter(r => inList(r.lid, where.lid) && inList(r.phone, where.phone))),- } as unknown as Repository<LidMapping>);- const qb = makeFilteringQb();- (repository.createQueryBuilder as jest.Mock).mockReturnValue(qb);- const withStore = new MessageService(- repository as Repository<Message>,- engines,- messageProjector as unknown as MessageProjector,- hookManager as HookManager,- store,- inertPacing(),- {} as MessageSendService,- );+ it.each([true, false])(+ 'uses table origin for a phone filter with an empty cache (reported=%s)',+ async isReported => {+ // The mapping is only in the table: past the preload cap or evicted by the LRU.+ const table = [{ lid: '111', phone: '628999', isReported }];+ const store = new LidMappingStoreService({+ find: ({ where }: { where: { lid?: FindOperator<string>; phone?: FindOperator<string> } }) =>+ Promise.resolve(table.filter(r => inList(r.lid, where.lid) && inList(r.phone, where.phone))),+ } as unknown as Repository<LidMapping>);+ const qb = makeFilteringQb();+ (repository.createQueryBuilder as jest.Mock).mockReturnValue(qb);+ const withStore = new MessageService(+ repository as Repository<Message>,+ engines,+ messageProjector as unknown as MessageProjector,+ hookManager as HookManager,+ store,+ inertPacing(),+ {} as MessageSendService,+ );- const { messages } = await withStore.getMessages('sess-1', { from: '628999' });+ const { messages } = await withStore.getMessages('sess-1', { from: '628999' });- expect(store.lidsForPhone('628999')).toEqual([]);- expect(messages.map(m => m.id).sort()).toEqual(['m-dm', 'm-lid']);- });+ expect(store.lidsForPhone('628999')).toEqual([]);+ expect(messages.map(m => m.id).sort()).toEqual(isReported ? ['m-dm', 'm-lid'] : ['m-dm']);+ },+ );- it('finds the phone-form message for a @lid filter through a real store whose cache lacks the lid', async () => {- const table = [{ lid: '111', phone: '628999' }];+ it.each([true, false])('uses table origin for a LID filter with an empty cache (reported=%s)', async isReported => {+ const table = [{ lid: '111', phone: '628999', isReported }];const store = new LidMappingStoreService({findOne: ({ where }: { where: { lid: string } }) =>Promise.resolve(table.find(r => r.lid === where.lid) ?? null),@@ -438,7 +441,7 @@ describe('MessageService', () => {const { messages } = await withStore.getMessages('sess-1', { from: '111@lid' });- expect(messages.map(m => m.id).sort()).toEqual(['m-dm', 'm-lid']);+ expect(messages.map(m => m.id).sort()).toEqual(isReported ? ['m-dm', 'm-lid'] : ['m-lid']);});it('misses the lid-authored message when the table has no mapping (the prior silent miss)', async () => {@@ -572,7 +575,7 @@ describe('MessageService', () => {// This node cached lid 111 -> 628999; another node has since re-mapped it to 628777 in the// shared table. The chat fence reads the table, so history must expand to the same phone or a// key allowed only 111@lid would read chat 628999.- const table = [{ lid: '111', phone: '628999' }];+ const table = [{ lid: '111', phone: '628999', isReported: true }];const store = new LidMappingStoreService({find: ({ where }: { where: { lid?: FindOperator<string>; phone?: FindOperator<string> } }) =>Promise.resolve(table.filter(r => inList(r.lid, where.lid) && inList(r.phone, where.phone))),diff --git a/src/modules/status-store/status-store.service.spec.ts b/src/modules/status-store/status-store.service.spec.tsindex da67981f0..e4a3ce764 100644--- a/src/modules/status-store/status-store.service.spec.ts+++ b/src/modules/status-store/status-store.service.spec.ts@@ -591,8 +591,8 @@ describe('StatusStoreService contact identity (read-time lid resolution)', () =>expect(ids).toEqual(['own-lid', 'own-phone']);});- it('listByContact forward-resolves a lid the store only holds in its table, not its cache', async () => {- const table = [{ lid: '111', phone: '628111' }];+ it.each([true, false])('listByContact checks table origin with an empty cache (reported=%s)', async isReported => {+ const table = [{ lid: '111', phone: '628111', isReported }];const store = new LidMappingStoreService({find: () => Promise.resolve([]),findOne: ({ where }: { where: { lid: string } }) => Promise.resolve(table.find(r => r.lid === where.lid) ?? null),@@ -601,7 +601,7 @@ describe('StatusStoreService contact identity (read-time lid resolution)', () =>await svc.ingest('sess', { waStatusId: 'l6', contactJid: '[email protected]', type: 'text', postedAt: Date.now() });const out = await svc.listByContact('sess', '111@lid');- expect(out).toHaveLength(1);+ expect(out).toHaveLength(isReported ? 1 : 0);});it('never resolves phone-shaped JIDs through the lid map (digit-collision guard)', async () => {