test: add unit tests for whatsapp utils
parent
be653f0355
commit
3309385348
@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
Loading…
Reference in New Issue