gx
chenyc
5 天以前 747a86bb94006aaca721cfc0c0ce7061643a9ea6
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
75
76
77
78
79
80
81
82
83
84
85
86
87
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']);
  });
});