<?php
declare(strict_types=1);
$req=static function($module,$name,$action,$params=[],$expected=200,$message=null,$auth=true)use($runtime,$base,$tokens,$check,$save,$own,&$cases,&$collections,&$stats){$scenario='PILOT_SG_'.(getenv('SIGN_PHASE')?:'G').'_'.strtoupper($module.'_'.$name);$method=in_array($action,['list','detail'],true)?'GET':'POST';$headers=[['key'=>'Host','value'=>$module.'.sign-config.invalid'],['key'=>'Platform','value'=>'POC'],['key'=>'X-Pilot-Scenario','value'=>$scenario]];if($auth)$headers[]=['key'=>'Access-Token','value'=>$tokens[$module]];$url=$base.'/'.$module.'/market.sign/'.$action;if($method==='GET')$url.='?'.http_build_query($params);else $headers[]=['key'=>'Content-Type','value'=>'application/json'];$request=['method'=>$method,'url'=>$url,'header'=>$headers];if($method==='POST')$request['body']=['mode'=>'raw','raw'=>json_encode($params,JSON_UNESCAPED_UNICODE|JSON_PRESERVE_ZERO_FRACTION),'options'=>['raw'=>['language'=>'json']]];
$tests=["pm.test('HTTP 200',function(){pm.response.to.have.status(200);});var b=pm.response.json();","pm.test('Numeric business status',function(){pm.expect(b.status).to.eql(".$expected.");});","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');});"];if($message!==null)$tests[]="pm.test('Exact message',function(){pm.expect(b.message).to.eql(".json_encode($message,JSON_UNESCAPED_UNICODE).");});";
$item=['id'=>$scenario,'name'=>$module.' '.$name,'type'=>'http','metaInfo'=>['type'=>'HTTP'],'request'=>$request,'event'=>[['listen'=>'test','script'=>['type'=>'text/javascript','exec'=>$tests]]]];$col=['apifoxCli'=>'2.2.8','info'=>['name'=>'智慧餐厅Sign配置原生请求'],'item'=>[['name'=>'Sign场景','item'=>[$item]]]];$path=$runtime.'/private-request.json';file_put_contents($path,json_encode($col,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE));chmod($path,0600);$dir=$runtime.'/private-cli-report/'.$scenario;mkdir($dir,0700,true);$start=time();$p=proc_open(['node',getenv('SIGN_CLI'),'run',$path,'-r','json','--out-dir',$dir,'--out-file','result','--on-error','end','--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);$end=time();$wire=null;foreach(file($runtime.'/private-wire.jsonl',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES)?:[] as $l){$w=json_decode($l,true);if($w['scenario']===$scenario)$wire=$w;}$check($wire!==null,'Native raw capture '.$scenario);$body=json_decode($wire['raw']);$read=$own();$case=['scenario'=>$scenario,'module'=>$module,'action'=>$action,'method'=>$method,'path'=>'/'.$module.'/market.Sign/'.$action,'wire_path'=>$wire['path'],'http_status'=>$wire['http_status'],'body'=>$body,'raw_sha256'=>hash('sha256',$wire['raw']),'window'=>[$start,$end],'native_cli_exit'=>$exit,'params'=>$params,'independent_original'=>$read,'expected_status'=>$expected,'expected_message'=>$message];$cases[]=$case;$collections[]=$col;foreach(glob($dir.'/*.json') as $f){$report=json_decode(file_get_contents($f),true);if(isset($report['run']))$stats[]=['scenario'=>$scenario,'stats'=>$report['run']['stats'],'failures'=>count($report['run']['failures']??[]),'raw_report_sha256'=>hash_file('sha256',$f)];}$save();return $case;};
