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); }); });