<?php
declare(strict_types=1);
$runtime=$argv[1]??'';$target=$argv[2]??'';if(!is_dir($runtime)||!is_dir($target))exit(78);$db=json_decode(file_get_contents($runtime.'/cli-db-evidence.json'),true);$lane=$db['lane'];$c=json_decode(file_get_contents($runtime.'/private-cli-collection.json'),true);$names=[];foreach($c['item'][0]['item'] as &$i){foreach($i['request']['header'] as &$h)if($h['key']==='Access-Token')$h['value']='{{'.(strpos($i['id'],'STORE')!==false?'store':'terminal').'_token}}';unset($h);$i['request']['url']=preg_replace('#^http://127\.0\.0\.1:1832[12]#','{{base_url}}',$i['request']['url']);$names[$i['id']]=[];foreach($i['event'][0]['script']['exec'] as $s)if(preg_match('/pm\.test\(\x27([^\x27]*)\x27/',$s,$m))$names[$i['id']][]=$m[1];}unset($i);$runs=[];foreach(glob($runtime.'/private-cli-report/*.json') as $file){$r=json_decode(file_get_contents($file),true);$stats=$r['run']['stats'];if($stats['requests']['total']!==6||$stats['assertions']['total']!==36||$stats['assertions']['failed']!==0||$stats['steps']['passed']!==6||count($r['run']['failures'])!==0)throw new RuntimeException('Actual CLI run red');$runs[]=['raw_report_sha256'=>hash_file('sha256',$file),'stats'=>$stats,'timings'=>$r['run']['timings'],'failures'=>[],'stepTree_empty_cli_report_limitation'=>$r['run']['stepTree']===[]];}$wire=[];foreach(file($runtime.'/private-cli-wire.jsonl',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES) as $line){$v=json_decode($line,true);$body=json_decode($v['raw']);if(!is_object($body)||$body->status!==200||$v['http_status']!==200||!preg_match('/^[0-9a-f]{13}$/D',$body->trace_id)||!isset($names[$v['scenario']]))throw new RuntimeException('Actual native CLI captured HTTP red');$body->trace_id='VALID_DYNAMIC_TRACE';$v['body']=$body;unset($v['raw']);$v['named_assertions']=$names[$v['scenario']];$v['CLI_all_assertions_passed']=true;$wire[]=$v;}if(count($wire)!==6||count(array_unique(array_column($wire,'scenario')))!==6||count($runs)!==2)throw new RuntimeException('Actual native CLI run and wire count');$out=['lane'=>$lane,'execution'=>'Native Apifox CLI2.2.8 local file, no project/cloud/upload/notifications','actual_runs'=>$runs,'actual_HTTP_requests_total'=>12,'named_assertions_total'=>72,'captured_wire_requests'=>6,'wire'=>$wire,'independent_original_database'=>$db,'limitation'=>'First CLI JSON stepTree empty; second read-only run captured server-side full actual wire and elapsed time. Both actual CLI reports retained by SHA and stats, no writes replayed.'];file_put_contents($target.'/apifox-cli-evidence-'.$lane.'.json',json_encode($out,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES));file_put_contents($target.'/client-settings-readonly.apifox-cli.json',json_encode($c,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES));echo json_encode(['lane'=>$lane,'actual_runs'=>count($runs),'actual_requests'=>12,'assertions'=>72,'captured_wires'=>6,'raw_tokens_redacted'=>true]),"\n";
