Derive device voltages from project settings

This commit is contained in:
2026-07-29 09:53:58 +02:00
parent b1a11397b3
commit 084103bf54
39 changed files with 2696 additions and 76 deletions
@@ -12,6 +12,11 @@ import { ProjectSettingsProjectCommandRepository } from "../src/db/repositories/
import { projectRevisions } from "../src/db/schema/project-revisions.js";
import { projects } from "../src/db/schema/projects.js";
import { distributionBoards } from "../src/db/schema/distribution-boards.js";
import { circuitLists } from "../src/db/schema/circuit-lists.js";
import { circuitSections } from "../src/db/schema/circuit-sections.js";
import { circuits } from "../src/db/schema/circuits.js";
import { projectDevices } from "../src/db/schema/project-devices.js";
import { DistributionBoardFixtureRepository } from "./support/distribution-board-fixture.js";
import { ProjectRevisionConflictError } from "../src/domain/errors/project-revision-conflict.error.js";
import { createProjectSettingsUpdateProjectCommand } from "../src/domain/models/project-settings-project-command.model.js";
import { updateProjectSettings } from "../src/frontend/utils/api.js";
@@ -78,6 +83,86 @@ function settings(
};
}
function seedDerivedVoltageRecords(context: DatabaseContext) {
context.db.insert(projectDevices).values([
{
id: "device-single",
projectId: "project-1",
name: "Leuchte",
displayName: "Leuchte",
phaseType: "single_phase",
quantity: 1,
powerPerUnit: 0.1,
simultaneityFactor: 1,
voltageV: 230,
},
{
id: "device-three",
projectId: "project-1",
name: "Pumpe",
displayName: "Pumpe",
phaseType: "three_phase",
quantity: 1,
powerPerUnit: 2,
simultaneityFactor: 1,
voltageV: 400,
},
]).run();
const board = new DistributionBoardFixtureRepository(
context.db
).createWithCircuitListAndDefaultSections("project-1", "UV-01");
const list = context.db
.select()
.from(circuitLists)
.where(eq(circuitLists.distributionBoardId, board.id))
.get();
assert.ok(list);
const sections = context.db
.select()
.from(circuitSections)
.where(eq(circuitSections.circuitListId, list.id))
.all();
const singleSection = sections.find((section) => section.key === "single_phase");
const threeSection = sections.find((section) => section.key === "three_phase");
assert.ok(singleSection);
assert.ok(threeSection);
context.db.insert(circuits).values([
{
id: "circuit-single",
circuitListId: list.id,
sectionId: singleSection.id,
equipmentIdentifier: "-2F1",
sortOrder: 10,
voltage: 230,
isReserve: 1,
},
{
id: "circuit-three",
circuitListId: list.id,
sectionId: threeSection.id,
equipmentIdentifier: "-3F1",
sortOrder: 10,
voltage: 400,
isReserve: 1,
},
]).run();
}
function getDerivedVoltages(context: DatabaseContext) {
return {
devices: context.db
.select({ id: projectDevices.id, voltageV: projectDevices.voltageV })
.from(projectDevices)
.all()
.sort((left, right) => left.id.localeCompare(right.id)),
circuits: context.db
.select({ id: circuits.id, voltage: circuits.voltage })
.from(circuits)
.all()
.sort((left, right) => left.id.localeCompare(right.id)),
};
}
describe("project settings project-command repository", () => {
it("validates settings commands and sends the expected revision from the frontend", async () => {
assert.throws(
@@ -137,6 +222,7 @@ describe("project settings project-command repository", () => {
it("updates both voltages and supports persisted undo and redo", () => {
const context = createTestDatabase();
try {
seedDerivedVoltageRecords(context);
const repository = new ProjectSettingsProjectCommandRepository(
context.db
);
@@ -171,6 +257,16 @@ describe("project settings project-command repository", () => {
}),
currentRevision: 1,
});
assert.deepEqual(getDerivedVoltages(context), {
devices: [
{ id: "device-single", voltageV: 240 },
{ id: "device-three", voltageV: 415 },
],
circuits: [
{ id: "circuit-single", voltage: 240 },
{ id: "circuit-three", voltage: 415 },
],
});
assert.deepEqual(forward.inverse.payload, {
name: "Testprojekt",
internalProjectNumber: "INT-1",
@@ -216,6 +312,16 @@ describe("project settings project-command repository", () => {
],
currentRevision: 2,
});
assert.deepEqual(getDerivedVoltages(context), {
devices: [
{ id: "device-single", voltageV: 230 },
{ id: "device-three", voltageV: 400 },
],
circuits: [
{ id: "circuit-single", voltage: 230 },
{ id: "circuit-three", voltage: 400 },
],
});
const redoStep = history.getNextCommand("project-1", "redo");
assert.ok(redoStep);
@@ -244,6 +350,16 @@ describe("project settings project-command repository", () => {
],
currentRevision: 3,
});
assert.deepEqual(getDerivedVoltages(context), {
devices: [
{ id: "device-single", voltageV: 240 },
{ id: "device-three", voltageV: 415 },
],
circuits: [
{ id: "circuit-single", voltage: 240 },
{ id: "circuit-three", voltage: 415 },
],
});
} finally {
context.close();
}