Files
lingxi-web/server/test/whatsapp-identity.test.js
T

52 lines
2.7 KiB
JavaScript

import assert from 'node:assert/strict'
import test from 'node:test'
import {
canonicalWhatsAppJid,
isWeakWhatsAppDisplayName,
resolvedWhatsAppJid,
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))
assert.equal(canonicalWhatsAppJid('15551234567:4@s.whatsapp.net'), phoneJid)
assert.equal(canonicalWhatsAppJid('987654321@lid', { canonicalJid: '15551234567:0@s.whatsapp.net' }), phoneJid)
})
test('a newly resolved phone identity overrides a stale cached LID', () => {
const lid = '987654321@lid'
const phoneJid = '15551234567@s.whatsapp.net'
assert.equal(resolvedWhatsAppJid(lid, {}, lid), lid)
assert.equal(resolvedWhatsAppJid(lid, { phone: '+1 555 123 4567' }, lid), phoneJid)
assert.equal(resolvedWhatsAppJid(lid, {}, phoneJid), 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)
})