125 lines
3.4 KiB
JavaScript
125 lines
3.4 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const EventEmitter = require('events');
|
|
|
|
const Valve = require('../../src/specificClass');
|
|
|
|
function buildValve({ runtimeOptions = {} } = {}) {
|
|
return new Valve(
|
|
{
|
|
general: {
|
|
name: 'valve-fluid-test',
|
|
logging: { enabled: false, logLevel: 'error' },
|
|
},
|
|
asset: {
|
|
supplier: 'binder',
|
|
category: 'valve',
|
|
type: 'control',
|
|
model: 'ECDV',
|
|
unit: 'm3/h',
|
|
},
|
|
functionality: {
|
|
positionVsParent: 'atEquipment',
|
|
},
|
|
},
|
|
{
|
|
general: {
|
|
logging: { enabled: false, logLevel: 'error' },
|
|
},
|
|
movement: { speed: 1 },
|
|
time: { starting: 0, warmingup: 0, stopping: 0, coolingdown: 0 },
|
|
},
|
|
runtimeOptions
|
|
);
|
|
}
|
|
|
|
function buildFluidSource({
|
|
id,
|
|
softwareType,
|
|
serviceType = null,
|
|
status = 'resolved',
|
|
}) {
|
|
const emitter = new EventEmitter();
|
|
let contract = { status, serviceType };
|
|
return {
|
|
emitter,
|
|
config: {
|
|
general: { id, name: id },
|
|
functionality: { softwareType },
|
|
asset: {
|
|
serviceType: serviceType || undefined,
|
|
},
|
|
},
|
|
getFluidContract() {
|
|
return { ...contract };
|
|
},
|
|
setFluidContract(next) {
|
|
contract = { ...contract, ...next };
|
|
},
|
|
};
|
|
}
|
|
|
|
test('valve flags mismatch for direct machine source with incompatible fluid', () => {
|
|
const valve = buildValve({ runtimeOptions: { serviceType: 'gas' } });
|
|
const source = buildFluidSource({
|
|
id: 'machine-1',
|
|
softwareType: 'machine',
|
|
serviceType: 'liquid',
|
|
});
|
|
|
|
assert.equal(valve.registerChild(source, 'machine'), true);
|
|
const compatibility = valve.getFluidCompatibility();
|
|
assert.equal(compatibility.status, 'mismatch');
|
|
assert.equal(compatibility.expectedServiceType, 'gas');
|
|
assert.equal(compatibility.receivedServiceType, 'liquid');
|
|
|
|
valve.destroy();
|
|
});
|
|
|
|
test('valve flags conflict when grouped upstream sources expose mixed fluids', () => {
|
|
const valve = buildValve();
|
|
const machine = buildFluidSource({
|
|
id: 'machine-1',
|
|
softwareType: 'machine',
|
|
serviceType: 'liquid',
|
|
});
|
|
const group = buildFluidSource({
|
|
id: 'vgc-1',
|
|
softwareType: 'valvegroupcontrol',
|
|
serviceType: 'gas',
|
|
});
|
|
|
|
assert.equal(valve.registerChild(machine, 'machine'), true);
|
|
assert.equal(valve.registerChild(group, 'valvegroupcontrol'), true);
|
|
|
|
const compatibility = valve.getFluidCompatibility();
|
|
assert.equal(compatibility.status, 'conflict');
|
|
assert.deepEqual(new Set(compatibility.upstreamServiceTypes), new Set(['liquid', 'gas']));
|
|
|
|
valve.destroy();
|
|
});
|
|
|
|
test('valve updates compatibility when upstream group fluid contract changes', async () => {
|
|
const valve = buildValve({ runtimeOptions: { serviceType: 'gas' } });
|
|
const group = buildFluidSource({
|
|
id: 'vgc-1',
|
|
softwareType: 'valvegroupcontrol',
|
|
serviceType: 'gas',
|
|
});
|
|
|
|
assert.equal(valve.registerChild(group, 'valvegroupcontrol'), true);
|
|
assert.equal(valve.getFluidCompatibility().status, 'match');
|
|
|
|
group.setFluidContract({ serviceType: 'liquid' });
|
|
group.emitter.emit('fluidContractChange');
|
|
|
|
// Event handlers run synchronously; await microtask for deterministic test sequencing.
|
|
await Promise.resolve();
|
|
|
|
const compatibility = valve.getFluidCompatibility();
|
|
assert.equal(compatibility.status, 'mismatch');
|
|
assert.equal(compatibility.receivedServiceType, 'liquid');
|
|
|
|
valve.destroy();
|
|
});
|