Create protected board defaults

This commit is contained in:
2026-07-30 19:30:53 +02:00
parent 012308984d
commit 9d4d4082fe
10 changed files with 504 additions and 156 deletions
@@ -12,6 +12,7 @@ import { ProjectHistoryRepository } from "../src/db/repositories/project-history
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 { distributionBoardComponents } from "../src/db/schema/distribution-board-components.js";
import { distributionBoards } from "../src/db/schema/distribution-boards.js";
import { floors } from "../src/db/schema/floors.js";
import { projectRevisions } from "../src/db/schema/project-revisions.js";
@@ -51,10 +52,51 @@ function getStructureCounts(context: DatabaseContext) {
boards: context.db.select().from(distributionBoards).all().length,
lists: context.db.select().from(circuitLists).all().length,
sections: context.db.select().from(circuitSections).all().length,
components: context.db
.select()
.from(distributionBoardComponents)
.all().length,
revisions: context.db.select().from(projectRevisions).all().length,
};
}
function legacySections(circuitListId: string) {
return [
{
id: "legacy-lighting",
circuitListId,
key: "lighting",
displayName: "Lighting",
prefix: "-1F",
sortOrder: 10,
},
{
id: "legacy-single-phase",
circuitListId,
key: "single_phase",
displayName: "Single-phase circuits",
prefix: "-2F",
sortOrder: 20,
},
{
id: "legacy-three-phase",
circuitListId,
key: "three_phase",
displayName: "Three-phase circuits",
prefix: "-3F",
sortOrder: 30,
},
{
id: "legacy-unassigned",
circuitListId,
key: "unassigned",
displayName: "Unassigned",
prefix: "-UF",
sortOrder: 90,
},
];
}
describe("distribution-board structure project command", () => {
it("builds the fixed setup and sends the expected frontend revision", async () => {
const structure = createDistributionBoardStructureSnapshot(
@@ -74,17 +116,26 @@ describe("distribution-board structure project command", () => {
section.prefix,
]),
[
["lighting", "-1F"],
["single_phase", "-2F"],
["three_phase", "-3F"],
["unassigned", "-UF"],
["lighting", "-1F1."],
["single_phase", "-2F1."],
["three_phase", "-3F1."],
]
);
assert.deepEqual(
structure.components.map((component) => [
component.equipmentIdentifier,
component.name,
]),
[
["-Q0", "Hauptschalter"],
["-FA", "Überspannungsableiter"],
]
);
assert.throws(
() =>
createDistributionBoardInsertProjectCommand({
...structure,
sections: structure.sections.slice(0, 3),
sections: structure.sections.slice(0, 2),
}),
/incomplete/
);
@@ -150,7 +201,8 @@ describe("distribution-board structure project command", () => {
assert.deepEqual(getStructureCounts(context), {
boards: 1,
lists: 1,
sections: 4,
sections: 3,
components: 2,
revisions: 1,
});
@@ -167,6 +219,7 @@ describe("distribution-board structure project command", () => {
boards: 0,
lists: 0,
sections: 0,
components: 0,
revisions: 2,
});
@@ -182,7 +235,8 @@ describe("distribution-board structure project command", () => {
assert.deepEqual(getStructureCounts(context), {
boards: 1,
lists: 1,
sections: 4,
sections: 3,
components: 2,
revisions: 3,
});
assert.ok(
@@ -274,8 +328,9 @@ describe("distribution-board structure project command", () => {
type: "distribution-board.insert",
payload: {
structure: {
...current,
distributionBoard: legacyBoard,
circuitList: current.circuitList,
sections: legacySections(current.circuitList.id),
},
},
} as unknown as DistributionBoardStructureProjectCommand;
@@ -291,6 +346,59 @@ describe("distribution-board structure project command", () => {
.get();
assert.equal(inserted?.floorId, null);
assert.equal(inserted?.supplyType, null);
assert.equal(
context.db.select().from(circuitSections).all().length,
4
);
assert.equal(
context.db.select().from(distributionBoardComponents).all()
.length,
0
);
} finally {
context.close();
}
});
it("keeps stored version-two setup commands component-free", () => {
const context = createTestDatabase();
try {
const repository =
new DistributionBoardStructureProjectCommandRepository(
context.db
);
const current = createDistributionBoardStructureSnapshot(
"project-1",
"UV Version 2"
);
const command = {
schemaVersion: 2,
type: "distribution-board.insert",
payload: {
structure: {
distributionBoard: current.distributionBoard,
circuitList: current.circuitList,
sections: legacySections(current.circuitList.id),
},
},
} as DistributionBoardStructureProjectCommand;
const inserted = repository.execute({
projectId: "project-1",
expectedRevision: 0,
source: "user",
command,
});
assert.equal(inserted.inverse.schemaVersion, 2);
assert.equal(
context.db.select().from(circuitSections).all().length,
4
);
assert.equal(
context.db.select().from(distributionBoardComponents).all()
.length,
0
);
} finally {
context.close();
}
@@ -545,6 +653,7 @@ describe("distribution-board structure project command", () => {
boards: 0,
lists: 0,
sections: 0,
components: 0,
revisions: 0,
});
@@ -661,6 +770,7 @@ describe("distribution-board structure project command", () => {
boards: 0,
lists: 0,
sections: 0,
components: 0,
revisions: 0,
});
} finally {
+20 -6
View File
@@ -243,7 +243,13 @@ describe("project snapshot repository", () => {
);
assert.equal(state.circuits.length, 1);
assert.equal(state.circuitProtectionDevices.length, 1);
assert.equal(state.distributionBoardComponents.length, 1);
assert.equal(
state.distributionBoardComponents.length,
context.db
.select()
.from(distributionBoardComponents)
.all().length
);
assert.equal(
state.distributionBoardComponentProtectionDevices.length,
1
@@ -427,18 +433,26 @@ describe("portable project transfer", () => {
copied.state.circuits[0].id,
source.state.circuits[0].id
);
assert.notEqual(
copied.state.distributionBoardComponents[0].id,
source.state.distributionBoardComponents[0].id
);
assert.equal(copied.state.distributionBoardComponents.length, 3);
assert.equal(
copied.state.circuitProtectionDevices[0].circuitId,
copied.state.circuits[0].id
);
assert.equal(
copied.state.distributionBoardComponents.some(
(component) =>
component.id ===
copied.state
.distributionBoardComponentProtectionDevices[0]
.componentId
),
true
);
assert.notEqual(
copied.state.distributionBoardComponentProtectionDevices[0]
.componentId,
copied.state.distributionBoardComponents[0].id
source.state.distributionBoardComponentProtectionDevices[0]
.componentId
);
assert.equal(
copied.state.circuits[0].deviceRows[0].linkedProjectDeviceId,
@@ -1,6 +1,7 @@
import type { AppDatabase } from "../../src/db/database-context.js";
import { circuitLists } from "../../src/db/schema/circuit-lists.js";
import { circuitSections } from "../../src/db/schema/circuit-sections.js";
import { distributionBoardComponents } from "../../src/db/schema/distribution-board-components.js";
import { distributionBoards } from "../../src/db/schema/distribution-boards.js";
import { createDistributionBoardStructureSnapshot } from "../../src/domain/models/distribution-board-structure-project-command.model.js";
@@ -21,6 +22,9 @@ export class DistributionBoardFixtureRepository {
.run();
tx.insert(circuitLists).values(structure.circuitList).run();
tx.insert(circuitSections).values(structure.sections).run();
tx.insert(distributionBoardComponents)
.values(structure.components)
.run();
});
return structure.distributionBoard;
}