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:
121
test/edge/output-format.edge.test.js
Normal file
121
test/edge/output-format.edge.test.js
Normal file
@@ -0,0 +1,121 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
|
||||
const Machine = require('../../src/specificClass');
|
||||
const { makeMachineConfig, makeStateConfig } = require('../helpers/factories');
|
||||
|
||||
test('getOutput contains all required fields in idle state', () => {
|
||||
const machine = new Machine(makeMachineConfig(), makeStateConfig());
|
||||
|
||||
const output = machine.getOutput();
|
||||
|
||||
// Core state fields
|
||||
assert.equal(output.state, 'idle');
|
||||
assert.ok('runtime' in output);
|
||||
assert.ok('ctrl' in output);
|
||||
assert.ok('moveTimeleft' in output);
|
||||
assert.ok('mode' in output);
|
||||
assert.ok('maintenanceTime' in output);
|
||||
|
||||
// Efficiency fields
|
||||
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);
|
||||
|
||||
// Prediction health fields
|
||||
assert.ok('predictionQuality' in output);
|
||||
assert.ok('predictionConfidence' in output);
|
||||
assert.ok('predictionPressureSource' in output);
|
||||
assert.ok('predictionFlags' in output);
|
||||
|
||||
// Pressure drift fields
|
||||
assert.ok('pressureDriftLevel' in output);
|
||||
assert.ok('pressureDriftSource' in output);
|
||||
assert.ok('pressureDriftFlags' in output);
|
||||
});
|
||||
|
||||
test('getOutput flow drift fields appear after sufficient measured flow samples', 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);
|
||||
|
||||
// Provide multiple measured flow samples to trigger valid drift assessment
|
||||
const baseTime = Date.now();
|
||||
for (let i = 0; i < 12; i++) {
|
||||
machine.updateMeasuredFlow(100 + i, 'downstream', {
|
||||
timestamp: baseTime + (i * 1000),
|
||||
unit: 'm3/h',
|
||||
childId: 'flow-sensor',
|
||||
childName: 'FT-1',
|
||||
});
|
||||
}
|
||||
|
||||
const output = machine.getOutput();
|
||||
|
||||
// Drift fields should appear once enough samples provide a valid assessment
|
||||
if ('flowNrmse' in output) {
|
||||
assert.ok(typeof output.flowNrmse === 'number');
|
||||
assert.ok('flowDriftValid' in output);
|
||||
}
|
||||
// At minimum, prediction health fields should always be present
|
||||
assert.ok('predictionQuality' in output);
|
||||
assert.ok('predictionConfidence' in output);
|
||||
});
|
||||
|
||||
test('getOutput prediction confidence is 0 in non-operational state', () => {
|
||||
const machine = new Machine(makeMachineConfig(), makeStateConfig());
|
||||
|
||||
const output = machine.getOutput();
|
||||
|
||||
assert.equal(output.predictionConfidence, 0);
|
||||
});
|
||||
|
||||
test('getOutput prediction confidence reflects differential pressure', () => {
|
||||
const machine = new Machine(makeMachineConfig(), makeStateConfig({ state: { current: 'operational' } }));
|
||||
|
||||
// Differential pressure → high confidence
|
||||
machine.updateMeasuredPressure(800, 'upstream', { timestamp: Date.now(), unit: 'mbar', childName: 'pt-up' });
|
||||
machine.updateMeasuredPressure(1200, 'downstream', { timestamp: Date.now(), unit: 'mbar', childName: 'pt-down' });
|
||||
|
||||
const output = machine.getOutput();
|
||||
|
||||
assert.ok(output.predictionConfidence >= 0.8, `Confidence ${output.predictionConfidence} should be >= 0.8 with differential pressure`);
|
||||
assert.equal(output.predictionPressureSource, 'differential');
|
||||
});
|
||||
|
||||
test('getOutput values are in configured output units not canonical', () => {
|
||||
const machine = new Machine(makeMachineConfig(), makeStateConfig({ state: { current: 'operational' } }));
|
||||
|
||||
machine.updateMeasuredPressure(1000, 'downstream', { timestamp: Date.now(), unit: 'mbar', childName: 'pt' });
|
||||
machine.updatePosition();
|
||||
|
||||
const output = machine.getOutput();
|
||||
|
||||
// Flow keys should contain values in m3/h (configured), not m3/s (canonical)
|
||||
// Predicted flow at minimum pressure should be in a reasonable m3/h range, not ~0.003 m3/s
|
||||
const flowKey = Object.keys(output).find(k => k.startsWith('flow.predicted.downstream'));
|
||||
if (flowKey) {
|
||||
const flowVal = output[flowKey];
|
||||
assert.ok(typeof flowVal === 'number', 'Flow output should be a number');
|
||||
// m3/h values are typically 0-300, m3/s values are 0-0.08
|
||||
// If in canonical units it would be very small
|
||||
if (flowVal > 0) {
|
||||
assert.ok(flowVal > 0.1, `Flow value ${flowVal} looks like canonical m3/s, should be m3/h`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('getOutput NCogPercent is correctly derived from NCog', () => {
|
||||
const machine = new Machine(makeMachineConfig(), makeStateConfig({ state: { current: 'operational' } }));
|
||||
|
||||
machine.updateMeasuredPressure(1000, 'downstream', { timestamp: Date.now(), unit: 'mbar', childName: 'pt' });
|
||||
machine.updatePosition();
|
||||
|
||||
const output = machine.getOutput();
|
||||
const expected = Math.round(output.NCog * 100 * 100) / 100;
|
||||
assert.equal(output.NCogPercent, expected, 'NCogPercent should be NCog * 100, rounded to 2 decimals');
|
||||
});
|
||||
Reference in New Issue
Block a user