From 3309385348cda357211fc23bf937f99ca736c569 Mon Sep 17 00:00:00 2001 From: "borja (aider)" Date: Mon, 5 May 2025 15:06:42 +0200 Subject: [PATCH] test: add unit tests for whatsapp utils --- tests/unit/utils/whatsapp.test.ts | 115 ++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 tests/unit/utils/whatsapp.test.ts diff --git a/tests/unit/utils/whatsapp.test.ts b/tests/unit/utils/whatsapp.test.ts new file mode 100644 index 0000000..083b627 --- /dev/null +++ b/tests/unit/utils/whatsapp.test.ts @@ -0,0 +1,115 @@ +import { describe, test, expect } from 'bun:test'; +import { normalizeWhatsAppId, isGroupId, isUserJid } from '../../../src/utils/whatsapp'; + +describe('WhatsApp Utilities', () => { + + describe('normalizeWhatsAppId', () => { + test('should normalize standard user JID', () => { + expect(normalizeWhatsAppId('1234567890@s.whatsapp.net')).toBe('1234567890'); + }); + + test('should normalize group JID', () => { + expect(normalizeWhatsAppId('1234567890-1234567890@g.us')).toBe('1234567890-1234567890'); + }); + + test('should normalize user JID with participant ID', () => { + expect(normalizeWhatsAppId('1234567890:12@s.whatsapp.net')).toBe('1234567890'); + }); + + test('should normalize group JID with participant ID (less common but possible)', () => { + expect(normalizeWhatsAppId('1234567890-9876543210:5@g.us')).toBe('1234567890-9876543210'); + }); + + test('should normalize status broadcast JID', () => { + expect(normalizeWhatsAppId('status_me@broadcast')).toBe('status_me'); + }); + + test('should handle JID without domain', () => { + expect(normalizeWhatsAppId('1234567890')).toBe('1234567890'); + }); + + test('should handle group JID without domain', () => { + expect(normalizeWhatsAppId('1234567890-9876543210')).toBe('1234567890-9876543210'); + }); + + test('should return null for null input', () => { + expect(normalizeWhatsAppId(null)).toBeNull(); + }); + + test('should return null for undefined input', () => { + expect(normalizeWhatsAppId(undefined)).toBeNull(); + }); + + test('should return null for empty string input', () => { + expect(normalizeWhatsAppId('')).toBeNull(); + }); + + test('should return null for invalid characters after normalization', () => { + // Example: JID containing characters not allowed after stripping domain/participant + expect(normalizeWhatsAppId('invalid!char@s.whatsapp.net')).toBeNull(); + expect(normalizeWhatsAppId('123 456@s.whatsapp.net')).toBeNull(); // Contains space + }); + + test('should return null for JID that becomes empty after normalization', () => { + expect(normalizeWhatsAppId('@s.whatsapp.net')).toBeNull(); + expect(normalizeWhatsAppId(':12@s.whatsapp.net')).toBeNull(); + }); + }); + + describe('isGroupId', () => { + test('should return true for valid group JID', () => { + expect(isGroupId('1234567890-1234567890@g.us')).toBe(true); + }); + + test('should return false for user JID', () => { + expect(isGroupId('1234567890@s.whatsapp.net')).toBe(false); + }); + + test('should return false for group JID without domain', () => { + expect(isGroupId('1234567890-1234567890')).toBe(false); + }); + + test('should return false for null input', () => { + expect(isGroupId(null)).toBe(false); + }); + + test('should return false for undefined input', () => { + expect(isGroupId(undefined)).toBe(false); + }); + + test('should return false for empty string input', () => { + expect(isGroupId('')).toBe(false); + }); + }); + + describe('isUserJid', () => { + test('should return true for valid user JID', () => { + expect(isUserJid('1234567890@s.whatsapp.net')).toBe(true); + }); + + test('should return true for user JID with participant', () => { + expect(isUserJid('1234567890:15@s.whatsapp.net')).toBe(true); + }); + + test('should return false for group JID', () => { + expect(isUserJid('1234567890-1234567890@g.us')).toBe(false); + }); + + test('should return false for user JID without domain', () => { + expect(isUserJid('1234567890')).toBe(false); + }); + + test('should return false for null input', () => { + expect(isUserJid(null)).toBe(false); + }); + + test('should return false for undefined input', () => { + expect(isUserJid(undefined)).toBe(false); + }); + + test('should return false for empty string input', () => { + expect(isUserJid('')).toBe(false); + }); + }); + +});