Files
reactor/test/basic/input-routing.basic.test.js

57 lines
2.6 KiB
JavaScript
Raw Normal View History

2026-02-19 17:37:42 +01:00
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');
2026-02-19 17:37:42 +01:00
const { makeNodeStub, makeREDStub } = 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 () => {
2026-02-19 17:37:42 +01:00
const inst = Object.create(NodeClass.prototype);
const node = makeNodeStub();
const calls = [];
const source = {
logger: { warn: () => {}, info: () => {}, debug: () => {}, error: () => {} },
updateState(t) { calls.push(['clock', t]); },
2026-02-19 17:37:42 +01:00
childRegistrationUtils: {
registerChild(childSource, position) { calls.push(['registerChild', childSource, position]); },
2026-02-19 17:37:42 +01:00
},
};
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]); } });
2026-02-19 17:37:42 +01:00
inst.node = node;
inst.RED = makeREDStub({ childA: { source: { id: 'child-source-A' } } });
2026-02-19 17:37:42 +01:00
inst.source = source;
inst._commands = createRegistry(commands, { logger: source.logger });
2026-02-19 17:37:42 +01:00
inst._attachInputHandler();
const onInput = node._handlers.input;
let doneCount = 0;
const done = () => { doneCount += 1; };
2026-02-19 17:37:42 +01:00
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);
2026-02-19 17:37:42 +01:00
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']);
});