before functional changes by codex
This commit is contained in:
0
test/basic/.gitkeep
Normal file
0
test/basic/.gitkeep
Normal file
55
test/basic/constructor.basic.test.js
Normal file
55
test/basic/constructor.basic.test.js
Normal file
@@ -0,0 +1,55 @@
|
||||
const test = require('node:test');
|
||||
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');
|
||||
|
||||
test('_loadConfig 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(
|
||||
makeUiConfig({
|
||||
volume: '12.5',
|
||||
length: '9',
|
||||
resolution_L: '7',
|
||||
alpha: '0.5',
|
||||
n_inlets: '3',
|
||||
timeStep: '2',
|
||||
S_O_init: '1.1',
|
||||
}),
|
||||
);
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
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('_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);
|
||||
});
|
||||
42
test/basic/cstr-tick.basic.test.js
Normal file
42
test/basic/cstr-tick.basic.test.js
Normal file
@@ -0,0 +1,42 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
|
||||
const { Reactor_CSTR } = require('../../src/specificClass');
|
||||
const { makeReactorConfig } = require('../helpers/factories');
|
||||
|
||||
const NUM_SPECIES = 13;
|
||||
|
||||
test('Reactor_CSTR tick clips negative concentrations to zero', () => {
|
||||
const reactor = new Reactor_CSTR(
|
||||
makeReactorConfig({
|
||||
reactor_type: 'CSTR',
|
||||
volume: 1,
|
||||
n_inlets: 1,
|
||||
kla: NaN,
|
||||
S_O_init: 0.1,
|
||||
S_I_init: 0.1,
|
||||
S_S_init: 0.1,
|
||||
S_NH_init: 0.1,
|
||||
S_N2_init: 0.1,
|
||||
S_NO_init: 0.1,
|
||||
S_HCO_init: 0.1,
|
||||
X_I_init: 0.1,
|
||||
X_S_init: 0.1,
|
||||
X_H_init: 0.1,
|
||||
X_STO_init: 0.1,
|
||||
X_A_init: 0.1,
|
||||
X_TS_init: 0.1,
|
||||
}),
|
||||
);
|
||||
|
||||
reactor.asm = {
|
||||
compute_dC: () => Array(NUM_SPECIES).fill(0),
|
||||
};
|
||||
reactor.Fs[0] = 1;
|
||||
reactor.Cs_in[0] = Array(NUM_SPECIES).fill(0);
|
||||
|
||||
reactor.tick(1);
|
||||
|
||||
assert.equal(reactor.state.every((v) => Number.isFinite(v) && v >= 0), true);
|
||||
assert.equal(reactor.state.every((v) => v === 0), true);
|
||||
});
|
||||
38
test/basic/effluent-shape.basic.test.js
Normal file
38
test/basic/effluent-shape.basic.test.js
Normal file
@@ -0,0 +1,38 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
|
||||
const { Reactor_CSTR, Reactor_PFR } = require('../../src/specificClass');
|
||||
const { makeReactorConfig } = require('../helpers/factories');
|
||||
|
||||
test('CSTR getEffluent returns flat concentration vector', () => {
|
||||
const reactor = new Reactor_CSTR(makeReactorConfig({ reactor_type: 'CSTR', n_inlets: 1 }));
|
||||
reactor.state = Array.from({ length: 13 }, (_, i) => i + 1);
|
||||
reactor.Fs[0] = 5;
|
||||
|
||||
const effluent = reactor.getEffluent;
|
||||
|
||||
assert.equal(effluent.topic, 'Fluent');
|
||||
assert.equal(effluent.payload.inlet, 0);
|
||||
assert.equal(effluent.payload.F, 5);
|
||||
assert.deepEqual(effluent.payload.C, reactor.state);
|
||||
});
|
||||
|
||||
test('PFR getEffluent returns last slice concentration vector', () => {
|
||||
const reactor = new Reactor_PFR(
|
||||
makeReactorConfig({ reactor_type: 'PFR', n_inlets: 1, length: 10, resolution_L: 4 }),
|
||||
);
|
||||
|
||||
reactor.state = [
|
||||
Array(13).fill(10),
|
||||
Array(13).fill(20),
|
||||
Array(13).fill(30),
|
||||
Array(13).fill(40),
|
||||
];
|
||||
reactor.Fs[0] = 7;
|
||||
|
||||
const effluent = reactor.getEffluent;
|
||||
|
||||
assert.equal(effluent.topic, 'Fluent');
|
||||
assert.equal(effluent.payload.F, 7);
|
||||
assert.deepEqual(effluent.payload.C, Array(13).fill(40));
|
||||
});
|
||||
77
test/basic/input-routing.basic.test.js
Normal file
77
test/basic/input-routing.basic.test.js
Normal file
@@ -0,0 +1,77 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
|
||||
const NodeClass = require('../../src/nodeClass');
|
||||
const { makeNodeStub, makeREDStub } = require('../helpers/factories');
|
||||
|
||||
test('_attachInputHandler routes supported topics to source methods/setters', () => {
|
||||
const inst = Object.create(NodeClass.prototype);
|
||||
const node = makeNodeStub();
|
||||
const calls = [];
|
||||
|
||||
const source = {
|
||||
updateState(timestamp) {
|
||||
calls.push(['clock', timestamp]);
|
||||
},
|
||||
childRegistrationUtils: {
|
||||
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]);
|
||||
},
|
||||
});
|
||||
|
||||
inst.node = node;
|
||||
inst.RED = makeREDStub({
|
||||
childA: {
|
||||
source: { id: 'child-source-A' },
|
||||
},
|
||||
});
|
||||
inst.source = source;
|
||||
|
||||
inst._attachInputHandler();
|
||||
|
||||
const onInput = node._handlers.input;
|
||||
const sent = [];
|
||||
let doneCount = 0;
|
||||
|
||||
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++);
|
||||
|
||||
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);
|
||||
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']);
|
||||
});
|
||||
27
test/basic/pfr-operators.basic.test.js
Normal file
27
test/basic/pfr-operators.basic.test.js
Normal file
@@ -0,0 +1,27 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
|
||||
const { Reactor_PFR } = require('../../src/specificClass');
|
||||
const { makeReactorConfig } = require('../helpers/factories');
|
||||
|
||||
test('Reactor_PFR derivative operators have expected dimensions and boundary rows', () => {
|
||||
const reactor = new Reactor_PFR(
|
||||
makeReactorConfig({
|
||||
reactor_type: 'PFR',
|
||||
length: 12,
|
||||
resolution_L: 6,
|
||||
volume: 60,
|
||||
n_inlets: 1,
|
||||
}),
|
||||
);
|
||||
|
||||
assert.equal(reactor.D_op.length, reactor.n_x);
|
||||
assert.equal(reactor.D2_op.length, reactor.n_x);
|
||||
assert.equal(reactor.D_op.every((row) => row.length === reactor.n_x), true);
|
||||
assert.equal(reactor.D2_op.every((row) => row.length === reactor.n_x), true);
|
||||
|
||||
assert.deepEqual(reactor.D_op[0], Array(reactor.n_x).fill(0));
|
||||
assert.deepEqual(reactor.D_op[reactor.n_x - 1], Array(reactor.n_x).fill(0));
|
||||
assert.deepEqual(reactor.D2_op[0], Array(reactor.n_x).fill(0));
|
||||
assert.deepEqual(reactor.D2_op[reactor.n_x - 1], Array(reactor.n_x).fill(0));
|
||||
});
|
||||
39
test/basic/register-child.basic.test.js
Normal file
39
test/basic/register-child.basic.test.js
Normal file
@@ -0,0 +1,39 @@
|
||||
const test = require('node:test');
|
||||
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', () => {
|
||||
const inst = Object.create(NodeClass.prototype);
|
||||
const node = makeNodeStub();
|
||||
|
||||
inst.node = node;
|
||||
inst.config = {
|
||||
functionality: {
|
||||
positionVsParent: 'downstream',
|
||||
},
|
||||
};
|
||||
|
||||
const originalSetTimeout = global.setTimeout;
|
||||
const delays = [];
|
||||
|
||||
global.setTimeout = (fn, ms) => {
|
||||
delays.push(ms);
|
||||
fn();
|
||||
return 1;
|
||||
};
|
||||
|
||||
try {
|
||||
inst._registerChild();
|
||||
} finally {
|
||||
global.setTimeout = originalSetTimeout;
|
||||
}
|
||||
|
||||
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].payload, node.id);
|
||||
assert.equal(node._sent[0][2].positionVsParent, 'downstream');
|
||||
});
|
||||
8
test/basic/structure-module-load.basic.test.js
Normal file
8
test/basic/structure-module-load.basic.test.js
Normal file
@@ -0,0 +1,8 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
|
||||
test('reactor module load smoke', () => {
|
||||
assert.doesNotThrow(() => {
|
||||
require('../../reactor.js');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user