<?php
/**
 * 子进程：运行 CheckPipeline fixture
 * 用法：php tools/run_pipeline_fixture.php <fixture.php>
 * 输出 JSON：{mode, result, reason, output_len, error}
 */
$root = dirname(__DIR__);
if ($argc < 2) {
    fwrite(STDERR, "usage: run_pipeline_fixture.php <fixture>\n");
    exit(2);
}

$fixture = $argv[1];
if (!is_readable($fixture)) {
    echo json_encode(['error' => 'fixture not readable']);
    exit(1);
}

$_SERVER['PHP_SELF']            = '/index.php';
$_SERVER['HTTP_HOST']           = 'test.local';
$_SERVER['REMOTE_ADDR']         = '8.8.8.8';
$_SERVER['HTTP_USER_AGENT']     = 'Mozilla/5.0 (Windows NT 10.0)';
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en-US';
$_SERVER['SERVER_PORT']         = 443;
$_SERVER['HTTPS']              = 'on';
$_SERVER['HTTP_REFERER']        = 'https://www.facebook.com/';
$_GET                            = ['fbclid' => 'IwAR' . str_repeat('z', 40)];

require_once $root . '/cong.php';
session_start();
$_SESSION = [];

include $fixture;

require_once $root . '/lib/bootstrap.php';

$v_info = new visitorInfo();
$v_info->get_visitor_Info(COSTM_IP_SCORE, CHECK_KEY, SHOW_SITE_COUNTRY);
$reason = '';
$__cloak_debug_on = defined('CLOAK_DEBUG_MODE') && strtoupper(CLOAK_DEBUG_MODE) === 'ON';

ob_start();
CheckPipeline::run();
$out = ob_get_clean();

echo json_encode([
    'mode'       => SHOW_SITE_MODE_SWITCH,
    'result'     => $_SESSION['check_result'] ?? null,
    'reason'     => $reason,
    'output_len' => strlen($out),
], JSON_UNESCAPED_UNICODE);
