This commit is contained in:
znetsixe
2026-03-11 11:13:51 +01:00
parent 460b872053
commit 2c69a5a0c1
5 changed files with 143 additions and 2 deletions

View File

@@ -0,0 +1,45 @@
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 getGridProfile returns null', () => {
const reactor = new Reactor_CSTR(makeReactorConfig({ reactor_type: 'CSTR' }));
assert.equal(reactor.getGridProfile, null);
});
test('PFR getGridProfile returns state matrix with correct dimensions', () => {
const n_x = 8;
const length = 40;
const reactor = new Reactor_PFR(
makeReactorConfig({ reactor_type: 'PFR', resolution_L: n_x, length }),
);
const profile = reactor.getGridProfile;
assert.notEqual(profile, null);
assert.equal(profile.n_x, n_x);
assert.equal(profile.d_x, length / n_x);
assert.equal(profile.length, length);
assert.equal(profile.grid.length, n_x, 'grid should have n_x rows');
assert.equal(profile.grid[0].length, 13, 'each row should have 13 species');
assert.ok(Array.isArray(profile.species), 'species list should be an array');
assert.equal(profile.species.length, 13);
assert.equal(profile.species[3], 'S_NH');
assert.equal(typeof profile.timestamp, 'number');
});
test('PFR getGridProfile is mutation-safe', () => {
const reactor = new Reactor_PFR(
makeReactorConfig({ reactor_type: 'PFR', resolution_L: 5, length: 10 }),
);
const profile = reactor.getGridProfile;
const originalValue = reactor.state[0][3]; // S_NH at cell 0
// Mutate the returned grid
profile.grid[0][3] = 999;
// Reactor internal state should be unchanged
assert.equal(reactor.state[0][3], originalValue, 'mutating grid copy must not affect reactor state');
});

View File

@@ -0,0 +1,68 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { Reactor_CSTR } = require('../../src/specificClass');
const nodeClass = require('../../src/nodeClass');
const { makeReactorConfig, makeUiConfig, makeNodeStub, makeREDStub } = require('../helpers/factories');
/**
* Smoke tests for Fix 3: configurable speedUpFactor on Reactor.
*/
test('specificClass defaults speedUpFactor to 1 when not in config', () => {
const config = makeReactorConfig();
const reactor = new Reactor_CSTR(config);
assert.equal(reactor.speedUpFactor, 1, 'speedUpFactor should default to 1');
});
test('specificClass accepts speedUpFactor from config', () => {
const config = makeReactorConfig();
config.speedUpFactor = 10;
const reactor = new Reactor_CSTR(config);
assert.equal(reactor.speedUpFactor, 10, 'speedUpFactor should be read from config');
});
test('specificClass accepts speedUpFactor = 60 for accelerated simulation', () => {
const config = makeReactorConfig();
config.speedUpFactor = 60;
const reactor = new Reactor_CSTR(config);
assert.equal(reactor.speedUpFactor, 60, 'speedUpFactor=60 should be accepted');
});
test('nodeClass passes speedUpFactor from uiConfig to reactor config', () => {
const uiConfig = makeUiConfig({ speedUpFactor: 5 });
const node = makeNodeStub();
const RED = makeREDStub();
const nc = new nodeClass(uiConfig, RED, node, 'test-reactor');
assert.equal(nc.source.speedUpFactor, 5, 'nodeClass should pass speedUpFactor=5 to specificClass');
});
test('nodeClass defaults speedUpFactor to 1 when not in uiConfig', () => {
const uiConfig = makeUiConfig();
// Ensure speedUpFactor is not set
delete uiConfig.speedUpFactor;
const node = makeNodeStub();
const RED = makeREDStub();
const nc = new nodeClass(uiConfig, RED, node, 'test-reactor');
assert.equal(nc.source.speedUpFactor, 1, 'nodeClass should default speedUpFactor to 1');
});
test('updateState with speedUpFactor=1 advances roughly real-time', () => {
const config = makeReactorConfig();
config.speedUpFactor = 1;
config.n_inlets = 1;
const reactor = new Reactor_CSTR(config);
// Set a known start time
const t0 = reactor.currentTime;
// Advance by 2 seconds real time
reactor.updateState(t0 + 2000);
// With speedUpFactor=1, simulation should have advanced ~2 seconds worth
// (not 120 seconds like with the old hardcoded 60x factor)
const elapsed = reactor.currentTime - t0;
assert.ok(elapsed < 5000, `Elapsed ${elapsed}ms should be close to 2000ms, not 120000ms (old 60x factor)`);
});