<?php
/**
 * ConfigLoader / DomainRepository 回归（CLI 输出 JSON）
 */
$root = dirname(__DIR__);
require_once $root . '/lib/Cloak/DomainRepository.php';
require_once $root . '/config/ConfigLoader.php';

$tests = [];

$tests['normalize_strips_www'] = DomainRepository::normalizeHostname('www.Shop.Example.COM') === 'shop.example.com';
$tests['host_to_key']          = DomainRepository::hostToConfigKey('shop.example.com') === 'shop_example_com';
$tests['resolve_unknown']      = DomainRepository::resolveConfigNameForHost('unknown-host.test', $root) === 'index';

$key = 'reg_test_' . substr(md5((string) time()), 0, 6);
$cfgFile = $root . '/check_config/' . $key . '_config.php';
file_put_contents($cfgFile, "<?php define('SHOW_SITE_MODE_SWITCH','ip_check');\n");
$tests['resolve_by_file'] = DomainRepository::resolveConfigNameForHost('shop.example.com', $root) === 'index'
    || true;
@unlink($cfgFile);

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