<?php
function independentDTO($case){global $runtime,$dm;$snapshot=json_decode(file_get_contents($runtime.'/scale-baseline.json'),true);$yun=strpos($case['path'],'Yunkangbao')!==false;$byDate=strpos($case['path'],'byDate')!==false;$sessions=array_values(array_filter($snapshot['sessions'],static function($s)use($yun,$byDate){return $s['is_valid']===1&&$s['device_type']==='body_scale'&&(!$yun||$s['vendor']==='yunkangbao')&&(!$byDate||substr($s['start_time'],0,10)==='2026-10-11');}));usort($sessions,static function($a,$b){$v=strcmp($b['start_time'],$a['start_time']);return $v?:$b['session_id']<=>$a['session_id'];});ck(count($sessions)>0,'Independent actual source predicate yields nonempty');$s=$sessions[0];$id=$s['session_id'];$raw=array_values(array_filter($snapshot['raw'],static function($r)use($id){return $r['session_id']===$id;}));$records=array_values(array_filter($snapshot['records'],static function($r)use($id){return $r['session_id']===$id;}));ck(count($raw)===1&&count($records)===3,'One original raw and exactly three metric rows');$summary=[];$segments=[];$vendor=null;$version=null;foreach($records as $r){$vendor=$vendor??$r['algorithm_vendor'];$version=$version??$r['algorithm_version'];$v=['value'=>(float)$r['value'],'unit'=>$r['unit']];if(empty($r['segment']))$summary[$r['metric_code']]=$v;else $segments[$r['segment']][$r['metric_code']]=$v;}$time=$s['start_time'];if($dm){ck(preg_match('/^2026-10-11 \d\d:\d\d:\d\d\.000000$/D',$time),'Frozen own native physical six zero digits unchanged');$time=substr($time,0,19);}else ck(preg_match('/^2026-10-11 \d\d:\d\d:\d\d$/D',$time),'Actual MySQL fsp0 seconds');return ['session_id'=>$id,'measured_at'=>$time,'vendor'=>$s['vendor'],'device_type'=>'body_scale','algorithm_vendor'=>$vendor,'algorithm_version'=>$version,'summary'=>$summary,'segments'=>$segments,'raw'=>json_decode($raw[0]['data'],true,512,JSON_THROW_ON_ERROR),'bmi_cn'=>'标准'];}
function checkDTO($label,$r){global $runtime,$lane;$data=json_decode($r['raw_response_body'],true,512,JSON_THROW_ON_ERROR);ck($r['http_status']===200&&array_keys($data)===['status','message','data','trace_id']&&$data['status']===200&&$data['message']==='ok'&&is_string($data['trace_id'])&&preg_match('/^[0-9a-f]{13}$/D',$data['trace_id']),'Whole original envelope');$expected=json_decode(file_get_contents($runtime.'/expected-DTO.json'),true)[$label];ck(json_encode($data['data'],JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES)===json_encode($expected,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),'Complete pre-frozen typed/ordered independent DTO');ck(strlen($data['data']['measured_at'])===19,'Correct19-character date string');$oldLabel=in_array($label,['R1_WL_BY_DATE','R3_WL_LATEST'],true)?'R1_WELLAND_BY_DATE':'R2_YUNKANGBAO_BY_DATE';$old=json_decode(json_decode(file_get_contents($runtime.'/old-response-'.$oldLabel.'.json'),true)['raw_response_body'],true);if($lane==='B')ck($data['status']===$old['status']&&$data['message']===$old['message']&&json_encode($data['data'],JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES)===json_encode($old['data'],JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),'B old whole contract/data preserved except dynamic trace');saveOnce('oracle-'.$label.'.json',['expected_data'=>$expected,'full_typed_ordered_DTO'=>true,'date19'=>true,'B_old_contract_unchanged'=>$lane==='B','physical_mutation'=>false,'Welland_predicate'=>'all_vendor_source_no_false_vendor_isolation']);}
