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); }); } });