chenyc
2026-05-20 c8ba0f92b3f84273a78f06de25359db20c1b2a4d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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');
  });
});