import { describe, expect, it } from "vitest"; import { detachComponentInstance, instantiateComponentShapes } from "@/lib/cloudComponents"; import type { WorkplaneShape } from "@/types/sketchforge"; function makeSampleShape(overrides: Partial = {}): WorkplaneShape { return { id: "orig-1", name: "Ahşap Kolon", kind: "box", color: "#b45309", x: 50, z: 30, elevation: 10, width: 20, depth: 20, height: 100, size: 20, rotation: 0, texture: "wood", componentDefId: "comp-kolon", componentName: "Ahşap Kolon", ...overrides, }; } describe("YakNet 3B Cloud Components — Instantiation & Make Unique", () => { it("instantiates multi-part components as a rigid centered unit with unique IDs", () => { const leg1 = makeSampleShape({ id: "leg-1", x: 40, z: 20, elevation: 0, width: 10, depth: 10, height: 40 }); const leg2 = makeSampleShape({ id: "leg-2", x: 80, z: 20, elevation: 0, width: 10, depth: 10, height: 40 }); const top = makeSampleShape({ id: "top-1", x: 60, z: 20, elevation: 40, width: 60, depth: 30, height: 6 }); let seq = 0; const placed = instantiateComponentShapes([leg1, leg2, top], { componentDefId: "yaknet-masa-01", componentName: "Çalışma Masası", targetOffset: { x: 0, z: 0, elevation: 5 }, createId: (prefix) => `${prefix}-${++seq}`, }); expect(placed).toHaveLength(3); expect(new Set(placed.map((s) => s.id)).size).toBe(3); // Relative distances preserved while center aligns on (0, 0) and base on y=5 expect(placed[0].x).toBe(-20); expect(placed[1].x).toBe(20); expect(placed[2].x).toBe(0); expect(placed[0].elevation).toBe(5); expect(placed[2].elevation).toBe(45); expect(placed.every((s) => s.componentDefId === "yaknet-masa-01")).toBe(true); expect(placed.every((s) => s.componentName === "Çalışma Masası")).toBe(true); }); it("deep-clones groupedShapes and sketchProfile so instances do not share mutable nested references", () => { const child = makeSampleShape({ id: "child-1" }); const parent = makeSampleShape({ id: "parent-1", groupedShapes: [child], sketchProfile: { points: [{ id: "p1", x: 0, z: 0, mode: "corner" }], segments: [{ id: "s1", startPointId: "p1", endPointId: "p1", kind: "line" }], }, }); let seq = 0; const [inst] = instantiateComponentShapes([parent], { createId: (prefix) => `${prefix}-${++seq}`, }); expect(inst.id).not.toBe(parent.id); expect(inst.groupedShapes?.[0].id).not.toBe(child.id); expect(inst.sketchProfile?.points[0]).not.toBe(parent.sketchProfile?.points[0]); }); it("detaches selected component instances cleanly on Make Unique", () => { const a = makeSampleShape({ id: "win-1", componentDefId: "comp-win", componentName: "Pencere" }); const b = makeSampleShape({ id: "win-2", x: 90, componentDefId: "comp-win", componentName: "Pencere" }); const plain = makeSampleShape({ id: "wall-1", componentDefId: undefined, componentName: undefined }); const detached = detachComponentInstance([a, b, plain], ["win-2"], () => "compdef-unique-1"); expect(detached[0].componentDefId).toBe("comp-win"); expect(detached[0].componentName).toBe("Pencere"); expect(detached[1].componentDefId).toBe("compdef-unique-1"); expect(detached[1].componentName).toBe("Pencere (Benzersiz)"); expect(detached[2].componentDefId).toBeUndefined(); }); });