Files
SGF/scripts/finance-bundle-boot-check.mjs
2026-11-04 09:37:20 +02:00

12 lines
2.5 KiB
JavaScript

import fs from 'node:fs';import path from 'node:path';import crypto from 'node:crypto';import {spawnSync} from 'node:child_process';
const read=p=>JSON.parse(fs.readFileSync(p,'utf8')),state=read('C:/laragon/data/sgf-tempo/run/modular-regression-current.json'),tag=process.argv[2];if(state.port!==55439||!/^[a-f0-9]{12}$/.test(tag??''))throw Error('Owned native fixture required');
const root=path.join(state.root,'private/ledger-native-'+tag),settings=read(path.join(root,'settings.json')),artifact=read(path.join(settings.common_bundle,'manifest.json')),testRoot=path.join(root,'bundle-boot-'+crypto.randomBytes(6).toString('hex'));fs.mkdirSync(testRoot);let checks=0;
for(const [name,expected] of [['modified','FINANCE_BUNDLE_FILE_MISMATCH'],['extra','FINANCE_BUNDLE_EXTRA_OR_MISSING_FILE'],['pin','FINANCE_BUNDLE_MANIFEST_MISMATCH'],['loader','FINANCE_BUNDLE_LOADER_MISMATCH']]){
const dir=path.join(testRoot,name);fs.mkdirSync(dir);for(const f of [...artifact.files.map(f=>f.file),'manifest.json']){const to=path.join(dir,f);fs.mkdirSync(path.dirname(to),{recursive:true});fs.copyFileSync(path.join(settings.common_bundle,f),to);}
if(name==='modified')fs.appendFileSync(path.join(dir,'payables/bootstrap.php'),'\n// QA altered content\n');if(name==='extra')fs.writeFileSync(path.join(dir,'unexpected.txt'),'QA');
const marker=path.join(dir,'executed.txt');if(name==='loader')fs.writeFileSync(path.join(dir,'Bundle.php'),'<?php file_put_contents('+JSON.stringify(marker.replaceAll('\\','/'))+', "should never run");');
const cfg=path.join(testRoot,name+'-settings.json');fs.writeFileSync(cfg,JSON.stringify({...settings,common_bundle:dir,common_bundle_sha256:name==='pin'?'0'.repeat(64):settings.common_bundle_sha256}));
const r=spawnSync('C:/laragon/tools/sgf-tempo/php-8.4.27/php.exe',['scripts/payable-history-sgf-proof.php',cfg,'native-cycle-bundle'],{encoding:'utf8',windowsHide:true,maxBuffer:1024*1024});const log=(r.stdout??'')+(r.stderr??'');fs.writeFileSync(path.join(testRoot,name+'.log'),log);if(!log.includes(expected)||fs.existsSync(marker))throw Error('Native boot did not fail before execution: '+name);checks++;
}
const result={passed:true,checks,refused_before_business_boot:['modified','extra','wrong_pin','modified_loader'],loader_marker_never_executed:true,no_fallback:true,original_proof_preserved:true};fs.writeFileSync(path.join(testRoot,'proof.json'),JSON.stringify(result,null,2)+'\n');fs.writeFileSync(path.join(state.root,'finance-bundle-boot-latest.json'),JSON.stringify({root:testRoot,...result},null,2)+'\n');console.log(JSON.stringify(result));