From 58cec53713249b659d92d34e52082bd46f7de1de Mon Sep 17 00:00:00 2001 From: Julian Appel Date: Thu, 29 Oct 2026 13:23:51 +0100 Subject: [PATCH] Keep manual quantity in editor group-delete snapshots The editor built the circuit-group subtree snapshot without manualQuantity, while the server compares it with an exact capture that includes the column. Deleting any group that contained a device row was therefore rejected with "Circuit-group subtree changed before deletion." The new integration test builds the snapshot from the real circuit-tree read model, as the editor does, and deletes the group for a manual row and for a row with a Revit quantity share. Co-Authored-By: Claude Opus 5.5 --- src/frontend/utils/circuit-group-editing.ts | 1 + ...rcuit-group-delete-editor-snapshot.test.ts | 88 +++++++++++++++++++ tests/circuit-group-numbering.test.ts | 6 +- 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 tests/circuit-group-delete-editor-snapshot.test.ts diff --git a/src/frontend/utils/circuit-group-editing.ts b/src/frontend/utils/circuit-group-editing.ts index 9e5f799..87077ca 100644 --- a/src/frontend/utils/circuit-group-editing.ts +++ b/src/frontend/utils/circuit-group-editing.ts @@ -150,6 +150,7 @@ export function buildCircuitGroupSubtreeSnapshot( roomNumberSnapshot: row.roomNumberSnapshot ?? null, roomNameSnapshot: row.roomNameSnapshot ?? null, quantity: row.quantity, + manualQuantity: row.manualQuantity, powerPerUnit: row.powerPerUnit, simultaneityFactor: row.simultaneityFactor, cosPhi: row.cosPhi ?? null, diff --git a/tests/circuit-group-delete-editor-snapshot.test.ts b/tests/circuit-group-delete-editor-snapshot.test.ts new file mode 100644 index 0000000..671b660 --- /dev/null +++ b/tests/circuit-group-delete-editor-snapshot.test.ts @@ -0,0 +1,88 @@ +import assert from "node:assert/strict"; +import { before, describe, it } from "node:test"; +import { eq } from "drizzle-orm"; +import { CircuitGroupSubtreeProjectCommandRepository } from "../src/db/repositories/circuit-group-subtree-project-command.repository.js"; +import { circuitDeviceRows } from "../src/db/schema/circuit-device-rows.js"; +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 { projects } from "../src/db/schema/projects.js"; +import { createCircuitGroupDeleteSubtreeProjectCommand } from "../src/domain/models/circuit-group-subtree-project-command.model.js"; +import { buildCircuitGroupSubtreeSnapshot } from "../src/frontend/utils/circuit-group-editing.js"; +import { DistributionBoardFixtureRepository } from "./support/distribution-board-fixture.js"; +import { migrateDatabase } from "./support/migrate-database.js"; + +// The circuit-tree read model uses the application database singleton, so this +// file runs in its own test process against an in-memory database. +process.env.LB_DATABASE_PATH = ":memory:"; + +type ClientModule = typeof import("../src/db/client.js"); +type CircuitTreeControllerModule = typeof import("../src/server/controllers/circuit-tree.controller.js"); + +let db: ClientModule["db"]; +let buildProjectCircuitTrees: CircuitTreeControllerModule["buildProjectCircuitTrees"]; + +before(async () => { + ({ db } = await import("../src/db/client.js")); + ({ buildProjectCircuitTrees } = await import("../src/server/controllers/circuit-tree.controller.js")); + migrateDatabase(db); +}); + +describe("circuit-group deletion from the editor", () => { + for (const [label, quantity, manualQuantity] of [ + ["a manual device row", 2, 2], + ["a device row with a Revit quantity share", 3, 1], + ] as const) { + it(`accepts the editor snapshot of a group containing ${label}`, async () => { + const projectId = `project-${quantity}`; + db.insert(projects).values({ id: projectId, name: "Projekt" }).run(); + new DistributionBoardFixtureRepository(db).createWithCircuitListAndDefaultSections(projectId, `UV-${quantity}`); + const list = db.select().from(circuitLists).where(eq(circuitLists.projectId, projectId)).get()!; + const group = db + .select() + .from(circuitSections) + .where(eq(circuitSections.circuitListId, list.id)) + .all() + .find((section) => section.category === "lighting")!; + const circuitId = `circuit-${quantity}`; + db.insert(circuits).values({ + id: circuitId, + circuitListId: list.id, + sectionId: group.id, + equipmentIdentifier: "-1F1.1", + sortOrder: 10, + voltage: 230, + isReserve: 0, + }).run(); + db.insert(circuitDeviceRows).values({ + id: `row-${quantity}`, + circuitId, + sortOrder: 10, + name: "Leuchte", + displayName: "Leuchte", + phaseType: "single_phase", + quantity, + manualQuantity, + powerPerUnit: 0.03, + simultaneityFactor: 1, + cosPhi: 0.95, + }).run(); + + const trees = await buildProjectCircuitTrees(projectId); + const section = trees!.trees + .find((tree) => tree.circuitListId === list.id)! + .sections.find((entry) => entry.id === group.id)!; + const snapshot = buildCircuitGroupSubtreeSnapshot(section, list.id); + + new CircuitGroupSubtreeProjectCommandRepository(db).execute({ + projectId, + expectedRevision: 0, + source: "user", + command: createCircuitGroupDeleteSubtreeProjectCommand(snapshot), + }); + + assert.equal(db.select().from(circuits).where(eq(circuits.id, circuitId)).all().length, 0); + assert.equal(db.select().from(circuitSections).where(eq(circuitSections.id, group.id)).all().length, 0); + }); + } +}); diff --git a/tests/circuit-group-numbering.test.ts b/tests/circuit-group-numbering.test.ts index fa41c0c..d34ee12 100644 --- a/tests/circuit-group-numbering.test.ts +++ b/tests/circuit-group-numbering.test.ts @@ -354,7 +354,7 @@ describe("circuit group numbering", () => { displayName: "Leuchte Flur", phaseType: "single_phase", quantity: 2, - manualQuantity: 2, + manualQuantity: 1, powerPerUnit: 0.05, simultaneityFactor: 1, cosPhi: 0.95, @@ -380,6 +380,10 @@ describe("circuit group numbering", () => { .linkedProjectDeviceId, "device-1" ); + assert.equal( + snapshot.circuits[0].circuit.deviceRows[0].manualQuantity, + 1 + ); assert.equal( snapshot.circuits[0].circuit.deviceRows[0] .overriddenFields,