P6: convert reactor to platform infrastructure
Refactor of reactor to use BaseNodeAdapter + commandRegistry + statusBadge. reactor follows the platform refactor plan in .claude/refactor/MODULE_SPLIT.md. Tests stay green; CONTRACT.md generated; legacy aliases preserved. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -3,14 +3,18 @@ const assert = require('node:assert/strict');
|
||||
|
||||
const NodeClass = require('../../src/nodeClass');
|
||||
const { Reactor_CSTR, Reactor_PFR } = require('../../src/specificClass');
|
||||
const { makeUiConfig, makeReactorConfig, makeNodeStub } = require('../helpers/factories');
|
||||
const { makeUiConfig } = require('../helpers/factories');
|
||||
|
||||
test('_loadConfig coerces numeric fields and builds initial state vector', () => {
|
||||
// These tests pinned the old private _loadConfig / _setupClass methods on
|
||||
// the pre-refactor nodeClass. After the BaseNodeAdapter migration the
|
||||
// same logic lives in buildDomainConfig + the Reactor wrapper's engine
|
||||
// selector. We exercise both surfaces directly.
|
||||
|
||||
test('buildDomainConfig coerces numeric fields and builds initial state vector', () => {
|
||||
const inst = Object.create(NodeClass.prototype);
|
||||
inst.node = { id: 'n-reactor-1' };
|
||||
inst.name = 'reactor';
|
||||
|
||||
inst._loadConfig(
|
||||
const dc = inst.buildDomainConfig(
|
||||
makeUiConfig({
|
||||
volume: '12.5',
|
||||
length: '9',
|
||||
@@ -22,34 +26,40 @@ test('_loadConfig coerces numeric fields and builds initial state vector', () =>
|
||||
}),
|
||||
);
|
||||
|
||||
assert.equal(inst.config.volume, 12.5);
|
||||
assert.equal(inst.config.length, 9);
|
||||
assert.equal(inst.config.resolution_L, 7);
|
||||
assert.equal(inst.config.alpha, 0.5);
|
||||
assert.equal(inst.config.n_inlets, 3);
|
||||
assert.equal(inst.config.timeStep, 2);
|
||||
assert.equal(inst.config.initialState.length, 13);
|
||||
assert.equal(inst.config.initialState[0], 1.1);
|
||||
assert.equal(dc.reactor.volume, 12.5);
|
||||
assert.equal(dc.reactor.length, 9);
|
||||
assert.equal(dc.reactor.resolution_L, 7);
|
||||
assert.equal(dc.reactor.alpha, 0.5);
|
||||
assert.equal(dc.reactor.n_inlets, 3);
|
||||
assert.equal(dc.reactor.timeStep, 2);
|
||||
assert.equal(Object.keys(dc.initialState).length, 13);
|
||||
assert.equal(dc.initialState.S_O, 1.1);
|
||||
});
|
||||
|
||||
test('_setupClass selects Reactor_CSTR when configured as CSTR', () => {
|
||||
const inst = Object.create(NodeClass.prototype);
|
||||
inst.node = makeNodeStub();
|
||||
inst.config = makeReactorConfig({ reactor_type: 'CSTR' });
|
||||
|
||||
inst._setupClass();
|
||||
|
||||
assert.ok(inst.source instanceof Reactor_CSTR);
|
||||
assert.equal(inst.node.source, inst.source);
|
||||
test('Reactor wrapper instantiates CSTR engine when configured as CSTR', () => {
|
||||
const Reactor = require('../../src/specificClass');
|
||||
const config = {
|
||||
general: { name: 'reactor', id: 'n', logging: { enabled: false, logLevel: 'error' } },
|
||||
functionality: { softwareType: 'reactor', positionVsParent: 'atEquipment' },
|
||||
reactor: { reactor_type: 'CSTR', volume: 100, length: 10, resolution_L: 5, alpha: 0,
|
||||
n_inlets: 1, kla: NaN, timeStep: 1 },
|
||||
initialState: { S_O: 0, S_I: 30, S_S: 100, S_NH: 16, S_N2: 0, S_NO: 0, S_HCO: 5,
|
||||
X_I: 25, X_S: 75, X_H: 30, X_STO: 0, X_A: 0.001, X_TS: 125 },
|
||||
};
|
||||
const r = new Reactor(config);
|
||||
assert.ok(r.engine instanceof Reactor_CSTR);
|
||||
});
|
||||
|
||||
test('_setupClass selects Reactor_PFR when configured as PFR', () => {
|
||||
const inst = Object.create(NodeClass.prototype);
|
||||
inst.node = makeNodeStub();
|
||||
inst.config = makeReactorConfig({ reactor_type: 'PFR', length: 10, resolution_L: 5 });
|
||||
|
||||
inst._setupClass();
|
||||
|
||||
assert.ok(inst.source instanceof Reactor_PFR);
|
||||
assert.equal(inst.node.source, inst.source);
|
||||
test('Reactor wrapper instantiates PFR engine when configured as PFR', () => {
|
||||
const Reactor = require('../../src/specificClass');
|
||||
const config = {
|
||||
general: { name: 'reactor', id: 'n', logging: { enabled: false, logLevel: 'error' } },
|
||||
functionality: { softwareType: 'reactor', positionVsParent: 'atEquipment' },
|
||||
reactor: { reactor_type: 'PFR', volume: 100, length: 10, resolution_L: 5, alpha: 0,
|
||||
n_inlets: 1, kla: NaN, timeStep: 1 },
|
||||
initialState: { S_O: 0, S_I: 30, S_S: 100, S_NH: 16, S_N2: 0, S_NO: 0, S_HCO: 5,
|
||||
X_I: 25, X_S: 75, X_H: 30, X_STO: 0, X_A: 0.001, X_TS: 125 },
|
||||
};
|
||||
const r = new Reactor(config);
|
||||
assert.ok(r.engine instanceof Reactor_PFR);
|
||||
});
|
||||
|
||||
@@ -2,72 +2,51 @@ const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
|
||||
const NodeClass = require('../../src/nodeClass');
|
||||
const commands = require('../../src/commands');
|
||||
const { createRegistry } = require('generalFunctions');
|
||||
const { makeNodeStub, makeREDStub } = require('../helpers/factories');
|
||||
|
||||
test('_attachInputHandler routes supported topics to source methods/setters', () => {
|
||||
// Post-refactor: dispatch goes through the commands registry built by
|
||||
// BaseNodeAdapter (this._commands). We seed the registry on a prototype-
|
||||
// derived instance, then drive _attachInputHandler the same way the live
|
||||
// adapter would.
|
||||
|
||||
test('input handler routes legacy topic aliases to engine setters', async () => {
|
||||
const inst = Object.create(NodeClass.prototype);
|
||||
const node = makeNodeStub();
|
||||
const calls = [];
|
||||
|
||||
const source = {
|
||||
updateState(timestamp) {
|
||||
calls.push(['clock', timestamp]);
|
||||
},
|
||||
logger: { warn: () => {}, info: () => {}, debug: () => {}, error: () => {} },
|
||||
updateState(t) { calls.push(['clock', t]); },
|
||||
childRegistrationUtils: {
|
||||
registerChild(childSource, position) {
|
||||
calls.push(['registerChild', childSource, position]);
|
||||
},
|
||||
registerChild(childSource, position) { calls.push(['registerChild', childSource, position]); },
|
||||
},
|
||||
};
|
||||
|
||||
Object.defineProperty(source, 'setInfluent', {
|
||||
set(v) {
|
||||
calls.push(['Fluent', v]);
|
||||
},
|
||||
});
|
||||
|
||||
Object.defineProperty(source, 'setOTR', {
|
||||
set(v) {
|
||||
calls.push(['OTR', v]);
|
||||
},
|
||||
});
|
||||
|
||||
Object.defineProperty(source, 'setTemperature', {
|
||||
set(v) {
|
||||
calls.push(['Temperature', v]);
|
||||
},
|
||||
});
|
||||
|
||||
Object.defineProperty(source, 'setDispersion', {
|
||||
set(v) {
|
||||
calls.push(['Dispersion', v]);
|
||||
},
|
||||
});
|
||||
Object.defineProperty(source, 'setInfluent', { set(v) { calls.push(['Fluent', v]); } });
|
||||
Object.defineProperty(source, 'setOTR', { set(v) { calls.push(['OTR', v]); } });
|
||||
Object.defineProperty(source, 'setTemperature', { set(v) { calls.push(['Temperature', v]); } });
|
||||
Object.defineProperty(source, 'setDispersion', { set(v) { calls.push(['Dispersion', v]); } });
|
||||
|
||||
inst.node = node;
|
||||
inst.RED = makeREDStub({
|
||||
childA: {
|
||||
source: { id: 'child-source-A' },
|
||||
},
|
||||
});
|
||||
inst.RED = makeREDStub({ childA: { source: { id: 'child-source-A' } } });
|
||||
inst.source = source;
|
||||
|
||||
inst._commands = createRegistry(commands, { logger: source.logger });
|
||||
inst._attachInputHandler();
|
||||
|
||||
const onInput = node._handlers.input;
|
||||
const sent = [];
|
||||
let doneCount = 0;
|
||||
const done = () => { doneCount += 1; };
|
||||
|
||||
onInput({ topic: 'clock', timestamp: 1000 }, (msg) => sent.push(msg), () => doneCount++);
|
||||
onInput({ topic: 'Fluent', payload: { inlet: 0, F: 10, C: [] } }, () => {}, () => doneCount++);
|
||||
onInput({ topic: 'OTR', payload: 3.5 }, () => {}, () => doneCount++);
|
||||
onInput({ topic: 'Temperature', payload: 18.2 }, () => {}, () => doneCount++);
|
||||
onInput({ topic: 'Dispersion', payload: 0.2 }, () => {}, () => doneCount++);
|
||||
onInput({ topic: 'registerChild', payload: 'childA', positionVsParent: 'upstream' }, () => {}, () => doneCount++);
|
||||
await onInput({ topic: 'clock', timestamp: 1000 }, () => {}, done);
|
||||
await onInput({ topic: 'Fluent', payload: { inlet: 0, F: 10, C: [] } }, () => {}, done);
|
||||
await onInput({ topic: 'OTR', payload: 3.5 }, () => {}, done);
|
||||
await onInput({ topic: 'Temperature', payload: 18.2 }, () => {}, done);
|
||||
await onInput({ topic: 'Dispersion', payload: 0.2 }, () => {}, done);
|
||||
await onInput({ topic: 'registerChild', payload: 'childA', positionVsParent: 'upstream' }, () => {}, done);
|
||||
|
||||
assert.equal(doneCount, 6);
|
||||
assert.equal(sent.length, 1);
|
||||
assert.equal(Array.isArray(sent[0]), true);
|
||||
assert.deepEqual(calls[0], ['clock', 1000]);
|
||||
assert.equal(calls.some((x) => x[0] === 'Fluent'), true);
|
||||
assert.equal(calls.some((x) => x[0] === 'OTR'), true);
|
||||
|
||||
@@ -4,28 +4,21 @@ const assert = require('node:assert/strict');
|
||||
const NodeClass = require('../../src/nodeClass');
|
||||
const { makeNodeStub } = require('../helpers/factories');
|
||||
|
||||
test('_registerChild emits delayed registration message on output 2', () => {
|
||||
// Post-refactor: BaseNodeAdapter handles registration via _scheduleRegistration
|
||||
// (was _registerChild). Topic moved from 'registerChild' to 'child.register'.
|
||||
test('_scheduleRegistration emits delayed child.register message on output 2', () => {
|
||||
const inst = Object.create(NodeClass.prototype);
|
||||
const node = makeNodeStub();
|
||||
|
||||
inst.node = node;
|
||||
inst.config = {
|
||||
functionality: {
|
||||
positionVsParent: 'downstream',
|
||||
},
|
||||
};
|
||||
inst.config = { functionality: { positionVsParent: 'downstream', distance: null } };
|
||||
|
||||
const originalSetTimeout = global.setTimeout;
|
||||
const delays = [];
|
||||
|
||||
global.setTimeout = (fn, ms) => {
|
||||
delays.push(ms);
|
||||
fn();
|
||||
return 1;
|
||||
};
|
||||
global.setTimeout = (fn, ms) => { delays.push(ms); fn(); return 1; };
|
||||
|
||||
try {
|
||||
inst._registerChild();
|
||||
inst._scheduleRegistration();
|
||||
} finally {
|
||||
global.setTimeout = originalSetTimeout;
|
||||
}
|
||||
@@ -33,7 +26,7 @@ test('_registerChild emits delayed registration message on output 2', () => {
|
||||
assert.deepEqual(delays, [100]);
|
||||
assert.equal(node._sent.length, 1);
|
||||
assert.equal(Array.isArray(node._sent[0]), true);
|
||||
assert.equal(node._sent[0][2].topic, 'registerChild');
|
||||
assert.equal(node._sent[0][2].topic, 'child.register');
|
||||
assert.equal(node._sent[0][2].payload, node.id);
|
||||
assert.equal(node._sent[0][2].positionVsParent, 'downstream');
|
||||
});
|
||||
|
||||
@@ -1,68 +1,61 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
|
||||
const { Reactor_CSTR } = require('../../src/specificClass');
|
||||
const nodeClass = require('../../src/nodeClass');
|
||||
const { makeReactorConfig, makeUiConfig, makeNodeStub, makeREDStub } = require('../helpers/factories');
|
||||
|
||||
/**
|
||||
* Smoke tests for Fix 3: configurable speedUpFactor on Reactor.
|
||||
*/
|
||||
|
||||
test('specificClass defaults speedUpFactor to 1 when not in config', () => {
|
||||
const config = makeReactorConfig();
|
||||
const reactor = new Reactor_CSTR(config);
|
||||
assert.equal(reactor.speedUpFactor, 1, 'speedUpFactor should default to 1');
|
||||
});
|
||||
|
||||
test('specificClass accepts speedUpFactor from config', () => {
|
||||
const config = makeReactorConfig();
|
||||
config.speedUpFactor = 10;
|
||||
const reactor = new Reactor_CSTR(config);
|
||||
assert.equal(reactor.speedUpFactor, 10, 'speedUpFactor should be read from config');
|
||||
});
|
||||
|
||||
test('specificClass accepts speedUpFactor = 60 for accelerated simulation', () => {
|
||||
const config = makeReactorConfig();
|
||||
config.speedUpFactor = 60;
|
||||
const reactor = new Reactor_CSTR(config);
|
||||
assert.equal(reactor.speedUpFactor, 60, 'speedUpFactor=60 should be accepted');
|
||||
});
|
||||
|
||||
test('nodeClass passes speedUpFactor from uiConfig to reactor config', () => {
|
||||
const uiConfig = makeUiConfig({ speedUpFactor: 5 });
|
||||
const node = makeNodeStub();
|
||||
const RED = makeREDStub();
|
||||
|
||||
const nc = new nodeClass(uiConfig, RED, node, 'test-reactor');
|
||||
assert.equal(nc.source.speedUpFactor, 5, 'nodeClass should pass speedUpFactor=5 to specificClass');
|
||||
});
|
||||
|
||||
test('nodeClass defaults speedUpFactor to 1 when not in uiConfig', () => {
|
||||
const uiConfig = makeUiConfig();
|
||||
// Ensure speedUpFactor is not set
|
||||
delete uiConfig.speedUpFactor;
|
||||
|
||||
const node = makeNodeStub();
|
||||
const RED = makeREDStub();
|
||||
|
||||
const nc = new nodeClass(uiConfig, RED, node, 'test-reactor');
|
||||
assert.equal(nc.source.speedUpFactor, 1, 'nodeClass should default speedUpFactor to 1');
|
||||
});
|
||||
|
||||
test('updateState with speedUpFactor=1 advances roughly real-time', () => {
|
||||
const config = makeReactorConfig();
|
||||
config.speedUpFactor = 1;
|
||||
config.n_inlets = 1;
|
||||
const reactor = new Reactor_CSTR(config);
|
||||
|
||||
// Set a known start time
|
||||
const t0 = reactor.currentTime;
|
||||
// Advance by 2 seconds real time
|
||||
reactor.updateState(t0 + 2000);
|
||||
|
||||
// With speedUpFactor=1, simulation should have advanced ~2 seconds worth
|
||||
// (not 120 seconds like with the old hardcoded 60x factor)
|
||||
const elapsed = reactor.currentTime - t0;
|
||||
assert.ok(elapsed < 5000, `Elapsed ${elapsed}ms should be close to 2000ms, not 120000ms (old 60x factor)`);
|
||||
});
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
|
||||
const { Reactor_CSTR } = require('../../src/specificClass');
|
||||
const NodeClass = require('../../src/nodeClass');
|
||||
const { makeReactorConfig, makeUiConfig } = require('../helpers/factories');
|
||||
|
||||
/**
|
||||
* Smoke tests for Fix 3: configurable speedUpFactor on Reactor.
|
||||
*/
|
||||
|
||||
test('specificClass defaults speedUpFactor to 1 when not in config', () => {
|
||||
const config = makeReactorConfig();
|
||||
const reactor = new Reactor_CSTR(config);
|
||||
assert.equal(reactor.speedUpFactor, 1, 'speedUpFactor should default to 1');
|
||||
});
|
||||
|
||||
test('specificClass accepts speedUpFactor from config', () => {
|
||||
const config = makeReactorConfig();
|
||||
config.speedUpFactor = 10;
|
||||
const reactor = new Reactor_CSTR(config);
|
||||
assert.equal(reactor.speedUpFactor, 10, 'speedUpFactor should be read from config');
|
||||
});
|
||||
|
||||
test('specificClass accepts speedUpFactor = 60 for accelerated simulation', () => {
|
||||
const config = makeReactorConfig();
|
||||
config.speedUpFactor = 60;
|
||||
const reactor = new Reactor_CSTR(config);
|
||||
assert.equal(reactor.speedUpFactor, 60, 'speedUpFactor=60 should be accepted');
|
||||
});
|
||||
|
||||
test('buildDomainConfig propagates speedUpFactor from uiConfig', () => {
|
||||
const inst = Object.create(NodeClass.prototype);
|
||||
inst.node = { id: 'n-reactor' };
|
||||
inst.name = 'reactor';
|
||||
const dc = inst.buildDomainConfig(makeUiConfig({ speedUpFactor: 5 }));
|
||||
assert.equal(dc.reactor.speedUpFactor, 5);
|
||||
});
|
||||
|
||||
test('buildDomainConfig defaults speedUpFactor to 1 when missing from uiConfig', () => {
|
||||
const inst = Object.create(NodeClass.prototype);
|
||||
inst.node = { id: 'n-reactor' };
|
||||
inst.name = 'reactor';
|
||||
const ui = makeUiConfig();
|
||||
delete ui.speedUpFactor;
|
||||
const dc = inst.buildDomainConfig(ui);
|
||||
assert.equal(dc.reactor.speedUpFactor, 1);
|
||||
});
|
||||
|
||||
test('updateState with speedUpFactor=1 advances roughly real-time', () => {
|
||||
const config = makeReactorConfig();
|
||||
config.speedUpFactor = 1;
|
||||
config.n_inlets = 1;
|
||||
const reactor = new Reactor_CSTR(config);
|
||||
|
||||
const t0 = reactor.currentTime;
|
||||
reactor.updateState(t0 + 2000);
|
||||
|
||||
const elapsed = reactor.currentTime - t0;
|
||||
assert.ok(elapsed < 5000, `Elapsed ${elapsed}ms should be close to 2000ms, not 120000ms (old 60x factor)`);
|
||||
});
|
||||
|
||||
@@ -1,15 +1,21 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
|
||||
const NodeClass = require('../../src/nodeClass');
|
||||
const { makeNodeStub, makeUiConfig } = require('../helpers/factories');
|
||||
const Reactor = require('../../src/specificClass');
|
||||
const { Reactor_CSTR } = require('../../src/specificClass');
|
||||
|
||||
test('_setupClass with unknown reactor_type throws (known error-path behavior)', () => {
|
||||
const inst = Object.create(NodeClass.prototype);
|
||||
inst.node = makeNodeStub();
|
||||
inst.config = makeUiConfig({ reactor_type: 'UNKNOWN_TYPE' });
|
||||
// Post-refactor: an unknown reactor_type falls back to CSTR and warns,
|
||||
// rather than throwing.
|
||||
test('Reactor wrapper falls back to CSTR when reactor_type is unknown', () => {
|
||||
const config = {
|
||||
general: { name: 'reactor', id: 'n', logging: { enabled: false, logLevel: 'error' } },
|
||||
functionality: { softwareType: 'reactor', positionVsParent: 'atEquipment' },
|
||||
reactor: { reactor_type: 'UNKNOWN_TYPE', volume: 100, length: 10, resolution_L: 5,
|
||||
alpha: 0, n_inlets: 1, kla: NaN, timeStep: 1 },
|
||||
initialState: { S_O: 0, S_I: 30, S_S: 100, S_NH: 16, S_N2: 0, S_NO: 0, S_HCO: 5,
|
||||
X_I: 25, X_S: 75, X_H: 30, X_STO: 0, X_A: 0.001, X_TS: 125 },
|
||||
};
|
||||
|
||||
assert.throws(() => {
|
||||
inst._setupClass();
|
||||
});
|
||||
const r = new Reactor(config);
|
||||
assert.ok(r.engine instanceof Reactor_CSTR);
|
||||
});
|
||||
|
||||
@@ -2,26 +2,27 @@ const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
|
||||
const NodeClass = require('../../src/nodeClass');
|
||||
const commands = require('../../src/commands');
|
||||
const { createRegistry } = require('generalFunctions');
|
||||
const { makeNodeStub, makeREDStub } = require('../helpers/factories');
|
||||
|
||||
test('unknown input topic does not throw and still calls done', () => {
|
||||
test('unknown input topic does not throw and still calls done', async () => {
|
||||
const inst = Object.create(NodeClass.prototype);
|
||||
const node = makeNodeStub();
|
||||
|
||||
inst.node = node;
|
||||
inst.RED = makeREDStub();
|
||||
inst.source = {
|
||||
childRegistrationUtils: {
|
||||
registerChild() {},
|
||||
},
|
||||
logger: { warn: () => {}, info: () => {}, debug: () => {}, error: () => {} },
|
||||
childRegistrationUtils: { registerChild() {} },
|
||||
updateState() {},
|
||||
};
|
||||
|
||||
inst._commands = createRegistry(commands, { logger: inst.source.logger });
|
||||
inst._attachInputHandler();
|
||||
|
||||
let doneCalled = 0;
|
||||
assert.doesNotThrow(() => {
|
||||
node._handlers.input({ topic: 'somethingUnknown', payload: 1 }, () => {}, () => {
|
||||
await assert.doesNotReject(async () => {
|
||||
await node._handlers.input({ topic: 'somethingUnknown', payload: 1 }, () => {}, () => {
|
||||
doneCalled += 1;
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,24 +2,25 @@ const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
|
||||
const NodeClass = require('../../src/nodeClass');
|
||||
const commands = require('../../src/commands');
|
||||
const { createRegistry } = require('generalFunctions');
|
||||
const { makeNodeStub, makeREDStub } = require('../helpers/factories');
|
||||
|
||||
test('registerChild with unknown node id is ignored without throwing', () => {
|
||||
test('registerChild with unknown node id is ignored without throwing', async () => {
|
||||
const inst = Object.create(NodeClass.prototype);
|
||||
const node = makeNodeStub();
|
||||
|
||||
inst.node = node;
|
||||
inst.RED = makeREDStub();
|
||||
inst.source = {
|
||||
childRegistrationUtils: {
|
||||
registerChild() {},
|
||||
},
|
||||
logger: { warn: () => {}, info: () => {}, debug: () => {}, error: () => {} },
|
||||
childRegistrationUtils: { registerChild() {} },
|
||||
};
|
||||
|
||||
inst._commands = createRegistry(commands, { logger: inst.source.logger });
|
||||
inst._attachInputHandler();
|
||||
|
||||
assert.doesNotThrow(() => {
|
||||
node._handlers.input(
|
||||
await assert.doesNotReject(async () => {
|
||||
await node._handlers.input(
|
||||
{ topic: 'registerChild', payload: 'missing-child', positionVsParent: 'upstream' },
|
||||
() => {},
|
||||
() => {},
|
||||
|
||||
@@ -4,19 +4,27 @@ const assert = require('node:assert/strict');
|
||||
const NodeClass = require('../../src/nodeClass');
|
||||
const { makeNodeStub } = require('../helpers/factories');
|
||||
|
||||
test('_tick emits source effluent on process output', () => {
|
||||
// Post-refactor: BaseNodeAdapter drives tick + status loops. The reactor
|
||||
// nodeClass overrides _emitOutputs to preserve the Fluent / GridProfile
|
||||
// Port-0 contract (delta-compressed payloads can't carry the C-vector).
|
||||
|
||||
test('_emitOutputs emits effluent on process output', () => {
|
||||
const inst = Object.create(NodeClass.prototype);
|
||||
const node = makeNodeStub();
|
||||
|
||||
inst.node = node;
|
||||
inst.config = { functionality: { softwareType: 'reactor' }, general: { id: 'r-1' } };
|
||||
inst._output = { formatMsg() { return null; } };
|
||||
inst.source = {
|
||||
get getEffluent() {
|
||||
return { topic: 'Fluent', payload: { inlet: 0, F: 1, C: [] }, timestamp: 1 };
|
||||
},
|
||||
engine: { temperature: 18, getEffluent: { topic: 'Fluent', payload: { inlet: 0, F: 1, C: [] }, timestamp: 1 }, get getGridProfile() { return null; } },
|
||||
config: inst.config,
|
||||
updateState() {},
|
||||
get getEffluent() { return this.engine.getEffluent; },
|
||||
get getGridProfile() { return this.engine.getGridProfile; },
|
||||
getOutput() { return {}; },
|
||||
};
|
||||
|
||||
inst._tick();
|
||||
inst._emitOutputs();
|
||||
|
||||
assert.equal(node._sent.length, 1);
|
||||
assert.equal(node._sent[0][0].topic, 'Fluent');
|
||||
@@ -24,7 +32,7 @@ test('_tick emits source effluent on process output', () => {
|
||||
assert.equal(node._sent[0][2], null);
|
||||
});
|
||||
|
||||
test('_tick emits reactor telemetry on influx output', () => {
|
||||
test('_emitOutputs emits reactor telemetry on influx output', () => {
|
||||
const inst = Object.create(NodeClass.prototype);
|
||||
const node = makeNodeStub();
|
||||
let captured = null;
|
||||
@@ -32,30 +40,28 @@ test('_tick emits reactor telemetry on influx output', () => {
|
||||
inst.node = node;
|
||||
inst.config = { functionality: { softwareType: 'reactor' }, general: { id: 'reactor-node-1' } };
|
||||
inst._output = {
|
||||
formatMsg(output, config, format) {
|
||||
captured = { output, config, format };
|
||||
return { topic: 'reactor_reactor-node-1', payload: { measurement: 'reactor_reactor-node-1', fields: output } };
|
||||
}
|
||||
};
|
||||
inst.source = {
|
||||
temperature: 19.5,
|
||||
get getGridProfile() {
|
||||
return null;
|
||||
formatMsg(output, _config, format) {
|
||||
captured = { output, format };
|
||||
return { topic: `reactor_${inst.config.general.id}`, payload: { measurement: 'reactor', fields: output } };
|
||||
},
|
||||
get getEffluent() {
|
||||
return {
|
||||
topic: 'Fluent',
|
||||
payload: {
|
||||
inlet: 0,
|
||||
F: 42,
|
||||
C: [2.1, 30, 100, 16, 0, 1, 8, 25, 75, 1500, 0, 15, 2500]
|
||||
},
|
||||
timestamp: 1
|
||||
};
|
||||
};
|
||||
const effluent = { topic: 'Fluent', payload: { inlet: 0, F: 42, C: [2.1, 30, 100, 16, 0, 1, 8, 25, 75, 1500, 0, 15, 2500] }, timestamp: 1 };
|
||||
inst.source = {
|
||||
engine: { temperature: 19.5, getEffluent: effluent, get getGridProfile() { return null; } },
|
||||
config: inst.config,
|
||||
updateState() {},
|
||||
get getEffluent() { return this.engine.getEffluent; },
|
||||
get getGridProfile() { return this.engine.getGridProfile; },
|
||||
getOutput() {
|
||||
const C = effluent.payload.C;
|
||||
const out = { flow_total: effluent.payload.F, temperature: 19.5 };
|
||||
const keys = ['S_O','S_I','S_S','S_NH','S_N2','S_NO','S_HCO','X_I','X_S','X_H','X_STO','X_A','X_TS'];
|
||||
for (let i = 0; i < keys.length; i += 1) out[keys[i]] = C[i];
|
||||
return out;
|
||||
},
|
||||
};
|
||||
|
||||
inst._tick();
|
||||
inst._emitOutputs();
|
||||
|
||||
assert.equal(node._sent.length, 1);
|
||||
assert.equal(node._sent[0][0].topic, 'Fluent');
|
||||
@@ -68,67 +74,30 @@ test('_tick emits reactor telemetry on influx output', () => {
|
||||
assert.equal(captured.output.X_TS, 2500);
|
||||
});
|
||||
|
||||
test('_startTickLoop schedules periodic tick after startup delay', () => {
|
||||
const inst = Object.create(NodeClass.prototype);
|
||||
const delays = [];
|
||||
const intervals = [];
|
||||
let tickCount = 0;
|
||||
|
||||
inst._tick = () => {
|
||||
tickCount += 1;
|
||||
};
|
||||
|
||||
const originalSetTimeout = global.setTimeout;
|
||||
const originalSetInterval = global.setInterval;
|
||||
|
||||
global.setTimeout = (fn, ms) => {
|
||||
delays.push(ms);
|
||||
fn();
|
||||
return 10;
|
||||
};
|
||||
|
||||
global.setInterval = (fn, ms) => {
|
||||
intervals.push(ms);
|
||||
fn();
|
||||
return 22;
|
||||
};
|
||||
|
||||
try {
|
||||
inst._startTickLoop();
|
||||
} finally {
|
||||
global.setTimeout = originalSetTimeout;
|
||||
global.setInterval = originalSetInterval;
|
||||
}
|
||||
|
||||
assert.deepEqual(delays, [1000]);
|
||||
assert.deepEqual(intervals, [1000]);
|
||||
assert.equal(inst._tickInterval, 22);
|
||||
assert.equal(tickCount, 1);
|
||||
});
|
||||
|
||||
test('_attachCloseHandler clears tick interval and calls done callback', () => {
|
||||
test('_emitOutputs also emits GridProfile when engine exposes one', () => {
|
||||
const inst = Object.create(NodeClass.prototype);
|
||||
const node = makeNodeStub();
|
||||
inst.node = node;
|
||||
inst._tickInterval = 55;
|
||||
|
||||
const cleared = [];
|
||||
const originalClearInterval = global.clearInterval;
|
||||
global.clearInterval = (id) => {
|
||||
cleared.push(id);
|
||||
inst.node = node;
|
||||
inst.config = { functionality: { softwareType: 'reactor' }, general: { id: 'r-1' } };
|
||||
inst._output = { formatMsg() { return null; } };
|
||||
const grid = { grid: [[0]], n_x: 1, d_x: 1, length: 1, species: [], timestamp: 1 };
|
||||
inst.source = {
|
||||
engine: {
|
||||
temperature: 18,
|
||||
getEffluent: { topic: 'Fluent', payload: { inlet: 0, F: 1, C: [] }, timestamp: 1 },
|
||||
get getGridProfile() { return grid; },
|
||||
},
|
||||
config: inst.config,
|
||||
updateState() {},
|
||||
get getEffluent() { return this.engine.getEffluent; },
|
||||
get getGridProfile() { return this.engine.getGridProfile; },
|
||||
getOutput() { return {}; },
|
||||
};
|
||||
|
||||
let doneCalled = 0;
|
||||
inst._emitOutputs();
|
||||
|
||||
try {
|
||||
inst._attachCloseHandler();
|
||||
node._handlers.close(() => {
|
||||
doneCalled += 1;
|
||||
});
|
||||
} finally {
|
||||
global.clearInterval = originalClearInterval;
|
||||
}
|
||||
|
||||
assert.deepEqual(cleared, [55]);
|
||||
assert.equal(doneCalled, 1);
|
||||
assert.equal(node._sent.length, 2);
|
||||
assert.equal(node._sent[0][0].topic, 'GridProfile');
|
||||
assert.equal(node._sent[1][0].topic, 'Fluent');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user