<?php
namespace think {
 class Config {public static $dm=false;public static function get($key){return self::$dm?'app\\cores\\database\\Dm':'mysql';}}
 class Db {public static function name($name){return new AuditQuery();}}
 class AuditQuery {
  public static $contextJson='{"中文":[1,true,null,"值"]}';
  private $where=[],$page=1,$size=20;
  private function rows(){ $out=[];foreach([[1,10001,0,'kept'],[2,10001,0,'kept'],[3,10001,0,'kept'],[4,10002,0,'kept'],[5,10001,1,'kept'],[6,10001,0,'other']]as $s){$r=['id'=>$s[0],'store_id'=>$s[1],'is_delete'=>$s[2],'event_code'=>$s[3],'context_json'=>self::$contextJson,'created_at'=>'2024-02-29 23:59:59'];$ok=true;foreach($this->where as $k=>$v)if($r[$k]!==$v)$ok=false;if($ok)$out[]=$r;}return $out;}
  public function where($k,$v=null){if(is_array($k))$this->where=$k;else $this->where[$k]=$v;return $this;}
  public function count(){$n=count($this->rows());$this->where=[];return $n;}
  public function getOptions($key){return ['AND'=>$this->where];}
  public function field($f){return $this;}public function order($v){return $this;}
  public function page($p,$s){$this->page=$p;$this->size=$s;return $this;}
  public function select(){return array_slice(array_reverse($this->rows()),($this->page-1)*$this->size,$this->size);}
 }
}
namespace {
 function getStoreId(){return 10001;}
 require $argv[1].'/common/service/BaseService.php';require $argv[1].'/common/service/SecurityAuditService.php';
 $n=0;$fail=[];$assert=static function($ok,$name)use(&$n,&$fail){$n++;if(!$ok)$fail[]=$name;};$service=new \app\common\service\SecurityAuditService();
 foreach([[1,'kept',[3,2],3],[2,'kept',[1],3],[1,'absent',[],0]]as $case){$r=$service->lists(['page'=>$case[0],'page_size'=>2,'event_code'=>$case[1]]);$assert(array_column($r['data'],'id')===$case[2],'count_preserves_tenant_delete_event_page_'.$case[0].'_'.$case[1]);$assert($r['total']===$case[3],'matching_count_'.$case[0].'_'.$case[1]);foreach($r['data']as $row)$assert($row['context']===['中文'=>[1,true,null,'值']]&&!isset($row['context_json']),'context_all_values');}
 $ref=new \ReflectionClass(\app\common\service\SecurityAuditService::class);$times=[null,'','2024-02-29 23:59:59','2024-02-29 23:59:59.0','2024-02-29 23:59:59.000000','2024-02-29 23:59:59.123456','2024-02-29 23:59:59.0000000','2026-02-29 23:59:59.000000','0000-01-01 00:00:00.000000','2024-02-29 24:59:59.000000','2024-02-29T23:59:59.000000',23];
 foreach([false,true]as $dm)foreach($times as $i=>$time){$row=['created_at'=>$time,'other'=>['x',null,true,7]];$expected=$row;if($dm&&in_array($i,[3,4],true))$expected['created_at']=substr($time,0,19);if(!$ref->hasMethod('normalizeCreatedAt')){$assert(false,'time_'.$dm.'_'.$i);continue;}$m=$ref->getMethod('normalizeCreatedAt');$m->setAccessible(true);$assert($m->invoke(null,$row,$dm)===$expected,'time_'.$dm.'_'.$i);}
 if($ref->hasMethod('normalizeCreatedAt'))$assert($m->invoke(null,['other'=>7],true)===['other'=>7],'missing_created_at');
 foreach([false,true]as $dm){\think\Config::$dm=$dm;foreach(['{}','[]','null','false','broken','{"中文":[1,true,null,"值"],"nested":{}}','[1,"中",false,null,{"a":2}]']as $i=>$json){\think\AuditQuery::$contextJson=$json;$r=$service->lists(['event_code'=>'kept']);$decoded=json_decode($json,true);$assert($r['data'][0]['context']===(is_array($decoded)?$decoded:[])&&!isset($r['data'][0]['context_json']),'json_all_values_types_'.$dm.'_'.$i);}}
 echo json_encode(['total'=>$n,'failed'=>$fail]),"\n";exit($fail?1:0);
}
