144 lines
5.0 KiB
TypeScript
144 lines
5.0 KiB
TypeScript
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);
|
|
});
|
|
});
|