release: publish v1.0.0

This commit is contained in:
Formsmith
2026-08-10 16:15:32 +03:00
parent decf33521a
commit a42922c4f3
56 changed files with 3914 additions and 125 deletions
+206
View File
@@ -0,0 +1,206 @@
import { beforeAll, describe, expect, it, vi } from "vitest";
import { fileURLToPath } from "node:url";
import { dirname, join } from "node:path";
import type { SketchProfile } from "@/types/sketchforge";
// Load the real OCCT kernel directly (same approach as stepRoundTrip.e2e.ts).
vi.mock("@/lib/brepKernel", async () => {
const brep = await import("brepjs");
const { OcctKernel } = await import("occt-wasm");
const wasm = join(dirname(fileURLToPath(import.meta.resolve("occt-wasm"))), "occt-wasm.wasm");
let ready: Promise<typeof brep> | null = null;
return {
loadBrepWithOcct: () =>
(ready ??= (async () => {
const kernel = await OcctKernel.init({ wasm });
brep.registerKernel("occt-wasm", brep.OcctWasmAdapter.fromKernel(kernel));
return brep;
})()),
};
});
let cadSketchRegions: typeof import("@/lib/sketchCadProfile").cadSketchRegions;
beforeAll(async () => {
({ cadSketchRegions } = await import("@/lib/sketchCadProfile"));
});
function rectangle(id: string, x: number, z: number, width: number, depth: number): SketchProfile {
const points = [
{ id: `${id}-0`, x, z },
{ id: `${id}-1`, x: x + width, z },
{ id: `${id}-2`, x: x + width, z: z + depth },
{ id: `${id}-3`, x, z: z + depth },
];
const segments = points.map((point, index) => ({
id: `${id}-s${index}`,
kind: "line" as const,
startId: point.id,
endId: points[(index + 1) % points.length].id,
}));
return { points, segments };
}
function mergeProfiles(...profiles: SketchProfile[]): SketchProfile {
return {
points: profiles.flatMap((p) => p.points),
segments: profiles.flatMap((p) => p.segments),
};
}
describe("Sketch CAD profile → B-Rep extrusion (real OCCT kernel)", () => {
it("produces valid B-Rep from a single closed rectangle", async () => {
const { OcctKernel } = await import("occt-wasm");
const profile = rectangle("rect", 0, 0, 20, 10);
const regions = cadSketchRegions(profile);
expect(regions).toHaveLength(1);
expect(regions[0].outer.closed).toBe(true);
expect(regions[0].holes).toHaveLength(0);
const wasm = join(dirname(fileURLToPath(import.meta.resolve("occt-wasm"))), "occt-wasm.wasm");
const kernel = await OcctKernel.init({ wasm });
const edges = regions[0].outer.steps.map(({ from, to }) =>
kernel.makeLineEdge({ x: from.x, y: 0, z: from.z }, { x: to.x, y: 0, z: to.z }),
);
const wire = kernel.makeWire(edges);
const face = kernel.makeFace(wire);
const solid = kernel.extrude(face, 0, 15, 0);
expect(kernel.isValid(solid)).toBe(true);
expect(kernel.isSolid(solid)).toBe(true);
const mesh = kernel.tessellate(solid, { linearDeflection: 0.05, angularDeflection: 0.16 });
expect(mesh.triangleCount).toBeGreaterThan(0);
expect(mesh.positions.length).toBeGreaterThanOrEqual(9);
expect(mesh.indices.length).toBeGreaterThanOrEqual(3);
const brep = kernel.toBREP(solid);
expect(brep).toBeTruthy();
expect(brep.length).toBeGreaterThan(0);
kernel.releaseAll();
});
it("extrudes a rectangle with a hole into a valid hollow solid", async () => {
const { OcctKernel } = await import("occt-wasm");
const profile = mergeProfiles(
rectangle("outer", 0, 0, 40, 40),
rectangle("hole", 10, 10, 20, 20),
);
const regions = cadSketchRegions(profile);
expect(regions).toHaveLength(1);
expect(regions[0].holes).toHaveLength(1);
const wasm = join(dirname(fileURLToPath(import.meta.resolve("occt-wasm"))), "occt-wasm.wasm");
const kernel = await OcctKernel.init({ wasm });
const outerEdges = regions[0].outer.steps.map(({ from, to }) =>
kernel.makeLineEdge({ x: from.x, y: 0, z: from.z }, { x: to.x, y: 0, z: to.z }),
);
const outerWire = kernel.makeWire(outerEdges);
let face = kernel.makeFace(outerWire);
const holeEdges = regions[0].holes[0].steps.map(({ from, to }) =>
kernel.makeLineEdge({ x: from.x, y: 0, z: from.z }, { x: to.x, y: 0, z: to.z }),
);
const holeWire = kernel.makeWire(holeEdges);
face = kernel.addHolesInFace(face, [holeWire]);
const solid = kernel.extrude(face, 0, 10, 0);
expect(kernel.isValid(solid)).toBe(true);
expect(kernel.isSolid(solid)).toBe(true);
const mesh = kernel.tessellate(solid, { linearDeflection: 0.05, angularDeflection: 0.16 });
expect(mesh.triangleCount).toBeGreaterThan(0);
kernel.releaseAll();
});
it("produces two separate solids from disjoint rectangles", async () => {
const { OcctKernel } = await import("occt-wasm");
const profile = mergeProfiles(
rectangle("left", 0, 0, 10, 10),
rectangle("right", 20, 0, 10, 10),
);
const regions = cadSketchRegions(profile);
expect(regions).toHaveLength(2);
const wasm = join(dirname(fileURLToPath(import.meta.resolve("occt-wasm"))), "occt-wasm.wasm");
const kernel = await OcctKernel.init({ wasm });
const solids = regions.map((region) => {
const edges = region.outer.steps.map(({ from, to }) =>
kernel.makeLineEdge({ x: from.x, y: 0, z: from.z }, { x: to.x, y: 0, z: to.z }),
);
const wire = kernel.makeWire(edges);
const face = kernel.makeFace(wire);
return kernel.extrude(face, 0, 5, 0);
});
const compound = kernel.makeCompound(solids);
expect(kernel.isValid(compound)).toBe(true);
const mesh = kernel.tessellate(compound, { linearDeflection: 0.05, angularDeflection: 0.16 });
expect(mesh.triangleCount).toBeGreaterThan(0);
kernel.releaseAll();
});
it("extrudes a profile with an island inside a hole", async () => {
const { OcctKernel } = await import("occt-wasm");
const profile = mergeProfiles(
rectangle("outer", 0, 0, 40, 40),
rectangle("hole", 5, 5, 30, 30),
rectangle("island", 12, 12, 16, 16),
);
const regions = cadSketchRegions(profile);
expect(regions).toHaveLength(2);
const outerRegion = regions.find((r) => r.outer.id.includes("outer"));
const islandRegion = regions.find((r) => r.outer.id.includes("island"));
expect(outerRegion).toBeDefined();
expect(islandRegion).toBeDefined();
expect(outerRegion!.holes).toHaveLength(1);
expect(islandRegion!.holes).toHaveLength(0);
const wasm = join(dirname(fileURLToPath(import.meta.resolve("occt-wasm"))), "occt-wasm.wasm");
const kernel = await OcctKernel.init({ wasm });
const buildSolid = (region: { outer: typeof regions[0]["outer"]; holes: typeof regions[0]["holes"] }) => {
const outerEdges = region.outer.steps.map(({ from, to }) =>
kernel.makeLineEdge({ x: from.x, y: 0, z: from.z }, { x: to.x, y: 0, z: to.z }),
);
let face = kernel.makeFace(kernel.makeWire(outerEdges));
if (region.holes.length > 0) {
const holeWires = region.holes.map((hole) => {
const holeEdges = hole.steps.map(({ from, to }) =>
kernel.makeLineEdge({ x: from.x, y: 0, z: from.z }, { x: to.x, y: 0, z: to.z }),
);
return kernel.makeWire(holeEdges);
});
face = kernel.addHolesInFace(face, holeWires);
}
return kernel.extrude(face, 0, 8, 0);
};
const solids = regions.map(buildSolid);
const compound = kernel.makeCompound(solids);
expect(kernel.isValid(compound)).toBe(true);
const mesh = kernel.tessellate(compound, { linearDeflection: 0.05, angularDeflection: 0.16 });
expect(mesh.triangleCount).toBeGreaterThan(0);
kernel.releaseAll();
});
it("rejects a profile with only open paths", () => {
const openProfile: SketchProfile = {
points: [
{ id: "a-0", x: 0, z: 0 },
{ id: "a-1", x: 10, z: 0 },
{ id: "a-2", x: 10, z: 10 },
],
segments: [
{ id: "a-s0", kind: "line", startId: "a-0", endId: "a-1" },
{ id: "a-s1", kind: "line", startId: "a-1", endId: "a-2" },
],
};
expect(() => cadSketchRegions(openProfile)).toThrow(/open/i);
});
});
+21
View File
@@ -0,0 +1,21 @@
import { describe, expect, it } from "vitest";
import { appUpdateIsAvailable, compareAppVersions } from "@/lib/appUpdates";
describe("application update versions", () => {
it("orders semantic releases numerically", () => {
expect(compareAppVersions("0.10.0", "0.9.9")).toBeGreaterThan(0);
expect(compareAppVersions("1.0.0", "0.99.99")).toBeGreaterThan(0);
expect(compareAppVersions("v1.0.0", "1.0.0")).toBe(0);
});
it("keeps prereleases below their stable release", () => {
expect(compareAppVersions("1.0.0-beta.2", "1.0.0-beta.1")).toBeGreaterThan(0);
expect(compareAppVersions("1.0.0", "1.0.0-rc.1")).toBeGreaterThan(0);
});
it("only offers a strictly newer version", () => {
expect(appUpdateIsAvailable("1.0.0", "1.0.1")).toBe(true);
expect(appUpdateIsAvailable("1.0.0", "1.0.0")).toBe(false);
expect(appUpdateIsAvailable("1.0.0", "0.9.9")).toBe(false);
});
});
+15
View File
@@ -0,0 +1,15 @@
import { describe, expect, it } from "vitest";
import { CHALLENGE_TUTORIAL_IDS, isChallengeTutorialId } from "@/lib/challenges";
describe("challenge tutorial ids", () => {
it("recognizes every supported challenge", () => {
expect(CHALLENGE_TUTORIAL_IDS).toEqual(["key-tag", "nameplate"]);
expect(CHALLENGE_TUTORIAL_IDS.every(isChallengeTutorialId)).toBe(true);
});
it("rejects stale or malformed stored values", () => {
expect(isChallengeTutorialId("phone-stand")).toBe(false);
expect(isChallengeTutorialId(null)).toBe(false);
expect(isChallengeTutorialId({ tutorial: "nameplate" })).toBe(false);
});
});
+12
View File
@@ -0,0 +1,12 @@
import { describe, expect, it } from "vitest";
import { sketchForgeToZUp, zUpToSketchForge } from "@/lib/meshCoordinates";
describe("mesh coordinate conventions", () => {
it("maps slicer Z-up coordinates to SketchForge Y-up and back", () => {
const slicerPoint: [number, number, number] = [12, 34, 56];
const sketchForgePoint = zUpToSketchForge(slicerPoint);
expect(sketchForgePoint).toEqual([12, 56, -34]);
expect(sketchForgeToZUp(sketchForgePoint)).toEqual(slicerPoint);
});
});
+43
View File
@@ -0,0 +1,43 @@
import { describe, expect, it } from "vitest";
import { exportMeshesToObj, type ObjExportMesh } from "@/lib/objExport";
function duplicatedTetrahedron(): ObjExportMesh {
const a: [number, number, number] = [0, 0, 0];
const b: [number, number, number] = [10, 0, 0];
const c: [number, number, number] = [0, 10, 0];
const d: [number, number, number] = [0, 0, 10];
return {
name: "closed_tetrahedron",
vertices: [a, c, b, a, b, d, b, c, d, c, a, d],
faces: [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]],
};
}
describe("OBJ export", () => {
it("preserves OBJ's conventional Y-up coordinates", () => {
const obj = exportMeshesToObj([duplicatedTetrahedron()]);
const vertices = obj.split("\n").filter((line) => line.startsWith("v "));
expect(vertices).toContain("v 0 0 10");
expect(vertices).toContain("v 0 10 0");
});
it("welds coincident triangle vertices so a closed mesh has no boundary edges", () => {
const obj = exportMeshesToObj([duplicatedTetrahedron()]);
const vertices = obj.split("\n").filter((line) => line.startsWith("v "));
const faces = obj.split("\n").filter((line) => line.startsWith("f "));
const edgeUse = new Map<string, number>();
faces.forEach((line) => {
const [a, b, c] = line.slice(2).trim().split(/\s+/).map(Number);
[[a, b], [b, c], [c, a]].forEach(([start, end]) => {
const key = start < end ? `${start}:${end}` : `${end}:${start}`;
edgeUse.set(key, (edgeUse.get(key) ?? 0) + 1);
});
});
expect(vertices).toHaveLength(4);
expect(faces).toHaveLength(4);
expect([...edgeUse.values()].every((count) => count === 2)).toBe(true);
});
});
+118
View File
@@ -0,0 +1,118 @@
import { describe, expect, it } from "vitest";
import { cadSketchRegions, orderedCadSketchPaths } from "@/lib/sketchCadProfile";
import type { SketchPoint, SketchProfile, SketchSegment } from "@/types/sketchforge";
function rectangle(id: string, x: number, z: number, width: number, depth: number) {
const points: SketchPoint[] = [
{ id: `${id}-0`, x, z },
{ id: `${id}-1`, x: x + width, z },
{ id: `${id}-2`, x: x + width, z: z + depth },
{ id: `${id}-3`, x, z: z + depth },
];
const segments: SketchSegment[] = points.map((point, index) => ({
id: `${id}-s${index}`,
kind: "line",
startId: point.id,
endId: points[(index + 1) % points.length].id,
}));
return { points, segments };
}
function profile(...rectangles: ReturnType<typeof rectangle>[]): SketchProfile {
return {
points: rectangles.flatMap((rectangle) => rectangle.points),
segments: rectangles.flatMap((rectangle) => rectangle.segments),
};
}
describe("OCCT sketch profile preparation", () => {
it("orders a closed loop even when its segments arrive out of order", () => {
const square = rectangle("outer", 0, 0, 20, 10);
const paths = orderedCadSketchPaths({ ...profile(square), segments: [...square.segments].reverse() });
expect(paths).toHaveLength(1);
expect(paths[0].closed).toBe(true);
expect(paths[0].steps).toHaveLength(4);
});
it("assigns an enclosed loop as a hole", () => {
const regions = cadSketchRegions(profile(
rectangle("outer", 0, 0, 20, 20),
rectangle("hole", 5, 5, 4, 4),
));
expect(regions).toHaveLength(1);
expect(regions[0].outer.id).toContain("outer");
expect(regions[0].holes).toHaveLength(1);
});
it("keeps disjoint loops as separate solids", () => {
const regions = cadSketchRegions(profile(
rectangle("left", 0, 0, 4, 4),
rectangle("right", 10, 0, 4, 4),
));
expect(regions).toHaveLength(2);
expect(regions.every((region) => region.holes.length === 0)).toBe(true);
});
it("rejects open paths with a clear error", () => {
const square = rectangle("open", 0, 0, 10, 10);
square.segments.pop();
expect(() => cadSketchRegions(profile(square))).toThrow(/open/i);
});
it("creates a solid island inside a hole (3-level nesting)", () => {
const regions = cadSketchRegions(profile(
rectangle("outer", 0, 0, 40, 40),
rectangle("hole", 5, 5, 30, 30),
rectangle("island", 12, 12, 16, 16),
));
expect(regions).toHaveLength(2);
const outerRegion = regions.find((r) => r.outer.id.includes("outer"));
const islandRegion = regions.find((r) => r.outer.id.includes("island"));
expect(outerRegion).toBeDefined();
expect(outerRegion!.holes).toHaveLength(1);
expect(outerRegion!.holes[0].id).toContain("hole");
expect(islandRegion).toBeDefined();
expect(islandRegion!.holes).toHaveLength(0);
});
it("handles 4-level nesting (outer > hole > island > island-hole)", () => {
const regions = cadSketchRegions(profile(
rectangle("outer", 0, 0, 60, 60),
rectangle("hole", 5, 5, 50, 50),
rectangle("island", 15, 15, 30, 30),
rectangle("ihole", 20, 20, 20, 20),
));
const outerRegion = regions.find((r) => r.outer.id.includes("outer"));
const islandRegion = regions.find((r) => r.outer.id.includes("island"));
expect(outerRegion).toBeDefined();
expect(outerRegion!.holes).toHaveLength(1);
expect(outerRegion!.holes[0].id).toContain("hole");
expect(islandRegion).toBeDefined();
expect(islandRegion!.holes).toHaveLength(1);
expect(islandRegion!.holes[0].id).toContain("ihole");
});
it("throws when all paths are open", () => {
const open1 = rectangle("a", 0, 0, 10, 10);
open1.segments.pop();
const open2 = rectangle("b", 20, 0, 10, 10);
open2.segments.pop();
expect(() => cadSketchRegions(profile(open1, open2))).toThrow(/open/i);
});
it("returns empty for a single degenerate zero-area path", () => {
const degenerate: SketchProfile = {
points: [
{ id: "d-0", x: 0, z: 0 },
{ id: "d-1", x: 1, z: 0 },
{ id: "d-2", x: 0, z: 0 },
],
segments: [
{ id: "d-s0", kind: "line", startId: "d-0", endId: "d-1" },
{ id: "d-s1", kind: "line", startId: "d-1", endId: "d-2" },
{ id: "d-s2", kind: "line", startId: "d-2", endId: "d-0" },
],
};
expect(cadSketchRegions(degenerate)).toEqual([]);
});
});
+13
View File
@@ -0,0 +1,13 @@
import { describe, expect, it } from "vitest";
import { exportMeshesToStl } from "@/lib/stlExport";
describe("STL export orientation", () => {
it("writes editor Y-up coordinates as slicer Z-up coordinates", () => {
const stl = exportMeshesToStl([{
vertices: [[0, 0, 0], [10, 0, 0], [0, 20, 5]],
faces: [[0, 1, 2]],
}]);
expect(stl).toContain("vertex 0 -5 20");
});
});
+48
View File
@@ -0,0 +1,48 @@
import { describe, expect, it } from "vitest";
import { importedShapeFromStl } from "@/lib/stlImport";
function boxStl(width: number, ySize: number, zSize: number, name = "box") {
const vertices = [
[0, 0, 0], [width, 0, 0], [width, ySize, 0], [0, ySize, 0],
[0, 0, zSize], [width, 0, zSize], [width, ySize, zSize], [0, ySize, zSize],
];
const faces = [
[0, 2, 1], [0, 3, 2], [4, 5, 6], [4, 6, 7],
[0, 1, 5], [0, 5, 4], [1, 2, 6], [1, 6, 5],
[2, 3, 7], [2, 7, 6], [3, 0, 4], [3, 4, 7],
];
const facets = faces.map(([a, b, c]) => `facet normal 0 0 0
outer loop
vertex ${vertices[a].join(" ")}
vertex ${vertices[b].join(" ")}
vertex ${vertices[c].join(" ")}
endloop
endfacet`).join("\n");
const source = `solid ${name}\n${facets}\nendsolid ${name}`;
return new TextEncoder().encode(source).buffer;
}
describe("STL import orientation", () => {
it("maps a clearly Z-up slicer model to SketchForge Y-up", () => {
const shape = importedShapeFromStl("z-up-box.stl", boxStl(80, 60, 20));
expect(shape.importedMesh?.baseWidth).toBeCloseTo(80);
expect(shape.importedMesh?.baseHeight).toBeCloseTo(20);
expect(shape.importedMesh?.baseDepth).toBeCloseTo(60);
});
it("preserves an unmistakably flat Y-up legacy model", () => {
const shape = importedShapeFromStl("raspberry-pi-like.stl", boxStl(89, 20, 58));
expect(shape.importedMesh?.baseWidth).toBeCloseTo(89);
expect(shape.importedMesh?.baseHeight).toBeCloseTo(20);
expect(shape.importedMesh?.baseDepth).toBeCloseTo(58);
});
it("preserves previous SketchForge STL exports regardless of proportions", () => {
const shape = importedShapeFromStl("legacy-sketchforge.stl", boxStl(20, 80, 10, "sketchforge_design"));
expect(shape.importedMesh?.baseHeight).toBeCloseTo(80);
expect(shape.importedMesh?.baseDepth).toBeCloseTo(10);
});
});