Merge remote-tracking branch 'origin/main' into codex/screw-hole-cutters

# Conflicts:
#	apps/web/src/components/WorkplaneViewport.tsx
This commit is contained in:
Francisco Rivero
2026-07-14 01:26:38 +09:00
36 changed files with 1784 additions and 365 deletions
+14
View File
@@ -0,0 +1,14 @@
import { describe, expect, it } from "vitest";
import { hasOneToOneCadComponentMapping } from "@/lib/cadModifierGroups";
describe("grouped CAD modifier component tracking", () => {
it("tracks grouped children only when every source still has one distinct CAD component", () => {
expect(hasOneToOneCadComponentMapping(2, [0, 1])).toBe(true);
expect(hasOneToOneCadComponentMapping(3, [2, 0, 1])).toBe(true);
});
it("falls back to the cumulative group body when touching parts fuse into fewer components", () => {
expect(hasOneToOneCadComponentMapping(2, [0])).toBe(false);
expect(hasOneToOneCadComponentMapping(2, [0, 0])).toBe(false);
});
});
+25
View File
@@ -0,0 +1,25 @@
import { describe, expect, it } from "vitest";
import {
CAD_MODIFIER_REQUEST_TIMEOUT_MS,
CAD_MODIFIER_RUNTIME_BASE,
cadModifierTimeoutMessage,
edgeModifierSelectionStatus,
} from "@/lib/cadModifierRuntime";
describe("CAD modifier runtime state", () => {
it("uses the build-managed OCCT runtime", () => {
expect(CAD_MODIFIER_RUNTIME_BASE).toBe("/occt");
});
it("does not report zero edges before preparation finishes", () => {
expect(edgeModifierSelectionStatus(false, 0, 0)).toBe("Preparing edges\u2026");
expect(edgeModifierSelectionStatus(true, 0, 0)).toBe("0 of 0 sharp edges selected");
expect(edgeModifierSelectionStatus(true, 2, 12)).toBe("2 of 12 sharp edges selected");
});
it("sets a bounded preparation wait and an actionable compatibility error", () => {
expect(CAD_MODIFIER_REQUEST_TIMEOUT_MS).toBeGreaterThanOrEqual(20_000);
expect(CAD_MODIFIER_REQUEST_TIMEOUT_MS).toBeLessThanOrEqual(60_000);
expect(cadModifierTimeoutMessage("prepare")).toContain("Firefox 121+");
});
});
+119
View File
@@ -0,0 +1,119 @@
import { describe, expect, it } from "vitest";
import { restoreShapeBeforeEdgeTreatment } from "@/lib/edgeTreatmentHistory";
import type { EdgeTreatmentHistoryEntry, WorkplaneShape } from "@/types/sketchforge";
function box(overrides: Partial<WorkplaneShape> = {}): WorkplaneShape {
return {
id: "box",
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,
};
}
function historyEntry(before: WorkplaneShape): EdgeTreatmentHistoryEntry {
return {
id: "history-1",
createdAt: 1,
feature: { kind: "chamfer", amount: 1, edgeCount: 1 },
before,
};
}
describe("edge treatment history restoration", () => {
it("restores a grouped snapshot instead of retaining the treated mesh bounding box", () => {
const before = box({
id: "group-before",
name: "Group",
kind: "mesh",
x: 8,
width: 56,
depth: 24,
height: 60,
size: 56,
groupedBaseWidth: 56,
groupedBaseDepth: 24,
groupedBaseHeight: 60,
groupedShapes: [
box({ id: "normal-child", x: -18 }),
box({ id: "tall-child", x: 14, width: 8, depth: 10, height: 60, size: 10 }),
],
});
const treated = box({
id: "group-current",
name: "Renamed group",
kind: "mesh",
color: "#12abef",
x: 0,
width: 80,
depth: 40,
height: 60,
size: 80,
importedMesh: {
positions: [-40, 0, -20, 40, 0, -20, 40, 60, 20],
baseWidth: 80,
baseDepth: 40,
baseHeight: 60,
triangleCount: 1,
sourceFormat: "json",
},
edgeTreatments: [{ kind: "chamfer", amount: 1, edgeCount: 1 }],
});
const restored = restoreShapeBeforeEdgeTreatment(treated, historyEntry(before));
expect(restored).toMatchObject({
id: "group-current",
name: "Renamed group",
color: "#12abef",
kind: "mesh",
x: 8,
width: 56,
depth: 24,
height: 60,
});
expect(restored.importedMesh).toBeUndefined();
expect(restored.edgeTreatments).toBeUndefined();
expect(restored.groupedShapes?.map((child) => [child.width, child.depth, child.height])).toEqual([
[20, 20, 20],
[8, 10, 60],
]);
});
it("does not resize a restored child primitive to a combined component's bounds", () => {
const before = box({ id: "thin-before", x: 18, width: 8, depth: 6, height: 50, size: 8 });
const combinedComponent = box({
id: "thin-current",
kind: "mesh",
x: 0,
width: 54,
depth: 30,
height: 50,
size: 54,
importedMesh: {
positions: [-27, 0, -15, 27, 0, -15, 27, 50, 15],
baseWidth: 54,
baseDepth: 30,
baseHeight: 50,
triangleCount: 1,
sourceFormat: "json",
},
});
const restored = restoreShapeBeforeEdgeTreatment(combinedComponent, historyEntry(before));
expect(restored).toMatchObject({ kind: "box", x: 18, width: 8, depth: 6, height: 50, size: 8 });
expect(restored.importedMesh).toBeUndefined();
});
});
+90
View File
@@ -0,0 +1,90 @@
import { describe, expect, it } from "vitest";
import { snapShapeFootprintToVisibleGrid } from "@/lib/gridSnap";
import { DEFAULT_WORKPLANE_WORKSPACE } from "@/lib/workplaneSettings";
import type { WorkplaneShape } from "@/types/sketchforge";
function groupedShape(): WorkplaneShape {
return {
id: "group",
name: "Group",
kind: "mesh",
color: "#d41721",
x: 3.2,
z: -1.7,
elevation: 2.4,
size: 43.5,
width: 43.5,
depth: 19.8,
height: 31.25,
rotation: 0,
groupedBaseWidth: 43.5,
groupedBaseDepth: 19.8,
groupedBaseHeight: 31.25,
groupedShapes: [
{
id: "child-a",
name: "Child A",
kind: "box",
color: "#d41721",
x: -10.75,
z: 1.2,
elevation: 0,
size: 20,
width: 20,
depth: 12,
height: 20,
rotation: 0,
},
{
id: "child-b",
name: "Child B",
kind: "box",
color: "#0098c7",
x: 14.5,
z: -2.8,
elevation: 0,
size: 10,
width: 8,
depth: 10,
height: 31.25,
rotation: 0,
},
],
};
}
describe("Snap to Grid", () => {
it("translates a group footprint to the visible grid without resizing it or changing child offsets", () => {
const group = groupedShape();
const snapped = snapShapeFootprintToVisibleGrid(
group,
{ minX: -17.3, minZ: -11.4 },
{ ...DEFAULT_WORKPLANE_WORKSPACE, gridBlockSize: 5 },
);
expect(snapped.x).toBe(5.5);
expect(snapped.z).toBe(-0.3);
expect(-17.3 + snapped.x - group.x).toBeCloseTo(-15, 6);
expect(-11.4 + snapped.z - group.z).toBeCloseTo(-10, 6);
expect(snapped).toMatchObject({
elevation: 2.4,
width: 43.5,
depth: 19.8,
height: 31.25,
size: 43.5,
});
expect(snapped.groupedShapes).toBe(group.groupedShapes);
});
it("uses the rendered grid origin for custom workplane dimensions", () => {
const group = groupedShape();
const snapped = snapShapeFootprintToVisibleGrid(
group,
{ minX: -18, minZ: -13 },
{ ...DEFAULT_WORKPLANE_WORKSPACE, width: 203, depth: 187, gridBlockSize: 10 },
);
expect(-18 + snapped.x - group.x).toBeCloseTo(-21.5, 6);
expect(-13 + snapped.z - group.z).toBeCloseTo(-13.5, 6);
});
});
+15
View File
@@ -0,0 +1,15 @@
import { describe, expect, it } from "vitest";
import { importExtensionSupported } from "@/lib/stlImport";
describe("importExtensionSupported", () => {
it("accepts STL and SVG imports", () => {
expect(importExtensionSupported("part.stl")).toBe(true);
expect(importExtensionSupported("logo.svg")).toBe(true);
expect(importExtensionSupported("profile.SVG")).toBe(true);
});
it("rejects unsupported dashboard import extensions", () => {
expect(importExtensionSupported("drawing.png")).toBe(false);
expect(importExtensionSupported("assembly.step")).toBe(false);
});
});
+135
View File
@@ -0,0 +1,135 @@
import { describe, expect, it } from "vitest";
import * as THREE from "three";
import {
MAX_SVG_GEOMETRY_ELEMENTS,
analyzeTriangleSoup,
buildSvgExtrusionFromPaths,
normalizeSvgUseReferences,
validateClosedSolidTriangleSoup,
validateSvgSourcePreflight,
} from "@/lib/svgImport";
function shapePath(points: Array<[number, number]>, options: { closed?: boolean; fill?: string; fillOpacity?: number; opacity?: number } = {}) {
const path = new THREE.ShapePath();
path.moveTo(points[0][0], points[0][1]);
for (const [x, y] of points.slice(1)) path.lineTo(x, y);
if (options.closed !== false) path.currentPath!.autoClose = true;
(path as THREE.ShapePath & { userData: unknown }).userData = {
style: {
fill: options.fill ?? "#000",
fillOpacity: options.fillOpacity ?? 1,
opacity: options.opacity ?? 1,
visibility: "visible",
},
};
return path;
}
function rectangle(x: number, y: number, width: number, height: number, options?: Parameters<typeof shapePath>[1]) {
return shapePath(
[
[x, y],
[x + width, y],
[x + width, y + height],
[x, y + height],
],
options,
);
}
function geometryPositions(geometry: THREE.BufferGeometry) {
const nonIndexed = geometry.index ? geometry.toNonIndexed() : geometry;
const attribute = nonIndexed.getAttribute("position");
const positions: number[] = [];
for (let index = 0; index < attribute.count; index += 1) {
positions.push(attribute.getX(index), attribute.getY(index), attribute.getZ(index));
}
if (nonIndexed !== geometry) nonIndexed.dispose();
geometry.dispose();
return positions;
}
describe("SVG source preflight", () => {
it("rejects XML entities and external references", () => {
expect(() => validateSvgSourcePreflight('<!DOCTYPE svg [<!ENTITY x "bad">]><svg/>')).toThrow(/document types and entities/i);
expect(() => validateSvgSourcePreflight('<svg><use href="https://example.com/art.svg#part"/></svg>')).toThrow(/external references/i);
});
it("rejects excessive geometry before SVGLoader runs", () => {
const source = `<svg>${"<rect width='1' height='1'/>".repeat(MAX_SVG_GEOMETRY_ELEMENTS + 1)}</svg>`;
expect(() => validateSvgSourcePreflight(source)).toThrow(/too many geometry elements/i);
});
it("normalizes modern local use href attributes for SVGLoader", () => {
const normalized = normalizeSvgUseReferences('<svg><defs><path id="p" d="M0 0L1 0L1 1Z"/></defs><use href="#p"/></svg>');
expect(normalized).toContain('xmlns:xlink="http://www.w3.org/1999/xlink"');
expect(normalized).toContain('xlink:href="#p"');
});
});
describe("SVG path extrusion", () => {
it("creates a watertight solid with real, unclamped dimensions", () => {
const result = buildSvgExtrusionFromPaths([rectangle(0, 0, 0.25, 0.5)]);
expect(result.analysis.width).toBeCloseTo(0.25);
expect(result.analysis.height).toBeCloseTo(4);
expect(result.analysis.depth).toBeCloseTo(0.5);
expect(result.analysis.volume).toBeCloseTo(0.5);
expect(result.analysis.boundaryEdges).toBe(0);
expect(result.analysis.nonManifoldEdges).toBe(0);
});
it("treats a separately defined contained contour as a hole", () => {
const result = buildSvgExtrusionFromPaths([rectangle(0, 0, 10, 10), rectangle(3, 3, 4, 4)]);
expect(result.analysis.volume).toBeCloseTo((100 - 16) * 4, 5);
expect(result.analysis.boundaryEdges).toBe(0);
});
it("ignores non-filled and fully transparent paths", () => {
expect(() => buildSvgExtrusionFromPaths([rectangle(0, 0, 10, 10, { fill: "none" })])).toThrow(/no readable visible filled paths/i);
expect(() => buildSvgExtrusionFromPaths([rectangle(0, 0, 10, 10, { opacity: 0 })])).toThrow(/no readable visible filled paths/i);
expect(() => buildSvgExtrusionFromPaths([rectangle(0, 0, 10, 10, { fillOpacity: 0 })])).toThrow(/no readable visible filled paths/i);
expect(() => buildSvgExtrusionFromPaths([rectangle(0, 0, 10, 10, { fill: "#00000000" })])).toThrow(/no readable visible filled paths/i);
});
it("imports open filled contours when they enclose usable area and skips lines", () => {
const result = buildSvgExtrusionFromPaths([shapePath([[0, 0], [10, 0], [5, 5]], { closed: false })]);
expect(result.analysis.volume).toBeCloseTo(100);
expect(() => buildSvgExtrusionFromPaths([shapePath([[0, 0], [10, 0]], { closed: false })])).toThrow(/no (?:readable )?filled (?:paths|contours)/i);
});
it("skips closed collinear paths", () => {
expect(() => buildSvgExtrusionFromPaths([shapePath([[0, 0], [5, 0], [10, 0]])])).toThrow(/no readable filled paths/i);
});
it("imports overlapping contours as separate valid components", () => {
const result = buildSvgExtrusionFromPaths([rectangle(0, 0, 10, 10), rectangle(5, 5, 10, 10)]);
expect(result.analysis.volume).toBeCloseTo(800);
});
});
describe("triangle-soup solid validation", () => {
it("accepts a closed box", () => {
const positions = geometryPositions(new THREE.BoxGeometry(2, 3, 4));
const analysis = validateClosedSolidTriangleSoup(positions);
expect(analysis.volume).toBeCloseTo(24);
expect(analysis.boundaryEdges).toBe(0);
expect(analysis.nonManifoldEdges).toBe(0);
});
it("rejects an open box shell", () => {
const positions = geometryPositions(new THREE.BoxGeometry(2, 3, 4)).slice(9);
expect(analyzeTriangleSoup(positions).boundaryEdges).toBeGreaterThan(0);
expect(() => validateClosedSolidTriangleSoup(positions)).toThrow(/not a watertight manifold/i);
});
it("rejects a topologically closed zero-volume shell", () => {
const positions = [
0, 0, 0, 1, 0, 0, 0, 1, 0,
0, 0, 0, 0, 1, 0, 1, 0, 0,
];
const analysis = analyzeTriangleSoup(positions);
expect(analysis.boundaryEdges).toBe(0);
expect(analysis.volume).toBe(0);
expect(() => validateClosedSolidTriangleSoup(positions)).toThrow(/non-zero volume/i);
});
});
+32
View File
@@ -0,0 +1,32 @@
import { describe, expect, it } from "vitest";
import { interiorWorkplaneGridCoordinates, workplaneGridPalette, WORKPLANE_LINE_ELEVATION } from "@/lib/workplaneGrid";
describe("workplane grid geometry", () => {
it("excludes both perimeter coordinates when spacing divides the workplane", () => {
const coordinates = interiorWorkplaneGridCoordinates(200, 5).map(({ coordinate }) => coordinate);
expect(coordinates).not.toContain(-100);
expect(coordinates).not.toContain(100);
expect(coordinates).toContain(0);
expect(coordinates.at(0)).toBe(-95);
expect(coordinates.at(-1)).toBe(95);
});
it("keeps the final interior line for custom spacing", () => {
const coordinates = interiorWorkplaneGridCoordinates(200, 30).map(({ coordinate }) => coordinate);
expect(coordinates).toEqual([-70, -40, -10, 20, 50, 80]);
});
it("uses one elevation for grid and border lines", () => {
expect(WORKPLANE_LINE_ELEVATION).toBe(0);
});
it("preserves the minor, major, and axis line hierarchy", () => {
const palette = workplaneGridPalette();
expect(palette.minor.opacity).toBeLessThan(palette.major.opacity);
expect(palette.major.opacity).toBeLessThan(palette.axis.opacity);
expect(palette.minor.color).not.toBe(palette.major.color);
});
});
+2
View File
@@ -80,9 +80,11 @@ describe("workplane settings helpers", () => {
it("fingerprints workspace and snap settings together", () => {
const base = workplaneSettingsFingerprint(DEFAULT_WORKPLANE_WORKSPACE, "1.0 mm");
const equivalentNewReference = 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(equivalentNewReference).toBe(base);
expect(changedSnap).not.toBe(base);
expect(changedWorkspace).not.toBe(base);
});
+24
View File
@@ -16,6 +16,7 @@ import {
serializeShapesForSync,
shapeDepth,
shapeWidth,
withHoleMode,
workplaneShapesEqual,
} from "@/lib/workplaneShapes";
@@ -114,6 +115,29 @@ describe("workplane shape helpers", () => {
expect(fallbackSolidColor(shape({ kind: "box" }))).toBe("#d41721");
});
it("applies an explicitly selected group color to every nested child", () => {
const grouped = shape({
kind: "mesh",
color: "#111111",
groupedShapes: [
shape({ id: "child-box", color: "#222222" }),
shape({
id: "child-group",
kind: "mesh",
color: "#333333",
groupedShapes: [shape({ id: "grandchild", kind: "sphere", color: "#444444" })],
}),
],
});
const recolored = withHoleMode(grouped, false, "#12abef");
expect(recolored.color).toBe("#12abef");
expect(recolored.groupedShapes?.map((child) => child.color)).toEqual(["#12abef", "#12abef"]);
expect(recolored.groupedShapes?.[1].groupedShapes?.[0].color).toBe("#12abef");
expect(grouped.groupedShapes?.[0].color).toBe("#222222");
});
it("can resize the body while preserving fillet and chamfer boundary distances", () => {
const modified = shape({
kind: "mesh",