Files
machineGroupControl/test/basic/groupEfficiency.basic.test.js
znetsixe 619b1311d2 P4 wave 1: extract MGC concerns into focused modules
src/groupOps/        groupOperatingPoint + groupCurves (pure functions)
  src/totals/          totalsCalculator (dynamic + absolute + active)
  src/combinatorics/   pumpCombinations (validPumpCombinations + checkSpecialCases)
  src/optimizer/       bestCombination (CoG) + bepGravitation (BEP-G + marginal-cost)
  src/efficiency/      groupEfficiency (calc + distance helpers)
  src/dispatch/        demandDispatcher (LatestWinsGate-based; replaces
                       _dispatchInFlight + _delayedCall)
  src/commands/        canonical names from start (set.mode/scaling/demand,
                       child.register) + legacy aliases
  CONTRACT.md          inputs/outputs/events surface

53 basic tests pass (52 new + 1 pre-existing).
specificClass.js / nodeClass.js untouched — integration in P4 wave 2.

Findings flagged via agents (TODO append to OPEN_QUESTIONS.md):
  - calcGroupEfficiency.maxEfficiency is actually the mean (misleading name)
  - checkSpecialCases has a no-op `return false` inside forEach
  - MGC doesn't route cmd.startup/shutdown/estop — confirm if station broadcasts need it

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-10 20:45:23 +02:00

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);
});