<?php
declare(strict_types=1);require __DIR__.'/bootstrap.php';$phase=getenv('COMMENT_PHASE');$check(in_array($phase,['ADD','READ','EDIT','DELETE'],true),'Frozen phase');$check(!is_file($runtime.'/run-'.$phase.'.json'),'No successful phase replay');$fixture=json_decode(file_get_contents($runtime.'/fixture-journal.json'),true);$check($fixture['committed'],'Own preparation committed');$template=json_decode(file_get_contents(__DIR__.'/fixture-values-template.json'),true);$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,$method,$path,$params=[],$form=null,$status=200,$auth=true)use(&$items,&$cases,$tokens,$base,$phase){$scenario='PILOT_GP_'.$phase.'_'.strtoupper($module.'_'.$name);$headers=[['key'=>'Host','value'=>$module.'.goods-printer.invalid'],['key'=>'Platform','value'=>'POC'],['key'=>'X-Pilot-Scenario','value'=>$scenario]];if($auth)$headers[]=['key'=>'Access-Token','value'=>$tokens[$module]];$request=['method'=>$method,'url'=>$base.'/'.$module.'/'.$path.'?'.http_build_query($params),'header'=>$headers];if($method==='POST'){$headers[]=['key'=>'Content-Type','value'=>'application/json'];$request['header']=$headers;$request['body']=['mode'=>'raw','raw'=>json_encode($form===null?[]:['form'=>$form]),'options'=>['raw'=>['language'=>'json']]];}$cases[]=['scenario'=>$scenario,'module'=>$module,'name'=>$name,'method'=>$method,'path'=>'/'.$module.'/'.$path,'params'=>$params,'form'=>$form,'expected_status'=>$status,'auth'=>$auth];$items[]=['id'=>$scenario,'name'=>$name,'type'=>'http','metaInfo'=>['type'=>'HTTP'],'request'=>$request,'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/message types',function(){pm.expect(typeof b.data).to.eql('object');pm.expect(typeof b.message).to.eql('string');});"]]]]];};
if($phase==='READ'){$g=$fixture['owned']['terminal']['goods'];$other=$fixture['owned']['store']['goods'];$add('terminal','GOODS_LIST','GET','Goods/list',['search'=>'CDXCM11F782_terminal']);$add('terminal','GOODS_LIST_IDS','GET','Goods/listByIds',['goodsIds'=>[$g]]);$add('terminal','GOODS_DETAIL','GET','Goods/detail',['goodsId'=>$g]);$add('terminal','GOODS_BASIC','GET','Goods/basic',['goodsId'=>$g]);foreach($tenants as $module=>$tenant){$add($module,'PRINTER_LIST','GET','setting.Printer/list',['search'=>$marker,'printer_type'=>'FEI_E_YUN']);$add($module,'PRINTER_ALL','GET','setting.Printer/all');}$add('terminal','CROSS_GOODS','GET','Goods/detail',['goodsId'=>$other],null,500);$add('terminal','MISSING_GOODS','GET','Goods/basic',[],null,500);$add('store','PRINTER_FILTER_EMPTY','GET','setting.Printer/list',['search'=>'CDXGP11E42A_NOT_MATCH']);$add('terminal','AUTH','GET','Goods/list',[],null,401,false);}
else foreach($tenants as $module=>$tenant){$params=[];$form=null;if($phase!=='ADD'){$rows=$read('SELECT * FROM yoshop_printer WHERE store_id=? AND printer_name=?',[$tenant,$marker.'_'.$module.($phase==='DELETE'?'_edited':'')]);$check(count($rows)===1,'Exact new own Printer identity');$params=['printerId'=>(int)$rows[0]['printer_id']];}if($phase!=='DELETE'){$form=$template[$phase==='ADD'?'printer_add_form':'printer_edit_form'];$form['printer_name']=str_replace('@module',$module,$form['printer_name']);}$add($module,'PRINTER_'.$phase,'POST','setting.Printer/'.strtolower($phase),$params,$form);}
$persist('cases-'.$phase.'.json',$cases);$persist('private-collection-'.$phase.'.json',['apifoxCli'=>'2.2.8','info'=>['name'=>'商品读取和Printer真实纯DB链'],'item'=>[['name'=>$phase,'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(),'protect'=>$protect(),'cache_cleared'=>true];$persist('run-'.$phase.'.json',$out);echo json_encode(['lane'=>$lane,'phase'=>$phase,'native_exit'=>$exit,'stats'=>$stats]),"\n";
