Add configurable distribution supply types
This commit is contained in:
@@ -13,14 +13,20 @@ import { circuitLists } from "../src/db/schema/circuit-lists.js";
|
||||
import { circuitSections } from "../src/db/schema/circuit-sections.js";
|
||||
import { circuits } from "../src/db/schema/circuits.js";
|
||||
import { distributionBoards } from "../src/db/schema/distribution-boards.js";
|
||||
import { floors } from "../src/db/schema/floors.js";
|
||||
import { projectRevisions } from "../src/db/schema/project-revisions.js";
|
||||
import { projects } from "../src/db/schema/projects.js";
|
||||
import { ProjectRevisionConflictError } from "../src/domain/errors/project-revision-conflict.error.js";
|
||||
import {
|
||||
createDistributionBoardInsertProjectCommand,
|
||||
createDistributionBoardStructureSnapshot,
|
||||
type DistributionBoardStructureProjectCommand,
|
||||
} from "../src/domain/models/distribution-board-structure-project-command.model.js";
|
||||
import { createDistributionBoard } from "../src/frontend/utils/api.js";
|
||||
import { createDistributionBoardUpdateProjectCommand } from "../src/domain/models/distribution-board-project-command.model.js";
|
||||
import {
|
||||
createDistributionBoard,
|
||||
updateDistributionBoard,
|
||||
} from "../src/frontend/utils/api.js";
|
||||
|
||||
function createTestDatabase(): DatabaseContext {
|
||||
const context = createDatabaseContext(":memory:");
|
||||
@@ -50,9 +56,11 @@ describe("distribution-board structure project command", () => {
|
||||
it("builds the fixed setup and sends the expected frontend revision", async () => {
|
||||
const structure = createDistributionBoardStructureSnapshot(
|
||||
"project-1",
|
||||
" UV-02 "
|
||||
" UV-02 ",
|
||||
{ floorId: null, supplyType: "SV" }
|
||||
);
|
||||
assert.equal(structure.distributionBoard.name, "UV-02");
|
||||
assert.equal(structure.distributionBoard.supplyType, "SV");
|
||||
assert.equal(
|
||||
structure.circuitList.name,
|
||||
"UV-02 Stromkreisliste"
|
||||
@@ -91,14 +99,27 @@ describe("distribution-board structure project command", () => {
|
||||
});
|
||||
};
|
||||
try {
|
||||
await createDistributionBoard("project-1", "UV-02", 7);
|
||||
await createDistributionBoard(
|
||||
"project-1",
|
||||
{
|
||||
name: "UV-02",
|
||||
floorId: null,
|
||||
supplyType: "SV",
|
||||
},
|
||||
7
|
||||
);
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
assert.deepEqual(requests, [
|
||||
{
|
||||
url: "/api/projects/project-1/distribution-boards",
|
||||
body: { name: "UV-02", expectedRevision: 7 },
|
||||
body: {
|
||||
name: "UV-02",
|
||||
floorId: null,
|
||||
supplyType: "SV",
|
||||
expectedRevision: 7,
|
||||
},
|
||||
},
|
||||
]);
|
||||
});
|
||||
@@ -178,6 +199,239 @@ describe("distribution-board structure project command", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("allows only supply types enabled in the project", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
context.db
|
||||
.update(projects)
|
||||
.set({
|
||||
enabledDistributionBoardSupplyTypes: ["MSR", "SiBe"],
|
||||
})
|
||||
.where(eq(projects.id, "project-1"))
|
||||
.run();
|
||||
const repository =
|
||||
new DistributionBoardStructureProjectCommandRepository(
|
||||
context.db
|
||||
);
|
||||
assert.throws(
|
||||
() =>
|
||||
repository.execute({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 0,
|
||||
source: "user",
|
||||
command: createDistributionBoardInsertProjectCommand(
|
||||
createDistributionBoardStructureSnapshot(
|
||||
"project-1",
|
||||
"UV AV",
|
||||
{ floorId: null, supplyType: "AV" }
|
||||
)
|
||||
),
|
||||
}),
|
||||
/nicht aktiviert/
|
||||
);
|
||||
const structure = createDistributionBoardStructureSnapshot(
|
||||
"project-1",
|
||||
"UV SiBe",
|
||||
{ floorId: null, supplyType: "SiBe" }
|
||||
);
|
||||
repository.execute({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 0,
|
||||
source: "user",
|
||||
command: createDistributionBoardInsertProjectCommand(structure),
|
||||
});
|
||||
assert.equal(
|
||||
context.db.select().from(distributionBoards).get()?.supplyType,
|
||||
"SiBe"
|
||||
);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("applies stored version-one setup commands with unassigned new fields", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
const repository =
|
||||
new DistributionBoardStructureProjectCommandRepository(
|
||||
context.db
|
||||
);
|
||||
const current = createDistributionBoardStructureSnapshot(
|
||||
"project-1",
|
||||
"UV Alt",
|
||||
{ floorId: null, supplyType: null }
|
||||
);
|
||||
const {
|
||||
floorId: _floorId,
|
||||
supplyType: _supplyType,
|
||||
...legacyBoard
|
||||
} = current.distributionBoard;
|
||||
const legacyCommand = {
|
||||
schemaVersion: 1,
|
||||
type: "distribution-board.insert",
|
||||
payload: {
|
||||
structure: {
|
||||
...current,
|
||||
distributionBoard: legacyBoard,
|
||||
},
|
||||
},
|
||||
} as unknown as DistributionBoardStructureProjectCommand;
|
||||
repository.execute({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 0,
|
||||
source: "user",
|
||||
command: legacyCommand,
|
||||
});
|
||||
const inserted = context.db
|
||||
.select()
|
||||
.from(distributionBoards)
|
||||
.get();
|
||||
assert.equal(inserted?.floorId, null);
|
||||
assert.equal(inserted?.supplyType, null);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("updates floor and supply type with persisted undo and validates project ownership", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
context.db
|
||||
.insert(floors)
|
||||
.values([
|
||||
{
|
||||
id: "floor-1",
|
||||
projectId: "project-1",
|
||||
name: "EG",
|
||||
sortOrder: 10,
|
||||
},
|
||||
{
|
||||
id: "floor-foreign",
|
||||
projectId: "project-2",
|
||||
name: "Fremd",
|
||||
sortOrder: 10,
|
||||
},
|
||||
])
|
||||
.run();
|
||||
const repository =
|
||||
new DistributionBoardStructureProjectCommandRepository(
|
||||
context.db
|
||||
);
|
||||
const structure = createDistributionBoardStructureSnapshot(
|
||||
"project-1",
|
||||
"UV-02",
|
||||
{ floorId: null, supplyType: "AV" }
|
||||
);
|
||||
repository.execute({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 0,
|
||||
source: "user",
|
||||
command:
|
||||
createDistributionBoardInsertProjectCommand(structure),
|
||||
});
|
||||
const forward = repository.executeUpdate({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 1,
|
||||
source: "user",
|
||||
command: createDistributionBoardUpdateProjectCommand(
|
||||
structure.distributionBoard.id,
|
||||
{ floorId: "floor-1", supplyType: "SV" }
|
||||
),
|
||||
});
|
||||
assert.deepEqual(
|
||||
context.db
|
||||
.select({
|
||||
floorId: distributionBoards.floorId,
|
||||
supplyType: distributionBoards.supplyType,
|
||||
})
|
||||
.from(distributionBoards)
|
||||
.get(),
|
||||
{ floorId: "floor-1", supplyType: "SV" }
|
||||
);
|
||||
|
||||
const undoStep = new ProjectHistoryRepository(
|
||||
context.db
|
||||
).getNextCommand("project-1", "undo");
|
||||
assert.ok(undoStep);
|
||||
repository.executeUpdate({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 2,
|
||||
source: "undo",
|
||||
historyTargetChangeSetId: undoStep.changeSetId,
|
||||
command: forward.inverse,
|
||||
});
|
||||
assert.deepEqual(
|
||||
context.db
|
||||
.select({
|
||||
floorId: distributionBoards.floorId,
|
||||
supplyType: distributionBoards.supplyType,
|
||||
})
|
||||
.from(distributionBoards)
|
||||
.get(),
|
||||
{ floorId: null, supplyType: "AV" }
|
||||
);
|
||||
assert.throws(
|
||||
() =>
|
||||
repository.executeUpdate({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 3,
|
||||
source: "user",
|
||||
command: createDistributionBoardUpdateProjectCommand(
|
||||
structure.distributionBoard.id,
|
||||
{ floorId: "floor-foreign" }
|
||||
),
|
||||
}),
|
||||
/does not belong/
|
||||
);
|
||||
assert.equal(
|
||||
context.db
|
||||
.select()
|
||||
.from(projectRevisions)
|
||||
.all().length,
|
||||
3
|
||||
);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("sends distribution-board updates to the revision-safe API", async () => {
|
||||
const requests: Array<{ url: string; method: string; body: unknown }> = [];
|
||||
const originalFetch = globalThis.fetch;
|
||||
globalThis.fetch = async (input, init) => {
|
||||
requests.push({
|
||||
url: String(input),
|
||||
method: init?.method ?? "GET",
|
||||
body: JSON.parse(String(init?.body)),
|
||||
});
|
||||
return new Response(JSON.stringify({}), {
|
||||
status: 200,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
};
|
||||
try {
|
||||
await updateDistributionBoard(
|
||||
"project-1",
|
||||
"board-1",
|
||||
{ floorId: "floor-1", supplyType: "USV" },
|
||||
9
|
||||
);
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
assert.deepEqual(requests, [
|
||||
{
|
||||
url: "/api/projects/project-1/distribution-boards/board-1",
|
||||
method: "PUT",
|
||||
body: {
|
||||
floorId: "floor-1",
|
||||
supplyType: "USV",
|
||||
expectedRevision: 9,
|
||||
},
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("rejects foreign, stale and changed structures without partial writes", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user