54 lines
1.8 KiB
JavaScript
54 lines
1.8 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
|
|
const Machine = require('../../src/specificClass');
|
|
const { makeMachineConfig, makeStateConfig, makeChildMeasurement } = require('../helpers/factories');
|
|
|
|
test('registerChild listens to measurement events and stores measured pressure', async () => {
|
|
const machine = new Machine(makeMachineConfig(), makeStateConfig());
|
|
const child = makeChildMeasurement({ positionVsParent: 'downstream', type: 'pressure', unit: 'mbar' });
|
|
|
|
machine.registerChild(child, 'measurement');
|
|
|
|
child.measurements
|
|
.type('pressure')
|
|
.variant('measured')
|
|
.position('downstream')
|
|
.value(123, Date.now(), 'mbar');
|
|
|
|
const stored = machine.measurements
|
|
.type('pressure')
|
|
.variant('measured')
|
|
.position('downstream')
|
|
.getCurrentValue('mbar');
|
|
|
|
assert.equal(typeof stored, 'number');
|
|
assert.equal(Math.round(stored), 123);
|
|
});
|
|
|
|
test('registerChild deduplicates listeners on re-registration', async () => {
|
|
const machine = new Machine(makeMachineConfig(), makeStateConfig());
|
|
const child = makeChildMeasurement({ id: 'pt-dup', positionVsParent: 'downstream', type: 'pressure', unit: 'mbar' });
|
|
const eventName = 'pressure.measured.downstream';
|
|
|
|
let handlerCalls = 0;
|
|
const originalUpdatePressure = machine.updateMeasuredPressure.bind(machine);
|
|
machine.updateMeasuredPressure = (...args) => {
|
|
handlerCalls += 1;
|
|
return originalUpdatePressure(...args);
|
|
};
|
|
|
|
machine.registerChild(child, 'measurement');
|
|
machine.registerChild(child, 'measurement');
|
|
|
|
assert.equal(child.measurements.emitter.listenerCount(eventName), 1);
|
|
|
|
child.measurements
|
|
.type('pressure')
|
|
.variant('measured')
|
|
.position('downstream')
|
|
.value(321, Date.now(), 'mbar');
|
|
|
|
assert.equal(handlerCalls, 1);
|
|
});
|