forked from jappel/leistungsbilanz-ts
Full-codebase review turned up five real correctness/security bugs and
a dozen smaller inconsistencies; all are fixed here with matching test
coverage:
- BMK uniqueness silently allowed German-umlaut duplicates ("Ä1" vs
"ä1") because the DB's normalized index only folds ASCII case. Added
a shared Unicode-aware pre-check used by every circuit/component
insert and rename path (one of which had no pre-check at all).
- CircuitDeviceRow.simultaneityFactor had no upper bound at the row
level (command model and snapshot/restore schema), unlike every
sibling entity, letting a bad value silently corrupt power totals.
- Grid cell editing silently misread German thousands-separator input
("1.500" parsed as 1.5); "." is now rejected outright with a clear
message instead of guessing.
- The editor's shared command runner (runCommand/applyHistory) had no
re-entrancy guard, so a double click/drop could fire the same
command twice and race a BMK collision or revision conflict. Added a
synchronous ref guard plus isSaving on the buttons that lacked it.
- GET .../next-identifier leaked circuit-numbering state for sections
in other projects (no ownership check, 400 instead of 404). Moved
under /projects/:projectId and scoped it.
Also: added the missing circuits.section_id / circuit_device_rows.
circuit_id indexes (migration 0006), gave FormModal a focus trap /
Escape-to-close / focus restore and rebuilt ProjectSettingsModal on
top of it instead of duplicated markup, removed dead code (3 orphaned
domain model files, an unused persistence helper, a wrapper only used
by its own test), pointed the project page at GET /projects/:id
instead of listing+filtering client-side, closed the gap between the
documented 18 MB CSV limit and the ~17.17 MiB actually enforced, added
missing upper bounds on several free-text fields, filled in nine
missing German labels in the revision timeline, replaced a
key-order-fragile JSON.stringify equality check with a real field
comparison, made an implicit sort-order assumption in three
renumbering helpers explicit, cleared the sidebar's target selection
when it no longer resolves after a tree reload, and fixed
updateGlobalDevice to check-then-write instead of write-then-check.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
242 lines
8.1 KiB
TypeScript
242 lines
8.1 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { describe, it } from "node:test";
|
|
import {
|
|
allColumns,
|
|
buildCircuitEditPatch,
|
|
buildDeviceRowEditPatch,
|
|
formatValue,
|
|
formatCellValue,
|
|
getCircuitSectionLabel,
|
|
getProjectColumnLayoutStorageKey,
|
|
isGridEditorControlTarget,
|
|
getBlockSortValue,
|
|
getCellKind,
|
|
getCircuitValue,
|
|
getDeviceValue,
|
|
parseStoredColumnLayout,
|
|
parseNumeric,
|
|
} from "../src/frontend/utils/circuit-grid-model.js";
|
|
import type { CircuitTreeCircuitDto, CircuitTreeDeviceRowDto } from "../src/frontend/types.js";
|
|
|
|
const device: CircuitTreeDeviceRowDto = {
|
|
id: "row-1",
|
|
sortOrder: 10,
|
|
name: "Technical light",
|
|
displayName: "Office light",
|
|
phaseType: "single_phase",
|
|
roomNumberSnapshot: "1.01",
|
|
roomNameSnapshot: "Office",
|
|
quantity: 2,
|
|
powerPerUnit: 0.05,
|
|
simultaneityFactor: 0.8,
|
|
rowTotalPower: 0.08,
|
|
remark: "Device remark",
|
|
};
|
|
|
|
const circuit: CircuitTreeCircuitDto = {
|
|
id: "circuit-1",
|
|
circuitListId: "list-1",
|
|
sectionId: "section-1",
|
|
equipmentIdentifier: "-1F1",
|
|
displayName: "Lighting circuit",
|
|
sortOrder: 10,
|
|
protectionDevice: {
|
|
type: "LS",
|
|
ratedCurrentA: 16,
|
|
tripCharacteristic: "B",
|
|
},
|
|
cableType: "NYM-J",
|
|
cableCrossSection: "1.5 mm²",
|
|
cableLength: 20,
|
|
voltage: 230,
|
|
controlRequirement: "DALI",
|
|
isReserve: false,
|
|
remark: "Circuit remark",
|
|
circuitTotalPower: 0.08,
|
|
deviceRows: [device],
|
|
};
|
|
|
|
describe("circuit grid model", () => {
|
|
it("does not route clicks from active editor controls back to the grid cell", () => {
|
|
const inputTarget = {
|
|
closest: (selector: string) =>
|
|
selector === "input, select, textarea" ? {} : null,
|
|
};
|
|
const cellTarget = { closest: () => null };
|
|
|
|
assert.equal(isGridEditorControlTarget(inputTarget), true);
|
|
assert.equal(isGridEditorControlTarget(cellTarget), false);
|
|
assert.equal(isGridEditorControlTarget(null), false);
|
|
});
|
|
|
|
it("keeps the equipment identifier locked as the first column", () => {
|
|
assert.equal(allColumns[0].key, "equipmentIdentifier");
|
|
assert.equal(allColumns[0].label, "SK-Nr.");
|
|
assert.equal(
|
|
allColumns[0].fullLabel,
|
|
"Stromkreisnummer / Betriebsmittelkennzeichen"
|
|
);
|
|
assert.equal(allColumns[0].locked, true);
|
|
});
|
|
|
|
it("stores valid column layouts under a project-specific key", () => {
|
|
assert.equal(
|
|
getProjectColumnLayoutStorageKey("project-1"),
|
|
"circuitTreeEditor.columnLayout.v3.project-1"
|
|
);
|
|
const layout = parseStoredColumnLayout(
|
|
JSON.stringify({
|
|
order: ["displayName", "equipmentIdentifier"],
|
|
visible: [
|
|
"displayName",
|
|
"equipmentIdentifier",
|
|
"unknown",
|
|
],
|
|
})
|
|
);
|
|
assert.ok(layout);
|
|
assert.equal(layout.order[0], "equipmentIdentifier");
|
|
assert.deepEqual(layout.visible, [
|
|
"displayName",
|
|
"equipmentIdentifier",
|
|
]);
|
|
assert.equal(parseStoredColumnLayout("{invalid"), null);
|
|
});
|
|
|
|
it("shows stable circuit sections with German labels", () => {
|
|
assert.equal(
|
|
getCircuitSectionLabel({
|
|
key: "lighting",
|
|
displayName: "Lighting",
|
|
category: "lighting",
|
|
}),
|
|
"Lighting"
|
|
);
|
|
assert.equal(
|
|
getCircuitSectionLabel({
|
|
key: "lighting",
|
|
displayName: "Lighting",
|
|
}),
|
|
"Licht"
|
|
);
|
|
assert.equal(
|
|
getCircuitSectionLabel({
|
|
key: "custom",
|
|
displayName: "Sonderbereich",
|
|
}),
|
|
"Sonderbereich"
|
|
);
|
|
});
|
|
|
|
it("maps shared fields to the correct level for every row shape", () => {
|
|
assert.equal(getCellKind("circuitCompact", "displayName"), "deviceField");
|
|
assert.equal(getCellKind("circuitCompact", "equipmentIdentifier"), "circuitField");
|
|
assert.equal(getCellKind("circuitSummary", "displayName"), "circuitField");
|
|
assert.equal(getCellKind("deviceRow", "displayName"), "deviceField");
|
|
assert.equal(getCellKind("reserveCircuit", "remark"), "deviceField");
|
|
assert.equal(getCellKind("placeholder", "displayName"), "deviceField");
|
|
assert.equal(getCellKind("deviceRow", "equipmentIdentifier"), "readonly");
|
|
assert.equal(getCellKind("circuitSummary", "controlRequirement"), "circuitField");
|
|
assert.equal(getCellKind("deviceRow", "controlRequirement"), "readonly");
|
|
assert.equal(getCellKind("circuitCompact", "rowTotalPower"), "computed");
|
|
assert.equal(getCellKind("circuitCompact", "protectionType"), "computed");
|
|
});
|
|
|
|
it("projects circuit and device values without mixing ownership", () => {
|
|
assert.equal(getDeviceValue(device, "roomSummary"), "1.01 Office");
|
|
assert.equal(getDeviceValue(device, "rowTotalPower"), 0.08);
|
|
assert.equal(getCircuitValue(circuit, "protectionSummary"), "LS · 16 A · B");
|
|
assert.equal(
|
|
getCircuitValue(
|
|
{
|
|
...circuit,
|
|
protectionDevice: {
|
|
type: "FI_LS",
|
|
ratedCurrentA: 16,
|
|
tripCharacteristic: "B",
|
|
rcdType: "A",
|
|
ratedResidualCurrentMa: 30,
|
|
},
|
|
},
|
|
"protectionSummary"
|
|
),
|
|
"FI/LS · 16 A · B · Typ A · 30 mA"
|
|
);
|
|
assert.equal(getCircuitValue(circuit, "voltage"), 230);
|
|
assert.equal(getCircuitValue(circuit, "controlRequirement"), "DALI");
|
|
assert.equal(getCircuitValue(circuit, "cableSummary"), "NYM-J, 1.5 mm², 20 m");
|
|
});
|
|
|
|
it("formats displayed numeric values without changing their stored precision", () => {
|
|
assert.equal(formatValue(0.123456, "powerPerUnit"), "0,123");
|
|
assert.equal(formatValue(0.87654, "simultaneityFactor"), "0,88");
|
|
assert.equal(formatValue(230.4, "voltage"), "230");
|
|
assert.equal(formatValue(16, "protectionRatedCurrent"), "16");
|
|
assert.equal(formatValue("single_phase", "phaseType"), "1-phasig");
|
|
assert.equal(formatValue("three_phase", "phaseType"), "3-phasig");
|
|
assert.equal(formatCellValue(2, "quantity"), "2 St.");
|
|
assert.equal(formatCellValue(0.123456, "powerPerUnit"), "0,123 kW");
|
|
assert.equal(formatCellValue(1.25, "rowTotalPower"), "1,25 kW");
|
|
assert.equal(formatCellValue(undefined, "circuitTotalPower"), "-");
|
|
});
|
|
|
|
it("uses circuit display values for block sorting and falls back to the first device", () => {
|
|
assert.equal(getBlockSortValue(circuit, "displayName"), "Lighting circuit");
|
|
assert.equal(getBlockSortValue({ ...circuit, displayName: undefined }, "displayName"), "Office light");
|
|
assert.equal(getBlockSortValue(circuit, "quantity"), 2);
|
|
});
|
|
|
|
it("parses numeric drafts and rejects invalid values", () => {
|
|
assert.equal(parseNumeric("quantity", " 1500 "), 1500);
|
|
assert.equal(parseNumeric("powerPerUnit", " 1,25 "), 1.25);
|
|
assert.equal(parseNumeric("quantity", ""), undefined);
|
|
assert.throws(() => parseNumeric("quantity", "two"), /Ungültiger Zahlenwert/);
|
|
});
|
|
|
|
it("rejects a dot instead of silently misreading it as a thousands separator", () => {
|
|
// "1.500" typed with German thousands-separator intent (meaning 1500)
|
|
// must never silently become 1.5 — it must fail loudly instead.
|
|
assert.throws(
|
|
() => parseNumeric("cableLength", "1.500"),
|
|
/Dezimalstellen mit Komma eingeben/
|
|
);
|
|
assert.throws(
|
|
() => parseNumeric("powerPerUnit", "2.5"),
|
|
/Dezimalstellen mit Komma eingeben/
|
|
);
|
|
});
|
|
|
|
it("builds nullable circuit command patches from grid drafts", () => {
|
|
assert.deepEqual(buildCircuitEditPatch("voltage", ""), {
|
|
voltage: null,
|
|
});
|
|
assert.deepEqual(
|
|
buildCircuitEditPatch("controlRequirement", " DALI "),
|
|
{ controlRequirement: "DALI" }
|
|
);
|
|
assert.deepEqual(buildCircuitEditPatch("isReserve", "ja"), {
|
|
isReserve: true,
|
|
});
|
|
});
|
|
|
|
it("builds device command patches with explicit clearing semantics", () => {
|
|
assert.deepEqual(buildDeviceRowEditPatch("roomSummary", ""), {
|
|
roomNumberSnapshot: null,
|
|
roomNameSnapshot: null,
|
|
});
|
|
assert.deepEqual(
|
|
buildDeviceRowEditPatch("roomSummary", "1.01 Büro"),
|
|
{
|
|
roomNumberSnapshot: "1.01",
|
|
roomNameSnapshot: "Büro",
|
|
}
|
|
);
|
|
assert.deepEqual(buildDeviceRowEditPatch("technicalName", " Leuchte "), {
|
|
name: "Leuchte",
|
|
});
|
|
assert.throws(
|
|
() => buildDeviceRowEditPatch("quantity", ""),
|
|
/darf nicht leer sein/
|
|
);
|
|
});
|
|
});
|