#!/usr/bin/env php
<?php
/**
 * 判定优先级顺序回归
 */
if (PHP_SAPI !== 'cli') {
    exit(1);
}

$root = dirname(__DIR__);
require_once $root . '/cong.php';
require_once $root . '/lib/Cloak/WhitelistGate.php';
require_once $root . '/lib/Cloak/RiskSecondaryGate.php';
require_once $root . '/lib/Cloak/CloakJudgmentCache.php';
require_once $root . '/lib/Cloak/Pipeline/CheckPipeline.php';

if (!defined('WHITE_PARAMS')) {
    define('WHITE_PARAMS', 'token=abc');
}
if (!defined('SHOW_SITE_IP')) {
    define('SHOW_SITE_IP', '8.8.8.8');
}
if (!defined('BLACKLIST_GROUP_ID')) {
    define('BLACKLIST_GROUP_ID', 0);
}

$tests = [];

$_GET = ['token' => 'abc'];
$tests['params_whitelist_fast'] = CheckPipeline::resolveWhitelistParamsFastPath();

$_GET = [];
$_SERVER['REMOTE_ADDR'] = '8.8.8.8';
$tests['ip_whitelist_when_no_params'] = CheckPipeline::resolveWhitelistIpFastPath();

$_GET = ['token' => 'abc'];
$tests['match_reason_params_first'] = WhitelistGate::matchReason((object) ['v_ip' => '8.8.8.8']) === '白名单链接参数';
$_GET = [];
$tests['match_reason_ip'] = WhitelistGate::matchReason((object) ['v_ip' => '8.8.8.8']) === '白名单';

$zpCmd = escapeshellarg(PHP_BINARY) . ' -r ' . escapeshellarg(
    'define("SHOW_SITE_MODE_SWITCH","zp");'
    . 'require ' . var_export($root . '/lib/Cloak/Pipeline/CheckPipeline.php', true) . ';'
    . 'echo CheckPipeline::resolveZpFastPath() ? "1" : "0";'
);
$fpCmd = escapeshellarg(PHP_BINARY) . ' -r ' . escapeshellarg(
    'define("SHOW_SITE_MODE_SWITCH","fp");'
    . 'require ' . var_export($root . '/lib/Cloak/Pipeline/CheckPipeline.php', true) . ';'
    . 'echo CheckPipeline::resolveFpFastPath() ? "1" : "0";'
);
$cacheCmd = escapeshellarg(PHP_BINARY) . ' -r ' . escapeshellarg(
    'define("SHOW_SITE_MODE_SWITCH","ip_check");'
    . 'define("CLOAK_RISK_NUMBER",0);'
    . 'require ' . var_export($root . '/lib/Cloak/CloakJudgmentCache.php', true) . ';'
    . 'require ' . var_export($root . '/lib/Cloak/Pipeline/CheckPipeline.php', true) . ';'
    . '$_SESSION=[];'
    . 'CloakJudgmentCache::storeFromByApi(["ip"=>"1.1.1.1","country"=>"US","result"=>"true","reason"=>""]);'
    . 'echo CheckPipeline::resolveClientCacheFastPath() ? "1" : "0";'
);
$zpCacheCmd = escapeshellarg(PHP_BINARY) . ' -r ' . escapeshellarg(
    'define("SHOW_SITE_MODE_SWITCH","zp");'
    . 'define("CLOAK_RISK_NUMBER",0);'
    . 'require ' . var_export($root . '/lib/Cloak/CloakJudgmentCache.php', true) . ';'
    . 'require ' . var_export($root . '/lib/Cloak/Pipeline/CheckPipeline.php', true) . ';'
    . '$_SESSION=[];'
    . 'CloakJudgmentCache::storeFromByApi(["ip"=>"1.1.1.1","country"=>"US","result"=>"true","reason"=>""]);'
    . 'echo (CheckPipeline::resolveZpFastPath() && !CheckPipeline::resolveClientCacheFastPath()) ? "1" : "0";'
);
$tests['zp_before_cache'] = trim((string) shell_exec($zpCacheCmd)) === '1';
$tests['fp_mode_fast'] = trim((string) shell_exec($fpCmd)) === '1';
$tests['cache_on_ip_check'] = trim((string) shell_exec($cacheCmd)) === '1';
$tests['zp_mode_fast'] = trim((string) shell_exec($zpCmd)) === '1';

$fpNoRiskCmd = escapeshellarg(PHP_BINARY) . ' -r ' . escapeshellarg(
    'define("SHOW_SITE_MODE_SWITCH","fp");'
    . 'define("CLOAK_RISK_NUMBER",51);'
    . 'require ' . var_export($root . '/lib/Cloak/RiskSecondaryGate.php', true) . ';'
    . 'echo RiskSecondaryGate::isEnabled() && !RiskSecondaryGate::isEnabledForRequest() ? "1" : "0";'
);
$tests['fp_mode_skips_secondary_risk'] = trim((string) shell_exec($fpNoRiskCmd)) === '1';

$ok = !in_array(false, $tests, true);
echo json_encode(['ok' => $ok, 'tests' => $tests], JSON_UNESCAPED_UNICODE) . "\n";
exit($ok ? 0 : 1);
