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:
@@ -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