import fs from 'node:fs'; import path from 'node:path'; import crypto from 'node:crypto'; import {spawnSync} from 'node:child_process'; const state=JSON.parse(fs.readFileSync('C:/laragon/data/sgf-tempo/run/modular-regression-current.json','utf8')); if(!/^[a-f0-9]{12}$/.test(state.id)||path.resolve(state.root)!==path.resolve('C:/laragon/data/sgf-tempo/run/modular-regression-'+state.id)||state.port!==55439)throw Error('Unexpected regression cluster'); const resume=process.argv[2];if(resume&&!/^[a-f0-9]{12}$/.test(resume))throw Error('Unexpected resume identifier'); const tag=resume??crypto.randomBytes(6).toString('hex'),root=path.join(state.root,'private','stock-composition-'+tag),app=path.join(root,'app'); const source=path.resolve('../compras'),php='C:/laragon/tools/sgf-tempo/php-8.4.27/php.exe',psql='C:/Program Files/PostgreSQL/17/bin/psql.exe'; fs.mkdirSync(app,{recursive:true}); const password=crypto.randomBytes(32).toString('hex'),database='stock_read_compras_'+tag,role='stock_read_qa_'+tag; const admin=fs.readFileSync(path.join(state.root,'private','admin-password.txt'),'utf8').trim(); if(!resume){ const identity=spawnSync(psql,['-h','127.0.0.1','-p','55439','-U','sgf_regression_owner','-d','postgres','-At','-v','ON_ERROR_STOP=1'],{ encoding:'utf8',windowsHide:true,env:{...process.env,PGPASSWORD:admin},input:"SELECT current_setting('cluster_name');"}); if(identity.status!==0||identity.stdout.trim()!=='sgf_modular_regression_'+state.id)throw Error('Refused to provision outside the expected QA cluster'); const pg=spawnSync(psql,['-h','127.0.0.1','-p','55439','-U','sgf_regression_owner','-d','postgres','-v','ON_ERROR_STOP=1'],{ encoding:'utf8',windowsHide:true,env:{...process.env,PGPASSWORD:admin},input:`CREATE ROLE ${role} LOGIN PASSWORD '${password}' NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS; CREATE DATABASE ${database} OWNER ${role}; REVOKE ALL ON DATABASE ${database} FROM PUBLIC; `}); if(pg.status!==0)throw Error('Isolated native database provisioning failed; reconcile private directory without retrying this fixture'); fs.writeFileSync(path.join(root,'state.json'),JSON.stringify({tag,root,app,database,role,port:55439,cluster:'sgf_modular_regression_'+state.id},null,2)); }else{ const old=JSON.parse(fs.readFileSync(path.join(root,'state.json'),'utf8')); if(old.database!==database||old.role!==role||path.resolve(old.app)!==path.resolve(app))throw Error('Unexpected private resume state'); } for(const dir of ['app','bootstrap','config','routes','resources','tests'])fs.cpSync(path.join(source,dir),path.join(app,dir),{recursive:true,filter:p=>!p.includes('bootstrap'+path.sep+'cache')}); for(const pkg of ['inventory-read','purchasing','budget-control','payables'])fs.cpSync(path.join(source,'packages',pkg),path.join(app,'packages',pkg),{recursive:true}); fs.copyFileSync(path.join(source,'composer.json'),path.join(app,'composer.json')); if(!fs.existsSync(path.join(app,'vendor')))fs.symlinkSync(path.join(source,'vendor'),path.join(app,'vendor'),'junction'); for(const dir of ['bootstrap/cache','storage/logs','storage/framework/cache/data','storage/framework/sessions','storage/framework/views'])fs.mkdirSync(path.join(app,dir),{recursive:true}); const key='base64:'+crypto.randomBytes(32).toString('base64'); if(!resume)fs.writeFileSync(path.join(app,'.env.testing'),`APP_ENV=testing\nAPP_KEY=${key}\nAPP_DEBUG=false\nAPP_URL=http://localhost\nDB_CONNECTION=pgsql\nDB_HOST=127.0.0.1\nDB_PORT=55439\nDB_DATABASE=${database}\nDB_USERNAME=${role}\nDB_PASSWORD=${password}\nDB_URL=\nSESSION_DRIVER=array\nCACHE_STORE=array\nQUEUE_CONNECTION=sync\nBCRYPT_ROUNDS=4\nSGF_ISOLATED_REGRESSION_RUN=${state.id}\nCOMMON_PAYABLES_ENABLED=false\nCOMMON_PAYABLES_HISTORY_ENABLED=false\nPAYABLE_SOURCE_INSTANCE=\n`); const config=path.join(root,'phpunit.xml'); fs.writeFileSync(config,``); console.log(JSON.stringify({prepared_database:database,restricted_role:role,native_application_copy:true,no_existing_database_migration:true})); const output=path.join(root,'native-results.json'); const r=spawnSync(php,[path.join(source,'vendor/phpunit/phpunit/phpunit'),'-c',config,path.join(app,'tests/Feature/CommonStockReadTest.php'),'--log-junit',path.join(root,'native-junit.xml')],{ cwd:app,stdio:'inherit',windowsHide:true,env:{...process.env,APP_BASE_PATH:app,APP_ENV:'testing',SGF_ISOLATED_REGRESSION_RUN:state.id,STOCK_READ_COMPOSITION_ROOT:root,STOCK_READ_COMPOSITION_OUTPUT:output}}); console.log(JSON.stringify({exit_code:r.status,root,native_results:fs.existsSync(output)}));process.exitCode=r.status??1;