<?php
declare(strict_types=1);
date_default_timezone_set('Asia/Shanghai');require __DIR__.'/proof.php';require __DIR__.'/cases.php';require __DIR__.'/dining_oracle.php';
$db=smPdo();$pre=smLoad('preflight-private.json');$actor=smLoad('actor-private.json');$sha=n6Sha();$root=getenv('SM7_ROOT');$runtime=getenv('SM7_RUNTIME');$base=getenv('SM7_BASE');$prefix=!empty($onlyDiningGreen)?'dining-green-':(!empty($onlyDining)?'dining-':(!empty($onlyGreen)?'green-':''));if(is_file($runtime.'/'.$prefix.'run-intent-private.json')){throw new RuntimeException('Do not replay completed read batch');}$queryOffset=is_file($runtime.'/queries.jsonl')?count(file($runtime.'/queries.jsonl',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES)):0;
define('APP_PATH',$root.'/application/');require $root.'/application/common/enum/EnumBasics.php';require $root.'/application/common/enum/EquipmentType.php';require $root.'/application/base/Security.php';
$boundary=json_decode(file_get_contents($base.'/__poc_boundary'),true);if(empty($boundary['private_source_reflection'])||empty($boundary['file_cache'])||empty($boundary['redis_disabled'])||empty($boundary['external_stubs'])||($boundary['real_queue']??true)!==false||($boundary['mqtt_path_enabled']??true)!==false||($boundary['json_encode_param']??null)!==JSON_UNESCAPED_UNICODE){throw new RuntimeException('Private boundary');}$pageSize=(int)$boundary['page_size'];if($pageSize<1||$pageSize>100){throw new RuntimeException('Actual pagination bound');}
$token=\app\base\Security::encrypt(implode("\t",[(string)$actor['id'],$actor['uuid'],$actor['marker'],'0','','',$actor['marker']]));$cases=n6Cases($pre);if(in_array($prefix,['dining-','dining-green-'],true)){$cases=array_values(array_filter($cases,static function($c){return $c['kind']==='dining';}));}elseif($prefix==='green-'){$cases=array_values(array_filter($cases,static function($c){return $c['kind']!=='dining';}));}$reports=[];$responses=[];$sources=[];smSave($prefix.'run-intent-private.json',['phase'=>'start','case_count'=>count($cases)]);
foreach($cases as $case){$name=$case['name'];try{
    $before=n6Proof($db,$case,$pageSize,(bool)$boundary['status_is_show']);$opts=['method'=>'POST','header'=>"Host: cdx-next6.invalid\r\nContent-Type: application/x-www-form-urlencoded;charset=UTF-8\r\ntoken: ".$token."\r\nX-Pilot-Scenario: PILOT_N6_".$name."\r\n",'content'=>http_build_query($case['params']),'ignore_errors'=>true,'follow_location'=>0,'timeout'=>25];$raw=file_get_contents($base.$case['path'],false,stream_context_create(['http'=>$opts]));preg_match('~^HTTP/\S+ (\d+)~',$http_response_header[0]??'',$m);$status=(int)($m[1]??0);$body=json_decode((string)$raw,false,512,JSON_THROW_ON_ERROR);$after=n6Proof($db,$case,$pageSize,(bool)$boundary['status_is_show']);$scope=$before['source_digest']===$after['source_digest'];$diff=[];n6Strict($before['body'],$body,'$',$diff);$positive=false;if($case['kind']==='read'){$positive=!empty($body->data);}elseif($case['kind']==='push'||$case['kind']==='dining'){$positive=!empty($body->data->rows);}elseif($case['kind']==='weigh'){$positive=!empty($body->data->dishes);}elseif($case['kind']==='recipe'){$positive=!empty($body->data);}elseif($case['kind']==='code'){$positive=!empty($before['source_sets']['versions']);}
    $groupOracle=null;if($case['kind']==='dining'&&$prefix==='dining-green-'){$groupOracle=n6DiningExpected($before,$body);$diff=[];n6Strict($groupOracle['expected'],$body,'$',$diff);}
    $reports[]=['case'=>$name,'method'=>'POST','path'=>$case['path'],'http_status'=>$status,'code'=>$body->code??null,'positive'=>$positive,'source_pass'=>$status===200&&$diff===[]&&$scope,'source_window_unchanged'=>$scope,'differences'=>$diff,'representative_selection_unresolved'=>$groupOracle!==null];$responses[$name]=['raw_wire'=>(string)$raw,'wire_sha256'=>hash('sha256',(string)$raw),'wire_bytes'=>strlen((string)$raw),'body'=>$body,'http_status'=>$status];$sources[$name]=['before'=>$before,'after'=>$after,'case'=>$case,'group_oracle'=>$groupOracle];
}catch(Throwable $e){$reports[]=['case'=>$name,'method'=>'POST','path'=>$case['path'],'positive'=>false,'source_pass'=>false,'limited'=>true,'error_class'=>get_class($e),'error_sha256'=>hash('sha256',$e->getMessage())];smSave('error-'.$name.'-private.json',['message'=>$e->getMessage()]);}
    smSave($prefix.'cases-private.json',$reports);smSave($prefix.'responses-private.json',$responses);smSave($prefix.'source-private.json',$sources);
}
$events=[];foreach(array_slice(file($runtime.'/queries.jsonl',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES)?:[],$queryOffset) as $line){$events[]=json_decode($line,true);}$dml=0;foreach($events as $event){if($event['kind']==='query_observed'&&in_array($event['operation'],['insert','update','delete','replace','merge'],true)){$dml++;}}$actorNow=n6Actor($db);$result=['lane'=>getenv('SM7_LANE'),'cases'=>$reports,'source_sha256'=>$sha,'boundary'=>$boundary,'events'=>$events,'query_offset'=>$queryOffset,'actor_unchanged'=>smDigest($actorNow)===$pre['actor_digest'],'http_dml'=>$dml,'new_fixture_dml'=>0,'ddl'=>0];smSave($prefix.'report-private.json',$result);smSave($prefix.'run-intent-private.json',['phase'=>'finished','case_count'=>count($reports)]);echo json_encode(['lane'=>$result['lane'],'cases'=>$reports,'http_dml'=>$dml,'ddl'=>0]),PHP_EOL;exit($dml?1:0);
