Files
vibemake/scripts/validate-duck-current-assembly.mjs
T
Jerry 1c837766cd feat: integrate robot assembly and simulation with resilient loading
Add backend-driven assembly and simulation experiences, MAKE branding, mobile rendering budgets and desktop-only CAD/PCB routes. Improve logo loading progress, document image recovery and URDF request handling; include dev packaging and regression coverage.
2026-10-30 18:38:39 +08:00

48 lines
4.4 KiB
JavaScript

// Independent integrity check against the supplied compressed whole model.
import fs from 'node:fs';
import path from 'node:path';
import assert from 'node:assert/strict';
import { createHash } from 'node:crypto';
import { validatePackage,validateEmbeddedGlb } from '../js/experience-packages.js';
const root=new URL('../deliverables/experience-packages/microduck-current/',import.meta.url).pathname;
const digest=b=>createHash('sha256').update(b).digest('hex');
function glb(filename){const bytes=fs.readFileSync(filename),length=bytes.readUInt32LE(12);validateEmbeddedGlb(bytes.buffer.slice(bytes.byteOffset,bytes.byteOffset+bytes.length));return{bytes,json:JSON.parse(bytes.subarray(20,20+length)),bin:20+length+8};}
function buffer(g,i){const v=g.json.bufferViews[i];return g.bytes.subarray(g.bin+(v.byteOffset??0),g.bin+(v.byteOffset??0)+v.byteLength);}
function material(g,i){if(i===undefined)return null;const m=structuredClone(g.json.materials[i]);function visit(v){for(const[k,x]of Object.entries(v))if(x&&typeof x==='object'){
if(/Texture$/.test(k)&&x.index!==undefined){const t=structuredClone(g.json.textures[x.index]),im=g.json.images[t.source];
t.source={...im,bufferView:digest(buffer(g,im.bufferView))};if(t.sampler!==undefined)t.sampler=g.json.samplers[t.sampler];x.index=t;
}else visit(x);}}visit(m);return m;}
const source=glb('/Users/jerry/Downloads/鸭子_压缩.glb'),m=JSON.parse(fs.readFileSync(path.join(root,'manifest.json'))),map=JSON.parse(fs.readFileSync(path.join(root,'mapping.json')));
const config=validatePackage(m,'assembly','http://localhost:8090/manifest.json');
assert.equal(digest(source.bytes),map.sourceSha256);assert.equal(m.steps.length,239);assert.equal(m.parts.length,242);assert.deepEqual(m.displayQuaternion,[0,0,0,1]);
const actualOrder=m.steps.map(s=>s.id.replace('step-',''));assert.deepEqual(actualOrder,map.steps.map(s=>s.code));
assert.equal(new Set(m.steps.flatMap(s=>s.parts)).size,242);
let primitives=0,textures=0;
for(const file of m.files){const f=path.join(root,file.path),b=fs.readFileSync(f);assert.equal(b.length,file.bytes);assert.equal(digest(b),file.sha256);}
for(const [i,p]of m.parts.entries()){
const n=source.json.nodes[i],g=glb(path.join(root,p.file));assert.equal(g.json.nodes.length,1);assert.equal(g.json.nodes[0].name,n.name);
assert.equal(g.json.nodes[0].translation,undefined);assert.equal(g.json.nodes[0].rotation,undefined);assert.equal(g.json.nodes[0].scale,undefined);
assert.deepEqual(p.position,n.translation??[0,0,0]);assert.deepEqual(p.scale,n.scale??[1,1,1]);
const q=n.rotation??[0,0,0,1],norm=Math.hypot(...q);assert.ok(p.quaternion.every((x,k)=>Math.abs(x-q[k]/norm)<1e-12));
const a=source.json.meshes[n.mesh].primitives,b=g.json.meshes[0].primitives;assert.equal(a.length,b.length);
for(let k=0;k<a.length;k++){
assert.deepEqual(material(g,b[k].material),material(source,a[k].material));
assert.deepEqual(buffer(g,b[k].extensions.KHR_draco_mesh_compression.bufferView),buffer(source,a[k].extensions.KHR_draco_mesh_compression.bufferView));
assert.deepEqual(b[k].extensions.KHR_draco_mesh_compression.attributes,a[k].extensions.KHR_draco_mesh_compression.attributes);
for(const sem of Object.keys(a[k].attributes)){
const aa=structuredClone(source.json.accessors[a[k].attributes[sem]]),bb=structuredClone(g.json.accessors[b[k].attributes[sem]]);
delete aa.bufferView;delete bb.bufferView;assert.deepEqual(bb,aa);
}
primitives++;
}
textures+=g.json.images.length;
assert.ok(p.installOffset.every(Number.isFinite));assert.ok(Math.hypot(...p.installOffset)<=.1);
if(p.installation){assert.ok(Math.abs(Math.hypot(...p.installation.axis)-1)<1e-8);assert.ok(p.installation.socketPosition.every(Number.isFinite));}
}
const r=JSON.parse(fs.readFileSync(path.join(root,'axis-review.json')));assert.equal(r.records.length,239);
const stats=r.records.reduce((a,x)=>(a[x.check.status]=(a[x.check.status]??0)+1,a),{});
const summary={sourceSha256:map.sourceSha256,steps:239,parts:242,files:m.files.length,bytes:config.totalBytes,primitives,embeddedTextureReferences:textures,
originalCompressedGeometry:'byte-identical',originalMaterialsAndTextureBytes:'identical',originalFinalPoses:'identical except quaternion normalization',
checkedSteps:r.records.length,translationChecks:stats,collisionCertified:false,specificationDifferences:map.mismatches.length};
fs.writeFileSync(path.join(root,'validation.json'),JSON.stringify(summary,null,2)+'\n');console.log(JSON.stringify(summary,null,2));