<?php
declare(strict_types=1);
try{require __DIR__.'/bootstrap.php';$started=microtime(true);$path=parse_url($_SERVER['REQUEST_URI']??'',PHP_URL_PATH);$scenario=$_SERVER['HTTP_X_PILOT_SCENARIO']??'';$method=$_SERVER['REQUEST_METHOD']??'';$isRead=$method==='GET'&&$path==='/terminal/Goods/basic';$isWrite=$method==='POST'&&preg_match('#^/terminal/Goods/(add|edit|state|delete)$#D',(string)$path);if((!$isRead&&!$isWrite)||!preg_match('/^PILOT_TG_[A-Z0-9_]{1,60}$/D',$scenario)){http_response_code(403);echo '{"denied":true}';return;}
ini_set('log_errors','1');ini_set('error_log',$runtime.'/private-errors.log');$_SERVER['SCRIPT_FILENAME']=$root.'/public/index.php';$_SERVER['SCRIPT_NAME']='/index.php';$_SERVER['PHP_SELF']='/index.php';$app=commentApp($root,$runtime);$hosts=['terminal.goods-write.invalid'=>2147001112];$app->env->set('store.host',$hosts);$check($app->request->host()==='terminal.goods-write.invalid'&&getStoreId()===2147001112,'Exact isolated tenant host');$before=$own();$protected=$protect();$cfg=$app->config->get('database');foreach($cfg['connections'] as &$c){$c['trigger_sql']=true;$c['debug']=false;}unset($c);$app->config->set($cfg,'database');$app->db->setConfig($app->config);
require __DIR__.'/SqlCoverageRecorder.php';$rec=new SqlCoverageRecorder($runtime.'/queries.jsonl',$root,$scenario);$ops=[];$app->db->listen(static function($sql,$elapsed=null)use($rec,$runtime,$scenario,$isRead,&$ops){if(strpos((string)$sql,'CONNECT:')===0)return;$fp=SqlCoverageRecorder::fingerprint($sql);$ops[]=$fp['operation'];if($isRead&&!in_array($fp['operation'],['select','other'],true))throw new RuntimeException('Read-phase write');if(in_array($fp['operation'],['insert','update','delete','merge','replace'],true)&&!preg_match('/\byoshop_goods(?:_sku|_image|_spec_rel)?\b/i',$sql))throw new RuntimeException('Unapproved write table');$rec->observe($sql,$elapsed);file_put_contents($runtime.'/private-sql.jsonl',json_encode(['scenario'=>$scenario,'sql'=>$sql],JSON_UNESCAPED_UNICODE)."\n",FILE_APPEND|LOCK_EX);chmod($runtime.'/private-sql.jsonl',0600);});register_shutdown_function([$rec,'finish']);
$http=$app->http;$response=$http->run();$hashes=[];foreach(['app/terminal/controller/Goods.php','app/terminal/model/Goods.php','app/common/model/Goods.php','app/common/model/GoodsSku.php','app/terminal/model/GoodsSku.php','app/terminal/model/GoodsImage.php','app/terminal/model/GoodsSpecRel.php','app/terminal/model/GoodsCategoryRel.php','app/terminal/model/goods/ServiceRel.php','app/terminal/service/goods/Apply.php','app/common/database/DmBuilder.php','cores/BaseModel.php'] as $f){$cl=str_replace('/','\\',substr($f,0,-4));$actual=(new ReflectionClass($cl))->getFileName();$check(realpath($actual)===realpath($root.'/'.$f),'Actual source root');$hashes[$f]=hash_file('sha256',$actual);}$after=$own();$wire=['scenario'=>$scenario,'method'=>$method,'path'=>$path,'http_status'=>$response->getCode(),'elapsed_ms'=>(microtime(true)-$started)*1000,'raw'=>$response->getContent(),'before'=>$before,'original'=>$after,'protect_before'=>$protected,'protect_after'=>$protect(),'source_sha256'=>$hashes,'operations'=>$ops];file_put_contents($runtime.'/private-wire.jsonl',json_encode($wire,JSON_UNESCAPED_UNICODE|JSON_PRESERVE_ZERO_FRACTION)."\n",FILE_APPEND|LOCK_EX);chmod($runtime.'/private-wire.jsonl',0600);
$baseline=json_decode(file_get_contents($runtime.'/preflight.json'),true)['baseline'];foreach($baseline as $table=>$rows){$pk=$pks[$table];$pk=trim($pk,'`"');$historical=array_column($rows,$pk);$retained=array_values(array_filter($after[$table],static function($r)use($pk,$historical){return in_array($r[$pk],$historical,true);}));$check($retained===$rows,'Sealed old rows preserved '.$table);if(!in_array($table,['goods','goods_sku','goods_image'],true))$check($after[$table]===$rows,'No unrelated table effect '.$table);if($isRead)$check($after[$table]===$before[$table],'Read exact no effect '.$table);}$newGoods=array_values(array_filter($after['goods'],static function($r)use($marker){return $r['goods_no']===$marker&&(int)$r['store_id']===2147001112;}));$check(count($newGoods)===1,'Exactly one new owned Goods');$gid=(int)$newGoods[0]['goods_id'];$check((int)$newGoods[0]['stock_total']===0,'New Goods stock remains zero');foreach(['goods_sku','goods_image'] as $t)foreach($after[$t] as $r){if(in_array($r,$baseline[$t],true))continue;$check((int)$r['goods_id']===$gid&&(int)$r['store_id']===2147001112,'Only new own relation '.$t);}$check($protect()===$protected,'Natural/default scope');$response->send();$http->end($response);
}catch(Throwable $e){if(isset($persist))$persist('harness-failure.json',['class'=>get_class($e),'message'=>$e->getMessage(),'file'=>$e->getFile(),'line'=>$e->getLine()]);http_response_code(500);header('Content-Type: application/json');echo json_encode(['harness_error'=>get_class($e),'message'=>$e->getMessage()]);}
