import assert from 'node:assert/strict'; import {readFileSync,writeFileSync,mkdirSync,mkdtempSync,copyFileSync} from 'node:fs'; import {join,relative,dirname} from 'node:path'; import {hashAt} from './boot-contract-report.mjs'; import {inspectK1080WaitMainHost} from './k1080-wait-main-host-report.mjs'; const root=process.cwd();assert.equal(process.argv.length,3);const path=process.argv[2],original=JSON.parse(readFileSync(join(root,path),'utf8'));const positive=inspectK1080WaitMainHost(original,root); const dir=mkdtempSync(join(root,'reports/os-implementation/K1080-wait-main-preparation-damaged-')),run=relative(root,dir),base=JSON.parse(JSON.stringify(original).split(original.runDirectory).join(run)); const artifacts=[...original.sources,...original.commands.map(c=>c.log),...original.mutants.flatMap(m=>[...m.sources,m.log])]; for(const a of artifacts){const dst=join(root,a.path.replace(original.runDirectory,run));mkdirSync(dirname(dst),{recursive:true});copyFileSync(join(root,a.path),dst);} assert.deepEqual(inspectK1080WaitMainHost(base,root),positive); const controls=[['schema',r=>r.schema='other'],['numeric',r=>r.numericAccepted=true],['guest',r=>r.guestAccepted=true],['scope',r=>r.measurementScope='ACTUAL'],['elf',r=>r.kernelElfBound=true],['arm',r=>r.arm64Status='RUN'],['status',r=>r.status='ACCEPTED'],['tests',r=>r.hostTests--],['pending',r=>r.pending=[]],['moves',r=>r.localMoves=[1080]],['boundary',r=>r.boundary='measured'],['anchor',r=>r.anchor.sha256='0'.repeat(64)],['anchor-result',r=>r.anchor.result.hostTests++],['inputs',r=>r.inputsBefore[0].sha256='0'.repeat(64)],['inputs-after',r=>r.inputsAfter=[]],['protected',r=>r.protectedAfter=[]],['no-command',r=>r.commands.pop()],['false-exit',r=>r.commands[0].exitCode=1],['wrong-argv',r=>r.commands[0].argv.pop()],['wrong-target',r=>r.commands[0].targetDirectory+='/x'],['no-mutant',r=>r.mutants.pop()],['surviving-mutant',r=>r.mutants[0].exitCode=0],['wrong-mutant-test',r=>r.mutants[0].test='other'],['mutant-compile-failure',r=>{const a=r.mutants[0].log;writeFileSync(join(root,a.path),'error[E0001]: failed\ncould not compile\n');a.sha256=hashAt(root,a.path);}],['no-source',r=>r.sources.pop()],['source-order',r=>r.sources.reverse()],['floating-source',r=>r.sources[0].path=original.sources[0].path],['floating-log',r=>r.commands[0].log.path=original.commands[0].log.path],['source-traversal',r=>r.sources[0].path=run+'/sources/../'+r.sources[0].originalPath],['rehashed-source',r=>{const a=r.sources.find(a=>a.originalPath==='os-k1080-wait-main/src/bridge.rs');writeFileSync(join(root,a.path),readFileSync(join(root,a.path))+'\n// altered whole source\n');a.sha256=hashAt(root,a.path);}],['rehashed-log',r=>{const a=r.commands[0].log;writeFileSync(join(root,a.path),'running 33 tests\ntest result: ok. 33 passed; 0 failed\n');a.sha256=hashAt(root,a.path);}],['empty-sources',r=>r.sources=[]]]; const results=[];for(const[id,edit]of controls){const r=structuredClone(base),backups=new Map();for(const a of[...r.sources,...r.commands.map(c=>c.log),...r.mutants.flatMap(m=>[...m.sources,m.log])])backups.set(a.path,readFileSync(join(root,a.path)));try{edit(r);assert.throws(()=>inspectK1080WaitMainHost(r,root),undefined,id);results.push({id,status:'REJECTED'});}finally{for(const[p,b]of backups)writeFileSync(join(root,p),b);}} assert.deepEqual(inspectK1080WaitMainHost(base,root),positive);const report={schema:'turkos-k1080-wait-main-damaged/v1',status:'ALL_REJECTED',source:{path,sha256:hashAt(root,path)},positive,mirror:{path:run+'/mirror.json',sha256:''},controls:results};writeFileSync(join(dir,'mirror.json'),JSON.stringify(base,null,2)+'\n');report.mirror.sha256=hashAt(root,run+'/mirror.json');writeFileSync(join(dir,'report.json'),JSON.stringify(report,null,2)+'\n');console.log('K1080 MAIN DAMAGED '+results.length+' '+run+'/report.json');