import { describe, expect, it } from "vitest"; import { exportMeshesToGlb } from "@/lib/glbExport"; import { importedShapeFromGlb } from "@/lib/glbImport"; import type { ObjExportMesh } from "@/lib/objExport"; function makeSimpleCubeMesh(): ObjExportMesh { return { name: "test_cube", vertices: [ [-5, -5, -5], [5, -5, -5], [5, 5, -5], [-5, 5, -5], [-5, -5, 5], [5, -5, 5], [5, 5, 5], [-5, 5, 5], ], faces: [ [0, 2, 1], [0, 3, 2], [4, 5, 6], [4, 6, 7], [0, 1, 5], [0, 5, 4], [2, 3, 7], [2, 7, 6], [0, 4, 7], [0, 7, 3], [1, 2, 6], [1, 6, 5], ], }; } describe("GLB export and import", () => { it("exports meshes to binary glTF (.glb) buffer with valid glTF header", async () => { const mesh = makeSimpleCubeMesh(); const glbBuffer = await exportMeshesToGlb([mesh], { test_cube: "#ef4444" }); expect(glbBuffer).toBeInstanceOf(ArrayBuffer); expect(glbBuffer.byteLength).toBeGreaterThan(50); // Verify GLB magic header 0x46546C67 ("glTF") const view = new DataView(glbBuffer); const magic = view.getUint32(0, true); expect(magic).toBe(0x46546c67); // Version should be 2 (glTF 2.0) const version = view.getUint32(4, true); expect(version).toBe(2); }); it("imports binary GLB and reconstitutes a valid WorkplaneShape", async () => { const mesh = makeSimpleCubeMesh(); const glbBuffer = await exportMeshesToGlb([mesh], { test_cube: "#3b82f6" }); const shape = await importedShapeFromGlb("cube.glb", glbBuffer); expect(shape.kind).toBe("mesh"); expect(shape.importedMesh).toBeDefined(); expect(shape.importedMesh?.sourceFormat).toBe("glb"); expect(shape.importedMesh?.triangleCount).toBe(12); // Dimensions of 10x10x10 cube expect(shape.importedMesh?.baseWidth).toBeCloseTo(10, 1); expect(shape.importedMesh?.baseHeight).toBeCloseTo(10, 1); expect(shape.importedMesh?.baseDepth).toBeCloseTo(10, 1); expect(shape.importedMesh?.positions.length).toBe(12 * 9); expect(shape.importedMesh?.normals?.length).toBe(12 * 9); }); });