Files
snapcad-studio/tests/unit/smartPlacement.test.ts
T

89 lines
2.3 KiB
TypeScript
Raw Normal View History

import { describe, it, expect } from "vitest";
import {
calculateSmartPlacementPoint,
calculateTopSurfaceElevation,
} from "@/lib/smartPlacement";
import type { WorkplaneShape } from "@/types/sketchforge";
describe("Smart Placement & Anti-Collision (Akıllı Yerleşim ve Çakışma Önleme)", () => {
it("places at origin when scene is empty", () => {
const point = calculateSmartPlacementPoint([], { width: 20, depth: 20 });
expect(point.x).toBe(0);
expect(point.z).toBe(0);
});
it("places cleanly beside an existing house centered at (0, 0)", () => {
// Miniature house centered at (0, 0), width 80, depth 60
const house: WorkplaneShape = {
id: "house-1",
name: "Minyatür Ev",
kind: "box",
color: "#f59e0b",
x: 0,
z: 0,
width: 80,
depth: 60,
height: 50,
elevation: 0,
rotation: 0,
};
// New 20x20 cube added
const point = calculateSmartPlacementPoint([house], { width: 20, depth: 20 });
// House extends from X = -40 to +40.
// New cube should be placed to the right: 40 + 10 (half width) + 12 (margin) = 62
expect(point.x).toBe(62);
expect(point.z).toBe(0);
});
it("calculates top surface elevation to pop an interior cube onto the roof", () => {
const houseBody: WorkplaneShape = {
id: "house-body",
name: "Ev Gövdesi",
kind: "box",
color: "#f59e0b",
x: 0,
z: 0,
width: 60,
depth: 60,
height: 40,
elevation: 0,
rotation: 0,
};
const roof: WorkplaneShape = {
id: "house-roof",
name: "Çatı",
kind: "roof",
color: "#dc2626",
x: 0,
z: 0,
width: 64,
depth: 64,
height: 25,
elevation: 40, // sits on top of body (40 + 25 = 65)
rotation: 0,
};
// Cube swallowed inside at (0, 0, 0)
const swallowedCube: WorkplaneShape = {
id: "cube-inside",
name: "Kutu",
kind: "box",
color: "#3b82f6",
x: 0,
z: 0,
width: 20,
depth: 20,
height: 20,
elevation: 0,
rotation: 0,
};
const topElevation = calculateTopSurfaceElevation(swallowedCube, [houseBody, roof, swallowedCube]);
// Top of the roof is 40 + 25 = 65mm
expect(topElevation).toBe(65);
});
});