Persist circuit group moves
This commit is contained in:
@@ -0,0 +1,305 @@
|
||||
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 { CircuitGroupMoveProjectCommandRepository } from "../src/db/repositories/circuit-group-move-project-command.repository.js";
|
||||
import { ProjectHistoryRepository } from "../src/db/repositories/project-history.repository.js";
|
||||
import { circuitDeviceRows } from "../src/db/schema/circuit-device-rows.js";
|
||||
import { circuitLists } from "../src/db/schema/circuit-lists.js";
|
||||
import { circuitProtectionDevices } from "../src/db/schema/circuit-protection-devices.js";
|
||||
import { circuitSections } from "../src/db/schema/circuit-sections.js";
|
||||
import { circuits } from "../src/db/schema/circuits.js";
|
||||
import { projectRevisions } from "../src/db/schema/project-revisions.js";
|
||||
import { projects } from "../src/db/schema/projects.js";
|
||||
import { createCircuitGroupMoveProjectCommand } from "../src/domain/models/circuit-group-move-project-command.model.js";
|
||||
import { createCircuitGroupMovePlan } from "../src/domain/services/circuit-group-move-planning.js";
|
||||
import { DistributionBoardFixtureRepository } from "./support/distribution-board-fixture.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: "Projekt" })
|
||||
.run();
|
||||
new DistributionBoardFixtureRepository(
|
||||
context.db
|
||||
).createWithCircuitListAndDefaultSections("project-1", "UV-01");
|
||||
const list = context.db.select().from(circuitLists).get()!;
|
||||
const source = context.db
|
||||
.select()
|
||||
.from(circuitSections)
|
||||
.where(eq(circuitSections.category, "lighting"))
|
||||
.get()!;
|
||||
context.db
|
||||
.insert(circuitSections)
|
||||
.values({
|
||||
id: "lighting-move-target",
|
||||
circuitListId: list.id,
|
||||
key: "lighting_2",
|
||||
displayName: "Beleuchtung 2",
|
||||
prefix: "-1F2.",
|
||||
sortOrder: 40,
|
||||
category: "lighting",
|
||||
groupNumber: 2,
|
||||
})
|
||||
.run();
|
||||
context.db
|
||||
.insert(circuits)
|
||||
.values([
|
||||
{
|
||||
id: "circuit-move",
|
||||
circuitListId: list.id,
|
||||
sectionId: source.id,
|
||||
equipmentIdentifier: "-1F1.7",
|
||||
displayName: "Zu verschieben",
|
||||
sortOrder: 20,
|
||||
},
|
||||
{
|
||||
id: "circuit-target-existing",
|
||||
circuitListId: list.id,
|
||||
sectionId: "lighting-move-target",
|
||||
equipmentIdentifier: "-1F2.4",
|
||||
displayName: "Bestand",
|
||||
sortOrder: 10,
|
||||
},
|
||||
])
|
||||
.run();
|
||||
context.db
|
||||
.insert(circuitDeviceRows)
|
||||
.values({
|
||||
id: "row-move",
|
||||
circuitId: "circuit-move",
|
||||
sortOrder: 10,
|
||||
name: "Leuchte",
|
||||
displayName: "Leuchte",
|
||||
quantity: 2,
|
||||
powerPerUnit: 0.1,
|
||||
simultaneityFactor: 1,
|
||||
})
|
||||
.run();
|
||||
context.db
|
||||
.insert(circuitProtectionDevices)
|
||||
.values({
|
||||
circuitId: "circuit-move",
|
||||
type: "LS",
|
||||
ratedCurrentA: 10,
|
||||
tripCharacteristic: "B",
|
||||
})
|
||||
.run();
|
||||
return context;
|
||||
}
|
||||
|
||||
function createMoveCommand(context: DatabaseContext) {
|
||||
const circuit = context.db
|
||||
.select()
|
||||
.from(circuits)
|
||||
.where(eq(circuits.id, "circuit-move"))
|
||||
.get()!;
|
||||
const source = context.db
|
||||
.select()
|
||||
.from(circuitSections)
|
||||
.where(eq(circuitSections.id, circuit.sectionId))
|
||||
.get()!;
|
||||
const target = context.db
|
||||
.select()
|
||||
.from(circuitSections)
|
||||
.where(eq(circuitSections.id, "lighting-move-target"))
|
||||
.get()!;
|
||||
return createCircuitGroupMoveProjectCommand(
|
||||
createCircuitGroupMovePlan({
|
||||
circuit,
|
||||
sourceGroup: {
|
||||
id: source.id,
|
||||
category: source.category!,
|
||||
groupNumber: source.groupNumber!,
|
||||
prefix: source.prefix,
|
||||
},
|
||||
targetGroup: {
|
||||
id: target.id,
|
||||
category: target.category!,
|
||||
groupNumber: target.groupNumber!,
|
||||
prefix: target.prefix,
|
||||
},
|
||||
targetCircuitEquipmentIdentifiers: context.db
|
||||
.select({ value: circuits.equipmentIdentifier })
|
||||
.from(circuits)
|
||||
.where(eq(circuits.sectionId, target.id))
|
||||
.all()
|
||||
.map(({ value }) => value),
|
||||
targetSortOrder: 30,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
describe("circuit group-move project command", () => {
|
||||
it("moves a complete circuit while preserving rows and protection through undo/redo", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
const command = createMoveCommand(context);
|
||||
const repository =
|
||||
new CircuitGroupMoveProjectCommandRepository(context.db);
|
||||
const moved = repository.execute({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 0,
|
||||
source: "user",
|
||||
command,
|
||||
});
|
||||
assert.deepEqual(
|
||||
context.db
|
||||
.select({
|
||||
sectionId: circuits.sectionId,
|
||||
equipmentIdentifier: circuits.equipmentIdentifier,
|
||||
sortOrder: circuits.sortOrder,
|
||||
})
|
||||
.from(circuits)
|
||||
.where(eq(circuits.id, "circuit-move"))
|
||||
.get(),
|
||||
{
|
||||
sectionId: "lighting-move-target",
|
||||
equipmentIdentifier: "-1F2.5",
|
||||
sortOrder: 30,
|
||||
}
|
||||
);
|
||||
assert.equal(
|
||||
context.db
|
||||
.select()
|
||||
.from(circuitDeviceRows)
|
||||
.where(eq(circuitDeviceRows.id, "row-move"))
|
||||
.get()?.circuitId,
|
||||
"circuit-move"
|
||||
);
|
||||
assert.equal(
|
||||
context.db
|
||||
.select()
|
||||
.from(circuitProtectionDevices)
|
||||
.where(eq(circuitProtectionDevices.circuitId, "circuit-move"))
|
||||
.get()?.ratedCurrentA,
|
||||
10
|
||||
);
|
||||
|
||||
repository.execute({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 1,
|
||||
source: "undo",
|
||||
historyTargetChangeSetId: new ProjectHistoryRepository(
|
||||
context.db
|
||||
).getNextCommand("project-1", "undo")?.changeSetId,
|
||||
command: moved.inverse,
|
||||
});
|
||||
assert.equal(
|
||||
context.db
|
||||
.select()
|
||||
.from(circuits)
|
||||
.where(eq(circuits.id, "circuit-move"))
|
||||
.get()?.equipmentIdentifier,
|
||||
"-1F1.7"
|
||||
);
|
||||
|
||||
repository.execute({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 2,
|
||||
source: "redo",
|
||||
historyTargetChangeSetId: new ProjectHistoryRepository(
|
||||
context.db
|
||||
).getNextCommand("project-1", "redo")?.changeSetId,
|
||||
command,
|
||||
});
|
||||
assert.equal(
|
||||
context.db
|
||||
.select()
|
||||
.from(circuits)
|
||||
.where(eq(circuits.id, "circuit-move"))
|
||||
.get()?.equipmentIdentifier,
|
||||
"-1F2.5"
|
||||
);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects stale state without moving the circuit", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
const command = createMoveCommand(context);
|
||||
context.db
|
||||
.update(circuits)
|
||||
.set({ sortOrder: 99 })
|
||||
.where(eq(circuits.id, "circuit-move"))
|
||||
.run();
|
||||
assert.throws(
|
||||
() =>
|
||||
new CircuitGroupMoveProjectCommandRepository(context.db).execute({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 0,
|
||||
source: "user",
|
||||
command,
|
||||
}),
|
||||
/changed before group move/
|
||||
);
|
||||
assert.equal(
|
||||
context.db
|
||||
.select()
|
||||
.from(circuits)
|
||||
.where(eq(circuits.id, "circuit-move"))
|
||||
.get()?.sectionId,
|
||||
command.payload.expectedSectionId
|
||||
);
|
||||
assert.equal(
|
||||
context.db.select().from(projectRevisions).all().length,
|
||||
0
|
||||
);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("rolls back the circuit move when history persistence fails", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
const command = createMoveCommand(context);
|
||||
context.sqlite.exec(`
|
||||
CREATE TRIGGER fail_circuit_group_move_history
|
||||
BEFORE INSERT ON project_history_stack_entries
|
||||
BEGIN
|
||||
SELECT RAISE(ABORT, 'forced circuit group move history failure');
|
||||
END;
|
||||
`);
|
||||
assert.throws(
|
||||
() =>
|
||||
new CircuitGroupMoveProjectCommandRepository(context.db).execute({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 0,
|
||||
source: "user",
|
||||
command,
|
||||
}),
|
||||
/forced circuit group move history failure/
|
||||
);
|
||||
assert.deepEqual(
|
||||
context.db
|
||||
.select({
|
||||
sectionId: circuits.sectionId,
|
||||
equipmentIdentifier: circuits.equipmentIdentifier,
|
||||
sortOrder: circuits.sortOrder,
|
||||
})
|
||||
.from(circuits)
|
||||
.where(eq(circuits.id, "circuit-move"))
|
||||
.get(),
|
||||
{
|
||||
sectionId: command.payload.expectedSectionId,
|
||||
equipmentIdentifier: "-1F1.7",
|
||||
sortOrder: 20,
|
||||
}
|
||||
);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -12,6 +12,7 @@ import "./circuit-group-structure-project-command.repository.test.js";
|
||||
import { createCircuitGroupRenumberPlan } from "../src/domain/services/circuit-group-renumbering.js";
|
||||
import "./circuit-group-renumber-project-command.repository.test.js";
|
||||
import { createCircuitGroupMovePlan } from "../src/domain/services/circuit-group-move-planning.js";
|
||||
import "./circuit-group-move-project-command.repository.test.js";
|
||||
|
||||
describe("circuit group numbering", () => {
|
||||
it("formats the agreed identifiers including the leading hyphen", () => {
|
||||
|
||||
@@ -16,6 +16,7 @@ import { CircuitSectionRenumberProjectCommandRepository } from "../src/db/reposi
|
||||
import { CircuitStructureProjectCommandRepository } from "../src/db/repositories/circuit-structure-project-command.repository.js";
|
||||
import { CircuitGroupStructureProjectCommandRepository } from "../src/db/repositories/circuit-group-structure-project-command.repository.js";
|
||||
import { CircuitGroupRenumberProjectCommandRepository } from "../src/db/repositories/circuit-group-renumber-project-command.repository.js";
|
||||
import { CircuitGroupMoveProjectCommandRepository } from "../src/db/repositories/circuit-group-move-project-command.repository.js";
|
||||
import { DistributionBoardFixtureRepository } from "./support/distribution-board-fixture.js";
|
||||
import { DistributionBoardStructureProjectCommandRepository } from "../src/db/repositories/distribution-board-structure-project-command.repository.js";
|
||||
import { DistributionBoardComponentStructureProjectCommandRepository } from "../src/db/repositories/distribution-board-component-structure-project-command.repository.js";
|
||||
@@ -56,6 +57,8 @@ import {
|
||||
} from "../src/domain/models/circuit-group-structure-project-command.model.js";
|
||||
import { createCircuitGroupRenumberProjectCommand } from "../src/domain/models/circuit-group-renumber-project-command.model.js";
|
||||
import { createCircuitGroupRenumberPlan } from "../src/domain/services/circuit-group-renumbering.js";
|
||||
import { createCircuitGroupMoveProjectCommand } from "../src/domain/models/circuit-group-move-project-command.model.js";
|
||||
import { createCircuitGroupMovePlan } from "../src/domain/services/circuit-group-move-planning.js";
|
||||
import {
|
||||
createDistributionBoardInsertProjectCommand,
|
||||
createDistributionBoardStructureSnapshot,
|
||||
@@ -142,6 +145,7 @@ function createService(context: DatabaseContext) {
|
||||
),
|
||||
new CircuitGroupStructureProjectCommandRepository(context.db),
|
||||
new CircuitGroupRenumberProjectCommandRepository(context.db),
|
||||
new CircuitGroupMoveProjectCommandRepository(context.db),
|
||||
new ProjectHistoryRepository(context.db)
|
||||
);
|
||||
}
|
||||
@@ -1370,6 +1374,79 @@ describe("project command service", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("dispatches same-category circuit group moves", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
const service = createService(context);
|
||||
const source = context.db
|
||||
.select()
|
||||
.from(circuitSections)
|
||||
.where(eq(circuitSections.category, "three_phase"))
|
||||
.get()!;
|
||||
const target = {
|
||||
id: "group-move-service-target",
|
||||
circuitListId: source.circuitListId,
|
||||
key: "three_phase_2",
|
||||
displayName: "3-phasig 2",
|
||||
prefix: "-3F2.",
|
||||
sortOrder: 40,
|
||||
category: "three_phase" as const,
|
||||
groupNumber: 2,
|
||||
};
|
||||
service.executeUser({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 0,
|
||||
command: createCircuitGroupInsertProjectCommand(target),
|
||||
});
|
||||
context.db
|
||||
.insert(circuits)
|
||||
.values({
|
||||
id: "circuit-group-move-service",
|
||||
circuitListId: source.circuitListId,
|
||||
sectionId: source.id,
|
||||
equipmentIdentifier: "-3F1.1",
|
||||
displayName: "Drehstrom",
|
||||
sortOrder: 10,
|
||||
})
|
||||
.run();
|
||||
const result = service.executeUser({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 1,
|
||||
command: createCircuitGroupMoveProjectCommand(
|
||||
createCircuitGroupMovePlan({
|
||||
circuit: {
|
||||
id: "circuit-group-move-service",
|
||||
circuitListId: source.circuitListId,
|
||||
sectionId: source.id,
|
||||
equipmentIdentifier: "-3F1.1",
|
||||
sortOrder: 10,
|
||||
},
|
||||
sourceGroup: {
|
||||
id: source.id,
|
||||
category: "three_phase",
|
||||
groupNumber: 1,
|
||||
prefix: "-3F1.",
|
||||
},
|
||||
targetGroup: target,
|
||||
targetCircuitEquipmentIdentifiers: [],
|
||||
targetSortOrder: 20,
|
||||
})
|
||||
),
|
||||
});
|
||||
assert.equal(result.history.currentRevision, 2);
|
||||
assert.equal(
|
||||
context.db
|
||||
.select()
|
||||
.from(circuits)
|
||||
.where(eq(circuits.id, "circuit-group-move-service"))
|
||||
.get()?.equipmentIdentifier,
|
||||
"-3F2.1"
|
||||
);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("dispatches floor and room setup with their persisted inverses", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
|
||||
@@ -16,6 +16,7 @@ import { CircuitSectionReorderProjectCommandRepository } from "../src/db/reposit
|
||||
import { CircuitStructureProjectCommandRepository } from "../src/db/repositories/circuit-structure-project-command.repository.js";
|
||||
import { CircuitGroupStructureProjectCommandRepository } from "../src/db/repositories/circuit-group-structure-project-command.repository.js";
|
||||
import { CircuitGroupRenumberProjectCommandRepository } from "../src/db/repositories/circuit-group-renumber-project-command.repository.js";
|
||||
import { CircuitGroupMoveProjectCommandRepository } from "../src/db/repositories/circuit-group-move-project-command.repository.js";
|
||||
import { DistributionBoardFixtureRepository } from "./support/distribution-board-fixture.js";
|
||||
import { DistributionBoardStructureProjectCommandRepository } from "../src/db/repositories/distribution-board-structure-project-command.repository.js";
|
||||
import { DistributionBoardComponentStructureProjectCommandRepository } from "../src/db/repositories/distribution-board-component-structure-project-command.repository.js";
|
||||
@@ -199,6 +200,7 @@ function createService(context: DatabaseContext) {
|
||||
),
|
||||
new CircuitGroupStructureProjectCommandRepository(context.db),
|
||||
new CircuitGroupRenumberProjectCommandRepository(context.db),
|
||||
new CircuitGroupMoveProjectCommandRepository(context.db),
|
||||
new ProjectHistoryRepository(context.db)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -169,6 +169,12 @@ describe("project version history presentation", () => {
|
||||
),
|
||||
"Stromkreisgruppen neu nummeriert"
|
||||
);
|
||||
assert.equal(
|
||||
getProjectRevisionDescription(
|
||||
revision(13, { commandType: "circuit.move-group" })
|
||||
),
|
||||
"Stromkreis in andere Gruppe verschoben"
|
||||
);
|
||||
assert.equal(getProjectSnapshotKindLabel("named"), "Benannt");
|
||||
assert.equal(
|
||||
getProjectSnapshotKindLabel("automatic"),
|
||||
|
||||
Reference in New Issue
Block a user