Hide WhatsApp LID ids from session-header peer labels (4.9.1-ops.21).

Resolve nickname/phone via contact LID mapping and show only group title, pushName, and phone.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
oliver 2026-09-05 19:14:38 +08:00
parent 37742043a9
commit a92080e490
5 changed files with 331 additions and 215 deletions

View file

@ -5,8 +5,11 @@ import { join } from 'node:path';
import { test } from 'node:test';
import {
formatChannelPeerLabel,
lidFromWhatsappIdentity,
parseConversationKey,
resolveChannelPeerFromBinding,
resolveContactIdentity,
} from '../../../src/channels/shared/channel-peer.mjs';
import { ConversationStateStore } from '../../../src/channels/shared/conversation-state-store.mjs';
@ -32,7 +35,11 @@ test('parseConversationKey covers direct, shared group, and per-user group', ()
test('resolveChannelPeerFromBinding builds DM and group labels from access grant', () => {
const grant = {
contacts: [{ phone: '8618222222222', pushName: 'Alice' }],
contacts: [{
phone: '8618222222222',
lids: ['91010910658657@lid'],
pushName: 'Alice',
}],
groups: {
'120363@g.us': { title: 'Ops Room', admins: [], members: [] },
},
@ -47,14 +54,25 @@ test('resolveChannelPeerFromBinding builds DM and group labels from access grant
assert.equal(direct.kind, 'direct');
assert.equal(direct.phone, '8618222222222');
const lidDirect = resolveChannelPeerFromBinding({
channel: 'whatsapp',
botId: 'bot-1',
conversationKey: 'direct:91010910658657@lid',
grant,
});
assert.equal(lidDirect.label, 'Alice · 8618222222222');
assert.equal(lidDirect.phone, '8618222222222');
assert.doesNotMatch(lidDirect.label, /@lid/);
const groupUser = resolveChannelPeerFromBinding({
channel: 'whatsapp',
botId: 'bot-1',
conversationKey: 'group:120363@g.us:user:8618222222222@s.whatsapp.net',
conversationKey: 'group:120363@g.us:user:91010910658657@lid',
grant,
});
assert.equal(groupUser.label, 'Ops Room · Alice');
assert.equal(groupUser.label, 'Ops Room · Alice · 8618222222222');
assert.equal(groupUser.groupTitle, 'Ops Room');
assert.doesNotMatch(groupUser.label, /@lid/);
const groupShared = resolveChannelPeerFromBinding({
channel: 'whatsapp',
@ -62,7 +80,54 @@ test('resolveChannelPeerFromBinding builds DM and group labels from access grant
conversationKey: 'group:120363@g.us',
grant,
});
assert.equal(groupShared.label, '群 · Ops Room');
assert.equal(groupShared.label, 'Ops Room');
});
test('resolveChannelPeerFromBinding never falls back to raw LID when contact is incomplete', () => {
const grant = {
contacts: [{ lids: ['91010910658657@lid'], pushName: 'Bob' }],
groups: {
'120363@g.us': { title: 'test2', admins: [], members: [] },
},
};
const direct = resolveChannelPeerFromBinding({
channel: 'whatsapp',
botId: 'bot-1',
conversationKey: 'direct:91010910658657@lid',
grant,
});
assert.equal(direct.label, 'Bob');
assert.equal(direct.phone, null);
const unknown = resolveChannelPeerFromBinding({
channel: 'whatsapp',
botId: 'bot-1',
conversationKey: 'direct:111222333444@lid',
grant,
});
assert.equal(unknown, null);
const groupOnlyLid = resolveChannelPeerFromBinding({
channel: 'whatsapp',
botId: 'bot-1',
conversationKey: 'group:120363@g.us:user:111222333444@lid',
grant,
});
assert.equal(groupOnlyLid.label, 'test2');
assert.doesNotMatch(groupOnlyLid.label, /@lid|\d{10,}@/);
});
test('lid and label helpers stay human-facing', () => {
assert.equal(lidFromWhatsappIdentity('91010910658657@lid'), '91010910658657@lid');
assert.equal(lidFromWhatsappIdentity('91010910658657:12@lid'), '91010910658657@lid');
assert.equal(lidFromWhatsappIdentity('8618222222222@s.whatsapp.net'), null);
assert.equal(formatChannelPeerLabel('Ops', '', '8618', null), 'Ops · 8618');
assert.deepEqual(
resolveContactIdentity({
contacts: [{ phone: '8618', lids: ['9@lid'], pushName: 'N' }],
}, '9@lid'),
{ phone: '8618', pushName: 'N' },
);
});
test('ConversationStateStore.keyForSession reverses session bindings', async () => {