Files
snapcad-studio/tests/unit/ifcImport.test.ts
T

59 lines
2.5 KiB
TypeScript
Raw Normal View History

import { describe, expect, it } from "vitest";
import { importedShapeFromIfc, importedShapeWithDetailsFromIfc } from "@/lib/ifcImport";
const SAMPLE_IFC4_BOX = [
"ISO-10303-21;",
"HEADER;",
"FILE_DESCRIPTION(('ViewDefinition [CoordinationView]'),'2;1');",
"FILE_NAME('wall_block.ifc','2026-01-01T00:00:00',(''),(''),'SnapCAD','SnapCAD','');",
"FILE_SCHEMA(('IFC4'));",
"ENDSEC;",
"DATA;",
"#1=IFCPERSON($,$,'User',$,$,$,$,$);",
"#2=IFCORGANIZATION($,'SnapCAD',$,$,$);",
"#3=IFCPERSONANDORGANIZATION(#1,#2,$);",
"#4=IFCAPPLICATION(#2,'1.0','SnapCAD','SnapCAD');",
"#5=IFCOWNERHISTORY(#3,#4,$,.NOCHANGE.,$,$,$,0);",
"#6=IFCCARTESIANPOINT((0.,0.,0.));",
"#7=IFCDIRECTION((0.,0.,1.));",
"#8=IFCDIRECTION((1.,0.,0.));",
"#9=IFCAXIS2PLACEMENT3D(#6,#7,#8);",
"#10=IFCGEOMETRICREPRESENTATIONCONTEXT($,'Model',3,1.E-05,#9,$);",
"#12=IFCSIUNIT(*,.LENGTHUNIT.,.MILLI.,.METRE.);",
"#13=IFCUNITASSIGNMENT((#12));",
"#14=IFCPROJECT('0001',#5,'Test',$,$,$,$,(#10),#13);",
"#15=IFCLOCALPLACEMENT($,#9);",
"#16=IFCBUILDINGELEMENTPROXY('0002',#5,'WallBlock',$,$,#15,#22,$,$);",
"#17=IFCCARTESIANPOINT((0.,0.));",
"#23=IFCAXIS2PLACEMENT2D(#17,$);",
"#18=IFCRECTANGLEPROFILEDEF(.AREA.,$,#23,200.,100.);",
"#19=IFCAXIS2PLACEMENT3D(#6,#7,#8);",
"#20=IFCEXTRUDEDAREASOLID(#18,#19,#7,50.);",
"#21=IFCSHAPEREPRESENTATION(#10,'Body','SweptSolid',(#20));",
"#22=IFCPRODUCTDEFINITIONSHAPE($,$,(#21));",
"ENDSEC;",
"END-ISO-10303-21;",
].join("\n");
describe("IFC BIM Importer via web-ifc WASM", () => {
it("rejects non-IFC arbitrary buffers cleanly before invoking WASM", async () => {
const garbage = new TextEncoder().encode("NOT AN IFC MODEL\n");
await expect(importedShapeFromIfc("bad.ifc", garbage)).rejects.toThrow(/IFC/i);
});
it("extracts extruded solid from IFC4 STEP text with exact millimeter dimensions", async () => {
const bytes = new TextEncoder().encode(SAMPLE_IFC4_BOX);
const details = await importedShapeWithDetailsFromIfc("wall_block.ifc", bytes);
expect(details.schema).toBe("IFC4");
expect(details.productCount).toBeGreaterThanOrEqual(1);
expect(details.triangleCount).toBeGreaterThanOrEqual(12);
expect(details.shape.importedMesh?.sourceFormat).toBe("ifc");
// 200 x 100 x 50 mm extruded solid (axes depend on Z->Y orientation, dimensions exact)
const dims = [details.shape.width, details.shape.height, details.shape.depth].sort((a, b) => a - b);
expect(dims[0]).toBeCloseTo(50, 0);
expect(dims[1]).toBeCloseTo(100, 0);
expect(dims[2]).toBeCloseTo(200, 0);
});
});