Files
client/test/whisper-preview-history.test.mjs
T

81 lines
4.9 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { previewTargets, findChannelByPath } from '../app/renderer/js/whisper-preview.js';
import { WhisperHistory, historyTime } from '../app/renderer/js/whisper-history.js';
const channel = (id, parent_id, name) => ({ id, parent_id, name });
const client = (id, uid, channel_id, extra = {}) => ({ id, uid, nickname: `n${id}`, channel_id, client_type: 'normal', server_groups: [], channel_group_id: 8, ...extra });
const snapshot = (clients, extra = {}) => ({
server: { uid: 'srv', name: 'Server' }, own_client_id: 1, own_channel_id: 11,
channels: [channel(10, 0, 'Lobby'), channel(11, 10, 'Talk'), channel(12, 11, 'Deep'), channel(20, 0, 'Other')],
clients, ...extra,
});
const list = (entries) => ({ type: 1, whisper_entry_list: entries });
test('preview resolves list entries by UID and this server\'s channel path, never the own client', () => {
const snap = snapshot([client(1, 'me', 11), client(2, 'alice', 20), client(3, 'bob', 12), client(4, 'q', 12, { client_type: 'query' })]);
const r = previewTargets(list([{ type: 1, uuid: 'alice', name: 'x' }, { type: 2, uuid: 'srv', name: 'Lobby/Talk/Deep' }, { type: 2, uuid: 'other-server', name: 'Other' }, { type: 1, uuid: 'me', name: '' }]), snap);
assert.deepEqual([...r.clients].sort(), [2, 3]);
assert.deepEqual([...r.channels], [12]);
assert.equal(findChannelByPath(snap.channels, 'Missing/Talk'), null);
assert.equal(findChannelByPath(snap.channels, 'Lobby/Gone/Talk'), 11);
});
test('a recycled client id with another UID is not previewed; a new snapshot never reuses old ids', () => {
const entries = list([{ type: 1, uuid: 'alice', name: 'alice' }]);
assert.deepEqual([...previewTargets(entries, snapshot([client(1, 'me', 11), client(5, 'alice', 10)])).clients], [5]);
const recycled = snapshot([client(1, 'me', 11), client(5, 'mallory', 10)]);
assert.equal(previewTargets(entries, recycled).clients.size, 0);
assert.equal(previewTargets(entries, null).clients.size, 0);
});
test('group preview needs this server for server/channel groups and only channels with matching clients', () => {
const snap = snapshot([client(1, 'me', 11, { server_groups: [7] }), client(2, 'a', 12, { server_groups: [7] }), client(3, 'b', 11), client(4, 'c', 20, { server_groups: [7] })]);
const group = { type: 2, group_type: 0, group_target_mode: 4, target_server_uuid: 'srv', target_group_id: '7' };
const r = previewTargets(group, snap);
assert.deepEqual([...r.channels].sort(), [11, 12]); // own family; channel 11 matches via the own client
assert.deepEqual([...r.clients], [2]);
assert.equal(previewTargets({ ...group, target_server_uuid: 'elsewhere' }, snap).channels.size, 0);
assert.deepEqual([...previewTargets({ ...group, group_type: 3, group_target_mode: 6 }, snap).channels], [12]);
});
test('a whisper received before its speaker is in a snapshot resolves on the next snapshot of the same session only', () => {
let now = 1000;
const h = new WhisperHistory({ now: () => now });
const conn = { session: 1, closed: false, snap: snapshot([client(1, 'me', 11)]) };
assert.equal(h.received(conn, 9), null);
conn.snap = snapshot([client(1, 'me', 11), client(9, 'late', 10)]);
now = 5000;
const [entry] = h.snapshot(conn);
assert.equal(entry.uid, 'late'); assert.equal(entry.timestamp, 1000);
assert.equal(h.received(conn, 7), null);
conn.session = 2;
conn.snap = snapshot([client(1, 'me', 11), client(7, 'someone-else', 10)]);
assert.deepEqual(h.snapshot(conn), []);
assert.deepEqual(h.entries(conn).map((e) => e.uid), ['late']);
});
test('reply/presence ignore other sessions and recycled ids; same UID or id replaces the older entry; clear empties', () => {
const h = new WhisperHistory({ now: () => 1 });
const conn = { session: 1, closed: false, snap: snapshot([client(1, 'me', 11), client(2, 'a', 10), client(3, 'b', 10)]) };
h.received(conn, 2); h.received(conn, 3); h.received(conn, 2);
assert.deepEqual(h.entries(conn).map((e) => e.id), [2, 3]);
assert.deepEqual(h.lastWhisperer(conn), { id: 2, uid: 'a' });
conn.snap = snapshot([client(1, 'me', 11), client(2, 'mallory', 10), client(3, 'b', 10)]);
assert.equal(h.present(conn, h.entries(conn)[0]), null);
assert.deepEqual(h.lastWhisperer(conn), { id: 3, uid: 'b' });
conn.session = 2;
assert.equal(h.lastWhisperer(conn), null);
assert.equal(h.has(conn), true);
h.clear(conn);
assert.equal(h.has(conn), false); assert.deepEqual(h.entries(conn), []);
});
test('history time follows the official calendar buckets', () => {
const now = new Date(2026, 10, 10, 12, 0).getTime();
const clock = (d) => `${d.getHours()}:${String(d.getMinutes()).padStart(2, '0')}`;
assert.equal(historyTime(new Date(2026, 10, 10, 9, 5).getTime(), { now, locale: 'en', clock }), '9:05');
assert.match(historyTime(new Date(2026, 10, 9, 9, 5).getTime(), { now, locale: 'en', clock }), /^9:05 - \S+/);
assert.doesNotMatch(historyTime(new Date(2026, 9, 1).getTime(), { now, locale: 'en', clock }), /:/);
});