72 lines
2.6 KiB
JavaScript
72 lines
2.6 KiB
JavaScript
|
|
'use strict';
|
||
|
|
|
||
|
|
const test = require('node:test');
|
||
|
|
const assert = require('node:assert/strict');
|
||
|
|
|
||
|
|
const { interpolation } = require('generalFunctions');
|
||
|
|
const GroupEfficiency = require('../../src/efficiency/groupEfficiency.js');
|
||
|
|
|
||
|
|
function makeMachines(cogs) {
|
||
|
|
const out = {};
|
||
|
|
cogs.forEach((cog, i) => { out[`m${i}`] = { cog }; });
|
||
|
|
return out;
|
||
|
|
}
|
||
|
|
|
||
|
|
function makeGE(extra = {}) {
|
||
|
|
return new GroupEfficiency({
|
||
|
|
interpolation: new interpolation(),
|
||
|
|
logger: { warn() {}, error() {}, debug() {}, info() {} },
|
||
|
|
...extra,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
test('calcGroupEfficiency aggregates across 3 machines', () => {
|
||
|
|
const ge = makeGE();
|
||
|
|
const machines = makeMachines([0.9, 0.8, 0.7]);
|
||
|
|
const { maxEfficiency, lowestEfficiency } = ge.calcGroupEfficiency(machines);
|
||
|
|
assert.equal(lowestEfficiency, 0.7);
|
||
|
|
// maxEfficiency in the original code is actually the MEAN cog.
|
||
|
|
assert.ok(Math.abs(maxEfficiency - 0.8) < 1e-12);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('calcDistanceFromPeak returns |a - b|', () => {
|
||
|
|
const ge = makeGE();
|
||
|
|
assert.ok(Math.abs(ge.calcDistanceFromPeak(0.85, 0.92) - 0.07) < 1e-12);
|
||
|
|
assert.ok(Math.abs(ge.calcDistanceFromPeak(0.92, 0.85) - 0.07) < 1e-12);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('calcRelativeDistanceFromPeak maps current onto [0..1]', () => {
|
||
|
|
const ge = makeGE();
|
||
|
|
// current=0.85, max=0.92, min=0.7 → maps 0.85 in [0.92..0.7] onto [0..1].
|
||
|
|
// interpolate_lin_single_point treats first range as input domain:
|
||
|
|
// 0.85 → ((0.85 - 0.92) / (0.7 - 0.92)) * (1 - 0) + 0 = 0.07/0.22 ≈ 0.3181818...
|
||
|
|
const v = ge.calcRelativeDistanceFromPeak(0.85, 0.92, 0.7);
|
||
|
|
const expected = (0.85 - 0.92) / (0.7 - 0.92);
|
||
|
|
assert.ok(Math.abs(v - expected) < 1e-9, `got ${v} expected ${expected}`);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('calcDistanceBEP returns both abs + rel', () => {
|
||
|
|
const ge = makeGE();
|
||
|
|
const { absDistFromPeak, relDistFromPeak } = ge.calcDistanceBEP(0.85, 0.92, 0.7);
|
||
|
|
assert.ok(Math.abs(absDistFromPeak - 0.07) < 1e-12);
|
||
|
|
const expectedRel = (0.85 - 0.92) / (0.7 - 0.92);
|
||
|
|
assert.ok(Math.abs(relDistFromPeak - expectedRel) < 1e-9);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('calcRelativeDistanceFromPeak returns 1 when max === min (degenerate)', () => {
|
||
|
|
const ge = makeGE();
|
||
|
|
assert.equal(ge.calcRelativeDistanceFromPeak(0.85, 0.8, 0.8), 1);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('calcRelativeDistanceFromPeak returns 1 when current is null', () => {
|
||
|
|
const ge = makeGE();
|
||
|
|
assert.equal(ge.calcRelativeDistanceFromPeak(null, 0.92, 0.7), 1);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('calcGroupEfficiency handles a single machine', () => {
|
||
|
|
const ge = makeGE();
|
||
|
|
const { maxEfficiency, lowestEfficiency } = ge.calcGroupEfficiency(makeMachines([0.77]));
|
||
|
|
assert.equal(maxEfficiency, 0.77);
|
||
|
|
assert.equal(lowestEfficiency, 0.77);
|
||
|
|
});
|