import assert from 'node:assert/strict' import test from 'node:test' import { canonicalWhatsAppJid, isWeakWhatsAppDisplayName, reusableWhatsAppConversations, whatsappCustomerStableId, } from '../src/whatsapp-identity.js' test('WhatsApp LID resolves to the same canonical phone identity', () => { const phoneJid = '15551234567@s.whatsapp.net' assert.equal(canonicalWhatsAppJid('987654321@lid', { phone: '+1 555 123 4567' }), phoneJid) assert.equal(canonicalWhatsAppJid('987654321@lid', { canonicalJid: phoneJid }), phoneJid) assert.equal(whatsappCustomerStableId(canonicalWhatsAppJid('987654321@lid', { phone: '+1 555 123 4567' })), whatsappCustomerStableId(phoneJid)) }) test('WhatsApp customer id remains stable after gateway reauthorization', () => { assert.equal( whatsappCustomerStableId('15551234567@s.whatsapp.net'), whatsappCustomerStableId('15551234567@s.whatsapp.net'), ) assert.match(whatsappCustomerStableId('15551234567@s.whatsapp.net'), /^wa-customer-[a-f0-9]{24}$/) }) test('the latest inactive conversation is reused while another active account stays isolated', () => { const jid = '15551234567@s.whatsapp.net' const conversations = [ { id: 'old:newest', channel: 'whatsapp', channelId: 'old', whatsappJid: jid, customerId: 'abigail', lastMessage: { createdAt: '2026-07-31T07:51:37.000Z' } }, { id: 'older:history', channel: 'whatsapp', channelId: 'older', whatsappJid: jid, customerId: 'legacy', lastMessage: { createdAt: '2026-07-29T17:25:42.000Z' } }, { id: 'other:active', channel: 'whatsapp', channelId: 'other', whatsappJid: jid, customerId: 'other-account', lastMessage: { createdAt: '2026-08-01T00:00:00.000Z' } }, ] const result = reusableWhatsAppConversations(conversations, new Set(['current', 'other']), 'current', jid) assert.deepEqual(result.map((item) => item.customerId), ['abigail', 'legacy']) }) test('a saved contact name is not replaced by a phone-number fallback', () => { const jid = '15551234567@s.whatsapp.net' assert.equal(isWeakWhatsAppDisplayName('Abigail', jid), false) assert.equal(isWeakWhatsAppDisplayName('+1 555 123 4567', jid), true) })