Harden settler runtime and scaffold tests
This commit is contained in:
8
examples/README.md
Normal file
8
examples/README.md
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
# settler Example Flows
|
||||||
|
|
||||||
|
Import-ready Node-RED examples for settler.
|
||||||
|
|
||||||
|
## Files
|
||||||
|
- basic.flow.json
|
||||||
|
- integration.flow.json
|
||||||
|
- edge.flow.json
|
||||||
6
examples/basic.flow.json
Normal file
6
examples/basic.flow.json
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
[
|
||||||
|
{"id":"settler_basic_tab","type":"tab","label":"settler basic","disabled":false,"info":"settler basic example"},
|
||||||
|
{"id":"settler_basic_node","type":"settler","z":"settler_basic_tab","name":"settler basic","x":420,"y":180,"wires":[["settler_basic_dbg"]]},
|
||||||
|
{"id":"settler_basic_inj","type":"inject","z":"settler_basic_tab","name":"basic trigger","props":[{"p":"topic","vt":"str"},{"p":"payload","vt":"str"}],"topic":"ping","payload":"1","payloadType":"str","x":160,"y":180,"wires":[["settler_basic_node"]]},
|
||||||
|
{"id":"settler_basic_dbg","type":"debug","z":"settler_basic_tab","name":"settler basic debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":660,"y":180,"wires":[]}
|
||||||
|
]
|
||||||
6
examples/edge.flow.json
Normal file
6
examples/edge.flow.json
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
[
|
||||||
|
{"id":"settler_edge_tab","type":"tab","label":"settler edge","disabled":false,"info":"settler edge example"},
|
||||||
|
{"id":"settler_edge_node","type":"settler","z":"settler_edge_tab","name":"settler edge","x":420,"y":180,"wires":[["settler_edge_dbg"]]},
|
||||||
|
{"id":"settler_edge_inj","type":"inject","z":"settler_edge_tab","name":"unknown topic","props":[{"p":"topic","vt":"str"},{"p":"payload","vt":"str"}],"topic":"doesNotExist","payload":"x","payloadType":"str","x":170,"y":180,"wires":[["settler_edge_node"]]},
|
||||||
|
{"id":"settler_edge_dbg","type":"debug","z":"settler_edge_tab","name":"settler edge debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":660,"y":180,"wires":[]}
|
||||||
|
]
|
||||||
6
examples/integration.flow.json
Normal file
6
examples/integration.flow.json
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
[
|
||||||
|
{"id":"settler_int_tab","type":"tab","label":"settler integration","disabled":false,"info":"settler integration example"},
|
||||||
|
{"id":"settler_int_node","type":"settler","z":"settler_int_tab","name":"settler integration","x":420,"y":180,"wires":[["settler_int_dbg"]]},
|
||||||
|
{"id":"settler_int_inj","type":"inject","z":"settler_int_tab","name":"registerChild","props":[{"p":"topic","vt":"str"},{"p":"payload","vt":"str"}],"topic":"registerChild","payload":"example-child-id","payloadType":"str","x":170,"y":180,"wires":[["settler_int_node"]]},
|
||||||
|
{"id":"settler_int_dbg","type":"debug","z":"settler_int_tab","name":"settler integration debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":680,"y":180,"wires":[]}
|
||||||
|
]
|
||||||
@@ -16,7 +16,7 @@
|
|||||||
"author": "P.R. van der Wilt",
|
"author": "P.R. van der Wilt",
|
||||||
"main": "settler.js",
|
"main": "settler.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "node settler.js"
|
"test": "node --test test/basic/*.test.js test/integration/*.test.js test/edge/*.test.js"
|
||||||
},
|
},
|
||||||
"node-red": {
|
"node-red": {
|
||||||
"nodes": {
|
"nodes": {
|
||||||
|
|||||||
@@ -30,19 +30,26 @@ class nodeClass {
|
|||||||
*/
|
*/
|
||||||
_attachInputHandler() {
|
_attachInputHandler() {
|
||||||
this.node.on('input', (msg, send, done) => {
|
this.node.on('input', (msg, send, done) => {
|
||||||
|
try {
|
||||||
switch (msg.topic) {
|
switch (msg.topic) {
|
||||||
case 'registerChild':
|
case 'registerChild': {
|
||||||
// Register this node as a parent of the child node
|
const childId = msg.payload;
|
||||||
const childId = msg.payload;
|
const childObj = this.RED.nodes.getNode(childId);
|
||||||
const childObj = this.RED.nodes.getNode(childId);
|
if (!childObj || !childObj.source) {
|
||||||
this.source.childRegistrationUtils.registerChild(childObj.source, msg.positionVsParent);
|
this.source?.logger?.warn(`registerChild skipped: missing child/source for id=${childId}`);
|
||||||
break;
|
break;
|
||||||
default:
|
}
|
||||||
console.log("Unknown topic: " + msg.topic);
|
this.source.childRegistrationUtils.registerChild(childObj.source, msg.positionVsParent);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
this.source?.logger?.warn(`Unknown topic: ${msg.topic}`);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
this.source?.logger?.error(`Input handler failure: ${error.message}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (done) {
|
if (typeof done === 'function') {
|
||||||
done();
|
done();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -106,7 +113,7 @@ class nodeClass {
|
|||||||
_attachCloseHandler() {
|
_attachCloseHandler() {
|
||||||
this.node.on('close', (done) => {
|
this.node.on('close', (done) => {
|
||||||
clearInterval(this._tickInterval);
|
clearInterval(this._tickInterval);
|
||||||
done();
|
if (typeof done === 'function') done();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,18 @@
|
|||||||
const { childRegistrationUtils, logger, MeasurementContainer } = require('generalFunctions');
|
const { childRegistrationUtils, logger, MeasurementContainer } = require('generalFunctions');
|
||||||
const EventEmitter = require('events');
|
const EventEmitter = require('events');
|
||||||
|
|
||||||
|
// Compatibility-safe array clone for Node runtimes without global structuredClone.
|
||||||
|
function cloneArray(values) {
|
||||||
|
if (typeof structuredClone === 'function') {
|
||||||
|
return structuredClone(values);
|
||||||
|
}
|
||||||
|
return Array.isArray(values) ? [...values] : values;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Settler domain model.
|
||||||
|
* Splits influent into effluent, sludge and return sludge based on solids balance.
|
||||||
|
*/
|
||||||
class Settler {
|
class Settler {
|
||||||
constructor(config) {
|
constructor(config) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
@@ -31,7 +43,7 @@ class Settler {
|
|||||||
const F_so = F_s - F_sr;
|
const F_so = F_s - F_sr;
|
||||||
|
|
||||||
// effluent
|
// effluent
|
||||||
const Cs_eff = structuredClone(this.Cs_in);
|
const Cs_eff = cloneArray(this.Cs_in);
|
||||||
if (F_s > 0) {
|
if (F_s > 0) {
|
||||||
Cs_eff[7] = 0;
|
Cs_eff[7] = 0;
|
||||||
Cs_eff[8] = 0;
|
Cs_eff[8] = 0;
|
||||||
@@ -42,7 +54,7 @@ class Settler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// sludge
|
// sludge
|
||||||
const Cs_s = structuredClone(this.Cs_in);
|
const Cs_s = cloneArray(this.Cs_in);
|
||||||
if (F_s > 0) {
|
if (F_s > 0) {
|
||||||
Cs_s[7] = this.F_in * this.Cs_in[7] / F_s;
|
Cs_s[7] = this.F_in * this.Cs_in[7] / F_s;
|
||||||
Cs_s[8] = this.F_in * this.Cs_in[8] / F_s;
|
Cs_s[8] = this.F_in * this.Cs_in[8] / F_s;
|
||||||
@@ -113,7 +125,8 @@ class Settler {
|
|||||||
|
|
||||||
reactorChild.emitter.on("stateChange", (eventData) => {
|
reactorChild.emitter.on("stateChange", (eventData) => {
|
||||||
this.logger.debug(`State change of upstream reactor detected.`);
|
this.logger.debug(`State change of upstream reactor detected.`);
|
||||||
const effluent = this.upstreamReactor.getEffluent[0];
|
const raw = this.upstreamReactor.getEffluent;
|
||||||
|
const effluent = Array.isArray(raw) ? raw[0] : raw;
|
||||||
this.F_in = effluent.payload.F;
|
this.F_in = effluent.payload.F;
|
||||||
this.Cs_in = effluent.payload.C;
|
this.Cs_in = effluent.payload.C;
|
||||||
});
|
});
|
||||||
|
|||||||
12
test/README.md
Normal file
12
test/README.md
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
# settler Test Suite Layout
|
||||||
|
|
||||||
|
Required EVOLV layout:
|
||||||
|
- basic/
|
||||||
|
- integration/
|
||||||
|
- edge/
|
||||||
|
- helpers/
|
||||||
|
|
||||||
|
Baseline structure tests:
|
||||||
|
- basic/structure-module-load.basic.test.js
|
||||||
|
- integration/structure-examples.integration.test.js
|
||||||
|
- edge/structure-examples-node-type.edge.test.js
|
||||||
0
test/basic/.gitkeep
Normal file
0
test/basic/.gitkeep
Normal file
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('settler module load smoke', () => {
|
||||||
|
assert.doesNotThrow(() => {
|
||||||
|
require('../../settler.js');
|
||||||
|
});
|
||||||
|
});
|
||||||
0
test/edge/.gitkeep
Normal file
0
test/edge/.gitkeep
Normal file
11
test/edge/structure-examples-node-type.edge.test.js
Normal file
11
test/edge/structure-examples-node-type.edge.test.js
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
const test = require('node:test');
|
||||||
|
const assert = require('node:assert/strict');
|
||||||
|
const fs = require('node:fs');
|
||||||
|
const path = require('node:path');
|
||||||
|
|
||||||
|
const flow = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../../examples/basic.flow.json'), 'utf8'));
|
||||||
|
|
||||||
|
test('basic example includes node type settler', () => {
|
||||||
|
const count = flow.filter((n) => n && n.type === 'settler').length;
|
||||||
|
assert.equal(count >= 1, true);
|
||||||
|
});
|
||||||
0
test/helpers/.gitkeep
Normal file
0
test/helpers/.gitkeep
Normal file
0
test/integration/.gitkeep
Normal file
0
test/integration/.gitkeep
Normal file
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 settler', () => {
|
||||||
|
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 settler', () => {
|
||||||
|
for (const file of ['basic.flow.json', 'integration.flow.json', 'edge.flow.json']) {
|
||||||
|
const parsed = loadJson(file);
|
||||||
|
assert.equal(Array.isArray(parsed), true);
|
||||||
|
}
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user