Persist circuit reorders
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user