// Generic K2101+ host preparation: the feature crate's full test target, the // roadmap's named positive/negative tests (each must run exactly one test), // an aarch64-unknown-none library check and source mutants that must make a // named test fail. Sources are copied first; every command runs on the copy. import assert from 'node:assert/strict'; import {copyFileSync,cpSync,mkdirSync,mkdtempSync,readFileSync,readdirSync,rmSync,writeFileSync} from 'node:fs'; import {dirname,join,relative} from 'node:path'; import {spawnSync} from 'node:child_process'; import {hashAt,frozenSnapshot} from './boot-contract-report.mjs'; import {buildOsPlan} from '../os-roadmap.mjs'; assert.equal(process.argv.length,3,'usage: node feature-preparation.mjs '); const root=process.cwd(),specPath=relative(root,process.argv[2]),spec=JSON.parse(readFileSync(specPath,'utf8')); assert.match(spec.milestone,/^K2[1-5]\d\d$/);assert.match(spec.slug,/^[a-z0-9-]+$/);assert.match(spec.crate,/^os-[a-z0-9-]+$/); const guest=buildOsPlan().moves.find(m=>m.id===spec.milestone); assert.equal(guest?.stage,'integration',`${spec.milestone} must be a guest-acceptance move`); assert.equal(spec.feature,guest.feature); const positive=guest.testPlan.positiveName,negative=guest.testPlan.negativeName; const directory=mkdtempSync(`reports/os-implementation/${spec.milestone}-${spec.slug}-staging-local-`),source=join(directory,'source'); const artifact=p=>({path:relative(root,p),sha256:hashAt(root,relative(root,p))}); const report={schema:'turkos-feature-preparation/v1',milestone:spec.milestone,feature:spec.feature,status:'RUNNING',guestAccepted:false,productAccepted:false, positiveName:positive,negativeName:negative,sources:[],commands:[],mutants:[],frozenBefore:frozenSnapshot(root)}; copyFileSync(specPath,join(directory,'spec.json'));report.spec=artifact(join(directory,'spec.json')); copyFileSync('scripts/os-acceptance/feature-preparation.mjs',join(directory,'harness.mjs.txt'));report.harness=artifact(join(directory,'harness.mjs.txt')); function copy(path){const to=join(source,path);mkdirSync(dirname(to),{recursive:true});copyFileSync(path,to);report.sources.push(artifact(to));} function walk(path){for(const e of readdirSync(path,{withFileTypes:true})){const child=join(path,e.name);if(e.isDirectory())walk(child);else{assert.ok(e.isFile(),child);copy(child);}}} for(const p of[`${spec.crate}/Cargo.toml`,`${spec.crate}/Cargo.lock`,spec.contract])copy(p);walk(`${spec.crate}/src`);walk(`${spec.crate}/tests`); const env={...process.env,CARGO_BUILD_JOBS:'4'};delete env.RUSTFLAGS;delete env.CARGO_ENCODED_RUSTFLAGS; function run(name,args,expected,target){const r=spawnSync('nice',['-n','10','cargo',...args],{cwd:root,env:{...env,CARGO_TARGET_DIR:join(root,target)},encoding:'utf8',timeout:900000,maxBuffer:32*1024*1024}),log=join(directory,name+'.log'); writeFileSync(log,(r.stdout??'')+(r.stderr??''));report.commands.push({name,args,exitCode:r.status,log:artifact(log)});assert.ifError(r.error);assert.equal(r.signal,null,name);assert.equal(r.status,expected,name);return readFileSync(log,'utf8');} const targets=[]; try{ const manifest=join(source,spec.crate,'Cargo.toml'),target=join(directory,'target');targets.push(target); const all=run('host-tests',['test','--manifest-path',manifest,'--locked','--offline','--test',spec.test],0,target); assert.match(all,new RegExp(`test result: ok\\. ${spec.tests} passed; 0 failed`),'expected test count'); for(const[name,test]of[['positive',positive],['negative',negative]]) assert.match(run(name,['test','--manifest-path',manifest,'--locked','--offline','--test',spec.test,test,'--','--exact'],0,target),/test result: ok\. 1 passed; 0 failed/,name); run('target',['check','--manifest-path',manifest,'--locked','--offline','--lib','--target','aarch64-unknown-none'],0,target); const names=new Set(); for(const m of spec.mutants){ assert.match(m.name,/^[a-z0-9-]+$/);assert.ok(!names.has(m.name),m.name);names.add(m.name); const dir=join(directory,'mutant-'+m.name);cpSync(source,dir,{recursive:true}); const path=join(dir,spec.crate,'src',m.file),text=readFileSync(path,'utf8'); assert.equal(text.split(m.before).length,(m.count??1)+1,`mutant ${m.name} anchor count`);writeFileSync(path,text.replaceAll(m.before,m.after)); const mutantTarget=join(directory,'target-mutant');targets.push(mutantTarget); const out=run('mutant-'+m.name,['test','--manifest-path',join(dir,spec.crate,'Cargo.toml'),'--locked','--offline','--test',spec.test,m.test,'--','--exact'],101,mutantTarget); assert.match(out,new RegExp(`${m.test} \\.\\.\\. FAILED`),m.name);report.mutants.push({name:m.name,test:m.test,source:artifact(path)}); } assert.ok(report.mutants.length>=spec.minimumMutants,'too few source mutants'); report.status='STAGING_LOCAL_PASSED';report.tests=spec.tests;report.mutationsRejected=report.mutants.length; }catch(error){report.status='STAGING_TEST_FAILED';report.error=error.stack?.slice(0,6000);process.exitCode=1;} // Build outputs are not evidence; logs and copied sources are. Free the disk. for(const t of new Set(targets))rmSync(join(root,t),{recursive:true,force:true});report.buildOutputsRemoved=true; report.frozenAfter=frozenSnapshot(root);assert.deepEqual(report.frozenAfter,report.frozenBefore); writeFileSync(join(directory,'report.json'),JSON.stringify(report,null,2)+'\n'); console.log(JSON.stringify({status:report.status,report:join(directory,'report.json'),tests:report.tests,mutationsRejected:report.mutationsRejected,error:report.error?.slice(0,800)}));