// Validate freshly prepared upload assets with the supplied latest Sandbox schema // and the community assembly schema. This does not imply browser simulation tests. import {readFile,writeFile,readdir,mkdtemp,rm} from 'node:fs/promises'; import {join,resolve,dirname} from 'node:path'; import {tmpdir} from 'node:os'; import {createRequire} from 'node:module'; import {createHash} from 'node:crypto'; import assert from 'node:assert/strict'; import {build} from 'esbuild'; import {validatePackage as validateAssembly,validateEmbeddedGlb} from '../js/experience-packages.js'; import {resourceIndex, validateRobotResources, bindAssemblyFiles} from '../js/project-resources.js'; const repo=new URL('..',import.meta.url).pathname; const root=resolve(process.argv[2]??'/Users/jerry/Downloads/三个机器人-后台上传-20261030'); const source=join(repo,'sandbox/src/platform'); const temp=await mkdtemp(join(tmpdir(),'make-upload-validators-')); const readJSON=async path=>JSON.parse(await readFile(path,'utf8')); const hash=b=>createHash('sha256').update(b).digest('hex'); const arrayBuffer=b=>b.buffer.slice(b.byteOffset,b.byteOffset+b.byteLength); try{ const compiled=await build({stdin:{contents:` export {validatePackage as validateRobot,validateWorld} from ${JSON.stringify(source+'/loader/SchemaValidator.js')}; export {JointMotionReference} from ${JSON.stringify(source+'/policy/JointMotionReference.js')}; export {validateBodyGlb} from ${JSON.stringify(source+'/render/BodyVisuals.js')}; export {resolveWorldId} from ${JSON.stringify(source+'/worlds/arena.js')}; `,resolveDir:repo},bundle:true,platform:'node',format:'cjs',write:false, nodePaths:[join(repo,'sandbox/node_modules'),join(repo,'node_modules')],define:{'import.meta.env.BASE_URL':'"/"'},logLevel:'silent'}); const bundled=join(temp,'validators.cjs');await writeFile(bundled,compiled.outputFiles[0].contents); const {validateRobot,validateWorld,JointMotionReference,validateBodyGlb,resolveWorldId}=createRequire(import.meta.url)(bundled); const results=[]; for(const name of ['01-小鸭子-Microduck','02-人形机器人-Microban','03-双足机器人-RP1']){ const folder=join(root,name),id=name.endsWith('Microduck')?'microduck':name.endsWith('Microban')?'microban':'rp1',motion=join(repo,'deliverables/current-robots',id,'motion'),result={folder:name,motion:[]}; const project=(await readJSON(join(repo,'deliverables/current-robots/preview.json')))[id]; const resources=resourceIndex(project.walkingResourcesFiles); const bound=validateRobotResources(await readJSON(join(motion,'robot.json')),'robot.json',resources); assert.equal(bound.warnings?.length??0,0); for(const filename of await readdir(join(folder,'01-整机模型')))if(filename.endsWith('.glb'))validateEmbeddedGlb(arrayBuffer(await readFile(join(folder,'01-整机模型',filename)))); const entries=name.startsWith('01')?['robot.json','variants/rollers.json']:['robot.json']; for(const entry of entries){ const file=join(motion,entry),config=validateRobot(await readJSON(file)); const ref=[]; for(const skill of config.skills.filter(s=>s.reference)){ const spec=await readJSON(resolve(dirname(file),skill.reference)); const data=await readFile(resolve(dirname(file),spec.data)); const reader=new JointMotionReference(spec,arrayBuffer(data),config.joints.groups[skill.owns],config.sim.timestep*config.sim.decimation); ref.push({skill:skill.id,frames:reader.frames}); } for(const visual of config.render.body_visuals??[])validateBodyGlb(arrayBuffer(await readFile(resolve(dirname(file),visual.path)))); validateWorld(await readJSON(config.adapter==='microduck'?join(motion,'worlds',resolveWorldId(config),'world.json'):join(repo,'sandbox/robot-preview/world.json'))); result.motion.push({entry,id:config.id,skills:config.skills.map(s=>s.id),bodyVisuals:config.render.body_visuals?.length??0,references:ref}); } if(name.startsWith('01')){ const stepsFolder=join(folder,'03-拼装步骤'),files=(await readdir(stepsFolder)).filter(f=>f.endsWith('.json')); assert.equal(files.length,1,'Only one current assembly file should be offered'); const data=await readFile(join(stepsFolder,files[0])); const config=validateAssembly(JSON.parse(data),'assembly','https://assets.example/manifest.json'); for(const record of config.files.values()){ const bytes=await readFile(join(folder,'02-零件模型',record.path)); assert.equal(bytes.length,record.bytes);assert.equal(hash(bytes),record.sha256,record.path); validateEmbeddedGlb(arrayBuffer(bytes)); } for(const filename of await readdir(join(folder,'01-整机模型'))){ if(filename.endsWith('.glb'))validateEmbeddedGlb(arrayBuffer(await readFile(join(folder,'01-整机模型',filename)))); } result.assembly={file:files[0],steps:config.steps.length,instances:config.parts.length,referencedFiles:config.files.size,sha256:hash(data)}; if(name.startsWith('01')){ const latest=await readJSON(join(repo,'deliverables/experience-packages/microduck-current/manifest.json')); const delivered=JSON.parse(data); assert.deepEqual(delivered.steps,latest.steps,'Assembly order and previously confirmed axes must stay unchanged'); assert.deepEqual(delivered.tools,latest.tools,'Screw tool animation must stay unchanged'); assert.deepEqual(delivered.parts.map(({file,...p})=>p),latest.parts.map(({file,...p})=>p),'Final transforms must stay unchanged'); assert.equal(config.steps.length,239);assert.equal(config.parts.length,242);assert.equal(config.files.size,243); assert.ok(config.tools.some(t=>t.file==='内六角扳手.glb')); assert.equal(bindAssemblyFiles(config.files,resourceIndex(project.modelPartsFiles)).size,243); result.assembly.collisionCertified=false; } }else result.assembly=null; results.push(result);console.log(JSON.stringify(result)); } const report={checkedAt:new Date().toISOString(),browserPlaybackTested:false, scope:'Latest source schemas, world schemas, GLB files/material bytes, assembly references/SHA256, motion reference values/joint order/control clock',results}; await writeFile(join(root,'开发核对/前端Schema与装配校验.json'),JSON.stringify(report,null,2)+'\n'); }finally{await rm(temp,{recursive:true,force:true});}