Files
snapcad-studio/tests/unit/offsetKernel.test.ts
T
Enes Yakıştır ee28672e23
Build and Push Docker Images / build (push) Canceled after 0s
Desktop Release / Build linux desktop (push) Canceled after 0s
Desktop Release / Build windows desktop (push) Canceled after 0s
Desktop Release / Attach packages to release (push) Canceled after 0s
Desktop Release / Build macos desktop (push) Canceled after 0s
CI / Typecheck and unit tests (push) Canceled after 0s
release: SnapCAD Studio 1.0.10 (desktop menuler, macOS kisayollari, YakNet SSO)
2026-11-12 10:12:35 +03:00

98 lines
3.3 KiB
TypeScript

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, it, expect } from "vitest";
import { offsetPolygon, polygonSignedArea, offsetSketchProfile, type Point2D } from "@/lib/offsetKernel";
describe("Offset Tool Kernel (Ofset Aracı & Duvar Kalınlığı)", () => {
it("calculates signed area of a rectangle correctly", () => {
// 100 x 50 rectangle (CCW)
const rect: Point2D[] = [
{ x: 0, z: 0 },
{ x: 100, z: 0 },
{ x: 100, z: 50 },
{ x: 0, z: 50 },
];
const area = polygonSignedArea(rect);
expect(area).toBe(5000);
});
it("offsets a rectangle outward by 10mm", () => {
const rect: Point2D[] = [
{ x: 0, z: 0 },
{ x: 100, z: 0 },
{ x: 100, z: 50 },
{ x: 0, z: 50 },
];
const outset = offsetPolygon(rect, 10);
expect(outset.length).toBe(4);
// Outward expands by 10 on all sides: width becomes 120, height becomes 70
const minX = Math.min(...outset.map((p) => p.x));
const maxX = Math.max(...outset.map((p) => p.x));
const minZ = Math.min(...outset.map((p) => p.z));
const maxZ = Math.max(...outset.map((p) => p.z));
expect(Math.round(maxX - minX)).toBe(120);
expect(Math.round(maxZ - minZ)).toBe(70);
});
it("offsets a room perimeter inward by 15mm to generate interior wall", () => {
const room: Point2D[] = [
{ x: 0, z: 0 },
{ x: 200, z: 0 },
{ x: 200, z: 150 },
{ x: 0, z: 150 },
];
// Inset by -15mm
const inset = offsetPolygon(room, -15);
expect(inset.length).toBe(4);
const minX = Math.min(...inset.map((p) => p.x));
const maxX = Math.max(...inset.map((p) => p.x));
const minZ = Math.min(...inset.map((p) => p.z));
const maxZ = Math.max(...inset.map((p) => p.z));
expect(Math.round(maxX - minX)).toBe(170); // 200 - 30 = 170
expect(Math.round(maxZ - minZ)).toBe(120); // 150 - 30 = 120
});
it("offsets a SketchProfile into a dual-walled footprint", () => {
const initialProfile = {
points: [
{ id: "p1", x: 0, z: 0 },
{ id: "p2", x: 100, z: 0 },
{ id: "p3", x: 100, z: 80 },
{ id: "p4", x: 0, z: 80 },
],
segments: [
{ id: "s1", startId: "p1", endId: "p2" },
{ id: "s2", startId: "p2", endId: "p3" },
{ id: "s3", startId: "p3", endId: "p4" },
{ id: "s4", startId: "p4", endId: "p1" },
],
};
const walledProfile = offsetSketchProfile(initialProfile, -10);
expect(walledProfile.points.length).toBe(8); // 4 outer + 4 inner wall points
expect(walledProfile.segments.length).toBe(8); // 4 outer + 4 inner segments
});
it("handles concave L-shaped polygon inset cleanly without inverted bowties (Clipper2)", () => {
const lShape: Point2D[] = [
{ x: 0, z: 0 },
{ x: 100, z: 0 },
{ x: 100, z: 30 },
{ x: 40, z: 30 },
{ x: 40, z: 100 },
{ x: 0, z: 100 },
];
const inset = offsetPolygon(lShape, -5);
expect(inset.length).toBeGreaterThanOrEqual(6);
expect(Math.abs(polygonSignedArea(inset))).toBeLessThan(Math.abs(polygonSignedArea(lShape)));
// Collapsing a narrow 20x20 square inward by -15mm returns empty cleanly
const small: Point2D[] = [
{ x: 0, z: 0 },
{ x: 20, z: 0 },
{ x: 20, z: 20 },
{ x: 0, z: 20 },
];
expect(offsetPolygon(small, -15)).toEqual([]);
});
});