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

function loadDashboardFlow() {
  const flowPath = path.join(__dirname, '../../examples/basic-dashboard.flow.json');
  return JSON.parse(fs.readFileSync(flowPath, 'utf8'));
}

function makeContextStub() {
  const store = {};
  return {
    get(key) {
      return store[key];
    },
    set(key, value) {
      store[key] = value;
    },
  };
}

test('basic dashboard flow contains the pumpingStation node and trend widgets', () => {
  const flow = loadDashboardFlow();
  const ps = flow.find((n) => n.id === 'ps_node_basic');
  const parser = flow.find((n) => n.id === 'ps_parse_output');
  const levelChart = flow.find((n) => n.id === 'ps_chart_level');
  const demandChart = flow.find((n) => n.id === 'ps_chart_demand');

  assert.ok(ps, 'ps_node_basic should exist');
  assert.equal(ps.type, 'pumpingStation');
  assert.equal(ps.controlMode, 'levelbased');
  assert.equal(ps.levelCurveType, 'linear');
  assert.equal(ps.inletPipeDiameter, 0.4);
  assert.equal(ps.outletPipeDiameter, 0.3);
  assert.ok(parser, 'ps_parse_output should exist');
  assert.equal(parser.outputs, 6);
  assert.equal(levelChart.type, 'ui-chart');
  assert.equal(demandChart.type, 'ui-chart');
});

test('basic dashboard parser routes process fields to charts and state text', () => {
  const flow = loadDashboardFlow();
  const parser = flow.find((n) => n.id === 'ps_parse_output');
  assert.ok(parser, 'ps_parse_output should exist');

  const func = new Function('msg', 'context', 'node', parser.func);
  const context = makeContextStub();
  const node = { send() {} };

  // Flatten format is `${type}.${variant}.${position}.${childId}`. When the
  // runtime writes without an explicit .child(), childId='default'. Mirror
  // the real shape here. (See generalFunctions/src/measurements/
  // MeasurementContainer.js getFlattenedOutput.)
  const out = func({
    payload: {
      'level.predicted.atequipment.default': 3.25,
      'volume.predicted.atequipment.default': 32.5,
      'netFlowRate.predicted.atequipment.default': 0.003,
      percControl: 25,
      direction: 'filling',
      safetyState: 'normal',
      isOverflowing: false,
      timeleft: 400,
    },
  }, context, node);

  assert.ok(Array.isArray(out));
  assert.equal(out.length, 6);
  assert.equal(out[0].topic, 'level');
  assert.equal(out[0].payload, 3.25);
  assert.equal(out[1].topic, 'volume');
  assert.equal(out[1].payload, 32.5);
  assert.equal(out[2].topic, 'demand');
  assert.equal(out[2].payload, 25);
  assert.equal(out[3].topic, 'net_flow');
  assert.equal(out[3].payload, 0.003);
  assert.match(out[4].payload, /normal/);
  assert.match(out[5].payload, /level=3.25 m/);
});

test('basic dashboard parser keeps previous values when process output sends only changed fields', () => {
  const flow = loadDashboardFlow();
  const parser = flow.find((n) => n.id === 'ps_parse_output');
  const func = new Function('msg', 'context', 'node', parser.func);
  const context = makeContextStub();
  const node = { send() {} };

  func({ payload: { 'level.predicted.atequipment.default': 3.1, percControl: 10 } }, context, node);
  const out = func({ payload: { percControl: 20 } }, context, node);

  assert.equal(out[0].payload, 3.1);
  assert.equal(out[2].payload, 20);
});
