<?php
declare(strict_types=1);require __DIR__.'/bootstrap.php';$check(!is_file($runtime.'/read-'.getenv('COMMENT_PHASE').'.json'),'No read phase replay');$fixture=json_decode(file_get_contents($runtime.'/fixture-journal.json'),true);$check($fixture['committed'],'Committed fixtures');$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);}$persist('private-tokens.json',$tokens);$cases=[];
$add=static function($module,$name,$action,$params=[],$status=200,$auth=true)use(&$cases,$base,$tokens){$scenario='PILOT_CM_'.getenv('COMMENT_PHASE').'_'.strtoupper($module.'_'.$name);$method=in_array($action,['list','detail'],true)?'GET':'POST';$headers=[['key'=>'Host','value'=>$module.'.comment.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.'/goods.comment/'.$action,'header'=>$headers];if($method==='GET')$request['url'].='?'.http_build_query($params);else{$request['header'][]=['key'=>'Content-Type','value'=>'application/json'];$request['body']=['mode'=>'raw','raw'=>json_encode($params,JSON_UNESCAPED_UNICODE),'options'=>['raw'=>['language'=>'json']]];}$exec=["pm.test('HTTP 200',function(){pm.response.to.have.status(200);});var b=pm.response.json();","pm.test('Numeric expected 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('Message and data types',function(){pm.expect(typeof b.message).to.eql('string');pm.expect(typeof b.data).to.eql('object');});"];$cases[]=['scenario'=>$scenario,'module'=>$module,'name'=>$name,'action'=>$action,'method'=>$method,'params'=>$params,'expected_status'=>$status,'auth'=>$auth,'item'=>['id'=>$scenario,'name'=>$name,'type'=>'http','metaInfo'=>['type'=>'HTTP'],'request'=>$request,'event'=>[['listen'=>'test','script'=>['type'=>'text/javascript','exec'=>$exec]]]]];};

foreach($tenants as $module=>$tenant){$id=$fixture['owned'][$module];$add($module,'LIST','list');$add($module,'GOODS','list',['goodsName'=>$marker]);$add($module,'ORDER','list',['orderNo'=>$marker]);$add($module,'USER','list',['userId'=>$id['user']]);$add($module,'SCORE','list',['score'=>20,'status'=>0]);$add($module,'EMPTY_FILTER','list',['goodsName'=>'MISSING_'.$marker]);$add($module,'PAGE2','list',['page'=>2]);$add($module,'LITERAL','list',['goodsName'=>"comment.order 中文' \\"]);}
$collection=['apifoxCli'=>'2.2.8','info'=>['name'=>'Comment修复只读控制'],'item'=>[['name'=>'只读不重放写','item'=>array_column($cases,'item')]]];$phase=getenv('COMMENT_PHASE');$persist('private-collection-'.$phase.'.json',$collection);$native=$runtime.'/native-'.$phase;mkdir($native,0700);$before=$own();$p=proc_open(['node',getenv('COMMENT_CLI'),'run',$runtime.'/private-collection-'.$phase.'.json','-r','json','--out-dir',$native,'--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($native.'/*.json') as $f){$r=json_decode(file_get_contents($f),true);if(isset($r['run']))$stats[]=['stats'=>$r['run']['stats'],'failure_count'=>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(),'protected_unchanged'=>$protect()===$fixture['before_protect'],'token_cache_cleared'=>true];$persist('read-'.$phase.'.json',$out);echo json_encode(['lane'=>$lane,'phase'=>$phase,'native_exit'=>$exit,'stats'=>$stats,'own_unchanged'=>$out['own_unchanged'],'protected_unchanged'=>$out['protected_unchanged']]),"\n";
