gx
chenyc
5 天以前 747a86bb94006aaca721cfc0c0ce7061643a9ea6
test/app.test.js
@@ -1,7 +1,87 @@
const assert = require('assert');
const { createOnMetricHandler, getBloodPressureOptions } = require('../app');
describe('应用程序功能测试', () => {
    it('应该返回正确的结果', () => {
        assert.strictEqual(1 + 1, 2);
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']);
  });
});