Files
leistungsbilanz-ts/tests/circuit-power-calculation.test.ts
T

90 lines
3.3 KiB
TypeScript

import assert from "node:assert/strict";
import { describe, it } from "node:test";
import {
applyDistributionBoardSimultaneityFactor,
calculateCircuitTotalPower,
calculateDistributionBoardTotalPower,
calculateRowTotalPower,
calculateSectionTotalPower,
} from "../src/domain/calculations/circuit-power-calculation.js";
import {
resolveCircuitPhaseType,
resolveProjectVoltage,
normalizeKnownElectricalPhaseType,
} from "../src/domain/services/project-voltage.service.js";
describe("circuit power calculation", () => {
it("calculates row and circuit totals from device rows", () => {
assert.equal(calculateRowTotalPower(2, 1.5, 0.5), 1.5);
const total = calculateCircuitTotalPower([
{ quantity: 2, powerPerUnit: 1.5, simultaneityFactor: 0.5 },
{ quantity: 1, powerPerUnit: 3, simultaneityFactor: 1 },
]);
assert.equal(total, 4.5);
});
it("calculates section and distribution-board totals", () => {
const lighting = calculateSectionTotalPower([
{ circuitTotalPower: 1.5 },
{ circuitTotalPower: 2.25 },
]);
const singlePhase = calculateSectionTotalPower([
{ circuitTotalPower: 3 },
]);
const total = calculateDistributionBoardTotalPower([
{ sectionTotalPower: lighting },
{ sectionTotalPower: singlePhase },
{ sectionTotalPower: 0 },
]);
assert.equal(lighting, 3.75);
assert.equal(total, 6.75);
assert.equal(
applyDistributionBoardSimultaneityFactor(total, 0.8),
5.4
);
assert.throws(
() => applyDistributionBoardSimultaneityFactor(total, 1.1),
/between zero and one/
);
});
});
describe("project voltage derivation", () => {
const settings = {
singlePhaseVoltageV: 240,
threePhaseVoltageV: 415,
};
it("derives device voltage exclusively from phase and project settings", () => {
assert.equal(resolveProjectVoltage("single_phase", settings), 240);
assert.equal(resolveProjectVoltage("three_phase", settings), 415);
});
it("uses section phase and handles unassigned circuits conservatively", () => {
assert.equal(resolveCircuitPhaseType("lighting", ["three_phase"]), "single_phase");
assert.equal(resolveCircuitPhaseType("single_phase", ["three_phase"]), "single_phase");
assert.equal(resolveCircuitPhaseType("three_phase", ["single_phase"]), "three_phase");
assert.equal(resolveCircuitPhaseType("unassigned", ["three_phase"]), "three_phase");
assert.equal(
resolveCircuitPhaseType("unassigned", ["three_phase", "single_phase"]),
"single_phase"
);
assert.equal(resolveCircuitPhaseType("unassigned", []), "single_phase");
assert.equal(resolveCircuitPhaseType("unassigned", [null]), "single_phase");
});
it("normalizes known legacy phase labels without guessing unknown values", () => {
assert.equal(normalizeKnownElectricalPhaseType("1phasig"), "single_phase");
assert.equal(normalizeKnownElectricalPhaseType("1-phasig"), "single_phase");
assert.equal(normalizeKnownElectricalPhaseType("3phase"), "three_phase");
assert.equal(normalizeKnownElectricalPhaseType("3-phasig"), "three_phase");
assert.equal(normalizeKnownElectricalPhaseType(null), null);
assert.throws(
() => normalizeKnownElectricalPhaseType("zweiphasig"),
/Unknown electrical phase type/
);
});
});