cangku/frontend/test/desktopPresenceRows.test.mjs

85 lines
2.4 KiB
JavaScript

import assert from 'node:assert/strict';
import test from 'node:test';
import {
buildOperatorPresenceRows,
filterOperatorPresenceRows,
getOperatorPresenceCounts,
selectCurrentPresenceDevice,
} from '../src/pages/OnlineDesktopUsers/presenceRows.js';
const onlineDevice = {
clientId: 'online-current',
deviceName: 'CURRENT-PC',
isOnline: true,
lastSeenAt: '2026-08-19T02:00:00.000Z',
};
test('current device prefers an online client over newer offline history', () => {
const selected = selectCurrentPresenceDevice([
onlineDevice,
{
clientId: 'offline-history',
isOnline: false,
lastSeenAt: '2026-08-19T03:00:00.000Z',
},
]);
assert.equal(selected.clientId, 'online-current');
});
test('current device uses the latest heartbeat when statuses match', () => {
const selected = selectCurrentPresenceDevice([
{ ...onlineDevice, clientId: 'older', lastSeenAt: '2026-08-19T01:00:00.000Z' },
{ ...onlineDevice, clientId: 'newer', lastSeenAt: '2026-08-19T02:00:00.000Z' },
]);
assert.equal(selected.clientId, 'newer');
});
test('operator rows contain one current client per authenticated user', () => {
const rows = buildOperatorPresenceRows([
{
userId: 7,
name: '胖大海',
username: 'pangdahai',
departmentName: '效率部',
deviceCount: 2,
activeDeviceCount: 1,
devices: [
{ ...onlineDevice },
{
clientId: 'offline-history',
deviceName: 'OLD-PC',
isOnline: false,
lastSeenAt: '2026-08-18T02:00:00.000Z',
},
],
},
{
userId: 8,
name: '胖大海',
username: 'another-user',
devices: [{ clientId: 'other-user', isOnline: false, lastSeenAt: '2026-08-17T02:00:00.000Z' }],
},
]);
assert.equal(rows.length, 2);
assert.equal(rows[0].userId, '7');
assert.equal(rows[0].clientId, 'online-current');
assert.equal(rows[0].deviceCount, 2);
assert.equal(rows[1].userId, '8');
});
test('operator filters and counts use one-row-per-user semantics', () => {
const rows = [
{ userId: '7', isOnline: true },
{ userId: '8', isOnline: false },
];
assert.deepEqual(getOperatorPresenceCounts(rows), { total: 2, online: 1, offline: 1 });
assert.deepEqual(filterOperatorPresenceRows(rows, 'online'), [rows[0]]);
assert.deepEqual(filterOperatorPresenceRows(rows, 'offline'), [rows[1]]);
assert.equal(filterOperatorPresenceRows(rows, 'all'), rows);
});