forked from jappel/leistungsbilanz-ts
Create feeder circuits while linking boards
This commit is contained in:
parent
552362bad1
commit
ade21fe386
11 changed files with 960 additions and 40 deletions
|
|
@ -0,0 +1,240 @@
|
|||
import path from "node:path";
|
||||
import assert from "node:assert/strict";
|
||||
import { describe, it } from "node:test";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { migrate } from "drizzle-orm/better-sqlite3/migrator";
|
||||
import {
|
||||
createDatabaseContext,
|
||||
type DatabaseContext,
|
||||
} from "../src/db/database-context.js";
|
||||
import { DistributionBoardStructureProjectCommandRepository } from "../src/db/repositories/distribution-board-structure-project-command.repository.js";
|
||||
import { ProjectHistoryRepository } from "../src/db/repositories/project-history.repository.js";
|
||||
import { circuits } from "../src/db/schema/circuits.js";
|
||||
import { distributionBoards } from "../src/db/schema/distribution-boards.js";
|
||||
import { projects } from "../src/db/schema/projects.js";
|
||||
import {
|
||||
assertDistributionBoardFeederCircuitProjectCommand,
|
||||
createDistributionBoardCreateFeederCircuitProjectCommand,
|
||||
} from "../src/domain/models/distribution-board-feeder-circuit-project-command.model.js";
|
||||
import { createDistributionBoardUpdateProjectCommand } from "../src/domain/models/distribution-board-project-command.model.js";
|
||||
import {
|
||||
createDistributionBoardInsertProjectCommand,
|
||||
createDistributionBoardStructureSnapshot,
|
||||
} from "../src/domain/models/distribution-board-structure-project-command.model.js";
|
||||
import { buildCircuitInsertSnapshot } from "../src/frontend/utils/circuit-structure-command.js";
|
||||
|
||||
function createTestDatabase(): DatabaseContext {
|
||||
const context = createDatabaseContext(":memory:");
|
||||
migrate(context.db, {
|
||||
migrationsFolder: path.resolve("src", "db", "migrations"),
|
||||
});
|
||||
context.db
|
||||
.insert(projects)
|
||||
.values({ id: "project-1", name: "Test project" })
|
||||
.run();
|
||||
return context;
|
||||
}
|
||||
|
||||
function setupBoards(
|
||||
context: DatabaseContext,
|
||||
repository: DistributionBoardStructureProjectCommandRepository
|
||||
) {
|
||||
const source = createDistributionBoardStructureSnapshot(
|
||||
"project-1",
|
||||
"NSHV"
|
||||
);
|
||||
const downstream = createDistributionBoardStructureSnapshot(
|
||||
"project-1",
|
||||
"UV EG"
|
||||
);
|
||||
repository.execute({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 0,
|
||||
source: "user",
|
||||
command: createDistributionBoardInsertProjectCommand(source),
|
||||
});
|
||||
repository.execute({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 1,
|
||||
source: "user",
|
||||
command: createDistributionBoardInsertProjectCommand(downstream),
|
||||
});
|
||||
const section = source.sections.find(
|
||||
(candidate) => candidate.key === "three_phase"
|
||||
)!;
|
||||
const circuit = {
|
||||
...buildCircuitInsertSnapshot({
|
||||
id: "feeder-circuit",
|
||||
circuitListId: source.circuitList.id,
|
||||
category: "three_phase",
|
||||
deviceRows: [],
|
||||
values: {
|
||||
sectionId: section.id,
|
||||
equipmentIdentifier: `${section.prefix}1`,
|
||||
displayName: undefined,
|
||||
sortOrder: 10,
|
||||
voltage: 400,
|
||||
isReserve: true,
|
||||
},
|
||||
}),
|
||||
protectionDevice: null,
|
||||
};
|
||||
const boardUpdate = createDistributionBoardUpdateProjectCommand(
|
||||
downstream.distributionBoard.id,
|
||||
{
|
||||
feedingDistributionBoardId: source.distributionBoard.id,
|
||||
feedingCircuitId: circuit.id,
|
||||
}
|
||||
);
|
||||
const command =
|
||||
createDistributionBoardCreateFeederCircuitProjectCommand(
|
||||
circuit,
|
||||
boardUpdate
|
||||
);
|
||||
return { source, downstream, circuit, command };
|
||||
}
|
||||
|
||||
describe("distribution-board feeder-circuit project command", () => {
|
||||
it("creates and links one feeder atomically and supports undo and redo", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
const repository =
|
||||
new DistributionBoardStructureProjectCommandRepository(context.db);
|
||||
const history = new ProjectHistoryRepository(context.db);
|
||||
const { source, downstream, circuit, command } = setupBoards(
|
||||
context,
|
||||
repository
|
||||
);
|
||||
|
||||
repository.executeFeederCircuit({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 2,
|
||||
source: "user",
|
||||
command,
|
||||
});
|
||||
assert.ok(
|
||||
context.db
|
||||
.select()
|
||||
.from(circuits)
|
||||
.where(eq(circuits.id, circuit.id))
|
||||
.get()
|
||||
);
|
||||
assert.deepEqual(
|
||||
context.db
|
||||
.select({
|
||||
boardId: distributionBoards.feedingDistributionBoardId,
|
||||
circuitId: distributionBoards.feedingCircuitId,
|
||||
})
|
||||
.from(distributionBoards)
|
||||
.where(eq(distributionBoards.id, downstream.distributionBoard.id))
|
||||
.get(),
|
||||
{
|
||||
boardId: source.distributionBoard.id,
|
||||
circuitId: circuit.id,
|
||||
}
|
||||
);
|
||||
|
||||
const undo = history.getNextCommand("project-1", "undo");
|
||||
assert.ok(undo);
|
||||
assertDistributionBoardFeederCircuitProjectCommand(undo.command);
|
||||
repository.executeFeederCircuit({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 3,
|
||||
source: "undo",
|
||||
historyTargetChangeSetId: undo.changeSetId,
|
||||
command: undo.command,
|
||||
});
|
||||
assert.equal(
|
||||
context.db
|
||||
.select()
|
||||
.from(circuits)
|
||||
.where(eq(circuits.id, circuit.id))
|
||||
.get(),
|
||||
undefined
|
||||
);
|
||||
assert.deepEqual(
|
||||
context.db
|
||||
.select({
|
||||
boardId: distributionBoards.feedingDistributionBoardId,
|
||||
circuitId: distributionBoards.feedingCircuitId,
|
||||
})
|
||||
.from(distributionBoards)
|
||||
.where(eq(distributionBoards.id, downstream.distributionBoard.id))
|
||||
.get(),
|
||||
{ boardId: null, circuitId: null }
|
||||
);
|
||||
|
||||
const redo = history.getNextCommand("project-1", "redo");
|
||||
assert.ok(redo);
|
||||
assertDistributionBoardFeederCircuitProjectCommand(redo.command);
|
||||
repository.executeFeederCircuit({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 4,
|
||||
source: "redo",
|
||||
historyTargetChangeSetId: redo.changeSetId,
|
||||
command: redo.command,
|
||||
});
|
||||
assert.equal(
|
||||
context.db
|
||||
.select({ equipmentIdentifier: circuits.equipmentIdentifier })
|
||||
.from(circuits)
|
||||
.where(eq(circuits.id, circuit.id))
|
||||
.get()?.equipmentIdentifier,
|
||||
circuit.equipmentIdentifier
|
||||
);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("rolls back unlinking when the generated circuit changed before undo", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
const repository =
|
||||
new DistributionBoardStructureProjectCommandRepository(context.db);
|
||||
const history = new ProjectHistoryRepository(context.db);
|
||||
const { downstream, circuit, command } = setupBoards(context, repository);
|
||||
repository.executeFeederCircuit({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 2,
|
||||
source: "user",
|
||||
command,
|
||||
});
|
||||
context.db
|
||||
.update(circuits)
|
||||
.set({ displayName: "Außerhalb der Historie geändert" })
|
||||
.where(eq(circuits.id, circuit.id))
|
||||
.run();
|
||||
|
||||
const undo = history.getNextCommand("project-1", "undo");
|
||||
assert.ok(undo);
|
||||
assertDistributionBoardFeederCircuitProjectCommand(undo.command);
|
||||
assert.throws(
|
||||
() =>
|
||||
repository.executeFeederCircuit({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 3,
|
||||
source: "undo",
|
||||
historyTargetChangeSetId: undo.changeSetId,
|
||||
command: undo.command,
|
||||
}),
|
||||
/changed before history removal/
|
||||
);
|
||||
const board = context.db
|
||||
.select()
|
||||
.from(distributionBoards)
|
||||
.where(eq(distributionBoards.id, downstream.distributionBoard.id))
|
||||
.get();
|
||||
assert.equal(board?.feedingCircuitId, circuit.id);
|
||||
assert.ok(
|
||||
context.db
|
||||
.select()
|
||||
.from(circuits)
|
||||
.where(eq(circuits.id, circuit.id))
|
||||
.get()
|
||||
);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
168
tests/distribution-board-feeder-circuit.test.ts
Normal file
168
tests/distribution-board-feeder-circuit.test.ts
Normal file
|
|
@ -0,0 +1,168 @@
|
|||
import assert from "node:assert/strict";
|
||||
import { describe, it } from "node:test";
|
||||
import {
|
||||
buildFeederCircuitSnapshot,
|
||||
getDefaultFeederCircuitSection,
|
||||
getNextFeederCircuitIdentifier,
|
||||
} from "../src/frontend/utils/distribution-board-feeder-circuit.js";
|
||||
import type { CircuitTreeResponseDto } from "../src/frontend/types.js";
|
||||
import { createFeederCircuitAndUpdateDistributionBoard } from "../src/frontend/utils/api.js";
|
||||
|
||||
function tree(): CircuitTreeResponseDto {
|
||||
return {
|
||||
circuitListId: "list-main",
|
||||
distributionBoardId: "board-main",
|
||||
distributionBoardName: "NSHV",
|
||||
currentRevision: 2,
|
||||
singlePhaseVoltageV: 230,
|
||||
threePhaseVoltageV: 400,
|
||||
distributionBoardSimultaneityFactor: 1,
|
||||
distributionBoardTotalPower: 0,
|
||||
distributionBoardTotalPowerWithSimultaneityFactor: 0,
|
||||
headerComponents: [],
|
||||
footerComponents: [],
|
||||
sections: [
|
||||
{
|
||||
id: "single",
|
||||
key: "single_phase",
|
||||
category: "single_phase",
|
||||
groupNumber: 1,
|
||||
displayName: "1-phasig 1",
|
||||
prefix: "-2F1.",
|
||||
sortOrder: 10,
|
||||
sectionTotalPower: 0,
|
||||
components: [],
|
||||
circuits: [],
|
||||
},
|
||||
{
|
||||
id: "three",
|
||||
key: "three_phase",
|
||||
category: "three_phase",
|
||||
groupNumber: 1,
|
||||
displayName: "3-phasig 1",
|
||||
prefix: "-3F1.",
|
||||
sortOrder: 20,
|
||||
sectionTotalPower: 0,
|
||||
components: [],
|
||||
circuits: [
|
||||
{
|
||||
id: "existing-2",
|
||||
circuitListId: "list-main",
|
||||
sectionId: "three",
|
||||
equipmentIdentifier: "-3F1.2",
|
||||
sortOrder: 10,
|
||||
isReserve: true,
|
||||
circuitTotalPower: 0,
|
||||
circuitApparentPower: 0,
|
||||
circuitCosPhi: 1,
|
||||
deviceRows: [],
|
||||
downstreamBoardLoads: [],
|
||||
},
|
||||
{
|
||||
id: "existing-5",
|
||||
circuitListId: "list-main",
|
||||
sectionId: "three",
|
||||
equipmentIdentifier: "-3F1.5",
|
||||
sortOrder: 30,
|
||||
isReserve: true,
|
||||
circuitTotalPower: 0,
|
||||
circuitApparentPower: 0,
|
||||
circuitCosPhi: 1,
|
||||
deviceRows: [],
|
||||
downstreamBoardLoads: [],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
describe("distribution-board feeder circuit planning", () => {
|
||||
it("prefers a three-phase section and does not fill BMK gaps", () => {
|
||||
const input = tree();
|
||||
const section = getDefaultFeederCircuitSection(input)!;
|
||||
assert.equal(section.id, "three");
|
||||
assert.equal(getNextFeederCircuitIdentifier(section), "-3F1.6");
|
||||
});
|
||||
|
||||
it("builds an empty circuit with stable id, optional name and defaults", () => {
|
||||
const input = tree();
|
||||
const snapshot = buildFeederCircuitSnapshot({
|
||||
id: "new-feeder",
|
||||
tree: input,
|
||||
sectionId: "three",
|
||||
displayName: " ",
|
||||
});
|
||||
assert.equal(snapshot.id, "new-feeder");
|
||||
assert.equal(snapshot.equipmentIdentifier, "-3F1.6");
|
||||
assert.equal(snapshot.displayName, null);
|
||||
assert.equal(snapshot.sortOrder, 40);
|
||||
assert.equal(snapshot.voltage, 400);
|
||||
assert.equal(snapshot.isReserve, true);
|
||||
assert.deepEqual(snapshot.deviceRows, []);
|
||||
assert.equal(snapshot.protectionDevice, null);
|
||||
});
|
||||
|
||||
it("sends circuit creation and board linking as one project command", async () => {
|
||||
const input = tree();
|
||||
const circuit = buildFeederCircuitSnapshot({
|
||||
id: "new-feeder",
|
||||
tree: input,
|
||||
sectionId: "three",
|
||||
displayName: "",
|
||||
});
|
||||
const requests: Array<{ url: string; method: string; body: any }> = [];
|
||||
const originalFetch = globalThis.fetch;
|
||||
globalThis.fetch = async (request, init) => {
|
||||
requests.push({
|
||||
url: String(request),
|
||||
method: init?.method ?? "GET",
|
||||
body: JSON.parse(String(init?.body)),
|
||||
});
|
||||
return new Response(JSON.stringify({}), {
|
||||
status: 200,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
};
|
||||
try {
|
||||
await createFeederCircuitAndUpdateDistributionBoard(
|
||||
"project-1",
|
||||
"board-child",
|
||||
{
|
||||
floorId: null,
|
||||
supplyType: "AV",
|
||||
simultaneityFactor: 0.8,
|
||||
feedingDistributionBoardId: "board-main",
|
||||
feedingCircuitId: circuit.id,
|
||||
},
|
||||
circuit,
|
||||
7
|
||||
);
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
assert.equal(requests.length, 1);
|
||||
assert.equal(requests[0].url, "/api/projects/project-1/commands");
|
||||
assert.equal(requests[0].method, "POST");
|
||||
assert.equal(requests[0].body.expectedRevision, 7);
|
||||
assert.equal(
|
||||
requests[0].body.command.type,
|
||||
"distribution-board.create-feeder-circuit"
|
||||
);
|
||||
assert.equal(requests[0].body.command.payload.circuit.id, "new-feeder");
|
||||
assert.deepEqual(
|
||||
requests[0].body.command.payload.boardUpdate.payload.changes
|
||||
.filter((change: { field: string }) =>
|
||||
change.field.startsWith("feeding")
|
||||
)
|
||||
.map((change: { field: string; value: unknown }) => [
|
||||
change.field,
|
||||
change.value,
|
||||
]),
|
||||
[
|
||||
["feedingDistributionBoardId", "board-main"],
|
||||
["feedingCircuitId", "new-feeder"],
|
||||
]
|
||||
);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue