Persist circuit reorders
This commit is contained in:
@@ -0,0 +1,190 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { describe, it } from "node:test";
|
||||
import {
|
||||
createCircuitSectionsReorderProjectCommand,
|
||||
} from "../src/domain/models/circuit-sections-reorder-project-command.model.js";
|
||||
import {
|
||||
buildCircuitSectionReorderAssignments,
|
||||
} from "../src/frontend/utils/circuit-section-reorder-command.js";
|
||||
import {
|
||||
reorderCircuitSectionCommand,
|
||||
reorderCircuitSectionsCommand,
|
||||
} from "../src/frontend/utils/api.js";
|
||||
import type { CircuitTreeCircuitDto } from "../src/frontend/types.js";
|
||||
|
||||
function circuit(
|
||||
id: string,
|
||||
sortOrder: number
|
||||
): CircuitTreeCircuitDto {
|
||||
return {
|
||||
id,
|
||||
circuitListId: "list-1",
|
||||
sectionId: "section-1",
|
||||
equipmentIdentifier: id,
|
||||
sortOrder,
|
||||
isReserve: true,
|
||||
circuitTotalPower: 0,
|
||||
deviceRows: [],
|
||||
};
|
||||
}
|
||||
|
||||
describe("circuit section reorder command adapters", () => {
|
||||
it("builds complete exact assignments for a requested order", () => {
|
||||
const circuits = [
|
||||
circuit("circuit-1", 10),
|
||||
circuit("circuit-2", 20),
|
||||
circuit("circuit-3", 30),
|
||||
];
|
||||
|
||||
assert.deepEqual(
|
||||
buildCircuitSectionReorderAssignments(circuits, [
|
||||
"circuit-3",
|
||||
"circuit-1",
|
||||
"circuit-2",
|
||||
]),
|
||||
[
|
||||
{
|
||||
circuitId: "circuit-3",
|
||||
expectedSortOrder: 30,
|
||||
targetSortOrder: 10,
|
||||
},
|
||||
{
|
||||
circuitId: "circuit-1",
|
||||
expectedSortOrder: 10,
|
||||
targetSortOrder: 20,
|
||||
},
|
||||
{
|
||||
circuitId: "circuit-2",
|
||||
expectedSortOrder: 20,
|
||||
targetSortOrder: 30,
|
||||
},
|
||||
]
|
||||
);
|
||||
});
|
||||
|
||||
it("omits unchanged orders and rejects incomplete orders", () => {
|
||||
const circuits = [
|
||||
circuit("circuit-1", 10),
|
||||
circuit("circuit-2", 20),
|
||||
];
|
||||
assert.deepEqual(
|
||||
buildCircuitSectionReorderAssignments(circuits, [
|
||||
"circuit-1",
|
||||
"circuit-2",
|
||||
]),
|
||||
[]
|
||||
);
|
||||
assert.throws(
|
||||
() =>
|
||||
buildCircuitSectionReorderAssignments(circuits, [
|
||||
"circuit-1",
|
||||
]),
|
||||
/jeden Stromkreis/
|
||||
);
|
||||
});
|
||||
|
||||
it("validates atomic multi-section reorder envelopes", () => {
|
||||
const assignments = [
|
||||
{
|
||||
circuitId: "circuit-1",
|
||||
expectedSortOrder: 10,
|
||||
targetSortOrder: 20,
|
||||
},
|
||||
{
|
||||
circuitId: "circuit-2",
|
||||
expectedSortOrder: 20,
|
||||
targetSortOrder: 10,
|
||||
},
|
||||
];
|
||||
const command = createCircuitSectionsReorderProjectCommand([
|
||||
{ sectionId: "section-1", assignments },
|
||||
{
|
||||
sectionId: "section-2",
|
||||
assignments: assignments.map((assignment) => ({
|
||||
...assignment,
|
||||
circuitId: `${assignment.circuitId}-other`,
|
||||
})),
|
||||
},
|
||||
]);
|
||||
assert.equal(command.type, "circuit.reorder-sections");
|
||||
assert.throws(
|
||||
() =>
|
||||
createCircuitSectionsReorderProjectCommand([
|
||||
{ sectionId: "section-1", assignments },
|
||||
{ sectionId: "section-1", assignments },
|
||||
]),
|
||||
/duplicate section ids/
|
||||
);
|
||||
});
|
||||
|
||||
it("sends single and multi-section commands through project history", async () => {
|
||||
const requests: Array<Record<string, unknown>> = [];
|
||||
const originalFetch = globalThis.fetch;
|
||||
globalThis.fetch = async (_input, init) => {
|
||||
requests.push(
|
||||
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: requests.length,
|
||||
createdAtIso: "2026-07-25T00:00:00.000Z",
|
||||
},
|
||||
history: {
|
||||
projectId: "project-1",
|
||||
currentRevision: requests.length,
|
||||
undoDepth: requests.length,
|
||||
redoDepth: 0,
|
||||
undoChangeSetId: "change-1",
|
||||
redoChangeSetId: null,
|
||||
},
|
||||
}),
|
||||
{ status: 200, headers: { "Content-Type": "application/json" } }
|
||||
);
|
||||
};
|
||||
|
||||
const assignments = [
|
||||
{
|
||||
circuitId: "circuit-1",
|
||||
expectedSortOrder: 10,
|
||||
targetSortOrder: 20,
|
||||
},
|
||||
{
|
||||
circuitId: "circuit-2",
|
||||
expectedSortOrder: 20,
|
||||
targetSortOrder: 10,
|
||||
},
|
||||
];
|
||||
try {
|
||||
await reorderCircuitSectionCommand(
|
||||
"project-1",
|
||||
0,
|
||||
"section-1",
|
||||
assignments,
|
||||
"single"
|
||||
);
|
||||
await reorderCircuitSectionsCommand(
|
||||
"project-1",
|
||||
1,
|
||||
[{ sectionId: "section-1", assignments }],
|
||||
"multiple"
|
||||
);
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
|
||||
assert.deepEqual(
|
||||
requests.map((request) => {
|
||||
const command = request.command as { type: string };
|
||||
return [request.expectedRevision, command.type];
|
||||
}),
|
||||
[
|
||||
[0, "circuit.reorder-section"],
|
||||
[1, "circuit.reorder-sections"],
|
||||
]
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -16,10 +16,12 @@ 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 { createCircuitSectionReorderProjectCommand } from "../src/domain/models/circuit-section-reorder-project-command.model.js";
|
||||
import { createCircuitSectionsReorderProjectCommand } from "../src/domain/models/circuit-sections-reorder-project-command.model.js";
|
||||
|
||||
interface TestFixture {
|
||||
context: DatabaseContext;
|
||||
sectionId: string;
|
||||
secondSectionId: string;
|
||||
foreignSectionId: string;
|
||||
}
|
||||
|
||||
@@ -44,17 +46,20 @@ function createTestDatabase(): TestFixture {
|
||||
"project-2",
|
||||
"UV-02"
|
||||
);
|
||||
const section = context.db
|
||||
const projectSections = context.db
|
||||
.select()
|
||||
.from(circuitSections)
|
||||
.where(eq(circuitSections.circuitListId, board.id))
|
||||
.get();
|
||||
.all();
|
||||
const section = projectSections[0];
|
||||
const secondSection = projectSections[1];
|
||||
const foreignSection = context.db
|
||||
.select()
|
||||
.from(circuitSections)
|
||||
.where(eq(circuitSections.circuitListId, foreignBoard.id))
|
||||
.get();
|
||||
assert.ok(section);
|
||||
assert.ok(secondSection);
|
||||
assert.ok(foreignSection);
|
||||
|
||||
context.db
|
||||
@@ -107,6 +112,7 @@ function createTestDatabase(): TestFixture {
|
||||
return {
|
||||
context,
|
||||
sectionId: section.id,
|
||||
secondSectionId: secondSection.id,
|
||||
foreignSectionId: foreignSection.id,
|
||||
};
|
||||
}
|
||||
@@ -226,6 +232,197 @@ describe("circuit section reorder project-command repository", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("reorders multiple sections atomically with one undo step", () => {
|
||||
const fixture = createTestDatabase();
|
||||
try {
|
||||
const firstCircuit = fixture.context.db
|
||||
.select()
|
||||
.from(circuits)
|
||||
.where(eq(circuits.id, "circuit-1"))
|
||||
.get();
|
||||
assert.ok(firstCircuit);
|
||||
fixture.context.db
|
||||
.insert(circuits)
|
||||
.values([
|
||||
{
|
||||
id: "circuit-4",
|
||||
circuitListId: firstCircuit.circuitListId,
|
||||
sectionId: fixture.secondSectionId,
|
||||
equipmentIdentifier: "-2F1",
|
||||
sortOrder: 10,
|
||||
},
|
||||
{
|
||||
id: "circuit-5",
|
||||
circuitListId: firstCircuit.circuitListId,
|
||||
sectionId: fixture.secondSectionId,
|
||||
equipmentIdentifier: "-2F2",
|
||||
sortOrder: 20,
|
||||
},
|
||||
])
|
||||
.run();
|
||||
|
||||
const store =
|
||||
new CircuitSectionReorderProjectCommandRepository(
|
||||
fixture.context.db
|
||||
);
|
||||
const command = createCircuitSectionsReorderProjectCommand([
|
||||
{
|
||||
sectionId: fixture.sectionId,
|
||||
assignments: createReorderCommand(fixture.sectionId)
|
||||
.payload.assignments,
|
||||
},
|
||||
{
|
||||
sectionId: fixture.secondSectionId,
|
||||
assignments: [
|
||||
{
|
||||
circuitId: "circuit-4",
|
||||
expectedSortOrder: 10,
|
||||
targetSortOrder: 20,
|
||||
},
|
||||
{
|
||||
circuitId: "circuit-5",
|
||||
expectedSortOrder: 20,
|
||||
targetSortOrder: 10,
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
||||
const reordered = store.execute({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 0,
|
||||
source: "user",
|
||||
command,
|
||||
});
|
||||
|
||||
assert.deepEqual(
|
||||
fixture.context.db
|
||||
.select({ id: circuits.id, sortOrder: circuits.sortOrder })
|
||||
.from(circuits)
|
||||
.all()
|
||||
.filter((circuit) => circuit.id !== "circuit-foreign")
|
||||
.sort((left, right) => left.id.localeCompare(right.id)),
|
||||
[
|
||||
{ id: "circuit-1", sortOrder: 20 },
|
||||
{ id: "circuit-2", sortOrder: 30 },
|
||||
{ id: "circuit-3", sortOrder: 10 },
|
||||
{ id: "circuit-4", sortOrder: 20 },
|
||||
{ id: "circuit-5", sortOrder: 10 },
|
||||
]
|
||||
);
|
||||
|
||||
store.execute({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 1,
|
||||
source: "undo",
|
||||
historyTargetChangeSetId:
|
||||
reordered.revision.changeSetId,
|
||||
command: reordered.inverse,
|
||||
});
|
||||
assert.deepEqual(
|
||||
fixture.context.db
|
||||
.select({ id: circuits.id, sortOrder: circuits.sortOrder })
|
||||
.from(circuits)
|
||||
.all()
|
||||
.filter((circuit) => circuit.id !== "circuit-foreign")
|
||||
.sort((left, right) => left.id.localeCompare(right.id)),
|
||||
[
|
||||
{ id: "circuit-1", sortOrder: 10 },
|
||||
{ id: "circuit-2", sortOrder: 20 },
|
||||
{ id: "circuit-3", sortOrder: 30 },
|
||||
{ id: "circuit-4", sortOrder: 10 },
|
||||
{ id: "circuit-5", sortOrder: 20 },
|
||||
]
|
||||
);
|
||||
assert.equal(
|
||||
new ProjectHistoryRepository(fixture.context.db).getState(
|
||||
"project-1"
|
||||
)?.redoDepth,
|
||||
1
|
||||
);
|
||||
} finally {
|
||||
fixture.context.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("rolls back every section when a later section is stale", () => {
|
||||
const fixture = createTestDatabase();
|
||||
try {
|
||||
const firstCircuit = fixture.context.db
|
||||
.select()
|
||||
.from(circuits)
|
||||
.where(eq(circuits.id, "circuit-1"))
|
||||
.get();
|
||||
assert.ok(firstCircuit);
|
||||
fixture.context.db
|
||||
.insert(circuits)
|
||||
.values([
|
||||
{
|
||||
id: "circuit-4",
|
||||
circuitListId: firstCircuit.circuitListId,
|
||||
sectionId: fixture.secondSectionId,
|
||||
equipmentIdentifier: "-2F1",
|
||||
sortOrder: 10,
|
||||
},
|
||||
{
|
||||
id: "circuit-5",
|
||||
circuitListId: firstCircuit.circuitListId,
|
||||
sectionId: fixture.secondSectionId,
|
||||
equipmentIdentifier: "-2F2",
|
||||
sortOrder: 20,
|
||||
},
|
||||
])
|
||||
.run();
|
||||
const command = createCircuitSectionsReorderProjectCommand([
|
||||
{
|
||||
sectionId: fixture.sectionId,
|
||||
assignments: createReorderCommand(fixture.sectionId)
|
||||
.payload.assignments,
|
||||
},
|
||||
{
|
||||
sectionId: fixture.secondSectionId,
|
||||
assignments: [
|
||||
{
|
||||
circuitId: "circuit-4",
|
||||
expectedSortOrder: 999,
|
||||
targetSortOrder: 20,
|
||||
},
|
||||
{
|
||||
circuitId: "circuit-5",
|
||||
expectedSortOrder: 20,
|
||||
targetSortOrder: 10,
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
||||
|
||||
assert.throws(
|
||||
() =>
|
||||
new CircuitSectionReorderProjectCommandRepository(
|
||||
fixture.context.db
|
||||
).execute({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 0,
|
||||
source: "user",
|
||||
command,
|
||||
}),
|
||||
/changed before/
|
||||
);
|
||||
assert.deepEqual(
|
||||
getCircuitState(fixture.context).map(
|
||||
(circuit) => circuit.sortOrder
|
||||
),
|
||||
[10, 20, 30, 10, 20]
|
||||
);
|
||||
assert.equal(
|
||||
fixture.context.db.select().from(projectRevisions).all()
|
||||
.length,
|
||||
0
|
||||
);
|
||||
} finally {
|
||||
fixture.context.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects incomplete, stale and foreign-section reorders", () => {
|
||||
const fixture = createTestDatabase();
|
||||
try {
|
||||
|
||||
@@ -150,65 +150,4 @@ describe("circuit-section transaction repository", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("commits a complete section reorder without changing identifiers", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
const persisted = listCircuits(context);
|
||||
const repository = new CircuitSectionTransactionRepository(context.db);
|
||||
|
||||
repository.updateSortOrders(persisted[0].sectionId, [
|
||||
"circuit-3",
|
||||
"circuit-1",
|
||||
"circuit-2",
|
||||
]);
|
||||
|
||||
assert.deepEqual(
|
||||
listCircuits(context).map((circuit) => [
|
||||
circuit.id,
|
||||
circuit.sortOrder,
|
||||
circuit.equipmentIdentifier,
|
||||
]),
|
||||
[
|
||||
["circuit-1", 20, "-1F1"],
|
||||
["circuit-2", 30, "-1F2"],
|
||||
["circuit-3", 10, "-1F3"],
|
||||
]
|
||||
);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("rolls back earlier sort orders when a later update fails", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
const persisted = listCircuits(context);
|
||||
context.sqlite.exec(`
|
||||
CREATE TRIGGER fail_last_sort_order
|
||||
BEFORE UPDATE OF sort_order ON circuits
|
||||
WHEN OLD.id = 'circuit-2' AND NEW.sort_order = 30
|
||||
BEGIN
|
||||
SELECT RAISE(ABORT, 'forced sort failure');
|
||||
END;
|
||||
`);
|
||||
const repository = new CircuitSectionTransactionRepository(context.db);
|
||||
|
||||
assert.throws(
|
||||
() =>
|
||||
repository.updateSortOrders(persisted[0].sectionId, [
|
||||
"circuit-3",
|
||||
"circuit-1",
|
||||
"circuit-2",
|
||||
]),
|
||||
/forced sort failure/
|
||||
);
|
||||
|
||||
assert.deepEqual(
|
||||
listCircuits(context).map((circuit) => circuit.sortOrder),
|
||||
[10, 20, 30]
|
||||
);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -109,39 +109,6 @@ describe("circuit write service rules", () => {
|
||||
assert.equal(safeCalled, 1);
|
||||
});
|
||||
|
||||
it("reorders circuits inside one section without renumbering identifiers", async () => {
|
||||
let safeReorder: { sectionId: string; circuitIds: string[] } | undefined;
|
||||
const service = new CircuitWriteService({
|
||||
circuitSectionRepository: {
|
||||
async findById() {
|
||||
return { id: "s1", circuitListId: "l1" } as never;
|
||||
},
|
||||
} as never,
|
||||
circuitRepository: {
|
||||
async listBySection() {
|
||||
return [
|
||||
{ id: "c1", sectionId: "s1", equipmentIdentifier: "-2F7", sortOrder: 10, isReserve: 0 },
|
||||
{ id: "c2", sectionId: "s1", equipmentIdentifier: "-2F9", sortOrder: 20, isReserve: 0 },
|
||||
{ id: "c3", sectionId: "s1", equipmentIdentifier: "-2F5", sortOrder: 30, isReserve: 1 },
|
||||
] as never[];
|
||||
},
|
||||
} as never,
|
||||
circuitSectionTransactionStore: {
|
||||
updateSortOrders(sectionId: string, circuitIds: string[]) {
|
||||
safeReorder = { sectionId, circuitIds };
|
||||
},
|
||||
} as never,
|
||||
});
|
||||
|
||||
await service.reorderCircuitsInSection("s1", {
|
||||
orderedCircuitIds: ["c3", "c1", "c2"],
|
||||
});
|
||||
assert.deepEqual(safeReorder, {
|
||||
sectionId: "s1",
|
||||
circuitIds: ["c3", "c1", "c2"],
|
||||
});
|
||||
});
|
||||
|
||||
it("validates the complete section set before safe identifier updates", async () => {
|
||||
let safeCalled = false;
|
||||
const service = new CircuitWriteService({
|
||||
|
||||
@@ -36,6 +36,7 @@ import {
|
||||
import { createCircuitDeviceRowInsertProjectCommand } from "../src/domain/models/circuit-device-row-structure-project-command.model.js";
|
||||
import { createCircuitUpdateProjectCommand } from "../src/domain/models/circuit-project-command.model.js";
|
||||
import { createCircuitSectionReorderProjectCommand } from "../src/domain/models/circuit-section-reorder-project-command.model.js";
|
||||
import { createCircuitSectionsReorderProjectCommand } from "../src/domain/models/circuit-sections-reorder-project-command.model.js";
|
||||
import { createCircuitSectionRenumberProjectCommand } from "../src/domain/models/circuit-section-renumber-project-command.model.js";
|
||||
import { createCircuitInsertProjectCommand } from "../src/domain/models/circuit-structure-project-command.model.js";
|
||||
import { createProjectDeviceRowSyncProjectCommand } from "../src/domain/models/project-device-row-sync-project-command.model.js";
|
||||
@@ -764,6 +765,34 @@ describe("project command service", () => {
|
||||
{ id: "circuit-2", sortOrder: 20 },
|
||||
]
|
||||
);
|
||||
|
||||
const multiSection = createService(context).executeUser({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 2,
|
||||
command: createCircuitSectionsReorderProjectCommand([
|
||||
{
|
||||
sectionId: firstCircuit.sectionId,
|
||||
assignments:
|
||||
createCircuitSectionReorderProjectCommand(
|
||||
firstCircuit.sectionId,
|
||||
[
|
||||
{
|
||||
circuitId: "circuit-1",
|
||||
expectedSortOrder: 10,
|
||||
targetSortOrder: 20,
|
||||
},
|
||||
{
|
||||
circuitId: "circuit-2",
|
||||
expectedSortOrder: 20,
|
||||
targetSortOrder: 10,
|
||||
},
|
||||
]
|
||||
).payload.assignments,
|
||||
},
|
||||
]),
|
||||
});
|
||||
assert.equal(multiSection.history.currentRevision, 3);
|
||||
assert.equal(multiSection.history.redoDepth, 0);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user