<?php
declare(strict_types=1);
// Correct the first offline comparator's assoc/object mistake, from immutable originals.
require __DIR__.'/contract.php';$runtime=$argv[1]??'';if(!is_dir($runtime)||is_link($runtime)){exit(78);}putenv('SM7_RUNTIME='.$runtime);$history=$runtime.'/tool-red-json-options';$sources=json_decode(file_get_contents($history.'/source-private.json'),false,512,JSON_THROW_ON_ERROR);$responses=json_decode(file_get_contents($history.'/responses-private.json'),false,512,JSON_THROW_ON_ERROR);$report=json_decode(file_get_contents($history.'/report-private.json'),true,512,JSON_THROW_ON_ERROR);$pass=0;
foreach($sources as $name=>$proof){foreach(['before','after'] as $stage){$proof->{$stage}->body=json_decode(json_encode($proof->{$stage}->body,JSON_UNESCAPED_UNICODE|JSON_THROW_ON_ERROR),false,512,JSON_THROW_ON_ERROR);}$response=$responses->{$name};$wire=$response->raw_wire;if(strlen($wire)!==$response->wire_bytes||hash('sha256',$wire)!==$response->wire_sha256){throw new RuntimeException('Immutable raw response changed');}$actual=json_decode($wire,false,512,JSON_THROW_ON_ERROR);$d=[];n6Strict($proof->before->body,$actual,'$',$d);foreach($report['cases'] as &$case){if($case['case']===$name){$case['differences']=$d;$case['source_pass']=$case['http_status']===200&&$d===[]&&$case['source_window_unchanged'];if($case['source_pass']){$pass++;}}}unset($case);}
smSave('source-private.json',$sources);smSave('report-private.json',$report);smSave('cases-private.json',$report['cases']);smSave('tool-json-options-recheck-v2-private.json',['original_source_and_wire_read'=>true,'json_decode_assoc'=>false,'original_json_options'=>JSON_UNESCAPED_UNICODE,'matched_cases'=>$pass,'business_http_replayed'=>0,'database_operations'=>0]);echo json_encode(['source_pass'=>$pass,'cases'=>count(get_object_vars($sources)),'business_http_replayed'=>0,'database_operations'=>0]),PHP_EOL;
