Files
FreeCADWasmDev/tools/test_ifc_native_contract.mjs
wangdequan 72152ef356 Save verified native IFC import and user validation evidence
Real IFC2X3/IFC4 import tested. Full BIM authoring/export remains out of scope; 349-second building import performance remains to optimize.
2026-09-16 04:27:37 +08:00

43 lines
3.9 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
import assert from 'node:assert/strict';
import { fileURLToPath } from 'node:url';
import { loadWasmProbe } from './wasm-probe-runtime.mjs';
const root=path.resolve(path.dirname(fileURLToPath(import.meta.url)),'..');
const output=path.join(root,'docs/current/user-tests-20260915',`ifc-contract-${new Date().toISOString().replace(/[:.]/g,'-')}`);
fs.mkdirSync(output,{recursive:true});
const log=s=>fs.appendFileSync(path.join(output,'native.log'),s+'\n');
const results=[];
try {
const {kernel}=await loadWasmProbe(path.join(root,'freecad-source/build-web/configure/bin'),'FreeCADWebPartBoxProbe',{print:log,printErr:log});
const call=(command,parameters,document='Contract')=>{
const reply=JSON.parse(kernel.ccall('fc_web_dispatch_json','string',['string'],[JSON.stringify({cmd:'part.viewCommand',args:{schema:'freecad-web/document-view/v1',owner:'PartBox',document,command,parameters:{...parameters,documentId:document}}})]));
results.push({command,reply});fs.writeFileSync(path.join(output,'results.json'),JSON.stringify(results,null,2)+'\n');assert.equal(reply.ok,true,JSON.stringify(reply));return reply.result;
};
for(const name of ['ColumnPSetsOfSets.ifc']) {
const bytes=fs.readFileSync(path.join(root,'freecad-source/build-web/ifcopenshell-native/fixtures',name));
const value=call('Std_Open',{operation:'open',files:[{filename:name,fileData:bytes.toString('base64')}]});
assert.equal(value.errors.length,0,JSON.stringify(value.errors));assert.ok(value.opened[0].documents[0].objectCount>0);
const doc=value.activeDocument;
const read=call('Std_CommandLine',{operation:'submit',source:`import json; print(json.dumps({'count':len(App.getDocument('${doc}').Objects),'properties':[(o.Name,o.IfcPropertySets) for o in App.getDocument('${doc}').Objects if 'IfcPropertySets' in o.PropertiesList],'volumes':[o.Shape.Volume for o in App.getDocument('${doc}').Objects if o.isDerivedFrom('Part::Feature')]}))`},doc);
assert.equal(read.stderr,'');const state=JSON.parse(read.stdout.trim());assert.ok(state.volumes.some(v=>v>0));
if(name==='ColumnPSetsOfSets.ifc') {
assert.ok(state.properties.length>0,'Property set not mapped');
const columnSets=JSON.parse(state.properties.find(([id])=>id==='Ifc_93')[1]).map(s=>s.Name);
assert.ok(columnSets.includes('PSet_1')&&columnSets.includes('PSet_2'),'IFC4 property-definition set was not expanded');
assert.ok(Math.abs(state.volumes[0]-30862141.083286542)<1,'Column geometry changed or units were scaled incorrectly');
}
fs.writeFileSync(path.join(output,name+'.json'),JSON.stringify(state,null,2)+'\n');
}
const before=call('Std_CommandLine',{operation:'submit',source:'import json; print(json.dumps(sorted(App.listDocuments())))'});
const library=call('Std_Open',{operation:'open',files:[{filename:'wall.ifc',fileData:fs.readFileSync(path.join(root,'freecad-source/build-web/ifcopenshell-native/fixtures/wall.ifc')).toString('base64')}]});
assert.equal(library.errors.length,1,'Type library without product geometry must not report a successful import');
const bad=call('Std_Open',{operation:'open',files:[{filename:'invalid.ifc',fileData:Buffer.from('this is not an IFC file').toString('base64')}]});
assert.equal(bad.errors.length,1);
const after=call('Std_CommandLine',{operation:'submit',source:'import json; print(json.dumps(sorted(App.listDocuments())))'});
assert.equal(after.stdout,before.stdout,'Failed IFC open modified existing documents');
fs.writeFileSync(path.join(output,'summary.json'),JSON.stringify({status:'passed',fixtures:2,invalidFileRejected:true,documentsPreserved:true},null,2)+'\n');
console.log(JSON.stringify({status:'passed',output}));
}catch(error){fs.writeFileSync(path.join(output,'failure.json'),JSON.stringify({error:String(error)},null,2)+'\n');console.error(String(error));process.exitCode=1;}
process.exit(process.exitCode??0);