const test = require('node:test');
const assert = require('node:assert/strict');

const OutputUtils = require('../src/helper/outputUtils.js');

const config = {
  functionality: { softwareType: 'measurement', role: 'sensor' },
  general: { id: 'abc', unit: 'mbar' },
  asset: {
    uuid: 'u1',
    tagcode: 't1',
    geoLocation: { lat: 51.6, lon: 4.7 },
    category: 'measurement',
    type: 'pressure',
    model: 'M1',
  },
};

test('process format emits message with changed fields only', () => {
  const out = new OutputUtils();

  const first = out.formatMsg({ a: 1, b: 2 }, config, 'process');
  assert.equal(first.topic, 'measurement_abc');
  assert.deepEqual(first.payload, { a: 1, b: 2 });

  const second = out.formatMsg({ a: 1, b: 2 }, config, 'process');
  assert.equal(second, null);

  const third = out.formatMsg({ a: 1, b: 3, c: { x: 1 } }, config, 'process');
  assert.deepEqual(third.payload, { b: 3, c: JSON.stringify({ x: 1 }) });
});

test('influx format flattens tags and stringifies tag values', () => {
  const out = new OutputUtils();
  const msg = out.formatMsg({ value: 10 }, config, 'influxdb');

  assert.equal(msg.topic, 'measurement_abc');
  assert.equal(msg.payload.measurement, 'measurement_abc');
  assert.equal(msg.payload.tags.geoLocation_lat, '51.6');
  assert.equal(msg.payload.tags.geoLocation_lon, '4.7');
  assert.ok(msg.payload.timestamp instanceof Date);
});
