Add logical distribution board feed links

This commit is contained in:
Julian Appel 2026-09-27 22:21:58 +02:00
parent b50c00c0ac
commit b893c72534
23 changed files with 3732 additions and 45 deletions

View file

@ -6,12 +6,25 @@ import {
projectStateSnapshotSchemaVersion,
serializeProjectStateSnapshot,
} from "../src/domain/models/project-state-snapshot.model.js";
import { remapProjectState } from "../src/domain/models/project-transfer.model.js";
import { createNamedProjectSnapshotSchema } from "../src/shared/validation/project-snapshot.schemas.js";
/** Removes the fields introduced by snapshot schema version 9. */
function withoutVersionNineFields<
T extends { distributionBoards: object[] },
>(snapshot: T): T {
for (const board of snapshot.distributionBoards) {
delete (board as Record<string, unknown>).feedingDistributionBoardId;
delete (board as Record<string, unknown>).feedingCircuitId;
}
return snapshot;
}
/** Removes the fields introduced by snapshot schema version 7. */
function withoutVersionSevenFields<
T extends { floors: object[] },
T extends { floors: object[]; distributionBoards: object[] },
>(snapshot: T): T {
withoutVersionNineFields(snapshot);
delete (snapshot as Record<string, unknown>).buildings;
for (const floor of snapshot.floors) {
delete (floor as Record<string, unknown>).buildingId;
@ -117,6 +130,8 @@ function protectedSnapshot() {
location: "Raum 003 Technik" as string | null,
fedFrom: "HV 0.1" as string | null,
remark: null as string | null,
feedingDistributionBoardId: null as string | null,
feedingCircuitId: null as string | null,
},
],
circuitLists: [
@ -206,6 +221,96 @@ describe("project state snapshot model", () => {
);
});
it("upgrades snapshot version 8 with empty feeding references", () => {
const previous = withoutVersionNineFields(protectedSnapshot());
previous.schemaVersion = 8 as typeof previous.schemaVersion;
const upgraded = parseProjectStateSnapshot(previous);
assert.equal(upgraded.schemaVersion, projectStateSnapshotSchemaVersion);
assert.equal(
upgraded.distributionBoards[0]?.feedingDistributionBoardId,
null
);
assert.equal(upgraded.distributionBoards[0]?.feedingCircuitId, null);
});
it("validates logical feeding references and cycles", () => {
const linked = protectedSnapshot();
linked.distributionBoards.push({
id: "board-2",
projectId: "project-1",
name: "UV 2",
floorId: null,
supplyType: null,
simultaneityFactor: 1,
boardNumber: "UV 2.1",
location: null,
fedFrom: null,
remark: null,
feedingDistributionBoardId: "board-1",
feedingCircuitId: "circuit-1",
});
linked.circuitLists.push({
id: "list-2",
projectId: "project-1",
distributionBoardId: "board-2",
name: "UV 2 Stromkreisliste",
});
linked.circuitSections.push({
id: "section-2",
circuitListId: "list-2",
key: "three_phase",
displayName: "Dreiphasig 1",
prefix: "-3F1.",
sortOrder: 10,
category: "three_phase",
groupNumber: 1,
});
const parsed = parseProjectStateSnapshot(linked);
assert.equal(parsed.distributionBoards[1]?.feedingCircuitId, "circuit-1");
let nextId = 0;
const remapped = remapProjectState(
parsed,
"project-copy",
() => `copy-${++nextId}`
);
const remappedUpstream = remapped.distributionBoards.find(
(board) => board.name === "UV 1"
);
const remappedDownstream = remapped.distributionBoards.find(
(board) => board.name === "UV 2"
);
assert.ok(remappedUpstream);
assert.ok(remappedDownstream);
assert.equal(
remappedDownstream.feedingDistributionBoardId,
remappedUpstream.id
);
assert.notEqual(remappedDownstream.feedingCircuitId, "circuit-1");
assert.ok(
remapped.circuits.some(
(circuit) => circuit.id === remappedDownstream.feedingCircuitId
)
);
const missingBoard = structuredClone(linked);
missingBoard.distributionBoards[1]!.feedingDistributionBoardId = null;
assert.throws(
() => parseProjectStateSnapshot(missingBoard),
/requires a feeding distribution board/
);
const cycle = structuredClone(linked);
cycle.distributionBoards[0]!.feedingDistributionBoardId = "board-2";
cycle.distributionBoards[0]!.feedingCircuitId = null;
cycle.distributionBoards[1]!.feedingCircuitId = null;
assert.throws(
() => parseProjectStateSnapshot(cycle),
/contains a cycle/
);
});
it("upgrades the supported baseline snapshot with a safe building default", () => {
const baseline = withoutVersionSixFields(structuredClone(minimalSnapshot()));
baseline.schemaVersion = 1 as typeof baseline.schemaVersion;