<?php
declare(strict_types=1);require __DIR__.'/bootstrap.php';require __DIR__.'/natural.php';$lock=fopen($runtime.'/phase.lock','c');$check(flock($lock,LOCK_EX|LOCK_NB),'Serial same-lane phase');$phase=getenv('COMMENT_PHASE');$check($phase==='NA','Only frozen original phase');$check(!is_file($runtime.'/run-'.$phase.'.json'),'No HTTP phase replay');$f=json_decode(file_get_contents($runtime.'/fixture-journal.json'),true);$check($f['committed']&&$own()===$f['after']&&$natural()===$f['natural_after'],'All committed original baselines exact');$app=commentApp($root,$runtime);$cache=new \think\cache\driver\File($app,['path'=>$runtime.'/cache/','prefix'=>'','expire'=>0]);$tokens=[];foreach($tenants as $module=>$tenant){$class='app\\'.$module.'\\model\\store\\User';$model=$class::withoutGlobalScope()->where('store_user_id','=',$f['owned'][$module])->where('store_id','=',$tenant)->find();$check($model!==null,'Actual own model principal exists');$profile=$model->toArray();$check(!array_key_exists('password',$profile)&&(int)$profile['is_super']===1,'Normal principal hides non-login credential');$tokens[$module]=bin2hex(random_bytes(24));$cache->set($tokens[$module],['user'=>$profile,'store_id'=>$tenant,'is_login'=>true],3600);}$tokens['admin']=bin2hex(random_bytes(24));$cache->set($tokens['admin'],['user'=>['admin_user_id'=>2147001141,'user_name'=>$marker.'_admin'],'is_login'=>true],3600);$items=[];$cases=[];
$add=static function($module,$name,$path,$params=[],$status=200,$auth=true)use(&$items,&$cases,$tokens,$base){$s='PILOT_OP_NA_'.strtoupper($module.'_'.$name);$headers=[['key'=>'Host','value'=>$module.'.operator-read.invalid'],['key'=>'Platform','value'=>'POC'],['key'=>'X-Pilot-Scenario','value'=>$s]];if($auth)$headers[]=['key'=>'Access-Token','value'=>$tokens[$module]];$cases[]=['scenario'=>$s,'module'=>$module,'name'=>$name,'method'=>'GET','path'=>'/'.$module.'/'.$path,'params'=>$params,'expected_status'=>$status,'auth'=>$auth];$items[]=['id'=>$s,'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('Expected business status',function(){pm.expect(b.status).to.eql(".$status.");});","pm.test('Trace contract',function(){pm.expect(b.trace_id).to.match(/^[0-9a-f]{13}$/);});","pm.test('Top DTO types',function(){pm.expect(typeof b.data).to.eql('object');pm.expect(typeof b.message).to.eql('string');});"]]]]];};
foreach($tenants as $m=>$t){$add($m,'INFO','store.User/info');$add($m,'LIST','store.User/list',['search'=>$marker,'user_type'=>2]);$add($m,'ALL','store.User/all',['user_type'=>2]);}$inspect=json_decode(file_get_contents(__DIR__.'/inspect-'.$lane.'.json'),true);$add('admin','API_INDEX','store.Api/index');$add('admin','MENU_INDEX','store.Menu/index');$add('admin','MENU_INFO','store.Menu/info',['menuId'=>$inspect['menu_info_id']]);$add('store','AUTH','store.User/list',[],401,false);$add('store','CROSS_SEARCH','store.User/list',['search'=>'cdxop11f64c_terminal','user_type'=>2]);$add('terminal','EMPTY_TYPE','store.User/all',['user_type'=>1]);$add('admin','MISSING_MENU','store.Menu/info',['menuId'=>0]);$persist('cases-NA.json',$cases);$persist('private-collection-NA.json',['apifoxCli'=>'2.2.8','info'=>['name'=>'operator profiles and admin permission dictionaries readonly'],'item'=>[['name'=>'NA','item'=>$items]]]);$dir=$runtime.'/native-NA';mkdir($dir,0700);$p=proc_open(['node',getenv('COMMENT_CLI'),'run',$runtime.'/private-collection-NA.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 $file){$r=json_decode(file_get_contents($file),true);if(isset($r['run']))$stats[]=['stats'=>$r['run']['stats'],'failures'=>count($r['run']['failures']),'sha256'=>hash_file('sha256',$file)];}foreach($tokens as $token)$cache->delete($token);$out=['lane'=>$lane,'native_exit'=>$exit,'stats'=>$stats,'after'=>$own(),'natural_after'=>$natural(),'own_tokens'=>count($tokens),'all_own_tokens_absent'=>count(array_filter($tokens,static function($t)use($cache){return $cache->has($t);}))===0];$persist('run-NA.json',$out);echo json_encode(['lane'=>$lane,'native_exit'=>$exit,'stats'=>$stats]),"\n";exit($exit);
