Support config-driven output formatting

This commit is contained in:
Rene De Ren
2026-03-12 16:13:39 +01:00
parent 31928fd124
commit 814ee3d763
6 changed files with 135 additions and 51 deletions

69
test/outputUtils.test.js Normal file
View File

@@ -0,0 +1,69 @@
const OutputUtils = require('../src/helper/outputUtils');
describe('OutputUtils', () => {
let outputUtils;
let config;
beforeEach(() => {
outputUtils = new OutputUtils();
config = {
general: {
name: 'Pump-1',
id: 'node-1',
unit: 'm3/h',
},
functionality: {
softwareType: 'pump',
role: 'test-role',
},
asset: {
supplier: 'EVOLV',
type: 'sensor',
},
output: {
process: 'process',
dbase: 'influxdb',
},
};
});
it('keeps legacy process output by default', () => {
const msg = outputUtils.formatMsg({ flow: 12.5 }, config, 'process');
expect(msg).toEqual({
topic: 'Pump-1',
payload: { flow: 12.5 },
});
});
it('keeps legacy influxdb output by default', () => {
const msg = outputUtils.formatMsg({ flow: 12.5 }, config, 'influxdb');
expect(msg.topic).toBe('Pump-1');
expect(msg.payload).toEqual(expect.objectContaining({
measurement: 'Pump-1',
fields: { flow: 12.5 },
tags: expect.objectContaining({
id: 'node-1',
name: 'Pump-1',
softwareType: 'pump',
}),
}));
});
it('supports config-driven json formatting on the process channel', () => {
config.output.process = 'json';
const msg = outputUtils.formatMsg({ flow: 12.5 }, config, 'process');
expect(msg.topic).toBe('Pump-1');
expect(typeof msg.payload).toBe('string');
expect(msg.payload).toContain('"measurement":"Pump-1"');
expect(msg.payload).toContain('"flow":12.5');
});
it('supports config-driven csv formatting on the database channel', () => {
config.output.dbase = 'csv';
const msg = outputUtils.formatMsg({ flow: 12.5 }, config, 'influxdb');
expect(msg.topic).toBe('Pump-1');
expect(typeof msg.payload).toBe('string');
expect(msg.payload).toContain('Pump-1');
expect(msg.payload).toContain('flow=12.5');
});
});