import assert from 'node:assert/strict'; import {spawnSync} from 'node:child_process'; import {mkdirSync,mkdtempSync,writeFileSync} from 'node:fs'; import {join,relative,dirname} from 'node:path'; import {hashAt,frozenSnapshot} from './boot-contract-report.mjs'; import {waitServicesInputs,waitServicesSources,waitServicesMutants,waitServicesBoundary,waitServicesAnchors,inspectWaitGuestServicesHost} from './wait-guest-services-host-report.mjs'; import {waitGuestServicesRust} from './wait-guest-services-sources.mjs'; const root=process.cwd();assert.equal(process.argv.length,2);const protectedBefore=frozenSnapshot(root),inputsBefore=waitServicesInputs(root),anchors=waitServicesAnchors(root),directory=mkdtempSync(join(root,'reports/os-implementation/K1080-wait-services-preparation-')),runDirectory=relative(root,directory),clean=s=>s.split(root).join(''),artifact=p=>({path:relative(root,p),sha256:hashAt(root,relative(root,p))}); function source(name,spec=null){const dest=join(directory,name,'sources');const map=waitServicesSources(root,spec);for(const[p,s]of map){const target=join(dest,p);mkdirSync(dirname(target),{recursive:true});writeFileSync(target,s);}return{dest,sources:[...map.keys()].map(originalPath=>({originalPath,...artifact(join(dest,originalPath))}))};} const initial=source('.');function command(id,argv,executable='cargo'){console.log('WAIT SERVICES '+id);const env={...process.env,CARGO_TARGET_DIR:join(directory,'target')};delete env.RUSTFLAGS;delete env.CARGO_ENCODED_RUSTFLAGS;const r=spawnSync(executable,argv,{cwd:root,env,encoding:'utf8',timeout:120000,maxBuffer:8*1024*1024});assert.equal(r.error,undefined);const p=join(directory,id+'.log');writeFileSync(p,clean((r.stdout??'')+(r.stderr??'')));return{id,executable,argv:argv.map(clean),targetDirectory:clean(env.CARGO_TARGET_DIR),exitCode:r.status,log:artifact(p)};} const args=dest=>['--manifest-path',join(dest,'os-wait-guest-services/Cargo.toml'),'--locked','--offline']; const commands=[command('tests',['test',...args(initial.dest),'--test','services','--target','aarch64-apple-darwin','--','--test-threads=1']),command('arm64',['check',...args(initial.dest),'--lib','--target','aarch64-unknown-none']),command('format',['--config','skip_children=true','--edition','2024','--check',...waitGuestServicesRust.map(p=>join(initial.dest,'bound-inputs',p))],'rustfmt'),command('clippy',['clippy',...args(initial.dest),'--lib','--test','services','--target','aarch64-apple-darwin','--','-D','warnings'])];for(const c of commands)assert.equal(c.exitCode,0,c.log.path); const mutants=[];for(const spec of waitServicesMutants){const branch=source('mutant-'+spec.id,spec),c=command('mutant-'+spec.id,['test',...args(branch.dest),'--test','services','--target','aarch64-apple-darwin',spec.test,'--','--exact','--test-threads=1']);assert.equal(c.exitCode,101,c.log.path);mutants.push({...spec,...c,id:spec.id,sources:branch.sources,source:artifact(join(branch.dest,'bound-inputs',spec.path))});} const report={schema:'turkos-wait-guest-services-host/v1',status:'HOST_PREPARED',measurementScope:'MODELED_ONLY',guestAccepted:false,kernelElfBound:false,arm64Status:'CHECK_ONLY',hostTests:22,boundary:waitServicesBoundary,localMoves:[],pending:[1065,1070,1075,1080],anchors,runDirectory,createdAt:new Date().toISOString(),inputsBefore,inputsAfter:waitServicesInputs(root),protectedBefore,protectedAfter:frozenSnapshot(root),sources:initial.sources,commands,mutants};const result=inspectWaitGuestServicesHost(report,root),p=join(directory,'report.json');writeFileSync(p,JSON.stringify(report,null,2)+'\n');console.log('WAIT SERVICES PREPARED '+JSON.stringify(result)+' '+relative(root,p));