const assert = require('assert'); const { buildDashboardSnapshot } = require('../dashboard-service'); const { StateCache } = require('../state-cache'); describe('dashboard-service', () => { it('builds device status snapshot from configured devices, cache, and TCP sessions', () => { const cache = new StateCache(); const device = { deviceId: 'JH-001', ip: '127.0.0.1', name: '1号机' }; cache.update(device, { F: 36.8, N: 120 }, { messageType: 'realtime' }); const snapshot = buildDashboardSnapshot({ devices: [device, { deviceId: 'JH-002', ip: '192.168.1.2', name: '2号机' }], cache, tcpService: { getConnectionSnapshot() { return [{ deviceId: 'JH-001', ip: '127.0.0.1', connectedAt: Date.now(), lastDataAt: Date.now(), online: true, }]; }, }, config: { title: '测试大屏', staleDataMs: 180000, }, }); assert.strictEqual(snapshot.title, '测试大屏'); assert.strictEqual(snapshot.totals.devices, 2); assert.strictEqual(snapshot.totals.online, 1); assert.strictEqual(snapshot.totals.offline, 1); assert.strictEqual(snapshot.devices[0].online, true); assert.strictEqual(snapshot.devices[0].dataStatus, 'active'); assert.deepStrictEqual(snapshot.devices[0].payload, { n: 'JH-001', F: 36.8, N: 120, }); assert.strictEqual(snapshot.devices[1].online, false); assert.strictEqual(snapshot.devices[1].dataStatus, 'waiting'); }); });