diff --git a/tests/e2e/stepRoundTrip.e2e.ts b/tests/e2e/stepRoundTrip.e2e.ts new file mode 100644 index 0000000..f08a73f --- /dev/null +++ b/tests/e2e/stepRoundTrip.e2e.ts @@ -0,0 +1,162 @@ +import { beforeAll, describe, expect, it, vi } from "vitest"; +import { fileURLToPath } from "node:url"; +import { dirname, join } from "node:path"; +import type { WorkplaneShape } from "@/types/sketchforge"; + +// Drive the REAL exporter/importer against the REAL OpenCascade kernel, loaded +// from node_modules instead of the browser-only /occt/ URL. Everything else in +// stepExport.ts / stepImport.ts runs unmodified. +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 | null = null; + return { + loadBrepWithOcct: () => + (ready ??= (async () => { + const kernel = await OcctKernel.init({ wasm }); + brep.registerKernel("occt-wasm", brep.OcctWasmAdapter.fromKernel(kernel)); + return brep; + })()), + }; +}); + +let brep: typeof import("brepjs"); +let exportShapesToStep: typeof import("@/lib/stepExport").exportShapesToStep; +let importedShapeFromStep: typeof import("@/lib/stepImport").importedShapeFromStep; + +beforeAll(async () => { + brep = await import("brepjs"); + ({ exportShapesToStep } = await import("@/lib/stepExport")); + ({ importedShapeFromStep } = await import("@/lib/stepImport")); + // Warm the kernel via the mocked loader so brepjs has a registered kernel for + // the re-import assertions below. + const { loadBrepWithOcct } = await import("@/lib/brepKernel"); + await loadBrepWithOcct(); +}); + +function shape(overrides: Partial): WorkplaneShape { + return { + id: Math.random().toString(36).slice(2), + name: "Shape", + kind: "box", + color: "#0098c7", + x: 0, + z: 0, + size: 10, + width: 10, + depth: 10, + height: 10, + rotation: 0, + ...overrides, + }; +} + +async function reimportVolume(blob: Blob): Promise { + const r = await brep.importSTEP(blob); + if (!r.ok) throw new Error(`reimport failed: ${String(r.error?.message ?? r.error)}`); + const v = brep.measureVolume(r.value); + if (!v.ok) throw new Error("measureVolume failed"); + return v.value; +} + +const PI = Math.PI; +const near = (a: number, b: number, relTol = 0.01) => Math.abs(a - b) <= relTol * Math.abs(b) + 1e-6; + +describe("STEP export round-trip (real OCCT kernel)", () => { + it("exports box + cylinder + sphere as exact B-Rep with conserved volume", async () => { + const box = shape({ kind: "box", name: "Box", x: -30, width: 10, depth: 6, height: 4 }); + const cyl = shape({ kind: "cylinder", name: "Cyl", x: 0, width: 8, depth: 8, height: 12 }); + const sph = shape({ kind: "sphere", name: "Sph", x: 30, width: 10, depth: 10, height: 10 }); + + const { blob, exportedCount, skipped } = await exportShapesToStep([box, cyl, sph]); + expect(exportedCount).toBe(3); + expect(skipped).toEqual([]); + + const expected = 10 * 6 * 4 + PI * 4 * 4 * 12 + (4 / 3) * PI * 5 ** 3; + expect(near(await reimportVolume(blob), expected)).toBe(true); + }); + + it("subtracts an overlapping hole and conserves the cut volume", async () => { + const body = shape({ kind: "box", name: "Plate", width: 20, depth: 20, height: 10, elevation: 0 }); + // Cylinder hole punched fully through the plate's full height (overhangs both faces). + const hole = shape({ kind: "cylinder", name: "Bore", hole: true, width: 6, depth: 6, height: 14, elevation: -2 }); + + const { blob, exportedCount, skipped } = await exportShapesToStep([body, hole]); + expect(exportedCount).toBe(1); + expect(skipped).toEqual([]); + + const expected = 20 * 20 * 10 - PI * 3 * 3 * 10; + expect(near(await reimportVolume(blob), expected)).toBe(true); + }); + + it("leaves a body untouched when the hole's AABB does not reach it", async () => { + const body = shape({ kind: "box", name: "Plate", x: 0, width: 10, depth: 10, height: 10 }); + const farHole = shape({ kind: "cylinder", name: "Bore", hole: true, x: 500, width: 4, depth: 4, height: 20 }); + + const { blob } = await exportShapesToStep([body, farHole]); + expect(near(await reimportVolume(blob), 1000)).toBe(true); + }); + + it("exports a full cone in a multi-solid assembly with conserved volume (occt-wasm 3.6.1 fix)", async () => { + const cone = shape({ kind: "cone", name: "Cone", x: -20, width: 8, depth: 8, height: 10, baseRadius: 4, topRadius: 0 }); + const box = shape({ kind: "box", name: "Box", x: 20, width: 6, depth: 6, height: 6 }); + + const { blob, exportedCount, skipped } = await exportShapesToStep([cone, box]); + expect(exportedCount).toBe(2); + expect(skipped).toEqual([]); + + const expected = (1 / 3) * PI * 4 * 4 * 10 + 6 * 6 * 6; + expect(near(await reimportVolume(blob), expected)).toBe(true); + }); + + it("exports a truncated cone with conserved volume", async () => { + const frustum = shape({ kind: "cone", name: "Frustum", width: 10, depth: 10, height: 12, baseRadius: 5, topRadius: 2 }); + const { blob, exportedCount } = await exportShapesToStep([frustum]); + expect(exportedCount).toBe(1); + // Frustum volume = (π h / 3)(R² + R r + r²), with R=5, r=2, h=12. + const expected = (PI * 12 / 3) * (25 + 10 + 4); + expect(near(await reimportVolume(blob), expected)).toBe(true); + }); + + it("skips non-exact shapes with descriptive reasons but still exports the rest", async () => { + const box = shape({ kind: "box", name: "Box", width: 8, depth: 8, height: 8 }); + const pyramid = shape({ kind: "pyramid", name: "Pyramid", width: 8, depth: 8, height: 8 }); + const meshNoBrep = shape({ kind: "mesh", name: "RawMesh" }); + + const { exportedCount, skipped } = await exportShapesToStep([box, pyramid, meshNoBrep]); + expect(exportedCount).toBe(1); + expect(skipped.map((s) => s.kind).sort()).toEqual(["mesh", "pyramid"]); + expect(skipped.find((s) => s.kind === "pyramid")?.reason).toMatch(/no exact B-Rep mapping/i); + expect(skipped.find((s) => s.kind === "mesh")?.reason).toMatch(/no B-Rep source/i); + }); + + it("throws when there is nothing exact to export", async () => { + await expect(exportShapesToStep([shape({ kind: "pyramid", name: "Pyramid" })])).rejects.toThrow(/No box\/cylinder\/sphere/i); + }); +}); + +describe("STEP import → re-export round-trip (real OCCT kernel)", () => { + it("imports a STEP body, stores its B-Rep, and re-exports it losslessly", async () => { + // Author a source STEP file straight from the kernel: a 12×8×6 box in CAD Z-up. + const src = brep.exportSTEP(brep.box(12, 8, 6, { centered: true })); + expect(src.ok).toBe(true); + const bytes = await (src as { value: Blob }).value.arrayBuffer(); + + const imported = await importedShapeFromStep("widget.step", bytes); + expect(imported.kind).toBe("mesh"); + expect(imported.importedMesh?.sourceFormat).toBe("step"); + expect(imported.importedMesh?.brepStep).toBeTruthy(); + // Importer maps CAD Z-up (X12,Y8,Z6) to SketchForge Y-up (width12, height6, depth8). + expect(near(imported.importedMesh!.baseWidth, 12)).toBe(true); + expect(near(imported.importedMesh!.baseHeight, 6)).toBe(true); + expect(near(imported.importedMesh!.baseDepth, 8)).toBe(true); + + // Re-export the imported body at native size and confirm volume survives the + // import-normalize → store → re-emit pipeline. + const reexport = await exportShapesToStep([imported]); + expect(reexport.exportedCount).toBe(1); + expect(reexport.skipped).toEqual([]); + expect(near(await reimportVolume(reexport.blob), 12 * 8 * 6)).toBe(true); + }); +}); diff --git a/tests/unit/exportNames.test.ts b/tests/unit/exportNames.test.ts index 295f47c..b5aaf9c 100644 --- a/tests/unit/exportNames.test.ts +++ b/tests/unit/exportNames.test.ts @@ -5,6 +5,7 @@ describe("project export filenames", () => { it("uses the project name for each supported export format", () => { expect(projectExportFileName("Gearbox Prototype", "stl")).toBe("Gearbox Prototype.stl"); expect(projectExportFileName("Gearbox Prototype", "obj")).toBe("Gearbox Prototype.obj"); + expect(projectExportFileName("Gearbox Prototype", "step")).toBe("Gearbox Prototype.step"); }); it("removes filesystem-reserved characters without discarding the project name", () => { diff --git a/tests/unit/stepExport.test.ts b/tests/unit/stepExport.test.ts new file mode 100644 index 0000000..5ae4918 --- /dev/null +++ b/tests/unit/stepExport.test.ts @@ -0,0 +1,76 @@ +import { describe, expect, it } from "vitest"; +import type { WorkplaneShape } from "@/types/sketchforge"; +import { aabbsOverlap, shapeYawDegrees, worldAabb } from "@/lib/stepExport"; + +function shape(overrides: Partial = {}): WorkplaneShape { + return { + id: "s1", + name: "Shape", + kind: "box", + color: "#ffffff", + x: 0, + z: 0, + size: 10, + width: 10, + depth: 10, + height: 10, + rotation: 0, + ...overrides, + }; +} + +describe("shapeYawDegrees", () => { + it("drops yaw on a circular cylinder so the exported diameter is invariant", () => { + expect(shapeYawDegrees(shape({ kind: "cylinder", width: 8, depth: 8, rotation: 30 }))).toBe(0); + }); + + it("drops yaw on a circular cone too", () => { + expect(shapeYawDegrees(shape({ kind: "cone", width: 8, depth: 8, rotation: 30 }))).toBe(0); + }); + + it("keeps yaw on an elliptical cylinder where orientation matters", () => { + expect(shapeYawDegrees(shape({ kind: "cylinder", width: 8, depth: 4, rotation: 30 }))).toBe(30); + }); + + it("keeps yaw on a box regardless of footprint", () => { + expect(shapeYawDegrees(shape({ kind: "box", width: 8, depth: 8, rotation: 45 }))).toBe(45); + }); +}); + +describe("worldAabb", () => { + it("builds a tight box for an axis-aligned shape, offset by elevation", () => { + const box = worldAabb(shape({ x: 5, z: -3, elevation: 2, width: 10, depth: 6, height: 4 })); + expect(box.min).toEqual([0, 2, -6]); + expect(box.max).toEqual([10, 6, 0]); + }); + + it("falls back to the bounding-sphere box for a rotated shape", () => { + const box = worldAabb(shape({ width: 10, depth: 6, height: 4, rotation: 30 })); + const r = 0.5 * Math.sqrt(10 * 10 + 4 * 4 + 6 * 6); + expect(box.min).toEqual([-r, 2 - r, -r]); + expect(box.max).toEqual([r, 2 + r, r]); + }); + + it("treats a circular cylinder's yaw as no rotation and keeps the tight box", () => { + const box = worldAabb(shape({ kind: "cylinder", width: 8, depth: 8, height: 4, rotation: 90 })); + expect(box.min).toEqual([-4, 0, -4]); + expect(box.max).toEqual([4, 4, 4]); + }); +}); + +describe("aabbsOverlap", () => { + const a = { min: [0, 0, 0] as [number, number, number], max: [10, 10, 10] as [number, number, number] }; + + it("detects overlapping boxes", () => { + expect(aabbsOverlap(a, { min: [5, 5, 5], max: [15, 15, 15] })).toBe(true); + }); + + it("treats face-touching boxes as overlapping (inclusive bounds)", () => { + expect(aabbsOverlap(a, { min: [10, 0, 0], max: [20, 10, 10] })).toBe(true); + }); + + it("rejects boxes separated on a single axis", () => { + expect(aabbsOverlap(a, { min: [11, 0, 0], max: [20, 10, 10] })).toBe(false); + expect(aabbsOverlap(a, { min: [0, 0, 11], max: [10, 10, 20] })).toBe(false); + }); +}); diff --git a/tests/vitest.e2e.config.ts b/tests/vitest.e2e.config.ts new file mode 100644 index 0000000..e75ffe2 --- /dev/null +++ b/tests/vitest.e2e.config.ts @@ -0,0 +1,19 @@ +import path from "node:path"; +import { defineConfig } from "vitest/config"; + +const rootDir = path.resolve(__dirname, ".."); + +export default defineConfig({ + root: rootDir, + resolve: { + alias: { + "@": path.resolve(rootDir, "apps/web/src"), + }, + }, + test: { + environment: "node", + include: ["tests/e2e/**/*.e2e.ts"], + testTimeout: 120000, + hookTimeout: 120000, + }, +});