const assert = require('assert'); const { createOnMetricHandler, getBloodPressureOptions } = require('../app'); describe('app', () => { it('defaults blood pressure immediate flush to enabled', () => { assert.deepStrictEqual(getBloodPressureOptions({}), { publishTime: true, flushImmediately: true, }); assert.deepStrictEqual(getBloodPressureOptions({ protocol: { bloodPressure: { publishTime: false, flushImmediately: false, }, }, }), { publishTime: false, flushImmediately: false, }); }); it('flushes the full cached payload immediately after blood pressure arrives in batch mode', async () => { const calls = []; const handler = createOnMetricHandler({ logger: { info() {}, error() {} }, sendOptions: { mode: 'batch' }, bloodPressureOptions: { flushImmediately: true }, aggregator: { ingest: async (device, metric) => { calls.push({ type: 'ingest', deviceId: device.deviceId, metric }); return true; }, flush: async (options) => { calls.push({ type: 'flush', options }); return true; }, }, }); await handler( { deviceId: 'JH-001' }, { N: 120, O: 80, P: 89, M: '2026-04-30 10:20:30' }, { protocol: 'blood-pressure' }, ); assert.deepStrictEqual(calls, [ { type: 'ingest', deviceId: 'JH-001', metric: { N: 120, O: 80, P: 89, M: '2026-04-30 10:20:30' }, }, { type: 'flush', options: { reason: 'blood-pressure', deviceId: 'JH-001' }, }, ]); }); it('does not trigger immediate flush for non-blood-pressure metrics', async () => { const calls = []; const handler = createOnMetricHandler({ logger: { info() {}, error() {} }, sendOptions: { mode: 'batch' }, bloodPressureOptions: { flushImmediately: true }, aggregator: { ingest: async () => { calls.push('ingest'); return true; }, flush: async () => { calls.push('flush'); return true; }, }, }); await handler( { deviceId: 'JH-001' }, { F: 36.7 }, { protocol: 'jhm' }, ); assert.deepStrictEqual(calls, ['ingest']); }); });