Organize test files

This commit is contained in:
Formsmith
2026-06-14 22:13:39 +03:00
parent 753a1c2833
commit 4ba8cb723c
7 changed files with 17 additions and 11 deletions
+73
View File
@@ -0,0 +1,73 @@
import { describe, expect, it } from "vitest";
import type { ShapeAsset } from "@/types/sketchforge";
import { makeShapeFromAsset, sceneShape, toolbarShapeAssets } from "@/lib/shapeCatalog";
describe("shape catalog", () => {
it("does not expose removed decorative shapes in the toolbar catalog", () => {
const kinds = toolbarShapeAssets.map((asset) => asset.kind);
expect(kinds).not.toContain("star");
expect(kinds).not.toContain("heart");
});
it("creates placed shapes from toolbar assets", () => {
const asset: ShapeAsset = { id: "box", name: "Box", src: "box.png", kind: "box", color: "#d41721" };
const placed = makeShapeFromAsset(asset, { x: 12, z: -8, elevation: 4 });
expect(placed.id).toMatch(/^box-/);
expect(placed).toMatchObject({
name: "Box",
kind: "box",
color: "#d41721",
x: 12,
z: -8,
elevation: 4,
size: 20,
width: 20,
depth: 20,
height: 20,
radius: 0,
steps: 10,
locked: false,
hidden: false,
});
});
it("uses shape-specific defaults for text and round profiles", () => {
const text = makeShapeFromAsset({ id: "text", name: "Text", src: "text.png", kind: "text", color: "#cf101b" });
const torus = makeShapeFromAsset({ id: "torus", name: "Torus", src: "torus.png", kind: "torus", color: "#0098c7" });
expect(text).toMatchObject({ width: 86, depth: 28, height: 10, text: "TEXT", font: "Multilanguage" });
expect(torus).toMatchObject({ size: 22, width: 22, depth: 22, height: 5 });
});
it("creates canonical scene shapes with stable defaults", () => {
const created = sceneShape({
name: "Part",
kind: "box",
color: "#d41721",
width: 12,
depth: 18,
rotation: 359.9,
mirrorX: false,
});
expect(created.id).toMatch(/^shape-/);
expect(created).toMatchObject({
name: "Part",
kind: "box",
color: "#d41721",
x: 0,
z: 0,
elevation: 0,
width: 12,
depth: 18,
height: 20,
size: 18,
rotation: 0,
locked: false,
hidden: false,
});
expect(created.mirrorX).toBeUndefined();
});
});
+61
View File
@@ -0,0 +1,61 @@
import { describe, expect, it } from "vitest";
import type { WorkplaneWorkspaceSettings } from "@/types/sketchforge";
import { DEFAULT_SNAP_GRID, DEFAULT_WORKPLANE_WORKSPACE, normalizeSnapGrid, normalizeWorkspaceSettings, workplaneSettingsFingerprint } from "@/lib/workplaneSettings";
describe("workplane settings helpers", () => {
it("accepts known snap grid values and falls back for unknown values", () => {
expect(normalizeSnapGrid("0.5 mm")).toBe("0.5 mm");
expect(normalizeSnapGrid("Huge")).toBe(DEFAULT_SNAP_GRID);
expect(normalizeSnapGrid(null, "Off")).toBe("Off");
});
it("normalizes workspace settings from partial or invalid data", () => {
const fallback: WorkplaneWorkspaceSettings = {
...DEFAULT_WORKPLANE_WORKSPACE,
width: 300,
depth: 250,
background: "#ffffff",
};
expect(normalizeWorkspaceSettings(null, fallback)).toEqual(fallback);
expect(
normalizeWorkspaceSettings(
{
width: 500,
depth: Number.NaN,
sizePreset: "",
gridBlockSize: 2.5,
gridBlockPreset: "Custom",
background: "#123456",
showShadows: false,
showGrid: false,
cruiseShapes: false,
zoomSpeed: Infinity,
units: "Bricks",
scale: "1:10 (centimeters)",
},
fallback,
),
).toEqual({
...fallback,
width: 500,
gridBlockSize: 2.5,
gridBlockPreset: "Custom",
background: "#123456",
showShadows: false,
showGrid: false,
cruiseShapes: false,
units: "Bricks",
scale: "1:10 (centimeters)",
});
});
it("fingerprints workspace and snap settings together", () => {
const base = workplaneSettingsFingerprint(DEFAULT_WORKPLANE_WORKSPACE, "1.0 mm");
const changedSnap = workplaneSettingsFingerprint(DEFAULT_WORKPLANE_WORKSPACE, "5.0 mm");
const changedWorkspace = workplaneSettingsFingerprint({ ...DEFAULT_WORKPLANE_WORKSPACE, width: 300 }, "1.0 mm");
expect(changedSnap).not.toBe(base);
expect(changedWorkspace).not.toBe(base);
});
});
+113
View File
@@ -0,0 +1,113 @@
import { describe, expect, it } from "vitest";
import type { WorkplaneShape } from "@/types/sketchforge";
import {
canonicalizeShape,
cleanNearZero,
cleanRotationDegrees,
fallbackSolidColor,
mirroredAxisCount,
mirrorSign,
normalizeDegrees,
proportionalResizeScale,
resizedShapeSize,
serializeShapesForSync,
shapeDepth,
shapeWidth,
workplaneShapesEqual,
} from "@/lib/workplaneShapes";
function shape(overrides: Partial<WorkplaneShape> = {}): WorkplaneShape {
return {
id: "box-1",
name: "Box",
kind: "box",
color: "#d41721",
x: 0,
z: 0,
elevation: 0,
size: 20,
width: 20,
depth: 20,
height: 20,
rotation: 0,
locked: false,
hidden: false,
...overrides,
};
}
describe("workplane shape helpers", () => {
it("normalizes and cleans rotations", () => {
expect(normalizeDegrees(-90)).toBe(270);
expect(normalizeDegrees(450)).toBe(90);
expect(cleanRotationDegrees(Number.NaN)).toBe(0);
expect(cleanRotationDegrees(-0.2)).toBe(0);
expect(cleanRotationDegrees(359.8)).toBe(0);
expect(cleanRotationDegrees(12.34)).toBe(12.3);
});
it("cleans near-zero values and derives dimensions", () => {
const base = shape({ size: 30, width: 18, depth: 24 });
expect(cleanNearZero(0.004)).toBe(0);
expect(cleanNearZero(0.006)).toBe(0.006);
expect(shapeWidth(base)).toBe(18);
expect(shapeDepth(base)).toBe(24);
expect(resizedShapeSize(18, 24)).toBe(24);
});
it("uses proportional scale instead of square dimensions while shift-resizing", () => {
expect(proportionalResizeScale(50, 100, 100, 150)).toBe(2);
expect(proportionalResizeScale(50, 100, 60, 200)).toBe(2);
expect(proportionalResizeScale(50, 100, 25, 80)).toBe(0.5);
});
it("canonicalizes mirror flags and nested group rotations", () => {
const canonical = canonicalizeShape(
shape({
rotation: 360,
rotationX: -0.1,
rotationZ: 45.04,
mirrorX: false,
mirrorY: true,
groupedShapes: [shape({ id: "child", rotation: 720, mirrorZ: false })],
}),
);
expect(canonical.rotation).toBe(0);
expect(canonical.rotationX).toBe(0);
expect(canonical.rotationZ).toBe(45);
expect(canonical.mirrorX).toBeUndefined();
expect(canonical.mirrorY).toBe(true);
expect(canonical.groupedShapes?.[0].rotation).toBe(0);
expect(canonical.groupedShapes?.[0].mirrorZ).toBeUndefined();
});
it("keeps shallow equality strict for shape payload references", () => {
const importedMesh = { positions: [0, 0, 0], baseWidth: 1, baseDepth: 1, baseHeight: 1, triangleCount: 0, sourceFormat: "json" as const };
const first = shape({ importedMesh });
const sameReference = shape({ importedMesh });
const sameValues = shape({ importedMesh: { ...importedMesh, positions: [...importedMesh.positions] } });
expect(workplaneShapesEqual(first, sameReference)).toBe(true);
expect(workplaneShapesEqual(first, sameValues)).toBe(false);
});
it("serializes canonical shapes for sync", () => {
expect(JSON.parse(serializeShapesForSync([shape({ rotation: 359.9, mirrorX: false })]))).toEqual([
expect.objectContaining({
id: "box-1",
rotation: 0,
rotationX: 0,
rotationZ: 0,
}),
]);
});
it("maps helper flags and fallback colors", () => {
expect(mirrorSign(true)).toBe(-1);
expect(mirrorSign(false)).toBe(1);
expect(mirroredAxisCount(shape({ mirrorX: true, mirrorY: true }))).toBe(2);
expect(fallbackSolidColor(shape({ kind: "sphere" }))).toBe("#0098c7");
expect(fallbackSolidColor(shape({ kind: "box" }))).toBe("#d41721");
});
});