before functional changes by codex
This commit is contained in:
0
test/integration/.gitkeep
Normal file
0
test/integration/.gitkeep
Normal file
26
test/integration/measurement-temperature.integration.test.js
Normal file
26
test/integration/measurement-temperature.integration.test.js
Normal file
@@ -0,0 +1,26 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
|
||||
const { Reactor_CSTR } = require('../../src/specificClass');
|
||||
const { makeReactorConfig, makeMeasurementChild } = require('../helpers/factories');
|
||||
|
||||
test('measurement child temperature event updates reactor temperature', () => {
|
||||
const reactor = new Reactor_CSTR(makeReactorConfig({ reactor_type: 'CSTR' }));
|
||||
|
||||
const measurement = makeMeasurementChild({
|
||||
type: 'temperature',
|
||||
distance: 'atEquipment',
|
||||
positionVsParent: 'upstream',
|
||||
});
|
||||
|
||||
reactor.registerChild(measurement, 'measurement');
|
||||
|
||||
measurement.measurements.emitter.emit('temperature.measured.atEquipment', {
|
||||
childName: 'T-1',
|
||||
value: 27.5,
|
||||
unit: 'C',
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
|
||||
assert.equal(reactor.temperature, 27.5);
|
||||
});
|
||||
85
test/integration/otr-kla.integration.test.js
Normal file
85
test/integration/otr-kla.integration.test.js
Normal file
@@ -0,0 +1,85 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
|
||||
const { Reactor_CSTR, Reactor_PFR } = require('../../src/specificClass');
|
||||
const { makeReactorConfig } = require('../helpers/factories');
|
||||
|
||||
const NUM_SPECIES = 13;
|
||||
|
||||
test('CSTR uses external OTR when kla is NaN', () => {
|
||||
const reactor = new Reactor_CSTR(
|
||||
makeReactorConfig({ reactor_type: 'CSTR', kla: NaN, n_inlets: 1 }),
|
||||
);
|
||||
|
||||
reactor.asm = {
|
||||
compute_dC: () => Array(NUM_SPECIES).fill(0),
|
||||
};
|
||||
reactor.Fs[0] = 0;
|
||||
reactor.OTR = 4;
|
||||
reactor.state = Array(NUM_SPECIES).fill(0);
|
||||
|
||||
reactor.tick(1);
|
||||
|
||||
assert.equal(reactor.state[0], 4);
|
||||
});
|
||||
|
||||
test('CSTR uses kla-based oxygen transfer when kla is finite', () => {
|
||||
const reactor = new Reactor_CSTR(
|
||||
makeReactorConfig({ reactor_type: 'CSTR', kla: 2, n_inlets: 1 }),
|
||||
);
|
||||
|
||||
reactor.asm = {
|
||||
compute_dC: () => Array(NUM_SPECIES).fill(0),
|
||||
};
|
||||
reactor.Fs[0] = 0;
|
||||
reactor.OTR = 1;
|
||||
reactor.state = Array(NUM_SPECIES).fill(0);
|
||||
|
||||
const expected = reactor._calcOTR(0, reactor.temperature);
|
||||
reactor.tick(1);
|
||||
|
||||
assert.ok(Math.abs(reactor.state[0] - expected) < 1e-9);
|
||||
});
|
||||
|
||||
test('PFR uses external OTR branch when kla is NaN', () => {
|
||||
const reactor = new Reactor_PFR(
|
||||
makeReactorConfig({ reactor_type: 'PFR', kla: NaN, n_inlets: 1, length: 8, resolution_L: 6, volume: 40 }),
|
||||
);
|
||||
|
||||
reactor.asm = {
|
||||
compute_dC: () => Array(NUM_SPECIES).fill(0),
|
||||
};
|
||||
reactor.Fs[0] = 0;
|
||||
reactor.D = 0;
|
||||
reactor.OTR = 3;
|
||||
reactor.state = Array.from({ length: reactor.n_x }, () => Array(NUM_SPECIES).fill(0));
|
||||
|
||||
reactor.tick(1);
|
||||
|
||||
assert.equal(reactor.state[1][0], 4.5);
|
||||
assert.equal(reactor.state[2][0], 4.5);
|
||||
assert.equal(reactor.state[3][0], 4.5);
|
||||
assert.equal(reactor.state[4][0], 4.5);
|
||||
});
|
||||
|
||||
test('PFR uses kla-based transfer branch when kla is finite', () => {
|
||||
const reactor = new Reactor_PFR(
|
||||
makeReactorConfig({ reactor_type: 'PFR', kla: 1, n_inlets: 1, length: 8, resolution_L: 6, volume: 40 }),
|
||||
);
|
||||
|
||||
reactor.asm = {
|
||||
compute_dC: () => Array(NUM_SPECIES).fill(0),
|
||||
};
|
||||
reactor.Fs[0] = 0;
|
||||
reactor.D = 0;
|
||||
reactor.OTR = 0;
|
||||
reactor.state = Array.from({ length: reactor.n_x }, () => Array(NUM_SPECIES).fill(0));
|
||||
|
||||
const expected = reactor._calcOTR(0, reactor.temperature) * (reactor.n_x / (reactor.n_x - 2));
|
||||
reactor.tick(1);
|
||||
|
||||
assert.ok(Math.abs(reactor.state[1][0] - expected) < 1e-9);
|
||||
assert.ok(Math.abs(reactor.state[2][0] - expected) < 1e-9);
|
||||
assert.ok(Math.abs(reactor.state[3][0] - expected) < 1e-9);
|
||||
assert.ok(Math.abs(reactor.state[4][0] - expected) < 1e-9);
|
||||
});
|
||||
35
test/integration/pfr-boundary.integration.test.js
Normal file
35
test/integration/pfr-boundary.integration.test.js
Normal file
@@ -0,0 +1,35 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
|
||||
const { Reactor_PFR } = require('../../src/specificClass');
|
||||
const { makeReactorConfig } = require('../helpers/factories');
|
||||
|
||||
test('_applyBoundaryConditions enforces Danckwerts inlet and Neumann outlet for flowing case', () => {
|
||||
const reactor = new Reactor_PFR(
|
||||
makeReactorConfig({ reactor_type: 'PFR', n_inlets: 1, length: 10, resolution_L: 5, volume: 50, alpha: 0.2 }),
|
||||
);
|
||||
|
||||
reactor.Fs[0] = 2;
|
||||
reactor.Cs_in[0] = Array(13).fill(9);
|
||||
reactor.D = 1;
|
||||
|
||||
const state = Array.from({ length: reactor.n_x }, (_, i) => Array(13).fill(i));
|
||||
reactor._applyBoundaryConditions(state);
|
||||
|
||||
assert.deepEqual(state[reactor.n_x - 1], state[reactor.n_x - 2]);
|
||||
assert.equal(state[0].every((v) => Number.isFinite(v)), true);
|
||||
});
|
||||
|
||||
test('_applyBoundaryConditions copies first interior slice when no flow is present', () => {
|
||||
const reactor = new Reactor_PFR(
|
||||
makeReactorConfig({ reactor_type: 'PFR', n_inlets: 1, length: 10, resolution_L: 5, volume: 50 }),
|
||||
);
|
||||
|
||||
reactor.Fs[0] = 0;
|
||||
const state = Array.from({ length: reactor.n_x }, (_, i) => Array(13).fill(i + 10));
|
||||
|
||||
reactor._applyBoundaryConditions(state);
|
||||
|
||||
assert.deepEqual(state[0], state[1]);
|
||||
assert.deepEqual(state[reactor.n_x - 1], state[reactor.n_x - 2]);
|
||||
});
|
||||
23
test/integration/structure-examples.integration.test.js
Normal file
23
test/integration/structure-examples.integration.test.js
Normal file
@@ -0,0 +1,23 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
|
||||
const dir = path.resolve(__dirname, '../../examples');
|
||||
|
||||
function loadJson(file) {
|
||||
return JSON.parse(fs.readFileSync(path.join(dir, file), 'utf8'));
|
||||
}
|
||||
|
||||
test('examples package exists for reactor', () => {
|
||||
for (const file of ['README.md', 'basic.flow.json', 'integration.flow.json', 'edge.flow.json']) {
|
||||
assert.equal(fs.existsSync(path.join(dir, file)), true, file + ' missing');
|
||||
}
|
||||
});
|
||||
|
||||
test('example flows are parseable arrays for reactor', () => {
|
||||
for (const file of ['basic.flow.json', 'integration.flow.json', 'edge.flow.json']) {
|
||||
const parsed = loadJson(file);
|
||||
assert.equal(Array.isArray(parsed), true);
|
||||
}
|
||||
});
|
||||
89
test/integration/tick-loop.integration.test.js
Normal file
89
test/integration/tick-loop.integration.test.js
Normal file
@@ -0,0 +1,89 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
|
||||
const NodeClass = require('../../src/nodeClass');
|
||||
const { makeNodeStub } = require('../helpers/factories');
|
||||
|
||||
test('_tick emits source effluent on process output', () => {
|
||||
const inst = Object.create(NodeClass.prototype);
|
||||
const node = makeNodeStub();
|
||||
|
||||
inst.node = node;
|
||||
inst.source = {
|
||||
get getEffluent() {
|
||||
return { topic: 'Fluent', payload: { inlet: 0, F: 1, C: [] }, timestamp: 1 };
|
||||
},
|
||||
};
|
||||
|
||||
inst._tick();
|
||||
|
||||
assert.equal(node._sent.length, 1);
|
||||
assert.equal(node._sent[0][0].topic, 'Fluent');
|
||||
assert.equal(node._sent[0][1], null);
|
||||
assert.equal(node._sent[0][2], null);
|
||||
});
|
||||
|
||||
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', () => {
|
||||
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);
|
||||
};
|
||||
|
||||
let doneCalled = 0;
|
||||
|
||||
try {
|
||||
inst._attachCloseHandler();
|
||||
node._handlers.close(() => {
|
||||
doneCalled += 1;
|
||||
});
|
||||
} finally {
|
||||
global.clearInterval = originalClearInterval;
|
||||
}
|
||||
|
||||
assert.deepEqual(cleared, [55]);
|
||||
assert.equal(doneCalled, 1);
|
||||
});
|
||||
48
test/integration/upstream-reactor.integration.test.js
Normal file
48
test/integration/upstream-reactor.integration.test.js
Normal file
@@ -0,0 +1,48 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
|
||||
const { Reactor_CSTR } = require('../../src/specificClass');
|
||||
const { makeReactorConfig } = require('../helpers/factories');
|
||||
|
||||
const DAY_MS = 1000 * 60 * 60 * 24;
|
||||
|
||||
test('registering upstream reactor subscribes to upstream stateChange events', () => {
|
||||
const downstream = new Reactor_CSTR(makeReactorConfig({ reactor_type: 'CSTR' }));
|
||||
const upstream = new Reactor_CSTR(makeReactorConfig({ reactor_type: 'CSTR' }));
|
||||
|
||||
let calledWith = null;
|
||||
downstream.updateState = (timestamp) => {
|
||||
calledWith = timestamp;
|
||||
};
|
||||
|
||||
downstream.registerChild(upstream, 'reactor');
|
||||
upstream.emitter.emit('stateChange', 12345);
|
||||
|
||||
assert.equal(downstream.upstreamReactor, upstream);
|
||||
assert.equal(calledWith, 12345);
|
||||
});
|
||||
|
||||
test('updateState pulls influent from upstream reactor effluent when linked', () => {
|
||||
const downstream = new Reactor_CSTR(makeReactorConfig({ reactor_type: 'CSTR', n_inlets: 1, timeStep: 1 }));
|
||||
const upstream = new Reactor_CSTR(makeReactorConfig({ reactor_type: 'CSTR', n_inlets: 1 }));
|
||||
|
||||
upstream.Fs[0] = 3;
|
||||
upstream.state = Array(13).fill(11);
|
||||
|
||||
downstream.upstreamReactor = upstream;
|
||||
downstream.currentTime = 0;
|
||||
downstream.timeStep = 1;
|
||||
downstream.speedUpFactor = 1;
|
||||
|
||||
let ticks = 0;
|
||||
downstream.tick = () => {
|
||||
ticks += 1;
|
||||
return downstream.state;
|
||||
};
|
||||
|
||||
downstream.updateState(DAY_MS);
|
||||
|
||||
assert.equal(ticks, 1);
|
||||
assert.equal(downstream.Fs[0], 3);
|
||||
assert.deepEqual(downstream.Cs_in[0], Array(13).fill(11));
|
||||
});
|
||||
Reference in New Issue
Block a user