import fs from 'node:fs'; import path from 'node:path'; import assert from 'node:assert/strict'; import { execFileSync, spawnSync } from 'node:child_process'; import { gitEnvironment } from '../../src/core/git-process.mjs'; import { sha } from '../live-ad/common.mjs'; export const python = path.resolve('.qa/reliability-1.25.2/python-env/Scripts/python.exe'); export const specs = { react: { baseline:'benchmarks/react-delivery-v1/baseline',allowed:['src/query.ts','src/summary.ts','src/transition.ts','src/App.tsx'],tests:9 }, python: { baseline:'benchmarks/python-delivery-v2/baseline',allowed:['normalize.py','summary.py','render.py'],tests:10 }, }; export function prepareProject(kind, output, save) { const spec = specs[kind], sourceRoot=path.join(output,'project'); assert.ok(spec); assert.equal(fs.existsSync(sourceRoot),false); fs.mkdirSync(output,{recursive:true}); fs.cpSync(spec.baseline,sourceRoot,{recursive:true,errorOnExist:true}); const git = args=>execFileSync('git',['--no-pager','--no-optional-locks','-C',sourceRoot,...args],{encoding:'utf8',windowsHide:true,env:gitEnvironment()}); const check = (name, executable, args, prefix)=>{ const execution=spawnSync(executable,args,{cwd:sourceRoot,encoding:'utf8',windowsHide:true,env:gitEnvironment(),maxBuffer:32*1024*1024}); save(prefix+'/'+name+'.txt',(execution.stdout??'')+(execution.stderr??'')); let stats=null; if(name==='test'&&kind==='react') stats=Object.fromEntries(['tests','pass','fail','cancelled','skipped','todo'].map(key=>[key,Number(execution.stdout?.match(new RegExp('^# '+key+' (\\d+)$','m'))?.[1]??NaN)])); if(name==='test'&&kind==='python') { const report=path.join(sourceRoot,args.at(-1).slice('--junitxml='.length)); if(fs.existsSync(report)){const xml=fs.readFileSync(report,'utf8');save(prefix+'/pytest.xml',xml);const tag=xml.match(/]*>/)?.[0]??''; const attr=key=>Number(tag.match(new RegExp('\\b'+key+'="(\\d+)"'))?.[1]??NaN);stats={tests:attr('tests'),fail:attr('failures')+attr('errors'),skipped:attr('skipped')};stats.pass=stats.tests-stats.fail-stats.skipped;} } return {status:execution.status,signal:execution.signal,error:execution.error?.message??null,stats,stdoutSha256:sha(execution.stdout??''),stderrSha256:sha(execution.stderr??'')}; }; if(!fs.existsSync(path.join(sourceRoot,'.gitignore')))fs.writeFileSync(path.join(sourceRoot,'.gitignore'),'node_modules/\ndist/\n.awb-*\n__pycache__/\n.pytest_cache/\n',{flag:'wx'}); for(const name of ['owner-staged.txt','owner-unstaged.txt'])fs.writeFileSync(path.join(sourceRoot,name),'Owner baseline\n',{flag:'wx'}); if(kind==='react'){ const npm=path.join(path.dirname(process.execPath),'node_modules/npm/bin/npm-cli.js'); const install=check('npm-ci',process.execPath,[npm,'ci','--offline','--ignore-scripts','--no-audit','--no-fund'],'prepare');assert.equal(install.status,0,'Offline dependency preparation failed before any model request.'); } git(['init','--initial-branch=main']);git(['config','core.autocrlf','false']);git(['config','user.name','Workbench QA']);git(['config','user.email','qa@workbench.invalid']); fs.appendFileSync(path.join(sourceRoot,'.git/info/exclude'),'\n.awb-*\n__pycache__/\n.pytest_cache/\n'); git(['add','--','.']);git(['commit','--no-gpg-sign','-m','Seal AE '+kind+' original baseline']);const initialHead=git(['rev-parse','HEAD']).trim(); fs.writeFileSync(path.join(sourceRoot,'owner-staged.txt'),'Owner staged change\n');git(['add','--','owner-staged.txt']); fs.writeFileSync(path.join(sourceRoot,'owner-staged.txt'),'Owner unstaged over original staging\n');fs.writeFileSync(path.join(sourceRoot,'owner-unstaged.txt'),'Owner unstaged change\n');fs.writeFileSync(path.join(sourceRoot,'owner-untracked.txt'),'Owner untracked change\n',{flag:'wx'}); const all=git(['ls-files','-z']).split('\0').filter(Boolean),protectedNames=all.filter(name=>!spec.allowed.includes(name)).concat('owner-untracked.txt'); const protectedBytes=Object.fromEntries(protectedNames.map(name=>[name,fs.readFileSync(path.join(sourceRoot,name))])); const index=git(['ls-files','--stage','--',...protectedNames]); const protect=()=>{for(const[name,bytes]of Object.entries(protectedBytes))assert.deepEqual(fs.readFileSync(path.join(sourceRoot,name)),bytes,'Protected '+name);assert.equal(git(['ls-files','--stage','--',...protectedNames]),index,'Protected original index');}; const checks=prefix=>{ const result=kind==='react'?{ test:check('test',process.execPath,['--test','--test-reporter=tap','test/domain.test.mjs'],prefix), typecheck:check('typecheck',process.execPath,['node_modules/typescript/bin/tsc','--noEmit'],prefix), build:check('build',process.execPath,['node_modules/vite/bin/vite.js','build'],prefix), }:{test:check('test',python,['-m','pytest','-q','--junitxml=.awb-qa-'+prefix.replaceAll('/','-')+'.xml'],prefix),build:check('build',python,['-m','compileall','-q',...spec.allowed],prefix)}; save(prefix+'/result.json',result);protect();return result; }; save('source-before.json',{initialHead,status:git(['status','--porcelain=v1']),protectedIndex:index,files:Object.fromEntries([...all,'owner-untracked.txt'].map(name=>[name,sha(fs.readFileSync(path.join(sourceRoot,name)))]))}); return {spec,sourceRoot,git,initialHead,protect,checks}; }