Persist section renumbering
This commit is contained in:
@@ -0,0 +1,166 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { describe, it } from "node:test";
|
||||
import {
|
||||
buildCircuitSectionRenumberAssignments,
|
||||
} from "../src/frontend/utils/circuit-section-renumber-command.js";
|
||||
import {
|
||||
renumberCircuitSectionCommand,
|
||||
} from "../src/frontend/utils/api.js";
|
||||
import type {
|
||||
CircuitTreeCircuitDto,
|
||||
CircuitTreeSectionDto,
|
||||
} from "../src/frontend/types.js";
|
||||
|
||||
function circuit(
|
||||
id: string,
|
||||
equipmentIdentifier: string,
|
||||
sortOrder: number
|
||||
): CircuitTreeCircuitDto {
|
||||
return {
|
||||
id,
|
||||
circuitListId: "list-1",
|
||||
sectionId: "section-1",
|
||||
equipmentIdentifier,
|
||||
sortOrder,
|
||||
isReserve: true,
|
||||
circuitTotalPower: 0,
|
||||
deviceRows: [],
|
||||
};
|
||||
}
|
||||
|
||||
function section(
|
||||
id: string,
|
||||
prefix: string,
|
||||
circuits: CircuitTreeCircuitDto[]
|
||||
): CircuitTreeSectionDto {
|
||||
return {
|
||||
id,
|
||||
key: id,
|
||||
displayName: id,
|
||||
prefix,
|
||||
sortOrder: 10,
|
||||
circuits: circuits.map((entry) => ({
|
||||
...entry,
|
||||
sectionId: id,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
describe("circuit section renumber command adapters", () => {
|
||||
it("plans sequential identifiers and skips conflicts outside the section", () => {
|
||||
const sections = [
|
||||
section("target", "-1F", [
|
||||
circuit("circuit-1", "-1F7", 10),
|
||||
circuit("circuit-2", "-1F9", 20),
|
||||
circuit("circuit-3", "-1F11", 30),
|
||||
]),
|
||||
section("other", "-2F", [
|
||||
circuit("circuit-other", "-1F2", 10),
|
||||
]),
|
||||
];
|
||||
|
||||
assert.deepEqual(
|
||||
buildCircuitSectionRenumberAssignments(sections, "target"),
|
||||
[
|
||||
{
|
||||
circuitId: "circuit-1",
|
||||
expectedEquipmentIdentifier: "-1F7",
|
||||
targetEquipmentIdentifier: "-1F1",
|
||||
},
|
||||
{
|
||||
circuitId: "circuit-2",
|
||||
expectedEquipmentIdentifier: "-1F9",
|
||||
targetEquipmentIdentifier: "-1F3",
|
||||
},
|
||||
{
|
||||
circuitId: "circuit-3",
|
||||
expectedEquipmentIdentifier: "-1F11",
|
||||
targetEquipmentIdentifier: "-1F4",
|
||||
},
|
||||
]
|
||||
);
|
||||
});
|
||||
|
||||
it("omits an already canonical section and rejects an unknown section", () => {
|
||||
const sections = [
|
||||
section("target", "-2F", [
|
||||
circuit("circuit-1", "-2F1", 10),
|
||||
circuit("circuit-2", "-2F2", 20),
|
||||
]),
|
||||
];
|
||||
assert.deepEqual(
|
||||
buildCircuitSectionRenumberAssignments(sections, "target"),
|
||||
[]
|
||||
);
|
||||
assert.throws(
|
||||
() =>
|
||||
buildCircuitSectionRenumberAssignments(
|
||||
sections,
|
||||
"missing"
|
||||
),
|
||||
/nicht gefunden/
|
||||
);
|
||||
});
|
||||
|
||||
it("sends the complete assignment set through project history", async () => {
|
||||
let requestBody: Record<string, unknown> | null = null;
|
||||
const originalFetch = globalThis.fetch;
|
||||
globalThis.fetch = async (_input, init) => {
|
||||
requestBody = JSON.parse(
|
||||
String(init?.body)
|
||||
) as Record<string, unknown>;
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
revision: {
|
||||
revisionId: "revision-1",
|
||||
changeSetId: "change-1",
|
||||
projectId: "project-1",
|
||||
revisionNumber: 1,
|
||||
createdAtIso: "2026-07-25T00:00:00.000Z",
|
||||
},
|
||||
history: {
|
||||
projectId: "project-1",
|
||||
currentRevision: 1,
|
||||
undoDepth: 1,
|
||||
redoDepth: 0,
|
||||
undoChangeSetId: "change-1",
|
||||
redoChangeSetId: null,
|
||||
},
|
||||
}),
|
||||
{ status: 200, headers: { "Content-Type": "application/json" } }
|
||||
);
|
||||
};
|
||||
|
||||
const assignments = [
|
||||
{
|
||||
circuitId: "circuit-1",
|
||||
expectedEquipmentIdentifier: "-2F9",
|
||||
targetEquipmentIdentifier: "-2F1",
|
||||
},
|
||||
];
|
||||
try {
|
||||
await renumberCircuitSectionCommand(
|
||||
"project-1",
|
||||
0,
|
||||
"section-1",
|
||||
assignments,
|
||||
"renumber"
|
||||
);
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
|
||||
assert.deepEqual(requestBody, {
|
||||
expectedRevision: 0,
|
||||
description: "renumber",
|
||||
command: {
|
||||
schemaVersion: 1,
|
||||
type: "circuit.renumber-section",
|
||||
payload: {
|
||||
sectionId: "section-1",
|
||||
assignments,
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,153 +0,0 @@
|
||||
import path from "node:path";
|
||||
import assert from "node:assert/strict";
|
||||
import { describe, it } from "node:test";
|
||||
import { asc, eq } from "drizzle-orm";
|
||||
import { migrate } from "drizzle-orm/better-sqlite3/migrator";
|
||||
import {
|
||||
createDatabaseContext,
|
||||
type DatabaseContext,
|
||||
} from "../src/db/database-context.js";
|
||||
import { CircuitSectionTransactionRepository } from "../src/db/repositories/circuit-section-transaction.repository.js";
|
||||
import { DistributionBoardRepository } from "../src/db/repositories/distribution-board.repository.js";
|
||||
import { circuitDeviceRows } from "../src/db/schema/circuit-device-rows.js";
|
||||
import { circuitSections } from "../src/db/schema/circuit-sections.js";
|
||||
import { circuits } from "../src/db/schema/circuits.js";
|
||||
import { projects } from "../src/db/schema/projects.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();
|
||||
const board = new DistributionBoardRepository(
|
||||
context.db
|
||||
).createWithCircuitListAndDefaultSections("project-1", "UV-01");
|
||||
const [section] = context.db
|
||||
.select()
|
||||
.from(circuitSections)
|
||||
.where(eq(circuitSections.circuitListId, board.id))
|
||||
.limit(1)
|
||||
.all();
|
||||
context.db
|
||||
.insert(circuits)
|
||||
.values([
|
||||
{
|
||||
id: "circuit-1",
|
||||
circuitListId: board.id,
|
||||
sectionId: section.id,
|
||||
equipmentIdentifier: "-1F1",
|
||||
displayName: "Stromkreis 1",
|
||||
sortOrder: 10,
|
||||
isReserve: 0,
|
||||
},
|
||||
{
|
||||
id: "circuit-2",
|
||||
circuitListId: board.id,
|
||||
sectionId: section.id,
|
||||
equipmentIdentifier: "-1F2",
|
||||
displayName: "Stromkreis 2",
|
||||
sortOrder: 20,
|
||||
isReserve: 1,
|
||||
},
|
||||
{
|
||||
id: "circuit-3",
|
||||
circuitListId: board.id,
|
||||
sectionId: section.id,
|
||||
equipmentIdentifier: "-1F3",
|
||||
displayName: "Stromkreis 3",
|
||||
sortOrder: 30,
|
||||
isReserve: 1,
|
||||
},
|
||||
])
|
||||
.run();
|
||||
context.db
|
||||
.insert(circuitDeviceRows)
|
||||
.values({
|
||||
id: "row-1",
|
||||
circuitId: "circuit-1",
|
||||
sortOrder: 10,
|
||||
name: "Leuchte",
|
||||
displayName: "Leuchte",
|
||||
quantity: 1,
|
||||
powerPerUnit: 0.1,
|
||||
simultaneityFactor: 1,
|
||||
})
|
||||
.run();
|
||||
return context;
|
||||
}
|
||||
|
||||
function listCircuits(context: DatabaseContext) {
|
||||
return context.db
|
||||
.select()
|
||||
.from(circuits)
|
||||
.orderBy(asc(circuits.id))
|
||||
.all();
|
||||
}
|
||||
|
||||
describe("circuit-section transaction repository", () => {
|
||||
it("commits identifier swaps without violating the circuit-list uniqueness constraint", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
const persisted = listCircuits(context);
|
||||
const repository = new CircuitSectionTransactionRepository(context.db);
|
||||
|
||||
repository.updateEquipmentIdentifiers(
|
||||
persisted[0].circuitListId,
|
||||
[
|
||||
{ id: "circuit-1", equipmentIdentifier: "-1F2" },
|
||||
{ id: "circuit-2", equipmentIdentifier: "-1F1" },
|
||||
],
|
||||
persisted[0].sectionId
|
||||
);
|
||||
|
||||
assert.deepEqual(
|
||||
listCircuits(context).map((circuit) => circuit.equipmentIdentifier),
|
||||
["-1F2", "-1F1", "-1F3"]
|
||||
);
|
||||
assert.equal(
|
||||
context.db.select().from(circuitDeviceRows).all()[0].circuitId,
|
||||
"circuit-1"
|
||||
);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("rolls back temporary and final identifiers when a later update fails", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
const persisted = listCircuits(context);
|
||||
context.sqlite.exec(`
|
||||
CREATE TRIGGER fail_second_final_identifier
|
||||
BEFORE UPDATE OF equipment_identifier ON circuits
|
||||
WHEN OLD.id = 'circuit-2' AND NEW.equipment_identifier = '-1F1'
|
||||
BEGIN
|
||||
SELECT RAISE(ABORT, 'forced identifier failure');
|
||||
END;
|
||||
`);
|
||||
const repository = new CircuitSectionTransactionRepository(context.db);
|
||||
|
||||
assert.throws(
|
||||
() =>
|
||||
repository.updateEquipmentIdentifiers(
|
||||
persisted[0].circuitListId,
|
||||
[
|
||||
{ id: "circuit-1", equipmentIdentifier: "-1F2" },
|
||||
{ id: "circuit-2", equipmentIdentifier: "-1F1" },
|
||||
],
|
||||
persisted[0].sectionId
|
||||
),
|
||||
/forced identifier failure/
|
||||
);
|
||||
|
||||
assert.deepEqual(
|
||||
listCircuits(context).map((circuit) => circuit.equipmentIdentifier),
|
||||
["-1F1", "-1F2", "-1F3"]
|
||||
);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
@@ -1,143 +0,0 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { describe, it } from "node:test";
|
||||
import { CircuitWriteService } from "../src/domain/services/circuit-write.service.js";
|
||||
|
||||
describe("circuit write service rules", () => {
|
||||
it("renumber uses safe bulk identifier update for swapped identifiers", async () => {
|
||||
let safeUpdatePayload: Array<{ id: string; equipmentIdentifier: string }> = [];
|
||||
const service = new CircuitWriteService({
|
||||
circuitSectionRepository: {
|
||||
async findById() {
|
||||
return { id: "s1", circuitListId: "l1", prefix: "-2F" } as never;
|
||||
},
|
||||
} as never,
|
||||
circuitRepository: {
|
||||
async listBySection() {
|
||||
return [
|
||||
{ id: "cB", sectionId: "s1", equipmentIdentifier: "-2F2", sortOrder: 10, isReserve: 0 },
|
||||
{ id: "cA", sectionId: "s1", equipmentIdentifier: "-2F1", sortOrder: 20, isReserve: 1 },
|
||||
] as never[];
|
||||
},
|
||||
async listByCircuitList() {
|
||||
return [{ id: "x1", sectionId: "s2", equipmentIdentifier: "-3F1" }] as never[];
|
||||
},
|
||||
} as never,
|
||||
circuitSectionTransactionStore: {
|
||||
updateEquipmentIdentifiers(
|
||||
_listId: string,
|
||||
updates: Array<{ id: string; equipmentIdentifier: string }>
|
||||
) {
|
||||
safeUpdatePayload = updates;
|
||||
},
|
||||
} as never,
|
||||
});
|
||||
|
||||
const result = await service.renumberSection("s1");
|
||||
assert.deepEqual(safeUpdatePayload, [
|
||||
{ id: "cB", equipmentIdentifier: "-2F1" },
|
||||
{ id: "cA", equipmentIdentifier: "-2F2" },
|
||||
]);
|
||||
assert.equal(result.length, 2);
|
||||
});
|
||||
|
||||
it("renumber shifts forward/backward and respects other sections", async () => {
|
||||
let safeUpdatePayload: Array<{ id: string; equipmentIdentifier: string }> = [];
|
||||
const service = new CircuitWriteService({
|
||||
circuitSectionRepository: {
|
||||
async findById() {
|
||||
return { id: "s1", circuitListId: "l1", prefix: "-1F" } as never;
|
||||
},
|
||||
} as never,
|
||||
circuitRepository: {
|
||||
async listBySection() {
|
||||
return [
|
||||
{ id: "c2", sectionId: "s1", equipmentIdentifier: "-1F5", sortOrder: 10, isReserve: 0 },
|
||||
{ id: "c1", sectionId: "s1", equipmentIdentifier: "-1F1", sortOrder: 20, isReserve: 0 },
|
||||
{ id: "c3", sectionId: "s1", equipmentIdentifier: "-1F9", sortOrder: 30, isReserve: 0 },
|
||||
] as never[];
|
||||
},
|
||||
async listByCircuitList() {
|
||||
return [{ id: "o1", sectionId: "s2", equipmentIdentifier: "-1F2" }] as never[];
|
||||
},
|
||||
} as never,
|
||||
circuitSectionTransactionStore: {
|
||||
updateEquipmentIdentifiers(
|
||||
_listId: string,
|
||||
updates: Array<{ id: string; equipmentIdentifier: string }>
|
||||
) {
|
||||
safeUpdatePayload = updates;
|
||||
},
|
||||
} as never,
|
||||
});
|
||||
|
||||
await service.renumberSection("s1");
|
||||
assert.deepEqual(safeUpdatePayload, [
|
||||
{ id: "c2", equipmentIdentifier: "-1F1" },
|
||||
{ id: "c1", equipmentIdentifier: "-1F3" },
|
||||
{ id: "c3", equipmentIdentifier: "-1F4" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("renumber handles gaps without touching device rows", async () => {
|
||||
let safeCalled = 0;
|
||||
const service = new CircuitWriteService({
|
||||
circuitSectionRepository: {
|
||||
async findById() {
|
||||
return { id: "s1", circuitListId: "l1", prefix: "-1F" } as never;
|
||||
},
|
||||
} as never,
|
||||
circuitRepository: {
|
||||
async listBySection() {
|
||||
return [
|
||||
{ id: "c1", sectionId: "s1", equipmentIdentifier: "-1F1", sortOrder: 10, isReserve: 0 },
|
||||
{ id: "c2", sectionId: "s1", equipmentIdentifier: "-1F5", sortOrder: 20, isReserve: 0 },
|
||||
{ id: "c3", sectionId: "s1", equipmentIdentifier: "-1F9", sortOrder: 30, isReserve: 0 },
|
||||
] as never[];
|
||||
},
|
||||
async listByCircuitList() {
|
||||
return [] as never[];
|
||||
},
|
||||
} as never,
|
||||
circuitSectionTransactionStore: {
|
||||
updateEquipmentIdentifiers() {
|
||||
safeCalled += 1;
|
||||
},
|
||||
} as never,
|
||||
});
|
||||
|
||||
await service.renumberSection("s1");
|
||||
assert.equal(safeCalled, 1);
|
||||
});
|
||||
|
||||
it("validates the complete section set before safe identifier updates", async () => {
|
||||
let safeCalled = false;
|
||||
const service = new CircuitWriteService({
|
||||
circuitSectionRepository: {
|
||||
async findById() {
|
||||
return { id: "s1", circuitListId: "l1", prefix: "-1F" } as never;
|
||||
},
|
||||
} as never,
|
||||
circuitRepository: {
|
||||
async listBySection() {
|
||||
return [
|
||||
{ id: "c1", sectionId: "s1", equipmentIdentifier: "-1F1", sortOrder: 10, isReserve: 0 },
|
||||
{ id: "c2", sectionId: "s1", equipmentIdentifier: "-1F2", sortOrder: 20, isReserve: 0 },
|
||||
] as never[];
|
||||
},
|
||||
} as never,
|
||||
circuitSectionTransactionStore: {
|
||||
updateEquipmentIdentifiers() {
|
||||
safeCalled = true;
|
||||
},
|
||||
} as never,
|
||||
});
|
||||
|
||||
await service.updateSectionEquipmentIdentifiers("s1", {
|
||||
identifiers: [
|
||||
{ circuitId: "c1", equipmentIdentifier: "-1F2" },
|
||||
{ circuitId: "c2", equipmentIdentifier: "-1F1" },
|
||||
],
|
||||
});
|
||||
assert.equal(safeCalled, true);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user