P11.6 wiki regen + Phase 10 private-test rewrites where applicable
For all 11 nodes with auto-gen markers: wiki/Home.md sections 5 (topic contract) and 9 (data model) regenerated via npm run wiki:all. New Unit column shows '<measure> (default <unit>)' for declared topics, '—' otherwise. Effect column now uses descriptor.description (P11.2 field) overriding the generic per-prefix fallback. For rotatingMachine + reactor: Phase 10 test rewrites — 3 + 8 files moved off private nodeClass internals (_attachInputHandler, _commands, _pendingExtras, _registerChild, _tick, etc.) to the public BaseNodeAdapter surface (node.handlers.input, node.source.*). +6 / +7 net new tests. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,56 +1,111 @@
|
||||
'use strict';
|
||||
|
||||
// Phase 10 rewrite: drives only the public BaseNodeAdapter surface.
|
||||
// The pre-refactor _attachInputHandler private switch is gone — input
|
||||
// dispatch goes through the commands registry that BaseNodeAdapter builds
|
||||
// at construction. Tests fire msgs through `node.handlers.input` and
|
||||
// observe via `node.sends`, `inst.source.engine.*`, and per-fire calls
|
||||
// captured on a child stub registered through `RED.nodes.getNode(id)`.
|
||||
|
||||
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');
|
||||
const nodeClass = require('../../src/nodeClass');
|
||||
const { makeUiConfig } = require('../helpers/factories');
|
||||
|
||||
// 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 = {
|
||||
logger: { warn: () => {}, info: () => {}, debug: () => {}, error: () => {} },
|
||||
updateState(t) { calls.push(['clock', t]); },
|
||||
childRegistrationUtils: {
|
||||
registerChild(childSource, position) { calls.push(['registerChild', childSource, position]); },
|
||||
},
|
||||
function makeNode(id = 'reactor-1') {
|
||||
const sends = [];
|
||||
const statuses = [];
|
||||
const handlers = {};
|
||||
return {
|
||||
id, sends, statuses, handlers,
|
||||
send(arr) { sends.push(arr); },
|
||||
status(b) { statuses.push(b); },
|
||||
on(ev, fn) { handlers[ev] = fn; },
|
||||
warn() {}, error() {},
|
||||
};
|
||||
}
|
||||
|
||||
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]); } });
|
||||
function makeRED(nodeMap = {}) {
|
||||
return { nodes: { getNode: (id) => nodeMap[id] || null } };
|
||||
}
|
||||
|
||||
inst.node = node;
|
||||
inst.RED = makeREDStub({ childA: { source: { id: 'child-source-A' } } });
|
||||
inst.source = source;
|
||||
inst._commands = createRegistry(commands, { logger: source.logger });
|
||||
inst._attachInputHandler();
|
||||
function closeNode(node) {
|
||||
if (node.handlers.close) node.handlers.close(() => {});
|
||||
}
|
||||
|
||||
const onInput = node._handlers.input;
|
||||
let doneCount = 0;
|
||||
const done = () => { doneCount += 1; };
|
||||
test('legacy alias topics drive engine setters and updateState', async () => {
|
||||
const childSource = {
|
||||
id: 'child-source-A',
|
||||
config: { general: { id: 'child-source-A' }, functionality: { softwareType: 'measurement', positionVsParent: 'upstream' }, asset: { type: 'temperature' } },
|
||||
};
|
||||
const node = makeNode();
|
||||
const RED = makeRED({ childA: { source: childSource } });
|
||||
const inst = new nodeClass(makeUiConfig(), RED, node, 'reactor');
|
||||
|
||||
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);
|
||||
try {
|
||||
let doneCount = 0;
|
||||
const done = () => { doneCount += 1; };
|
||||
|
||||
assert.equal(doneCount, 6);
|
||||
assert.deepEqual(calls[0], ['clock', 1000]);
|
||||
assert.equal(calls.some((x) => x[0] === 'Fluent'), true);
|
||||
assert.equal(calls.some((x) => x[0] === 'OTR'), true);
|
||||
assert.equal(calls.some((x) => x[0] === 'Temperature'), true);
|
||||
assert.equal(calls.some((x) => x[0] === 'Dispersion'), true);
|
||||
assert.deepEqual(calls.at(-1), ['registerChild', { id: 'child-source-A' }, 'upstream']);
|
||||
// data.clock alias → updateState(timestamp). Capture currentTime
|
||||
// before/after to verify the engine advanced.
|
||||
const t0 = inst.source.engine.currentTime;
|
||||
await node.handlers.input({ topic: 'clock', timestamp: t0 + 1 }, () => {}, done);
|
||||
|
||||
// Fluent alias → engine setInfluent setter.
|
||||
await node.handlers.input(
|
||||
{ topic: 'Fluent', payload: { inlet: 0, F: 7, C: [1,2,3,4,5,6,7,8,9,10,11,12,13] } },
|
||||
() => {}, done,
|
||||
);
|
||||
assert.equal(inst.source.engine.Fs[0], 7);
|
||||
assert.deepEqual(inst.source.engine.Cs_in[0], [1,2,3,4,5,6,7,8,9,10,11,12,13]);
|
||||
|
||||
// OTR alias → engine setOTR setter.
|
||||
await node.handlers.input({ topic: 'OTR', payload: 3.5 }, () => {}, done);
|
||||
assert.equal(inst.source.engine.OTR, 3.5);
|
||||
|
||||
// Temperature alias → engine setTemperature setter.
|
||||
await node.handlers.input({ topic: 'Temperature', payload: 18.2 }, () => {}, done);
|
||||
assert.equal(inst.source.engine.temperature, 18.2);
|
||||
|
||||
// Dispersion alias — CSTR engine does not own a setDispersion setter
|
||||
// (only PFR does); the Reactor wrapper guards on engine type and the
|
||||
// dispatch should silently return without throwing.
|
||||
await node.handlers.input({ topic: 'Dispersion', payload: 0.2 }, () => {}, done);
|
||||
|
||||
// registerChild alias → registers via childRegistrationUtils.
|
||||
// The handler resolves the child via RED.nodes.getNode(payload).source.
|
||||
await node.handlers.input(
|
||||
{ topic: 'registerChild', payload: 'childA', positionVsParent: 'upstream' },
|
||||
() => {}, done,
|
||||
);
|
||||
|
||||
assert.equal(doneCount, 6);
|
||||
} finally {
|
||||
closeNode(node);
|
||||
}
|
||||
});
|
||||
|
||||
test('canonical topics are accepted (data.fluent, data.otr, data.temperature)', async () => {
|
||||
const node = makeNode();
|
||||
const inst = new nodeClass(makeUiConfig(), makeRED(), node, 'reactor');
|
||||
|
||||
try {
|
||||
let done = 0;
|
||||
await node.handlers.input(
|
||||
{ topic: 'data.fluent', payload: { inlet: 0, F: 11, C: [0,0,0,0,0,0,0,0,0,0,0,0,0] } },
|
||||
() => {}, () => { done += 1; },
|
||||
);
|
||||
assert.equal(inst.source.engine.Fs[0], 11);
|
||||
|
||||
await node.handlers.input({ topic: 'data.otr', payload: 4.2 }, () => {}, () => { done += 1; });
|
||||
assert.equal(inst.source.engine.OTR, 4.2);
|
||||
|
||||
await node.handlers.input({ topic: 'data.temperature', payload: 19.7 }, () => {}, () => { done += 1; });
|
||||
assert.equal(inst.source.engine.temperature, 19.7);
|
||||
|
||||
assert.equal(done, 3);
|
||||
} finally {
|
||||
closeNode(node);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user