<?php
declare(strict_types=1);require __DIR__.'/bootstrap.php';$phase=getenv('COMMENT_PHASE');$check(!is_file($runtime.'/run-'.$phase.'.json'),'No phase replay');$fixture=json_decode(file_get_contents($runtime.'/fixture-journal.json'),true);$check($fixture['committed'],'Own preparation committed');$app=commentApp($root,$runtime);$cache=new \think\cache\driver\File($app,['path'=>$runtime.'/cache/','prefix'=>'','expire'=>0]);$tokens=[];foreach($tenants as $module=>$tenant){$tokens[$module]=bin2hex(random_bytes(24));$cache->set($tokens[$module],['user'=>['store_user_id'=>$tenant,'store_id'=>$tenant,'real_name'=>$marker,'user_name'=>$marker,'is_super'=>1],'store_id'=>$tenant,'is_login'=>true],3600);}$items=[];$cases=[];
$add=static function($module,$name,$path,$params=[],$status=200,$auth=true)use(&$items,&$cases,$tokens,$base,$phase){$scenario='PILOT_RD_'.$phase.'_'.strtoupper($module.'_'.$name);$headers=[['key'=>'Host','value'=>$module.'.order-related.invalid'],['key'=>'Platform','value'=>'POC'],['key'=>'X-Pilot-Scenario','value'=>$scenario]];if($auth)$headers[]=['key'=>'Access-Token','value'=>$tokens[$module]];$cases[]=['scenario'=>$scenario,'module'=>$module,'name'=>$name,'path'=>'/'.$module.'/'.$path,'params'=>$params,'expected_status'=>$status,'auth'=>$auth];$items[]=['id'=>$scenario,'name'=>$name,'type'=>'http','metaInfo'=>['type'=>'HTTP'],'request'=>['method'=>'GET','url'=>$base.'/'.$module.'/'.$path.'?'.http_build_query($params),'header'=>$headers],'event'=>[['listen'=>'test','script'=>['type'=>'text/javascript','exec'=>["pm.test('HTTP200',function(){pm.response.to.have.status(200);});var b=pm.response.json();","pm.test('Numeric intended business status',function(){pm.expect(b.status).to.eql(".$status.");});","pm.test('Trace string',function(){pm.expect(b.trace_id).to.match(/^[0-9a-f]{13}$/);});","pm.test('Data object or array contract',function(){pm.expect(typeof b.data).to.eql('object');pm.expect(typeof b.message).to.eql('string');});"]]]]];};
foreach($tenants as $module=>$tenant){$id=$fixture['owned'][$module];$other=$fixture['owned'][$module==='store'?'terminal':'store'];$add($module,'DELIVERY_LIST','order.delivery/list');if($phase==='NA')$add($module,'DELIVERY_DETAIL','order.delivery/detail',['deliveryId'=>$id['order_delivery']]);$add($module,'REFUND_LIST','order.refund/list');if($phase!=='GREEN')$add($module,'REFUND_DETAIL','order.refund/detail',['orderRefundId'=>$id['order_refund']]);if($phase==='NA'){$add($module,'AUTH','order.delivery/list',[],401,false);$add($module,'CROSS_REFUND_DETAIL','order.refund/detail',['orderRefundId'=>$other['order_refund']],500);}}
$persist('cases-'.$phase.'.json',$cases);$persist('private-collection-'.$phase.'.json',['apifoxCli'=>'2.2.8','info'=>['name'=>'Delivery和Refund八GET真实双库读取'],'item'=>[['name'=>'只读发货记录与已拒绝换货样本','item'=>$items]]]);$before=$own();$dir=$runtime.'/native-'.$phase;mkdir($dir,0700);$p=proc_open(['node',getenv('COMMENT_CLI'),'run',$runtime.'/private-collection-'.$phase.'.json','-r','json','--out-dir',$dir,'--out-file','result','--on-error','continue','--timeout-request','30000','--silent'],[0=>['file','/dev/null','r'],1=>['file',$runtime.'/private-cli.log','a'],2=>['file',$runtime.'/private-cli.log','a']],$pipes);$exit=proc_close($p);$stats=[];foreach(glob($dir.'/*.json') as $f){$r=json_decode(file_get_contents($f),true);if(isset($r['run']))$stats[]=['stats'=>$r['run']['stats'],'failures'=>count($r['run']['failures']),'sha256'=>hash_file('sha256',$f)];}foreach($tokens as $token)$cache->delete($token);$cache->clear();$out=['lane'=>$lane,'phase'=>$phase,'native_exit'=>$exit,'stats'=>$stats,'before'=>$before,'after'=>$own(),'own_unchanged'=>$before===$own(),'natural_count_unchanged'=>$protect()===$fixture['before_protect'],'cache_cleared'=>true];$persist('run-'.$phase.'.json',$out);echo json_encode(['lane'=>$lane,'phase'=>$phase,'native_exit'=>$exit,'stats'=>$stats,'own_unchanged'=>$out['own_unchanged']]),"\n";
