fix: production hardening — safety fixes, prediction accuracy, test coverage

Safety:
- Async input handler: await all handleInput() calls, prevents unhandled rejections
- Fix emergencyStop case mismatch: "emergencyStop" → "emergencystop" matching config
- Implement showCoG() method (was routing to undefined)
- Null guards on 6 methods for missing curve data
- Editor menu polling timeout (5s max)
- Listener cleanup on node close (child measurements + state emitter)
- Tick loop race condition: track startup timeout, clear on close

Prediction accuracy:
- Remove efficiency rounding that destroyed signal in canonical units
- Fix calcEfficiency variant: hydraulic power reads from correct variant
- Guard efficiency calculations against negative/zero values
- Division-by-zero protection in calcRelativeDistanceFromPeak
- Curve data anomaly detection (cross-pressure median-y ratio check)
- calcEfficiencyCurve O(n²) → O(n) with running min
- updateCurve bootstraps predictors when they were null

Tests: 43 new tests (76 total) covering emergency stop, shutdown/maintenance
sequences, efficiency/CoG, movement lifecycle, output format, null guards,
and listener cleanup.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
znetsixe
2026-04-07 13:41:00 +02:00
parent ea33b3bba3
commit 07af7cef40
10 changed files with 786 additions and 32 deletions

View File

@@ -0,0 +1,147 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const Machine = require('../../src/specificClass');
const { makeMachineConfig, makeStateConfig } = require('../helpers/factories');
function makePressurizedOperationalMachine() {
const machine = new Machine(makeMachineConfig(), makeStateConfig({ state: { current: 'operational' } }));
machine.updateMeasuredPressure(800, 'upstream', { timestamp: Date.now(), unit: 'mbar', childName: 'pt-up' });
machine.updateMeasuredPressure(1200, 'downstream', { timestamp: Date.now(), unit: 'mbar', childName: 'pt-down' });
return machine;
}
test('calcCog returns valid peak efficiency and index', () => {
const machine = makePressurizedOperationalMachine();
const result = machine.calcCog();
assert.ok(Number.isFinite(result.cog), 'cog should be finite');
assert.ok(result.cog > 0, 'peak efficiency should be positive');
assert.ok(Number.isFinite(result.cogIndex), 'cogIndex should be finite');
assert.ok(result.cogIndex >= 0, 'cogIndex should be non-negative');
assert.ok(Number.isFinite(result.NCog), 'NCog should be finite');
assert.ok(result.NCog >= 0 && result.NCog <= 1, 'NCog should be between 0 and 1');
assert.ok(Number.isFinite(result.minEfficiency), 'minEfficiency should be finite');
assert.ok(result.minEfficiency >= 0, 'minEfficiency should be non-negative');
});
test('calcCog peak is always >= minEfficiency', () => {
const machine = makePressurizedOperationalMachine();
const result = machine.calcCog();
assert.ok(result.cog >= result.minEfficiency, 'Peak must be >= min');
});
test('calcEfficiencyCurve produces correct specific flow ratio', () => {
const machine = makePressurizedOperationalMachine();
const { powerCurve, flowCurve } = machine.getCurrentCurves();
const { efficiencyCurve, peak, peakIndex, minEfficiency } = machine.calcEfficiencyCurve(powerCurve, flowCurve);
assert.ok(efficiencyCurve.length > 0, 'Efficiency curve should not be empty');
assert.equal(efficiencyCurve.length, powerCurve.y.length, 'Should match curve length');
// Verify each point: efficiency = flow / power (unrounded, canonical units)
for (let i = 0; i < efficiencyCurve.length; i++) {
const power = powerCurve.y[i];
const flow = flowCurve.y[i];
if (power > 0 && flow >= 0) {
const expected = flow / power;
assert.ok(Math.abs(efficiencyCurve[i] - expected) < 1e-12, `Mismatch at index ${i}`);
}
}
// Peak should be the max
const actualMax = Math.max(...efficiencyCurve);
assert.equal(peak, actualMax, 'Peak should match max of efficiency curve');
assert.equal(efficiencyCurve[peakIndex], peak, 'peakIndex should point to peak value');
assert.equal(minEfficiency, Math.min(...efficiencyCurve), 'minEfficiency should match min');
});
test('calcEfficiencyCurve handles empty curves gracefully', () => {
const machine = new Machine(makeMachineConfig(), makeStateConfig({ state: { current: 'operational' } }));
const result = machine.calcEfficiencyCurve({ x: [], y: [] }, { x: [], y: [] });
assert.deepEqual(result.efficiencyCurve, []);
assert.equal(result.peak, 0);
assert.equal(result.peakIndex, 0);
assert.equal(result.minEfficiency, 0);
});
test('calcDistanceBEP returns absolute and relative distances', () => {
const machine = makePressurizedOperationalMachine();
const efficiency = 5;
const maxEfficiency = 10;
const minEfficiency = 2;
const result = machine.calcDistanceBEP(efficiency, maxEfficiency, minEfficiency);
assert.ok(Number.isFinite(result.absDistFromPeak), 'abs distance should be finite');
assert.equal(result.absDistFromPeak, Math.abs(efficiency - maxEfficiency));
assert.ok(Number.isFinite(result.relDistFromPeak), 'rel distance should be finite');
});
test('calcRelativeDistanceFromPeak returns 1 when maxEfficiency equals minEfficiency', () => {
const machine = makePressurizedOperationalMachine();
const result = machine.calcRelativeDistanceFromPeak(5, 5, 5);
assert.equal(result, 1, 'Should return default distance when max==min (division by zero guard)');
});
test('showCoG returns structured data with curve guards', () => {
const machine = makePressurizedOperationalMachine();
const result = machine.showCoG();
assert.ok('cog' in result);
assert.ok('cogIndex' in result);
assert.ok('NCog' in result);
assert.ok('NCogPercent' in result);
assert.ok('minEfficiency' in result);
assert.ok('currentEfficiencyCurve' in result);
assert.ok(result.cog > 0);
assert.equal(result.NCogPercent, Math.round(result.NCog * 100 * 100) / 100);
});
test('showCoG returns safe fallback when no curve is available', () => {
const machine = new Machine(
makeMachineConfig({ asset: { model: null } }),
makeStateConfig()
);
const result = machine.showCoG();
assert.equal(result.cog, 0);
assert.ok('error' in result);
});
test('showWorkingCurves returns safe fallback when no curve is available', () => {
const machine = new Machine(
makeMachineConfig({ asset: { model: null } }),
makeStateConfig()
);
const result = machine.showWorkingCurves();
assert.ok('error' in result);
});
test('efficiency output fields are present in getOutput', () => {
const machine = makePressurizedOperationalMachine();
// Move to a position so predictions produce values
machine.state.transitionToState('operational');
machine.updatePosition();
const output = machine.getOutput();
assert.ok('cog' in output);
assert.ok('NCog' in output);
assert.ok('NCogPercent' in output);
assert.ok('effDistFromPeak' in output);
assert.ok('effRelDistFromPeak' in output);
assert.ok('predictionQuality' in output);
assert.ok('predictionConfidence' in output);
assert.ok('predictionPressureSource' in output);
});

View File

@@ -0,0 +1,59 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const Machine = require('../../src/specificClass');
const { makeMachineConfig, makeStateConfig } = require('../helpers/factories');
test('emergencystop sequence reaches off state from operational', async () => {
const machine = new Machine(makeMachineConfig(), makeStateConfig());
// First start the machine
await machine.handleInput('parent', 'execSequence', 'startup');
assert.equal(machine.state.getCurrentState(), 'operational');
// Execute emergency stop
await machine.handleInput('GUI', 'emergencystop');
assert.equal(machine.state.getCurrentState(), 'off');
});
test('emergencystop sequence reaches off state from idle', async () => {
const machine = new Machine(makeMachineConfig(), makeStateConfig());
assert.equal(machine.state.getCurrentState(), 'idle');
await machine.handleInput('GUI', 'emergencystop');
assert.equal(machine.state.getCurrentState(), 'off');
});
test('emergencystop clears predicted flow and power to zero', async () => {
const machine = new Machine(makeMachineConfig(), makeStateConfig());
// Start and set a position so predictions are non-zero
await machine.handleInput('parent', 'execSequence', 'startup');
machine.updateMeasuredPressure(1000, 'downstream', { timestamp: Date.now(), unit: 'mbar', childName: 'pt-down' });
await machine.handleInput('parent', 'execMovement', 50);
const flowBefore = machine.measurements.type('flow').variant('predicted').position('downstream').getCurrentValue();
assert.ok(flowBefore > 0, 'Flow should be positive before emergency stop');
// Emergency stop
await machine.handleInput('GUI', 'emergencystop');
const flowAfter = machine.measurements.type('flow').variant('predicted').position('downstream').getCurrentValue();
const powerAfter = machine.measurements.type('power').variant('predicted').position('atEquipment').getCurrentValue();
assert.equal(flowAfter, 0, 'Flow should be zero after emergency stop');
assert.equal(powerAfter, 0, 'Power should be zero after emergency stop');
});
test('emergencystop is rejected when source is not allowed in current mode', async () => {
const machine = new Machine(makeMachineConfig(), makeStateConfig());
// In auto mode, only 'parent' source is typically allowed for sequences
machine.setMode('auto');
await machine.handleInput('parent', 'execSequence', 'startup');
assert.equal(machine.state.getCurrentState(), 'operational');
// GUI source attempting emergency stop in auto mode — should still work
// because emergencystop is allowed from all sources in config
await machine.handleInput('GUI', 'emergencystop');
// If we get here without throwing, action was either accepted or safely rejected
});

View File

@@ -0,0 +1,75 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const Machine = require('../../src/specificClass');
const { makeMachineConfig, makeStateConfig } = require('../helpers/factories');
test('movement from 0 to 50% updates position and predictions', async () => {
const machine = new Machine(makeMachineConfig(), makeStateConfig());
await machine.handleInput('parent', 'execSequence', 'startup');
assert.equal(machine.state.getCurrentState(), 'operational');
machine.updateMeasuredPressure(1000, 'downstream', { timestamp: Date.now(), unit: 'mbar', childName: 'pt' });
await machine.handleInput('parent', 'execMovement', 50);
const pos = machine.state.getCurrentPosition();
const { min, max } = machine._resolveSetpointBounds();
// Position should be constrained to bounds
assert.ok(pos >= min && pos <= max, `Position ${pos} should be within [${min}, ${max}]`);
const flow = machine.measurements.type('flow').variant('predicted').position('downstream').getCurrentValue();
assert.ok(flow > 0, 'Predicted flow should be positive at non-zero position');
});
test('flowmovement sets position based on flow setpoint', async () => {
const machine = new Machine(makeMachineConfig(), makeStateConfig());
await machine.handleInput('parent', 'execSequence', 'startup');
machine.updateMeasuredPressure(1000, 'downstream', { timestamp: Date.now(), unit: 'mbar', childName: 'pt' });
// Request 100 m3/h flow — the machine should calculate the control position
await machine.handleInput('parent', 'flowMovement', 100);
const pos = machine.state.getCurrentPosition();
assert.ok(pos > 0, 'Position should be non-zero for a non-zero flow setpoint');
});
test('sequential movements update position correctly', async () => {
const machine = new Machine(makeMachineConfig(), makeStateConfig());
await machine.handleInput('parent', 'execSequence', 'startup');
machine.updateMeasuredPressure(1000, 'downstream', { timestamp: Date.now(), unit: 'mbar', childName: 'pt' });
await machine.handleInput('parent', 'execMovement', 30);
const pos30 = machine.state.getCurrentPosition();
await machine.handleInput('parent', 'execMovement', 60);
const pos60 = machine.state.getCurrentPosition();
assert.ok(pos60 > pos30, 'Position at 60 should be greater than at 30');
});
test('movement to 0 sets flow and power predictions to minimum curve values', async () => {
const machine = new Machine(makeMachineConfig(), makeStateConfig());
await machine.handleInput('parent', 'execSequence', 'startup');
machine.updateMeasuredPressure(1000, 'downstream', { timestamp: Date.now(), unit: 'mbar', childName: 'pt' });
await machine.handleInput('parent', 'execMovement', 0);
const pos = machine.state.getCurrentPosition();
assert.equal(pos, 0, 'Position should be at 0');
});
test('movement is rejected in non-operational state', async () => {
const machine = new Machine(makeMachineConfig(), makeStateConfig());
assert.equal(machine.state.getCurrentState(), 'idle');
// Attempt movement in idle state — handleInput should process but no movement happens
await machine.handleInput('parent', 'execMovement', 50);
// Machine should still be idle (movement requires operational state via sequence first)
assert.equal(machine.state.getCurrentState(), 'idle');
});

View File

@@ -0,0 +1,72 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const Machine = require('../../src/specificClass');
const { makeMachineConfig, makeStateConfig } = require('../helpers/factories');
test('shutdown sequence from operational reaches idle', async () => {
const machine = new Machine(makeMachineConfig(), makeStateConfig());
await machine.handleInput('parent', 'execSequence', 'startup');
assert.equal(machine.state.getCurrentState(), 'operational');
await machine.handleInput('parent', 'execSequence', 'shutdown');
assert.equal(machine.state.getCurrentState(), 'idle');
});
test('shutdown from operational ramps down position before stopping', async () => {
const machine = new Machine(makeMachineConfig(), makeStateConfig());
await machine.handleInput('parent', 'execSequence', 'startup');
await machine.handleInput('parent', 'execMovement', 50);
const posBefore = machine.state.getCurrentPosition();
assert.ok(posBefore > 0, 'Machine should be at non-zero position');
await machine.handleInput('parent', 'execSequence', 'shutdown');
const posAfter = machine.state.getCurrentPosition();
assert.ok(posAfter <= posBefore, 'Position should have decreased after shutdown');
assert.equal(machine.state.getCurrentState(), 'idle');
});
test('shutdown clears predicted flow and power', async () => {
const machine = new Machine(makeMachineConfig(), makeStateConfig());
await machine.handleInput('parent', 'execSequence', 'startup');
machine.updateMeasuredPressure(1000, 'downstream', { timestamp: Date.now(), unit: 'mbar', childName: 'pt' });
await machine.handleInput('parent', 'execMovement', 50);
await machine.handleInput('parent', 'execSequence', 'shutdown');
const flow = machine.measurements.type('flow').variant('predicted').position('downstream').getCurrentValue();
const power = machine.measurements.type('power').variant('predicted').position('atEquipment').getCurrentValue();
assert.equal(flow, 0, 'Flow should be zero after shutdown');
assert.equal(power, 0, 'Power should be zero after shutdown');
});
test('entermaintenance sequence from operational reaches maintenance state', async () => {
const machine = new Machine(makeMachineConfig(), makeStateConfig());
await machine.handleInput('parent', 'execSequence', 'startup');
assert.equal(machine.state.getCurrentState(), 'operational');
await machine.handleInput('parent', 'enterMaintenance', 'entermaintenance');
assert.equal(machine.state.getCurrentState(), 'maintenance');
});
test('exitmaintenance requires mode with exitmaintenance action allowed', async () => {
const machine = new Machine(makeMachineConfig(), makeStateConfig());
// Use auto mode (has execsequence + entermaintenance) to reach maintenance
await machine.handleInput('parent', 'execSequence', 'startup');
assert.equal(machine.state.getCurrentState(), 'operational');
await machine.handleInput('parent', 'enterMaintenance', 'entermaintenance');
assert.equal(machine.state.getCurrentState(), 'maintenance');
// Switch to fysicalControl which allows exitmaintenance
machine.setMode('fysicalControl');
await machine.handleInput('fysical', 'exitMaintenance', 'exitmaintenance');
assert.equal(machine.state.getCurrentState(), 'idle');
});