You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/CloudHostRegionDefinitionTe...

86 lines
5.4 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
// Static host-location contract: no application boot, database or engine connection.
$root = dirname(__DIR__);
$schema = file_get_contents($root . '/sql/cloud.sql');
$controller = file_get_contents($root . '/app/Controllers/CloudAdminController.php');
$pageController = file_get_contents($root . '/app/Controllers/Admin/CloudPageController.php');
$legacyPageController = file_get_contents($root . '/app/Controllers/Admin/HostController.php');
$catalog = file_get_contents($root . '/app/Services/Cloud/CloudCountryCatalog.php');
$template = file_get_contents($root . '/resources/views/venix/admin/hosts/index.tpl');
$client = file_get_contents($root . '/public/cloud/admin.js');
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$assert(str_contains($schema, "country_code CHAR(2) NOT NULL DEFAULT ''")
&& str_contains($schema,
"ALTER TABLE cloud_hosts ADD COLUMN IF NOT EXISTS\n country_code CHAR(2) NOT NULL DEFAULT '' AFTER region"),
'Fresh and upgraded cloud_hosts schemas must contain the structured country code.');
$assert(str_contains($controller, "'region', 'country_code'")
&& str_contains($controller,
"CloudCountryCatalog::normalize(\$body['country_code'] ?? null)")
&& !str_contains($controller, 'HOST_COUNTRY_CODES')
&& str_contains($controller,
"unset(\$body['name'], \$body['enabled'], \$body['country_code']);")
&& str_contains($controller, '$host->country_code = $countryCode;'),
'Host writes must validate and persist country_code without forwarding it to the engine.');
$unsetAt = strpos($controller,
"unset(\$body['name'], \$body['enabled'], \$body['country_code']);");
$enginePutAt = strpos($controller, "'PUT', 'host', \$actor, \$body");
$assert($unsetAt !== false && $enginePutAt !== false && $unsetAt < $enginePutAt,
'country_code must be removed before the strict engine HostBody receives the request.');
$assert(str_contains($catalog, "'CN' => ['name' => '中国'")
&& str_contains($catalog, "'HK' => ['name' => '中国香港'")
&& str_contains($catalog, "'JP' => ['name' => '日本'")
&& str_contains($catalog, "'US' => ['name' => '美国'")
&& str_contains($catalog, "'SG' => ['name' => '新加坡'")
&& str_contains($catalog, '$code = strtoupper(trim($value));')
&& !str_contains($catalog, 'safeText'),
'The shared country catalog must normalize complete values without truncation.');
$assert(str_contains($pageController,
"assign('cloud_country_options', CloudCountryCatalog::options())")
&& str_contains($legacyPageController,
"assign('cloud_country_options', CloudCountryCatalog::options())")
&& str_contains($template, '{foreach $cloud_country_options as $country}')
&& str_contains($template, '{$country.name|escape}')
&& str_contains($template, '({$country.code|escape})'),
'Host country selector must render the shared readable country catalog.');
$assert(str_contains($template, 'id="host-country-code"')
&& preg_match('/id="host-region"[^>]*required[^>]*maxlength="64"/', $template) === 1
&& str_contains($controller, '具体地区需为 1 至 64 字符'),
'Host editor must require a country and an exact region within the engine limit.');
$assert(str_contains($client, "'host-country-code': h.country_code || ''")
&& str_contains($client,
"country_code: byId('host-country-code').value"),
'Host editor must round-trip country_code through the admin API.');
$assert(str_contains($template,
'id="host-dns" class="form-control admin-mono" placeholder="例如 1.1.1.1, 8.8.8.8"')
&& !str_contains($template, 'id="host-dns" class="form-control admin-mono" value=')
&& str_contains($client, 'function hostPublicNetworkPayload()')
&& str_contains($client,
"'host-dns': Array.isArray(h.public_dns_servers) ? h.public_dns_servers.join(', ') : ''")
&& str_contains($client, '...publicNetwork'),
'A new host without a public pool must submit empty DNS and one normalized network payload.');
$normalizeAt = strpos($controller, 'self::normalizeHostPublicNetwork($body);');
$newHostAt = strpos($controller, '$host = isset($args[\'id\'])');
$assert($normalizeAt !== false && $newHostAt !== false && $normalizeAt < $newHostAt,
'Partial public-network input must fail before a host placeholder is created.');
$privateKeyCheckAt = strpos($controller, 'if (!$hasPrivateKey)', $newHostAt);
$newHostSaveAt = strpos($controller, '$host->save();', $newHostAt);
$assert(str_contains($template, 'id="host-private-key-help"')
&& str_contains($client, 'state.hostConfigured = h.configured === true;')
&& str_contains($client, 'if (!state.hostConfigured && !privateKey)')
&& str_contains($client, '该宿主机尚未完成引擎配置,必须重新粘贴现有 SSH 私钥')
&& str_contains($controller, "(\$engineHost['configured'] ?? false) !== true")
&& str_contains($controller, "new CloudError('首次添加宿主机必须提供现有 SSH 私钥', 422)")
&& str_contains($controller, "new CloudError('该宿主机尚未完成引擎配置,请重新粘贴现有 SSH 私钥', 422)")
&& $privateKeyCheckAt !== false && $newHostSaveAt !== false
&& $privateKeyCheckAt < $newHostSaveAt && $privateKeyCheckAt < $enginePutAt,
'Only an engine-configured host may retain its saved private key without resubmitting it.');
echo "Structured cloud host country and region contracts passed.\n";