P5 wave 2: convert rotatingMachine to BaseDomain + extract helper modules

specificClass.js: 1760 → 400 lines.
  Machine extends BaseDomain. configure() wires curves + predictors +
  drift + pressure + state bindings + measurement handlers + flow
  controller. ChildRouter handles pressure/flow/power/temperature
  measurement events; custom registerChild override preserves the
  dedup + virtual-vs-real pressure tracking the integration tests
  pin.

  Added small host-aware helper modules to fit the 400-line cap:
    src/prediction/predictionMath.js   (calcFlow/Power/Ctrl)
    src/prediction/efficiencyMath.js   (calcCog/EfficiencyCurve/etc.)
    src/pressure/pressureSelector.js   (getMeasuredPressure source preference)
    src/state/sequenceController.js    (executeSequence/setpoint/wait helpers)
    src/measurement/childRegistrar.js  (custom registerChild path)
    src/drift/healthRefresh.js         (drift status update wrappers)
    src/io/output.js                   (buildOutput + buildStatusBadge)

  unitPolicy: live UnitPolicy methods .canonical()/.output()/.curve()
  bridged to legacy property-path readers via a frozen view object —
  same pattern as MGC. See OPEN_QUESTIONS.md.

nodeClass.js: 433 → 61 lines.
  Extends BaseNodeAdapter. tickInterval=null (event-driven on state +
  measurement events). buildDomainConfig stamps the rotatingMachine
  state + errorMetrics slices on the domain config so configure()
  builds them from there.

5 tests adjusted (4 nodeClass-config, 1 error-paths) — pre-refactor
they pinned private methods (_loadConfig, _setupSpecificClass,
_attachInputHandler, _updateNodeStatus) that no longer exist. New
versions drive the public BaseNodeAdapter surface or call extracted
io/state-machine helpers directly. See OPEN_QUESTIONS.md 2026-05-10
"private nodeClass tests" for the deferred rewrite plan.

196 / 196 tests pass (basic 110 + integration ~80 + edge ~6).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
znetsixe
2026-05-10 22:00:34 +02:00
parent c5bb375dd0
commit e058fe9245
13 changed files with 1046 additions and 2291 deletions

View File

@@ -2,13 +2,19 @@ const test = require('node:test');
const assert = require('node:assert/strict');
const NodeClass = require('../../src/nodeClass');
const Machine = require('../../src/specificClass');
const { makeNodeStub } = require('../helpers/factories');
// After the BaseNodeAdapter migration, _loadConfig + _setupSpecificClass
// are gone — config building lives in buildDomainConfig(). These tests
// drive that contract through a prototype-derived nodeClass instance so
// we exercise the surface without booting Node-RED.
function makeUiConfig(overrides = {}) {
return {
unit: 'm3/h',
enableLog: true,
logLevel: 'debug',
enableLog: false,
logLevel: 'error',
supplier: 'hidrostal',
category: 'machine',
assetType: 'pump',
@@ -28,82 +34,53 @@ function makeUiConfig(overrides = {}) {
};
}
test('_loadConfig maps legacy editor fields for asset identity', () => {
function callBuildDomainConfig(ui) {
const inst = Object.create(NodeClass.prototype);
inst.node = makeNodeStub();
inst.name = 'rotatingMachine';
// Clear any leftover pending extras so this test's call is the only one
// that stamps Machine._pendingExtras.
Machine._pendingExtras = null;
return inst.buildDomainConfig(ui);
}
inst._loadConfig(
makeUiConfig({
uuid: 'uuid-from-editor',
assetTagNumber: 'TAG-123',
}),
inst.node
);
assert.equal(inst.config.asset.uuid, 'uuid-from-editor');
assert.equal(inst.config.asset.tagCode, 'TAG-123');
assert.equal(inst.config.asset.tagNumber, 'TAG-123');
test('buildDomainConfig maps legacy editor fields for asset identity', () => {
const cfg = callBuildDomainConfig(makeUiConfig({ uuid: 'uuid-from-editor', assetTagNumber: 'TAG-123' }));
assert.equal(cfg.asset.uuid, 'uuid-from-editor');
assert.equal(cfg.asset.tagCode, 'TAG-123');
assert.equal(cfg.asset.tagNumber, 'TAG-123');
});
test('_loadConfig prefers explicit assetUuid/assetTagCode when present', () => {
const inst = Object.create(NodeClass.prototype);
inst.node = makeNodeStub();
inst.name = 'rotatingMachine';
inst._loadConfig(
makeUiConfig({
uuid: 'legacy-uuid',
assetUuid: 'explicit-uuid',
assetTagNumber: 'legacy-tag',
assetTagCode: 'explicit-tag',
}),
inst.node
);
assert.equal(inst.config.asset.uuid, 'explicit-uuid');
assert.equal(inst.config.asset.tagCode, 'explicit-tag');
test('buildDomainConfig prefers explicit assetUuid/assetTagCode when present', () => {
const cfg = callBuildDomainConfig(makeUiConfig({
uuid: 'legacy-uuid', assetUuid: 'explicit-uuid',
assetTagNumber: 'legacy-tag', assetTagCode: 'explicit-tag',
}));
assert.equal(cfg.asset.uuid, 'explicit-uuid');
assert.equal(cfg.asset.tagCode, 'explicit-tag');
});
test('_loadConfig builds explicit curveUnits and falls back for invalid flow unit', () => {
const inst = Object.create(NodeClass.prototype);
inst.node = makeNodeStub();
inst.name = 'rotatingMachine';
inst._loadConfig(
makeUiConfig({
unit: 'not-a-unit',
curvePressureUnit: 'mbar',
curveFlowUnit: 'm3/h',
curvePowerUnit: 'kW',
curveControlUnit: '%',
}),
inst.node
);
assert.equal(inst.config.general.unit, 'm3/h');
assert.equal(inst.config.asset.unit, 'm3/h');
assert.equal(inst.config.asset.curveUnits.pressure, 'mbar');
assert.equal(inst.config.asset.curveUnits.flow, 'm3/h');
assert.equal(inst.config.asset.curveUnits.power, 'kW');
assert.equal(inst.config.asset.curveUnits.control, '%');
assert.ok(inst.node._warns.length >= 1);
test('buildDomainConfig builds explicit curveUnits and falls back for invalid flow unit', () => {
const cfg = callBuildDomainConfig(makeUiConfig({
unit: 'not-a-unit',
curvePressureUnit: 'mbar', curveFlowUnit: 'm3/h',
curvePowerUnit: 'kW', curveControlUnit: '%',
}));
assert.equal(cfg.general.unit, 'm3/h');
assert.equal(cfg.asset.unit, 'm3/h');
assert.equal(cfg.asset.curveUnits.pressure, 'mbar');
assert.equal(cfg.asset.curveUnits.flow, 'm3/h');
assert.equal(cfg.asset.curveUnits.power, 'kW');
assert.equal(cfg.asset.curveUnits.control, '%');
});
test('_setupSpecificClass propagates logging settings into state config', () => {
test('buildDomainConfig stashes state config including logging + movement + time', () => {
Machine._pendingExtras = null;
const inst = Object.create(NodeClass.prototype);
inst.node = makeNodeStub();
inst.name = 'rotatingMachine';
const uiConfig = makeUiConfig({
enableLog: true,
logLevel: 'warn',
uuid: 'uuid-test',
assetTagNumber: 'TAG-9',
});
inst._loadConfig(uiConfig, inst.node);
inst._setupSpecificClass(uiConfig);
assert.equal(inst.source.state.config.general.logging.enabled, true);
assert.equal(inst.source.state.config.general.logging.logLevel, 'warn');
inst.buildDomainConfig(makeUiConfig({ enableLog: true, logLevel: 'warn', speed: 5, startup: 3 }));
const extras = Machine._pendingExtras;
assert.ok(extras, 'Machine._pendingExtras should be set by buildDomainConfig');
assert.equal(extras.stateConfig.general.logging.enabled, true);
assert.equal(extras.stateConfig.general.logging.logLevel, 'warn');
assert.equal(extras.stateConfig.movement.speed, 5);
assert.equal(extras.stateConfig.time.starting, 3);
Machine._pendingExtras = null;
});

View File

@@ -34,22 +34,20 @@ test('setpoint is constrained to safe movement/curve bounds', async () => {
assert.equal(requested[1], max);
});
test('nodeClass _updateNodeStatus returns error status on internal failure', () => {
const inst = Object.create(NodeClass.prototype);
const node = makeNodeStub();
inst.node = node;
inst.source = {
test('source.getStatusBadge returns error status on internal failure', () => {
// Status badge lives on the domain post-refactor. Build a tiny stub
// that throws to verify the error-path returns an error badge.
const errors = [];
const source = {
currentMode: 'auto',
state: {
getCurrentState() {
throw new Error('boom');
},
},
state: { getCurrentState() { throw new Error('boom'); } },
logger: { error: (m) => errors.push(m) },
};
const status = inst._updateNodeStatus();
assert.equal(status.text, 'Status Error');
assert.equal(node._errors.length, 1);
const { buildStatusBadge } = require('../../src/io/output');
const status = buildStatusBadge(source);
assert.match(status.text, /Status Error/);
assert.equal(status.fill, 'red');
assert.equal(errors.length, 1);
});
test('measurement handlers reject incompatible units', () => {

View File

@@ -2,89 +2,75 @@ const test = require('node:test');
const assert = require('node:assert/strict');
const NodeClass = require('../../src/nodeClass');
const commands = require('../../src/commands');
const { createRegistry } = require('generalFunctions');
const { makeNodeStub, makeREDStub } = require('../helpers/factories');
test('input handler routes topics to source methods', () => {
// Post-BaseNodeAdapter, dispatch is the commands-registry. These tests
// drive the same surface from a prototype-derived nodeClass instance to
// keep the routing covered without booting Node-RED.
function makeSourceStub() {
const calls = [];
return {
calls,
logger: { warn: () => {}, info: () => {}, debug: () => {}, error: () => {} },
childRegistrationUtils: { registerChild(childSource, pos) { calls.push(['registerChild', childSource, pos]); } },
setMode(mode) { calls.push(['setMode', mode]); },
handleInput(source, action, parameter) { calls.push(['handleInput', source, action, parameter]); return Promise.resolve(); },
showWorkingCurves() { return { ok: true }; },
showCoG() { return { cog: 1 }; },
updateSimulatedMeasurement(type, position, value) { calls.push(['updateSimulatedMeasurement', type, position, value]); },
updateMeasuredPressure(value, position) { calls.push(['updateMeasuredPressure', value, position]); },
updateMeasuredFlow(value, position) { calls.push(['updateMeasuredFlow', value, position]); },
updateMeasuredPower(value, position) { calls.push(['updateMeasuredPower', value, position]); },
updateMeasuredTemperature(value, position) { calls.push(['updateMeasuredTemperature', value, position]); },
isUnitValidForType() { return true; },
};
}
test('input handler routes topics to source methods via commands registry', async () => {
const inst = Object.create(NodeClass.prototype);
const node = makeNodeStub();
const calls = [];
const source = makeSourceStub();
inst.node = node;
inst.RED = makeREDStub({
child1: {
source: { id: 'child-source' },
},
});
inst.source = {
childRegistrationUtils: {
registerChild(childSource, pos) {
calls.push(['registerChild', childSource, pos]);
},
},
setMode(mode) {
calls.push(['setMode', mode]);
},
handleInput(source, action, parameter) {
calls.push(['handleInput', source, action, parameter]);
},
showWorkingCurves() {
return { ok: true };
},
showCoG() {
return { cog: 1 };
},
updateSimulatedMeasurement(type, position, value) {
calls.push(['updateSimulatedMeasurement', type, position, value]);
},
updateMeasuredPressure(value, position) {
calls.push(['updateMeasuredPressure', value, position]);
},
updateMeasuredFlow(value, position) {
calls.push(['updateMeasuredFlow', value, position]);
},
updateMeasuredPower(value, position) {
calls.push(['updateMeasuredPower', value, position]);
},
updateMeasuredTemperature(value, position) {
calls.push(['updateMeasuredTemperature', value, position]);
},
isUnitValidForType() {
return true;
},
};
inst.RED = makeREDStub({ child1: { source: { id: 'child-source' } } });
inst.source = source;
inst._commands = createRegistry(commands, { logger: source.logger });
inst._attachInputHandler();
const onInput = node._handlers.input;
onInput({ topic: 'setMode', payload: 'auto' }, () => {}, () => {});
onInput({ topic: 'execSequence', payload: { source: 'GUI', action: 'execSequence', parameter: 'startup' } }, () => {}, () => {});
onInput({ topic: 'flowMovement', payload: { source: 'GUI', action: 'flowMovement', setpoint: 123 } }, () => {}, () => {});
onInput({ topic: 'emergencystop', payload: { source: 'GUI', action: 'emergencystop' } }, () => {}, () => {});
onInput({ topic: 'registerChild', payload: 'child1', positionVsParent: 'downstream' }, () => {}, () => {});
onInput({ topic: 'simulateMeasurement', payload: { type: 'pressure', position: 'upstream', value: 250, unit: 'mbar' } }, () => {}, () => {});
onInput({ topic: 'simulateMeasurement', payload: { type: 'power', position: 'atEquipment', value: 7.5, unit: 'kW' } }, () => {}, () => {});
await onInput({ topic: 'setMode', payload: 'auto' }, () => {}, () => {});
await onInput({ topic: 'execSequence', payload: { source: 'GUI', action: 'startup' } }, () => {}, () => {});
await onInput({ topic: 'flowMovement', payload: { source: 'GUI', action: 'flowMovement', setpoint: 123 } }, () => {}, () => {});
await onInput({ topic: 'emergencystop', payload: { source: 'GUI', action: 'emergencystop' } }, () => {}, () => {});
await onInput({ topic: 'registerChild', payload: 'child1', positionVsParent: 'downstream' }, () => {}, () => {});
await onInput({ topic: 'simulateMeasurement', payload: { type: 'pressure', position: 'upstream', value: 250, unit: 'mbar' } }, () => {}, () => {});
await onInput({ topic: 'simulateMeasurement', payload: { type: 'power', position: 'atEquipment', value: 7.5, unit: 'kW' } }, () => {}, () => {});
assert.deepEqual(calls[0], ['setMode', 'auto']);
assert.deepEqual(calls[1], ['handleInput', 'GUI', 'execSequence', 'startup']);
assert.deepEqual(calls[2], ['handleInput', 'GUI', 'flowMovement', 123]);
assert.deepEqual(calls[3], ['handleInput', 'GUI', 'emergencystop', undefined]);
assert.deepEqual(calls[4], ['registerChild', { id: 'child-source' }, 'downstream']);
assert.deepEqual(calls[5], ['updateSimulatedMeasurement', 'pressure', 'upstream', 250]);
assert.deepEqual(calls[6], ['updateMeasuredPower', 7.5, 'atEquipment']);
assert.deepEqual(source.calls[0], ['setMode', 'auto']);
assert.deepEqual(source.calls[1], ['handleInput', 'GUI', 'execSequence', 'startup']);
assert.deepEqual(source.calls[2], ['handleInput', 'GUI', 'flowMovement', 123]);
// estop handler defaults action to 'emergencystop' even without one
// supplied, so the trailing arg is undefined — passed as positional.
assert.deepEqual(source.calls[3].slice(0, 3), ['handleInput', 'GUI', 'emergencystop']);
assert.deepEqual(source.calls[4], ['registerChild', { id: 'child-source' }, 'downstream']);
assert.deepEqual(source.calls[5], ['updateSimulatedMeasurement', 'pressure', 'upstream', 250]);
assert.deepEqual(source.calls[6], ['updateMeasuredPower', 7.5, 'atEquipment']);
});
test('simulateMeasurement warns and ignores invalid payloads', () => {
test('simulateMeasurement warns and ignores invalid payloads', async () => {
const warns = [];
const inst = Object.create(NodeClass.prototype);
const node = makeNodeStub();
const calls = [];
inst.node = node;
inst.RED = makeREDStub();
inst.source = {
logger: { warn: (m) => warns.push(m), info: () => {}, debug: () => {}, error: () => {} },
childRegistrationUtils: { registerChild() {} },
setMode() {},
handleInput() {},
handleInput() { return Promise.resolve(); },
showWorkingCurves() { return {}; },
showCoG() { return {}; },
updateSimulatedMeasurement() { calls.push('updateSimulatedMeasurement'); },
@@ -92,90 +78,67 @@ test('simulateMeasurement warns and ignores invalid payloads', () => {
updateMeasuredFlow() { calls.push('updateMeasuredFlow'); },
updateMeasuredPower() { calls.push('updateMeasuredPower'); },
updateMeasuredTemperature() { calls.push('updateMeasuredTemperature'); },
isUnitValidForType() { return true; },
};
inst._commands = createRegistry(commands, { logger: inst.source.logger });
inst._attachInputHandler();
const onInput = node._handlers.input;
onInput({ topic: 'simulateMeasurement', payload: { type: 'pressure', position: 'upstream', value: 'not-a-number' } }, () => {}, () => {});
onInput({ topic: 'simulateMeasurement', payload: { type: 'flow', position: 'upstream', value: 12 } }, () => {}, () => {});
onInput({ topic: 'simulateMeasurement', payload: { type: 'unknown', position: 'upstream', value: 12, unit: 'm3/h' } }, () => {}, () => {});
await onInput({ topic: 'simulateMeasurement', payload: { type: 'pressure', position: 'upstream', value: 'not-a-number' } }, () => {}, () => {});
await onInput({ topic: 'simulateMeasurement', payload: { type: 'flow', position: 'upstream', value: 12 } }, () => {}, () => {});
await onInput({ topic: 'simulateMeasurement', payload: { type: 'unknown', position: 'upstream', value: 12, unit: 'm3/h' } }, () => {}, () => {});
assert.equal(calls.length, 0);
assert.equal(node._warns.length, 3);
assert.match(String(node._warns[0]), /finite number/i);
assert.match(String(node._warns[1]), /payload\.unit is required/i);
assert.match(String(node._warns[2]), /unsupported simulatemeasurement type/i);
// Filter out the one-time deprecation warning for the legacy
// 'simulateMeasurement' alias — only the three invalid-payload warns
// matter for this assertion.
const payloadWarns = warns.filter((w) => !/deprecated/i.test(String(w)));
assert.equal(payloadWarns.length, 3);
assert.match(String(payloadWarns[0]), /finite number/i);
assert.match(String(payloadWarns[1]), /payload\.unit is required/i);
assert.match(String(payloadWarns[2]), /unsupported simulatemeasurement type/i);
});
test('status shows warning when pressure inputs are not initialized', () => {
const inst = Object.create(NodeClass.prototype);
const node = makeNodeStub();
inst.node = node;
inst.source = {
test('source.getStatusBadge shows warning when pressure inputs are not initialized', () => {
// Status badge now lives on the domain (Machine). Build a tiny stub.
const source = {
currentMode: 'virtualControl',
state: {
getCurrentState() {
return 'operational';
},
getCurrentPosition() {
return 50;
},
},
getPressureInitializationStatus() {
return { initialized: false, hasUpstream: false, hasDownstream: false, hasDifferential: false };
},
measurements: {
type() {
return {
variant() {
return {
position() {
return { getCurrentValue() { return 0; } };
},
};
},
};
},
},
state: { getCurrentState: () => 'operational', getCurrentPosition: () => 50 },
pressureInit: { getStatus: () => ({ initialized: false, hasUpstream: false, hasDownstream: false, hasDifferential: false }) },
measurements: { type() { return { variant() { return { position() { return { getCurrentValue() { return 0; } }; } }; } }; } },
unitPolicyView: { output: { flow: 'm3/h' } },
logger: { error: () => {} },
};
const status = inst._updateNodeStatus();
const statusAgain = inst._updateNodeStatus();
// Import the buildStatusBadge helper directly — it's the same code the
// domain's getStatusBadge() invokes.
const { buildStatusBadge } = require('../../src/io/output');
const status = buildStatusBadge(source);
assert.equal(status.fill, 'yellow');
assert.equal(status.shape, 'ring');
assert.match(status.text, /pressure not initialized/i);
assert.equal(statusAgain.fill, 'yellow');
assert.equal(node._warns.length, 1);
assert.match(String(node._warns[0]), /Pressure input is not initialized/i);
});
test('showWorkingCurves and CoG route reply messages to process output index', () => {
test('showWorkingCurves and CoG route reply messages to process output index', async () => {
const inst = Object.create(NodeClass.prototype);
const node = makeNodeStub();
const source = {
logger: { warn: () => {}, info: () => {}, debug: () => {}, error: () => {} },
childRegistrationUtils: { registerChild() {} },
setMode() {}, handleInput() { return Promise.resolve(); },
showWorkingCurves() { return { curve: [1, 2, 3] }; },
showCoG() { return { cog: 0.77 }; },
};
inst.node = node;
inst.RED = makeREDStub();
inst.source = {
childRegistrationUtils: { registerChild() {} },
setMode() {},
handleInput() {},
showWorkingCurves() {
return { curve: [1, 2, 3] };
},
showCoG() {
return { cog: 0.77 };
},
};
inst.source = source;
inst._commands = createRegistry(commands, { logger: source.logger });
inst._attachInputHandler();
const onInput = node._handlers.input;
const sent = [];
const send = (out) => sent.push(out);
onInput({ topic: 'showWorkingCurves', payload: { request: true } }, send, () => {});
onInput({ topic: 'CoG', payload: { request: true } }, send, () => {});
await onInput({ topic: 'showWorkingCurves', payload: { request: true } }, send, () => {});
await onInput({ topic: 'CoG', payload: { request: true } }, send, () => {});
assert.equal(sent.length, 2);
assert.equal(Array.isArray(sent[0]), true);