|
| 1 | +import { UnprocessableEntityException } from '@nestjs/common'; |
| 2 | +import { ArgentinePhoneNumberRules } from '@waha/apps/argentine-phone-numbers/rules/ArgentinePhoneNumberRules'; |
| 3 | +import { PhoneNumbersBaseConfig } from '@waha/apps/phone-numbers/dto/config.dto'; |
| 4 | +import { |
| 5 | + buildPlugin as buildPhoneNumbersPlugin, |
| 6 | + resolveChat, |
| 7 | + stubLookup, |
| 8 | +} from '@waha/apps/phone-numbers/plugins/testing'; |
| 9 | + |
| 10 | +function buildPlugin(config: Partial<PhoneNumbersBaseConfig> = {}) { |
| 11 | + return buildPhoneNumbersPlugin({ |
| 12 | + config: config, |
| 13 | + rules: ArgentinePhoneNumberRules(), |
| 14 | + }); |
| 15 | +} |
| 16 | + |
| 17 | +describe('ArgentinePhoneNumbers', () => { |
| 18 | + it('uses the supplied form when it exists, without checking the other one', async () => { |
| 19 | + const { session } = buildPlugin(); |
| 20 | + stubLookup(session, { numberExists: true, chatId: '541112345678@c.us' }); |
| 21 | + |
| 22 | + const resolved = await resolveChat(session, '+541112345678'); |
| 23 | + |
| 24 | + expect(resolved).toBe('541112345678@c.us'); |
| 25 | + expect(session.checkNumberStatus).toHaveBeenCalledTimes(1); |
| 26 | + expect(session.checkNumberStatus).toHaveBeenCalledWith({ |
| 27 | + phone: '541112345678', |
| 28 | + session: 'test', |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + it('tries the form with 9 after the supplied one is absent, keeps a LID answer', async () => { |
| 33 | + const { session } = buildPlugin(); |
| 34 | + session.checkNumberStatus = jest |
| 35 | + .fn() |
| 36 | + .mockResolvedValueOnce({ numberExists: false }) |
| 37 | + .mockResolvedValueOnce({ numberExists: true, chatId: '123456@lid' }); |
| 38 | + |
| 39 | + const resolved = await resolveChat(session, '541112345678@c.us'); |
| 40 | + |
| 41 | + expect(resolved).toBe('123456@lid'); |
| 42 | + expect(session.checkNumberStatus).toHaveBeenLastCalledWith({ |
| 43 | + phone: '5491112345678', |
| 44 | + session: 'test', |
| 45 | + }); |
| 46 | + // Both forms hit the cache now |
| 47 | + expect(await resolveChat(session, '5491112345678@c.us')).toBe('123456@lid'); |
| 48 | + expect(session.checkNumberStatus).toHaveBeenCalledTimes(2); |
| 49 | + }); |
| 50 | + |
| 51 | + it('tries the form without 9 as the fallback', async () => { |
| 52 | + const { session } = buildPlugin(); |
| 53 | + session.checkNumberStatus = jest |
| 54 | + .fn() |
| 55 | + .mockResolvedValueOnce({ numberExists: false }) |
| 56 | + .mockResolvedValueOnce({ |
| 57 | + numberExists: true, |
| 58 | + chatId: '542920123456@c.us', |
| 59 | + }); |
| 60 | + |
| 61 | + const resolved = await resolveChat(session, '5492920123456@c.us'); |
| 62 | + |
| 63 | + expect(resolved).toBe('542920123456@c.us'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('prefers the pn when the engine answers with both pn and a LID', async () => { |
| 67 | + const { session } = buildPlugin(); |
| 68 | + stubLookup(session, { |
| 69 | + numberExists: true, |
| 70 | + chatId: '123@lid', |
| 71 | + pn: '5491112345678@c.us', |
| 72 | + }); |
| 73 | + |
| 74 | + expect(await resolveChat(session, '541112345678@c.us')).toBe( |
| 75 | + '5491112345678@c.us', |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + it('soft mode keeps the supplied form when both are absent, strict rejects', async () => { |
| 80 | + const soft = buildPlugin(); |
| 81 | + stubLookup(soft.session, { numberExists: false }); |
| 82 | + expect(await resolveChat(soft.session, '541112345678@c.us')).toBe( |
| 83 | + '541112345678@c.us', |
| 84 | + ); |
| 85 | + expect(soft.session.checkNumberStatus).toHaveBeenCalledTimes(2); |
| 86 | + |
| 87 | + const strict = buildPlugin({ strict: true }); |
| 88 | + stubLookup(strict.session, { numberExists: false }); |
| 89 | + await expect( |
| 90 | + resolveChat(strict.session, '541112345678@c.us'), |
| 91 | + ).rejects.toThrow(UnprocessableEntityException); |
| 92 | + }); |
| 93 | + |
| 94 | + it('leaves local forms and other countries untouched', async () => { |
| 95 | + const { session } = buildPlugin(); |
| 96 | + stubLookup(session, { numberExists: false }); |
| 97 | + |
| 98 | + expect(await resolveChat(session, '1112345678@c.us')).toBe( |
| 99 | + '1112345678@c.us', |
| 100 | + ); |
| 101 | + expect(await resolveChat(session, '5511912345678@c.us')).toBe( |
| 102 | + '5511912345678@c.us', |
| 103 | + ); |
| 104 | + expect(session.checkNumberStatus).not.toHaveBeenCalled(); |
| 105 | + }); |
| 106 | +}); |
0 commit comments