import {resolve} from 'node:path'; import {mkdir,mkdtemp,writeFile} from 'node:fs/promises'; const root=resolve(import.meta.dir,'..'); if(process.argv.includes('--check')){console.log('Isolated integration runner parsed without accessing the stand');process.exit(0);} const parent=resolve(root,'evidence/reconciliation');await mkdir(parent,{recursive:true}); const evidence=await mkdtemp(resolve(parent,'run-'));const runID=crypto.randomUUID(); const project='vp-lab4-integration-'+runID.slice(0,8); await writeFile(resolve(evidence,'run.json'),JSON.stringify({run_id:runID,project,started_at:new Date().toISOString(),state:'started'})+'\n',{flag:'wx'}); let stage='preflight',created=false,primary:unknown;const cleanupErrors:string[]=[]; async function command(args:string[]){const p=Bun.spawn(['docker','compose','-f',resolve(root,'compose.yaml'),'-p',project,...args],{cwd:root,stdout:'pipe',stderr:'pipe',signal:AbortSignal.timeout(180000)});const [out,err]=await Promise.all([new Response(p.stdout).text(),new Response(p.stderr).text()]);await writeFile(resolve(evidence,stage+'.txt'),out+err);if(await p.exited!==0)throw new Error(stage+' failed');return out;} try{ if((await command(['ps','-aq'])).trim())throw new Error('Fresh project unexpectedly exists'); stage='dependencies';created=true;await command(['up','-d','--wait','postgres','s3']); stage='migrations';await command(['run','--rm','--no-deps','migrate']); stage='bucket';await command(['run','--rm','--no-deps','bootstrap-s3']); stage='tests';const out=await command(['run','--rm','--no-deps','integration']); for(const name of ['TestReconciliationAgainstPostgresAndPrivateS3','TestS3InventoryAndStatReturnComparableVersionMetadata','TestFixtureCleanupRejectsForeignMembership'])if(!out.includes('--- PASS: '+name))throw new Error('Required integration test did not pass'); }catch(error){primary=error;} finally{if(created){stage='cleanup';try{await command(['down','--volumes']);}catch{cleanupErrors.push('isolated_project');}}} await writeFile(resolve(evidence,'result.json'),JSON.stringify({run_id:runID,complete:!primary&&!cleanupErrors.length,primary:primary instanceof Error?primary.name:primary?'unknown':null,cleanup_errors:cleanupErrors},null,2)+'\n',{flag:'wx'}); if(primary||cleanupErrors.length)throw new Error('Isolated integration failed; inspect this run'); console.log('PASS isolated S3/PostgreSQL reconciliation: '+evidence);