Files
client/test/contacts-model.test.mjs
T

130 lines
7.8 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {
PRESENCE, MAX_CONTACTS, withContact, withoutContact, withAlias, findContact, highlightsContact, sessionsFor, presenceOf,
displayName, buildEntries, sortEntries, arrangeEntries, chooseSession, cleanText,
} from '../app/renderer/js/contacts-model.js';
import { parseClientBadges, MAX_SHOWN_BADGES, MAX_BADGE_STRING } from '../app/renderer/js/badges-model.js';
const A = 'QUFBQUFBQUFBQUFBQUFBQUFBQUE=';
const B = 'QkJCQkJCQkJCQkJCQkJCQkJCQkI=';
const C = 'Q0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0M=';
const SELF = 'U0VMRlNFTEZTRUxGU0VMRlNFTEY=';
const client = (id, uid, nickname, extra = {}) => ({ id, uid, nickname, away: false, is_self: false, ...extra });
const conn = (id, clients, { connected = true, name = id } = {}) => ({
id, connected, snap: { own_client_id: 1, server: { name }, clients: [client(1, SELF, 'me', { is_self: true }), ...clients] },
});
test('adding the same UID again reuses the contact: alias and position kept, remembered nickname refreshed', () => {
let prefs = { contacts: withContact({}, A, 'Alice') };
prefs = { contacts: withContact(prefs, B, 'Bob') };
prefs = { contacts: withAlias(prefs, A, ' Ally ') };
const again = withContact(prefs, A, 'Alice2');
assert.deepEqual(again, [{ uid: A, alias: 'Ally', name: 'Alice2' }, { uid: B, alias: '', name: 'Bob' }]);
// An empty nickname never erases the remembered one.
assert.equal(withContact(prefs, B, '')[1].name, 'Bob');
assert.equal(findContact({ contacts: again }, A).alias, 'Ally');
assert.deepEqual(withoutContact({ contacts: again }, A).map((c) => c.uid), [B]);
assert.deepEqual(withAlias({ contacts: again }, A, '').find((c) => c.uid === A).alias, '');
assert.throws(() => withContact({}, 'not a uid', 'x'), /invalid UID/);
const full = { contacts: Array.from({ length: MAX_CONTACTS }, (_, i) => ({ uid: `u${i}`, alias: '', name: '' })) };
assert.throws(() => withContact(full, A, 'x'), /at most/);
assert.equal(withContact(full, 'u5', 'kept').length, MAX_CONTACTS);
});
test('text written to prefs is stripped of control characters and capped like the main schema', () => {
assert.equal(cleanText('a\u0000b\nc\u007f'), 'abc');
assert.equal(cleanText('x'.repeat(300)).length, 128);
assert.equal(cleanText(42), '');
assert.equal(withContact({}, A, 'Bad\u0007Name')[0].name, 'BadName');
});
test('presence comes only from connected snapshots; the same UID on two servers is one contact with two sessions', () => {
const connections = [
conn('c1', [client(5, A, 'Alice', { away: true }), client(6, B, 'Bob')], { name: 'One' }),
conn('c2', [client(9, A, 'Alice@Two')], { name: 'Two' }),
conn('c3', [client(7, C, 'Carol')], { connected: false }),
];
const sessions = sessionsFor(A, connections);
assert.deepEqual(sessions.map((s) => [s.connectionId, s.clientId, s.serverName, s.away]), [['c1', 5, 'One', true], ['c2', 9, 'Two', false]]);
assert.equal(presenceOf(sessions), PRESENCE.AVAILABLE, 'available on any server wins over away');
assert.equal(presenceOf(sessionsFor(A, [connections[0]])), PRESENCE.AWAY);
// A disconnected connection's last snapshot never counts as presence.
assert.deepEqual(sessionsFor(C, connections), []);
assert.equal(presenceOf([]), PRESENCE.OFFLINE);
// The own client (same identity on this connection) is never a contact session.
assert.deepEqual(sessionsFor(SELF, connections), []);
assert.deepEqual(sessionsFor('bad uid', connections), []);
assert.deepEqual(sessionsFor(A, null), []);
});
test('display name: alias, else current nickname, else remembered nickname', () => {
assert.equal(displayName({ alias: 'Ally', name: 'Alice' }, [{ nickname: 'Now' }]), 'Ally');
assert.equal(displayName({ alias: '', name: 'Alice' }, [{ nickname: 'Now' }]), 'Now');
assert.equal(displayName({ alias: '', name: 'Alice' }, []), 'Alice');
assert.equal(displayName({ alias: '', name: '' }, []), '');
});
test('highlight follows Highlight Contacts and never marks the own client', () => {
const prefs = { contacts: [{ uid: A, alias: '', name: '' }] };
assert.equal(highlightsContact(prefs, { uid: A }), true);
assert.equal(highlightsContact({ ...prefs, highlightContacts: false }, { uid: A }), false);
assert.equal(highlightsContact(prefs, { uid: A, is_self: true }), false);
assert.equal(highlightsContact(prefs, { uid: B }), false);
assert.equal(highlightsContact(prefs, null), false);
});
const entry = (uid, name, presence, unread = 0) => ({ uid, name, presence, unread, sessions: [], contact: { uid, alias: '', name } });
test('sorting: alphabetical (case-insensitive, UID tiebreak) or by presence then name', () => {
const list = [entry(C, 'carol', PRESENCE.OFFLINE), entry(B, 'Bob', PRESENCE.AWAY), entry(A, 'alice', PRESENCE.AVAILABLE), entry('zz', 'Bob', PRESENCE.AVAILABLE)];
assert.deepEqual(sortEntries(list, true).map((e) => e.uid), [A, B, 'zz', C]);
assert.deepEqual(sortEntries(list, false).map((e) => e.uid), [A, 'zz', B, C]);
// Input order is never mutated.
assert.deepEqual(list.map((e) => e.uid), [C, B, A, 'zz']);
});
test('offline arrangement matches the official show/group rules', () => {
const list = [entry(A, 'a', PRESENCE.AVAILABLE), entry(B, 'b', PRESENCE.OFFLINE, 2), entry(C, 'c', PRESENCE.OFFLINE)];
const ids = (xs) => xs?.map((e) => e.uid) ?? null;
// Show + group: online list, separate offline group.
let r = arrangeEntries(list, { showOffline: true, groupOffline: true });
assert.deepEqual([ids(r.main), ids(r.offline), r.grouped], [[A], [B, C], true]);
// Show without grouping: one list with everyone.
r = arrangeEntries(list, { showOffline: true, groupOffline: false });
assert.deepEqual([ids(r.main), ids(r.offline), r.grouped], [[A, B, C], null, false]);
// Hidden: only offline contacts with unread messages stay, grouping is ignored.
r = arrangeEntries(list, { showOffline: false, groupOffline: true });
assert.deepEqual([ids(r.main), ids(r.offline), r.grouped], [[A, B], null, false]);
// Filter by display name.
r = arrangeEntries(list, { filter: ' B ' });
assert.deepEqual([ids(r.main), ids(r.offline)], [[], [B]]);
});
test('entries combine prefs, presence and unread counts', () => {
const prefs = { contacts: [{ uid: A, alias: '', name: 'old' }, { uid: C, alias: 'Cee', name: 'Carol' }] };
const entries = buildEntries(prefs, [conn('c1', [client(5, A, 'Alice')])], (uid) => (uid === C ? 3 : 0));
assert.deepEqual(entries.map((e) => [e.uid, e.name, e.presence, e.unread]), [[A, 'Alice', PRESENCE.AVAILABLE, 0], [C, 'Cee', PRESENCE.OFFLINE, 3]]);
});
test('opening a contact present on several connections picks the viewed one, else creation order', () => {
const sessions = [{ connectionId: 'c1', clientId: 5 }, { connectionId: 'c2', clientId: 9 }];
assert.equal(chooseSession(sessions, 'c2').clientId, 9);
assert.equal(chooseSession(sessions, 'c3').clientId, 5);
assert.equal(chooseSession([], 'c1'), null);
});
test('client badge strings: only the badges key, valid UUIDs, deduplicated, at most three', () => {
const u = (n) => `${String(n).repeat(8)}-1111-2222-3333-444444444444`;
assert.deepEqual(parseClientBadges(`Overwolf=0:badges=${u(1)},${u(2)}`), [u(1), u(2)]);
assert.deepEqual(parseClientBadges(`badges=${u(1).toUpperCase()}, ${u(1)} ,not-a-uuid,${u(2)},${u(3)},${u(4)}`), [u(1), u(2), u(3)]);
assert.equal(parseClientBadges(`BADGES=${u(1)},${u(2)},${u(3)},${u(4)}`).length, MAX_SHOWN_BADGES);
assert.deepEqual(parseClientBadges(`overwolf=1:other=${u(1)}`), []);
assert.deepEqual(parseClientBadges(u(1)), [], 'a bare UUID without the badges key is not trusted');
assert.deepEqual(parseClientBadges('badges='), []);
assert.deepEqual(parseClientBadges(null), []);
assert.deepEqual(parseClientBadges(42), []);
assert.deepEqual(parseClientBadges(`badges=${u(1)}${','.repeat(MAX_BADGE_STRING)}`), [], 'oversized strings are ignored');
});