Make circuit reordering atomic
This commit is contained in:
@@ -580,7 +580,7 @@ describe("circuit write service rules", () => {
|
||||
});
|
||||
|
||||
it("reorders circuits inside one section without renumbering identifiers", async () => {
|
||||
const updates: Array<{ id: string; sortOrder: number; equipmentIdentifier: string }> = [];
|
||||
let safeReorder: { sectionId: string; circuitIds: string[] } | undefined;
|
||||
const service = new CircuitWriteService({
|
||||
circuitSectionRepository: {
|
||||
async findById() {
|
||||
@@ -595,18 +595,17 @@ describe("circuit write service rules", () => {
|
||||
{ id: "c3", sectionId: "s1", equipmentIdentifier: "-2F5", sortOrder: 30, isReserve: 1 },
|
||||
] as never[];
|
||||
},
|
||||
async update(id: string, payload: { sortOrder: number; equipmentIdentifier: string }) {
|
||||
updates.push({ id, sortOrder: payload.sortOrder, equipmentIdentifier: payload.equipmentIdentifier });
|
||||
updateSortOrdersSafely(sectionId: string, circuitIds: string[]) {
|
||||
safeReorder = { sectionId, circuitIds };
|
||||
},
|
||||
} as never,
|
||||
});
|
||||
|
||||
await service.reorderCircuitsInSection("s1", { orderedCircuitIds: ["c3", "c1", "c2"] });
|
||||
assert.deepEqual(updates, [
|
||||
{ id: "c3", sortOrder: 10, equipmentIdentifier: "-2F5" },
|
||||
{ id: "c1", sortOrder: 20, equipmentIdentifier: "-2F7" },
|
||||
{ id: "c2", sortOrder: 30, equipmentIdentifier: "-2F9" },
|
||||
]);
|
||||
assert.deepEqual(safeReorder, {
|
||||
sectionId: "s1",
|
||||
circuitIds: ["c3", "c1", "c2"],
|
||||
});
|
||||
});
|
||||
|
||||
it("safe section identifier bulk update validates full section set (undo safety)", async () => {
|
||||
@@ -698,6 +697,61 @@ describe("circuit write service rules", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("safe circuit reorder uses one synchronous transaction callback", () => {
|
||||
const repository = new CircuitRepository();
|
||||
const originalTransaction = (db as unknown as { transaction: unknown }).transaction;
|
||||
|
||||
let callbackReturnedPromise = false;
|
||||
const sortOrders: number[] = [];
|
||||
(db as unknown as { transaction: (cb: (tx: unknown) => unknown) => void }).transaction = (cb) => {
|
||||
const fakeTx = {
|
||||
select() {
|
||||
return {
|
||||
from() {
|
||||
return {
|
||||
where() {
|
||||
return {
|
||||
all() {
|
||||
return [{ id: "c1" }, { id: "c2" }, { id: "c3" }];
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
},
|
||||
update() {
|
||||
return {
|
||||
set(values: { sortOrder: number }) {
|
||||
sortOrders.push(values.sortOrder);
|
||||
return {
|
||||
where() {
|
||||
return {
|
||||
run() {
|
||||
return { changes: 1 };
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
const callbackResult = cb(fakeTx);
|
||||
callbackReturnedPromise = Boolean(
|
||||
callbackResult && typeof (callbackResult as Promise<unknown>).then === "function"
|
||||
);
|
||||
};
|
||||
|
||||
try {
|
||||
repository.updateSortOrdersSafely("s1", ["c3", "c1", "c2"]);
|
||||
assert.equal(callbackReturnedPromise, false);
|
||||
assert.deepEqual(sortOrders, [10, 20, 30]);
|
||||
} finally {
|
||||
(db as unknown as { transaction: unknown }).transaction = originalTransaction;
|
||||
}
|
||||
});
|
||||
|
||||
it("bulk device row move uses one synchronous transaction callback", () => {
|
||||
const repository = new CircuitDeviceRowRepository();
|
||||
const originalTransaction = (db as unknown as { transaction: unknown }).transaction;
|
||||
|
||||
Reference in New Issue
Block a user