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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const assert = require('assert');
const {
  createOnMetricHandler,
  getSendChannels,
  getSendOptions,
  validateConfig,
} = require('../app');
const { StateCache } = require('../state-cache');
 
describe('app', () => {
  it('normalizes send channels and options', () => {
    assert.deepStrictEqual(getSendChannels({ send: { channels: ['mqtt', 'aliyun', 'mqtt'] } }), ['mqtt', 'aliyun']);
    assert.deepStrictEqual(getSendOptions({}), {
      includeDeviceIdField: true,
      deviceIdField: 'n',
    });
  });
 
  it('validates minimal config', () => {
    assert.doesNotThrow(() => validateConfig({
      send: { channels: ['mqtt'] },
      tcp: { host: '0.0.0.0', port: 9000 },
      mqtt: {
        protocol: 'mqtt',
        host: '127.0.0.1',
        port: 1883,
        defaultTopicPrefix: 'touxiji',
      },
      protocol: { alModelPath: './alModel.json' },
      devices: [{ deviceId: 'JH-001', ip: '127.0.0.1' }],
    }));
  });
 
  it('updates cache and publishes complete payload to enabled channels', async () => {
    const calls = [];
    const cache = new StateCache();
    const handler = createOnMetricHandler({
      logger: { info() {}, error() {} },
      cache,
      mqttService: {
        publish: async (device, payload) => {
          calls.push({ channel: 'mqtt', deviceId: device.deviceId, payload });
        },
      },
      aliyunService: {
        publish: async (device, payload) => {
          calls.push({ channel: 'aliyun', deviceId: device.deviceId, payload });
        },
      },
    });
 
    await handler(
      { deviceId: 'JH-001' },
      { F: 36.8 },
      { messageType: 'realtime' },
    );
    await handler(
      { deviceId: 'JH-001' },
      { N: 120, O: 80, P: 76, M: '2026-05-12 10:20:30' },
      { messageType: 'blood-pressure' },
    );
 
    assert.strictEqual(calls.length, 4);
    assert.deepStrictEqual(calls[2].payload, {
      n: 'JH-001',
      F: 36.8,
      N: 120,
      O: 80,
      P: 76,
      M: '2026-05-12 10:20:30',
    });
    assert.deepStrictEqual(calls[3].payload, calls[2].payload);
  });
});