<?php
declare(strict_types=1);
require '/workspace/zhct/zhctproject/ai_api/vendor/autoload.php';$source=$argv[1]??'/workspace/zhct/zhctproject/ai_api/app/terminal/service/analysis/Analysis.php';require $source;$class=\app\terminal\service\analysis\Analysis::class;$pass=0;$fail=[];
$assert=static function($ok,$name)use(&$pass,&$fail){if($ok)$pass++;else $fail[]=$name;};
if(!method_exists($class,'decimalProjection')){$assert(false,'Observed DECIMAL direct fields must use source-bound response projection');echo json_encode(['assertions'=>$pass+count($fail),'passed'=>$pass,'failed'=>$fail]),"\n";exit(1);}
$manager=new \think\DbManager;$manager->setConfig(['default'=>'unit','connections'=>['unit'=>['type'=>\app\common\database\Dm::class,'hostname'=>'127.0.0.1','database'=>'UNIT_ONLY','username'=>'','password'=>'','hostport'=>5238]]]);\think\Container::getInstance()->instance(\think\DbManager::class,$manager);
$method=new ReflectionMethod($class,'decimalProjection');$method->setAccessible(true);foreach([[['x'=>12.3],'12.30'],[['x'=>0],'0.00'],[['x'=>'12.30'],'12.30'],[['x'=>'0.00'],'0.00'],[['x'=>null],0],[[],0],[['x'=>'99999999.99'],'99999999.99'],[['x'=>99999999.99],'99999999.99'],[['x'=>'-12.30'],'-12.30'],[['x'=>'1.20'],'1.20'],[['x'=>'0.01'],'0.01'],[['x'=>0.01],'0.01'],[['x'=>'12.3'],'12.3'],[['x'=>''],'']] as $i=>[$row,$want]){$before=$row;$actual=$method->invoke(null,$row,'x');$assert($actual===$want,'Exact scalar/type DM case '.$i);$assert($row===$before,'Original source row unchanged '.$i);}
$mysql=new \think\DbManager;$mysql->setConfig(['default'=>'unit','connections'=>['unit'=>['type'=>'mysql']]]);\think\Container::getInstance()->instance(\think\DbManager::class,$mysql);foreach([12.3,0,'12.30','12.3','',null] as $i=>$value)$assert($method->invoke(null,['x'=>$value],'x')===($value??0),'MySQL exact original scalar/type '.$i);$assert($manager->getQueryTimes()===0&&$mysql->getQueryTimes()===0,'Real connector objects no DB query in unit test');
$text=file_get_contents($source);foreach(['energy','carbohydrate','protein','fat'] as $field)$assert(strpos($text,"self::decimalProjection(\$current_meal, '".$field."')")!==false,'Only direct supply reality '.$field);$assert(strpos($text,"'weight' => self::decimalProjection(\$current_dishe, 'dietary_fiber')")!==false,'Only direct fiber weight projection');$assert(strpos($text,'$data[\'supply_ratio\'][\'fat\'] = round(')!==false,'Computed ratio remains numeric formula');
\think\Container::getInstance()->instance(\think\DbManager::class,$manager);$found=preg_match('/^\s*\$data\[\$field\]\[\x27actual\x27\] = self::decimalProjection.*;$/m',$text,$matches);$assert($found===1,'Real vitamin direct assignment located once');if($found){$call=Closure::bind(static function($current_meal,$lastMeal,$field,$line){$data=[];eval($line);return $data[$field]['actual'];},null,$class);set_error_handler(static function($severity,$message,$file,$line){throw new ErrorException($message,0,$severity,$file,$line);});foreach([[[],0],[[2=>null],0],[[2=>['va'=>null]],0],[[2=>[]],0],[[2=>['va'=>12.3]],'12.30']] as $i=>[$current,$expected]){try{$value=$call($current,['meal_times'=>2],'va',$matches[0]);$assert($value===$expected,'Vitamin actual call-layer missing/null/real value '.$i);}catch(Throwable $e){$assert(false,'Vitamin actual call-layer missing/null/real value '.$i);}}restore_error_handler();}
echo json_encode(['assertions'=>$pass+count($fail),'passed'=>$pass,'failed'=>$fail]),"\n";exit($fail?1:0);
