<?php
declare(strict_types=1);require __DIR__.'/bootstrap.php';$lock=fopen($runtime.'/phase.lock','c');$check(flock($lock,LOCK_EX|LOCK_NB),'Exactly one serial lane run');$check(!is_file($runtime.'/run-journal.json'),'No successful writes replay');$preflight=json_decode(file_get_contents($runtime.'/preflight.json'),true);$check($preflight['baseline']===$own(),'Twenty-table preflight baseline remains exact');$templates=json_decode(file_get_contents(__DIR__.'/request-values.json'),true);$app=commentApp($root,$runtime);$cache=new \think\cache\driver\File($app,['path'=>$runtime.'/cache/','prefix'=>'','expire'=>0]);$token=bin2hex(random_bytes(24));$cache->set($token,['user'=>['store_user_id'=>2147001112,'store_id'=>2147001112,'real_name'=>$marker,'user_name'=>$marker,'is_super'=>1],'store_id'=>2147001112,'is_login'=>true],3600);$journal=['lane'=>$lane,'cases'=>[],'stages'=>[],'before'=>$own(),'complete'=>false];$persist('run-journal.json',$journal);
try{foreach(['ADD','READ_ADD','EDIT','READ_EDIT','STATE_UP','STATE_DOWN','DELETE','READ_DELETE'] as $stage){$scenario='PILOT_TG_'.$stage;$params=[];$body=null;$method='POST';$action='';if($stage!=='ADD'){$r=$read('SELECT * FROM yoshop_goods WHERE store_id=2147001112 AND goods_no=?',[$marker]);$check(count($r)===1,'Exact newly API-created goods identity');$gid=(int)$r[0]['goods_id'];$journal['new_goods_id']=$gid;}$form=$templates['add']['form'];$form['imagesIds']=[(int)$preflight['file']['file_id']];if($stage==='ADD'){$action='add';$body=['form'=>$form];}elseif($stage==='EDIT'){$action='edit';$params=['goodsId'=>$gid];$body=['form'=>array_merge($form,$templates['edit_changes_only'])];}elseif(strpos($stage,'STATE_')===0){$action='state';$body=['goodsIds'=>[$gid],'state'=>$stage==='STATE_UP'];}elseif($stage==='DELETE'){$action='delete';$body=['goodsIds'=>[$gid]];}else{$method='GET';$action='basic';$params=['goodsId'=>$gid];}
$headers=[['key'=>'Host','value'=>'terminal.goods-write.invalid'],['key'=>'Platform','value'=>'POC'],['key'=>'X-Pilot-Scenario','value'=>$scenario],['key'=>'Access-Token','value'=>$token]];$request=['method'=>$method,'url'=>$base.'/terminal/Goods/'.$action.'?'.http_build_query($params),'header'=>$headers];if($body!==null){$request['header'][]=['key'=>'Content-Type','value'=>'application/json'];$request['body']=['mode'=>'raw','raw'=>json_encode($body,JSON_UNESCAPED_UNICODE),'options'=>['raw'=>['language'=>'json']]];}$case=['scenario'=>$scenario,'stage'=>$stage,'module'=>'terminal','method'=>$method,'path'=>'/terminal/Goods/'.$action,'params'=>$params,'body'=>$body,'expected_status'=>200];$journal['cases'][]=$case;$persist('run-journal.json',$journal);$item=['id'=>$scenario,'name'=>$stage,'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('Business success',function(){pm.expect(b.status).to.eql(200);});","pm.test('Trace contract',function(){pm.expect(b.trace_id).to.match(/^[0-9a-f]{13}$/);});","pm.test('DTO top types',function(){pm.expect(typeof b.data).to.eql('object');pm.expect(typeof b.message).to.eql('string');});"]]]]];$persist('private-collection-'.$stage.'.json',['apifoxCli'=>'2.2.8','info'=>['name'=>'terminal Goods real new own CRUD'],'item'=>[['name'=>$stage,'item'=>[$item]]]]);$dir=$runtime.'/native-'.$stage;mkdir($dir,0700);$p=proc_open(['node',getenv('COMMENT_CLI'),'run',$runtime.'/private-collection-'.$stage.'.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)];}$journal['stages'][$stage]=['native_exit'=>$exit,'stats'=>$stats,'after'=>$own()];$persist('run-journal.json',$journal);echo json_encode(['lane'=>$lane,'stage'=>$stage,'native_exit'=>$exit,'stats'=>$stats]),"\n";$check($exit===0,'Stop after actual native failure; do not replay successful stages');}$journal['complete']=true;$journal['after']=$own();}finally{$cache->delete($token);$journal['own_token_absent']=!$cache->has($token);$persist('run-journal.json',$journal);}
