import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import { describe, it } from "node:test"; import type { CircuitTreeResponseDto, ExternalProjectObjectDto, ExternalProjectObjectsDto, } from "../src/frontend/types.js"; import { assignObjects, buildAssignablePlanObjects, buildPlanCircuits, buildPlanSections, buildRoomTree, countObjectsWithoutBoard, flattenRoomTree, previewNewCircuitIdentifiers, revalidatePlan, selectRange, unassignObjects, } from "../src/frontend/utils/revit-assignment-model.js"; import { applyExternalObjectAssignmentPlan } from "../src/frontend/utils/api.js"; function object(id: string, overrides: Partial = {}): ExternalProjectObjectDto { return { id, worksharingId: `ws-${id}`, ifcGuid: null, deviceTypeId: "type-socket", familyAndType: "Steckdose: Doppel", selectionMarker: "SD", sourceCircuitIdentifier: "", sourceRoomNumber: "1.01", sourceRoomName: "Büro", roomId: "room-1", distributionBoardId: "board-1", linkedProjectDeviceId: null, circuitDeviceRowId: null, presenceStatus: "present", planningValues: { displayName: "Steckdose", category: "single_phase", connectionKind: null, effectiveQuantity: 2, powerPerUnitW: 200, simultaneityFactor: 1, cosPhi: null, costGroup: null, remark: null, }, ...overrides, }; } function objectsDto(objects: ExternalProjectObjectDto[]): ExternalProjectObjectsDto { return { currentRevision: 3, sourceName: "Revit", maxSocketsPerCircuit: 8, socketDeviceTypeIds: ["type-socket"], distributionBoards: [], projectDevices: [], objects, }; } function tree(): CircuitTreeResponseDto { return { circuitListId: "list-1", distributionBoardId: "board-1", distributionBoardName: "UV 1", currentRevision: 3, singlePhaseVoltageV: 230, threePhaseVoltageV: 400, distributionBoardSimultaneityFactor: 1, distributionBoardTotalPower: 0, distributionBoardTotalPowerWithSimultaneityFactor: 0, headerComponents: [], footerComponents: [], sections: [{ id: "section-2", key: "single", displayName: "1-phasig", prefix: "-2F1.", sortOrder: 20, category: "single_phase", groupNumber: 1, sectionTotalPower: 0, components: [], circuits: [{ id: "circuit-1", circuitListId: "list-1", sectionId: "section-2", equipmentIdentifier: "-2F1.7", displayName: "Steckdose", sortOrder: 10, isReserve: false, circuitTotalPower: 0, circuitApparentPower: 0, circuitCosPhi: 1, downstreamBoardLoads: [], deviceRows: [{ id: "row-1", sortOrder: 10, name: "Steckdose", displayName: "Steckdose", category: "single_phase", roomId: "room-1", quantity: 2, manualQuantity: 0, powerPerUnit: 0.2, simultaneityFactor: 1, rowTotalPower: 0.4, externalObjectCount: 1, }], }], }], }; } describe("Revit assignment dialog model", () => { it("offers only present, unassigned objects of the board and counts objects without board", () => { const dto = objectsDto([ object("free"), object("linked", { circuitDeviceRowId: "row-1" }), object("missing", { presenceStatus: "missing" }), object("other-board", { distributionBoardId: "board-2" }), object("no-board", { distributionBoardId: null }), object("light", { deviceTypeId: "type-light" }), ]); const planObjects = buildAssignablePlanObjects(dto, "board-1"); assert.deepEqual(planObjects.map((entry) => [entry.id, entry.isSocket]), [["free", true], ["light", false]]); assert.equal(countObjectsWithoutBoard(dto), 1); const circuits = buildPlanCircuits(tree(), dto); assert.equal(circuits[0]!.linkedSocketQuantity, 2); assert.deepEqual(circuits[0]!.rows[0]!.linkedSelectionMarkers, ["SD"]); assert.deepEqual(buildPlanSections(tree()), [{ id: "section-2", category: "single_phase" }]); }); it("builds a room tree without planned objects and selects ranges in visual order", () => { const dto = objectsDto([ object("b", { roomId: "room-2", sourceRoomNumber: "1.10" }), object("a2"), object("a1"), object("x", { roomId: null, sourceRoomNumber: "", sourceRoomName: "" }), ]); const planObjects = buildAssignablePlanObjects(dto, "board-1"); const rooms = buildRoomTree(planObjects, new Set(["a2"]), { search: "", deviceTypeId: "", socketsOnly: false }); assert.deepEqual(rooms.map((room) => room.label), ["1.01 Büro", "1.10 Büro", "Ohne Raum"]); const ordered = flattenRoomTree(rooms); assert.deepEqual(ordered, ["a1", "b", "x"]); assert.deepEqual(selectRange(ordered, "a1", "x"), ["a1", "b", "x"]); assert.deepEqual(selectRange(ordered, null, "b"), ["b"]); assert.deepEqual( buildRoomTree(planObjects, new Set(), { search: "1.10", deviceTypeId: "", socketsOnly: false }) .flatMap((room) => room.objectIds), ["b"] ); }); it("keeps valid assignments after a reload and previews BMKs without filling gaps", () => { const dto = objectsDto([object("a1"), object("a2")]); const planObjects = buildAssignablePlanObjects(dto, "board-1"); let plan = assignObjects( { newCircuits: [{ key: "n1", sectionId: "section-2" }, { key: "n2", sectionId: "section-2" }], assignments: [], newRowChoices: [] }, ["a1"], { kind: "new-circuit", key: "n1" } ); plan = assignObjects(plan, ["a2"], { kind: "new-circuit", key: "n2" }); assert.deepEqual([...previewNewCircuitIdentifiers(tree(), plan).values()], ["-2F1.8", "-2F1.9"]); const reloaded = revalidatePlan(plan, planObjects.filter((entry) => entry.id !== "a2"), buildPlanCircuits(tree(), dto), buildPlanSections(tree())); assert.equal(reloaded.removedCount, 1); assert.deepEqual(reloaded.plan.newCircuits.map((entry) => entry.key), ["n1"]); assert.deepEqual(unassignObjects(plan, ["a1", "a2"]).newCircuits, []); }); it("posts the plan to the assignment-plan route", 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({ history: { currentRevision: 4 }, newCircuitIds: [] }), { status: 200, headers: { "Content-Type": "application/json" } }); }; try { const body = { expectedRevision: 3, circuitListId: "list-1", newCircuits: [], rows: [{ kind: "existing-row" as const, rowId: "row-1", objectIds: ["a1"] }], confirmedConflictObjectIds: [], confirmedOverLimitCircuitKeys: [], }; await applyExternalObjectAssignmentPlan("project-1", body); assert.deepEqual(requests, [{ url: "/api/projects/project-1/external-model/objects/assignment-plan", method: "POST", body }]); } finally { globalThis.fetch = originalFetch; } }); it("replaces the drawer with the assignment dialog in the editor", () => { const editor = readFileSync("src/frontend/components/circuit-tree-editor.tsx", "utf8"); const dialog = readFileSync("src/frontend/components/revit-assignment-dialog.tsx", "utf8"); assert.doesNotMatch(editor, /revit-object-drawer|x-revit-object-ids|assignExternalProjectObjectsTo/); assert.match(editor, /Revit-Objekte zuordnen/); assert.match(editor, /RevitLinkedObjectsDialog/); assert.match(dialog, /Automatisch aufteilen/); assert.match(dialog, /Überschreitung ausdrücklich bestätigen/); assert.match(dialog, /Abweichungen bestätigen/); }); });