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)`);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user