Files
rotatingMachine/test/integration/emergency-stop.integration.test.js
znetsixe 07af7cef40 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>
2026-04-07 13:41:00 +02:00

60 lines
2.7 KiB
JavaScript

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
});