Add circuit group reordering
This commit is contained in:
@@ -17,6 +17,7 @@ import { projects } from "../src/db/schema/projects.js";
|
||||
import {
|
||||
createCircuitGroupDeleteProjectCommand,
|
||||
createCircuitGroupInsertProjectCommand,
|
||||
createCircuitGroupReorderProjectCommand,
|
||||
createCircuitGroupUpdateProjectCommand,
|
||||
type CircuitGroupSnapshot,
|
||||
} from "../src/domain/models/circuit-group-structure-project-command.model.js";
|
||||
@@ -293,4 +294,137 @@ describe("circuit-group structure project command", () => {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("reorders every group atomically without changing group identity", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
const circuitListId = getProjectList(context, "project-1").id;
|
||||
const groups = context.db
|
||||
.select()
|
||||
.from(circuitSections)
|
||||
.where(eq(circuitSections.circuitListId, circuitListId))
|
||||
.all();
|
||||
const expectedPrefixes = new Map(
|
||||
groups.map((group) => [group.id, group.prefix])
|
||||
);
|
||||
const assignments = groups.map((group, index) => ({
|
||||
groupId: group.id,
|
||||
expectedSortOrder: group.sortOrder,
|
||||
targetSortOrder: (groups.length - index) * 10,
|
||||
}));
|
||||
const repository =
|
||||
new CircuitGroupStructureProjectCommandRepository(context.db);
|
||||
const reordered = repository.execute({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 0,
|
||||
source: "user",
|
||||
command: createCircuitGroupReorderProjectCommand(
|
||||
circuitListId,
|
||||
assignments
|
||||
),
|
||||
});
|
||||
for (const assignment of assignments) {
|
||||
const group = context.db
|
||||
.select()
|
||||
.from(circuitSections)
|
||||
.where(eq(circuitSections.id, assignment.groupId))
|
||||
.get();
|
||||
assert.equal(group?.sortOrder, assignment.targetSortOrder);
|
||||
assert.equal(
|
||||
group?.prefix,
|
||||
expectedPrefixes.get(assignment.groupId)
|
||||
);
|
||||
}
|
||||
|
||||
repository.execute({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 1,
|
||||
source: "undo",
|
||||
historyTargetChangeSetId: new ProjectHistoryRepository(
|
||||
context.db
|
||||
).getNextCommand("project-1", "undo")?.changeSetId,
|
||||
command: reordered.inverse,
|
||||
});
|
||||
for (const assignment of assignments) {
|
||||
assert.equal(
|
||||
context.db
|
||||
.select()
|
||||
.from(circuitSections)
|
||||
.where(eq(circuitSections.id, assignment.groupId))
|
||||
.get()?.sortOrder,
|
||||
assignment.expectedSortOrder
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects incomplete reorders and rolls back late failures", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
const circuitListId = getProjectList(context, "project-1").id;
|
||||
const groups = context.db
|
||||
.select()
|
||||
.from(circuitSections)
|
||||
.where(eq(circuitSections.circuitListId, circuitListId))
|
||||
.all();
|
||||
const assignments = groups.map((group, index) => ({
|
||||
groupId: group.id,
|
||||
expectedSortOrder: group.sortOrder,
|
||||
targetSortOrder: (groups.length - index) * 10,
|
||||
}));
|
||||
const repository =
|
||||
new CircuitGroupStructureProjectCommandRepository(context.db);
|
||||
assert.throws(
|
||||
() =>
|
||||
repository.execute({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 0,
|
||||
source: "user",
|
||||
command: createCircuitGroupReorderProjectCommand(
|
||||
circuitListId,
|
||||
assignments.slice(1)
|
||||
),
|
||||
}),
|
||||
/include every unchanged group/
|
||||
);
|
||||
context.sqlite.exec(`
|
||||
CREATE TRIGGER fail_group_reorder_history
|
||||
BEFORE INSERT ON project_history_stack_entries
|
||||
BEGIN
|
||||
SELECT RAISE(ABORT, 'forced group reorder history failure');
|
||||
END;
|
||||
`);
|
||||
assert.throws(
|
||||
() =>
|
||||
repository.execute({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 0,
|
||||
source: "user",
|
||||
command: createCircuitGroupReorderProjectCommand(
|
||||
circuitListId,
|
||||
assignments
|
||||
),
|
||||
}),
|
||||
/forced group reorder history failure/
|
||||
);
|
||||
for (const assignment of assignments) {
|
||||
assert.equal(
|
||||
context.db
|
||||
.select()
|
||||
.from(circuitSections)
|
||||
.where(eq(circuitSections.id, assignment.groupId))
|
||||
.get()?.sortOrder,
|
||||
assignment.expectedSortOrder
|
||||
);
|
||||
}
|
||||
assert.equal(
|
||||
context.db.select().from(projectRevisions).all().length,
|
||||
0
|
||||
);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -50,6 +50,7 @@ import { createCircuitSectionRenumberProjectCommand } from "../src/domain/models
|
||||
import { createCircuitInsertProjectCommand } from "../src/domain/models/circuit-structure-project-command.model.js";
|
||||
import {
|
||||
createCircuitGroupInsertProjectCommand,
|
||||
createCircuitGroupReorderProjectCommand,
|
||||
createCircuitGroupUpdateProjectCommand,
|
||||
} from "../src/domain/models/circuit-group-structure-project-command.model.js";
|
||||
import {
|
||||
@@ -1270,6 +1271,26 @@ describe("project command service", () => {
|
||||
.get()?.displayName,
|
||||
expected.displayName
|
||||
);
|
||||
|
||||
const groups = context.db
|
||||
.select()
|
||||
.from(circuitSections)
|
||||
.where(eq(circuitSections.circuitListId, circuitListId))
|
||||
.all();
|
||||
const reorderAssignments = groups.map((group, index) => ({
|
||||
groupId: group.id,
|
||||
expectedSortOrder: group.sortOrder,
|
||||
targetSortOrder: (groups.length - index) * 10,
|
||||
}));
|
||||
const reordered = createService(context).executeUser({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 3,
|
||||
command: createCircuitGroupReorderProjectCommand(
|
||||
circuitListId,
|
||||
reorderAssignments
|
||||
),
|
||||
});
|
||||
assert.equal(reordered.history.currentRevision, 4);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
|
||||
@@ -157,6 +157,12 @@ describe("project version history presentation", () => {
|
||||
),
|
||||
"Stromkreisgruppe bearbeitet"
|
||||
);
|
||||
assert.equal(
|
||||
getProjectRevisionDescription(
|
||||
revision(11, { commandType: "circuit-group.reorder" })
|
||||
),
|
||||
"Stromkreisgruppen sortiert"
|
||||
);
|
||||
assert.equal(getProjectSnapshotKindLabel("named"), "Benannt");
|
||||
assert.equal(
|
||||
getProjectSnapshotKindLabel("automatic"),
|
||||
|
||||
Reference in New Issue
Block a user