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/CloudDeploymentPageSsrContr...

205 lines
13 KiB
PHP

<?php
// Static regression contract for deployment/reinstall first paint. The HTML
// response owns all data needed to draw the form; JavaScript only hydrates
// interactions and must not briefly replace the catalog with loading states.
$root = dirname(__DIR__);
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$section = static function (string $source, string $start, string $end): string {
$offset = strpos($source, $start);
if ($offset === false) {
return '';
}
$limit = strpos($source, $end, $offset + strlen($start));
return $limit === false ? substr($source, $offset)
: substr($source, $offset, $limit - $offset);
};
$controller = file_get_contents($root . '/app/Controllers/CloudPageController.php');
$service = file_get_contents($root . '/app/Services/Cloud/CloudDeploymentPageService.php');
$deployTemplate = file_get_contents($root . '/resources/views/venix/cloud/pages/deploy.tpl');
$reinstallTemplate = file_get_contents($root . '/resources/views/venix/cloud/pages/reinstall.tpl');
$deployModule = file_get_contents($root . '/public/cloud/deploy.js');
$reinstallModule = file_get_contents($root . '/public/cloud/reinstall.js');
$runtime = file_get_contents($root . '/public/cloud/runtime.js');
$cloudShell = file_get_contents($root . '/resources/views/venix/cloud/page.tpl');
$layoutAssets = file_get_contents($root . '/resources/views/venix/layout_assets.tpl');
$deployController = $section($controller, 'public function deploy(',
'public function isoImages(');
$reinstallController = $section($controller, 'public function reinstall(',
'public function rescue(');
$openCreate = $section($runtime, 'async function openCreate(',
'async function loadGlobalTemplates(');
$openReinstall = $section($runtime, 'async function openReinstall(',
'async function pollJob(');
$selectRegion = $section($runtime, 'async function selectRegion(',
'async function selectPlan(');
$selectPlan = $section($runtime, 'async function selectPlan(',
'async function openCreate(');
$sshKeyOptions = $section($controller, 'private function sshKeyOptions(',
'private function reinstallVm(');
$assert(str_contains($deployController, 'CloudAccess::actor(false)')
&& str_contains($deployController, 'CloudDeploymentPageService::publicPagePlans()')
&& str_contains($deployController, 'CloudDeploymentPageService::regions($plans)')
&& str_contains($deployController, 'CloudDeploymentPageService::templateCatalogForPage($actor)')
&& str_contains($deployController, '$this->sshKeyOptions((int) $actor[\'user_id\'])')
&& str_contains($deployController,
'$defaultHostId = $regions === [] ? 0 : (int) $regions[0][\'host_id\']')
&& str_contains($deployController, "'catalog_unavailable' => \$catalogUnavailable")
&& str_contains($deployController,
"'cloud_deploy_catalog_unavailable' => \$catalogUnavailable")
&& str_contains($deployController, "'cloud_deploy_bootstrap_json' => \$this->safeJson(\$payload)"),
'Deployment controller must build one complete, actor-scoped first-paint payload.');
$assert(str_contains($sshKeyOptions, "'id' => \$key['id']")
&& str_contains($sshKeyOptions, "'name' => \$key['name']")
&& str_contains($sshKeyOptions, "'fingerprint' => \$key['fingerprint']")
&& str_contains($sshKeyOptions, 'CloudDeploymentPageService::sshKeys($userId)')
&& !str_contains($sshKeyOptions, "'public_key'")
&& !str_contains($sshKeyOptions, "'created_at'"),
'Deployment bootstraps must expose only safe SSH key display metadata.');
$assert(str_contains($deployTemplate, 'class="mdi mdi-key-variant ssh-key-choice-icon"')
&& str_contains($deployTemplate, 'class="ssh-key-choice-fingerprint"')
&& str_contains($deployTemplate, 'class="ssh-key-choice-check"')
&& str_contains($deployTemplate, ',指纹 {$key.fingerprint|escape:"html"}')
&& str_contains($reinstallTemplate, 'class="mdi mdi-key-variant ssh-key-choice-icon"')
&& str_contains($reinstallTemplate, 'class="ssh-key-choice-fingerprint"')
&& str_contains($reinstallTemplate, 'class="ssh-key-choice-check"')
&& str_contains($reinstallTemplate, ',指纹 {$key.fingerprint|escape:"html"}'),
'Deployment and reinstall first paint must render the compact SSH key picker consistently.');
$assert(str_contains($service, "->orderBy('sort')->orderBy('id')")
&& str_contains($service, "->where('enabled', 1)")
&& str_contains($service, 'if ((int) $plan->stock === 0)')
&& str_contains($service, 'return array_values($regions);')
&& str_contains($service, "unset(\$plan['host_name'])"),
'Available plans and regions must retain administrator sale order and omit private host labels.');
$assert(str_contains($service, "private const TEMPLATE_SNAPSHOT_KEY")
&& str_contains($service, 'TEMPLATE_SNAPSHOT_REFRESH_SECONDS')
&& str_contains($service, "'checked_at' => time()")
&& str_contains($service, "'catalog_unavailable' =>")
&& str_contains($service, 'Configurations::where(')
&& str_contains($service, 'self::sanitizeTemplates(')
&& str_contains($service, "'id' => \$template['id']")
&& str_contains($service, "'os_family' => \$template['os_family']")
&& str_contains($service, "'os_version' => \$template['os_version']"),
'The first-paint image catalog must use a durable, sanitized public snapshot.');
$assert(str_contains($deployTemplate, 'id="deploy-bootstrap" type="application/json"')
&& str_contains($deployTemplate, '{$cloud_deploy_bootstrap_json nofilter}')
&& str_contains($deployTemplate, '{foreach $cloud_deploy_regions as $region}')
&& str_contains($deployTemplate, '$region.host_id == $cloud_deploy_default_host_id')
&& str_contains($deployTemplate, '{foreach $region.plans as $plan}')
&& str_contains($deployTemplate, '{foreach $cloud_deploy_template_groups as $group}')
&& str_contains($deployTemplate, '{foreach $cloud_deploy_ssh_keys as $key}')
&& str_contains($deployTemplate, '$cloud_deploy_catalog_unavailable')
&& str_contains($deployTemplate, '系统镜像目录暂时无法读取')
&& !str_contains($deployTemplate, '请先选择地区')
&& !str_contains($deployTemplate, '正在加载'),
'Deployment HTML must immediately show regions, first-region plans, images and saved keys.');
$assert(str_contains($deployModule, "readBootstrap('deploy-bootstrap')")
&& str_contains($deployModule, "initializeSession('create', bootstrap?.user)")
&& str_contains($deployModule, 'openCreate(bootstrap)')
&& !str_contains($deployModule, '/api/v1/session')
&& !str_contains($deployModule, '/api/v1/plans')
&& !str_contains($deployModule, '/api/v1/resources')
&& !str_contains($deployModule, '/api/v1/ssh-keys'),
'Deployment page initialization must hydrate embedded data without a second data request.');
$deployHydrationEnd = strpos($openCreate, ' return;');
$deployFallback = strpos($openCreate, 'Promise.allSettled([');
$assert($openCreate !== ''
&& str_contains($openCreate, 'Array.isArray(bootstrap.plans)')
&& str_contains($openCreate, 'Array.isArray(bootstrap.templates)')
&& str_contains($openCreate, "bootstrapItems(bootstrap, 'regions')")
&& str_contains($openCreate, 'bootstrap.catalog_unavailable === true')
&& str_contains($openCreate, 'bootstrap.default_host_id')
&& str_contains($openCreate, "(regions[0]?.hostId || '')")
&& str_contains($openCreate, "renderTemplateCatalog('vm-template')")
&& $deployHydrationEnd !== false && $deployFallback !== false
&& $deployHydrationEnd < $deployFallback
&& !str_contains(substr($openCreate, 0, $deployHydrationEnd), "api('/api/v1/"),
'Deployment hydration must select the requested/first region and paint all data before any fallback.');
$assert($selectRegion !== '' && $selectPlan !== ''
&& !str_contains($selectRegion, 'resetInstallChoices()')
&& !str_contains($selectRegion, 'renderTemplateCatalog(')
&& !str_contains($selectRegion, 'loadGlobalTemplates(')
&& !str_contains($selectRegion, "byId('vm-template').value")
&& !str_contains($selectPlan, 'resetInstallChoices()')
&& !str_contains($selectPlan, 'renderTemplateCatalog(')
&& !str_contains($selectPlan, 'loadGlobalTemplates(')
&& !str_contains($selectPlan, "byId('vm-template').value"),
'Changing a region or plan must never clear or reload the global image selection.');
$assert(str_contains($reinstallController, 'CloudAccess::actor(false)')
&& str_contains($reinstallController, 'CloudInstancePageService::detailForActor($actor, $vmId)')
&& str_contains($reinstallController, '$vm = $this->reinstallVm($vm)')
&& str_contains($reinstallController, 'CloudDeploymentPageService::templateCatalogForPage($actor)')
&& str_contains($reinstallController, '$this->sshKeyOptions((int) $actor[\'user_id\'])')
&& str_contains($reinstallController, "'catalog_unavailable' => \$catalogUnavailable")
&& str_contains($reinstallController,
"'cloud_reinstall_catalog_unavailable' => \$catalogUnavailable")
&& str_contains($reinstallController, "'cloud_reinstall_bootstrap_json' => \$this->safeJson(\$payload)"),
'Reinstall controller must embed an owner-scoped VM plus its image and key choices.');
$assert(str_contains($reinstallTemplate, 'id="reinstall-bootstrap" type="application/json"')
&& str_contains($reinstallTemplate, '{$cloud_reinstall_bootstrap_json nofilter}')
&& str_contains($reinstallTemplate, '$cloud_reinstall_vm.name')
&& str_contains($reinstallTemplate, '$cloud_reinstall_vm.public_ip')
&& str_contains($reinstallTemplate, '{foreach $cloud_reinstall_template_groups as $group}')
&& str_contains($reinstallTemplate, '{foreach $cloud_reinstall_ssh_keys as $key}')
&& str_contains($reinstallTemplate, '$cloud_reinstall_catalog_unavailable')
&& str_contains($reinstallTemplate, '系统镜像目录暂时无法读取')
&& !str_contains($reinstallTemplate, '正在加载'),
'Reinstall HTML must immediately show the VM, network, image catalog and saved keys.');
$assert(str_contains($reinstallTemplate, '$cloud_reinstall_vm.can_operate')
&& str_contains($reinstallTemplate, 'inert aria-disabled="true"')
&& str_contains($reinstallTemplate, '当前不能重装系统')
&& str_contains($openReinstall, "byId('reinstall-submit').disabled = true"),
'A policy-blocked instance must expose reinstall information without an actionable destructive form.');
$assert(str_contains($reinstallModule, "readBootstrap('reinstall-bootstrap')")
&& str_contains($reinstallModule, "initializeSession('reinstall', bootstrap?.user)")
&& str_contains($reinstallModule,
'selectVmByApiId(document.body.dataset.vmId || \'\', bootstrap?.vm)')
&& str_contains($reinstallModule, 'openReinstall(vm, bootstrap)')
&& !str_contains($reinstallModule, '/api/v1/session')
&& !str_contains($reinstallModule, '/api/v1/vms')
&& !str_contains($reinstallModule, '/api/v1/resources')
&& !str_contains($reinstallModule, '/api/v1/ssh-keys'),
'Reinstall initialization must use embedded VM/catalog data without initial data APIs.');
$reinstallBootstrapStart = strpos($openReinstall,
'if (bootstrap && Array.isArray(bootstrap.templates))');
$reinstallHydrationEnd = $reinstallBootstrapStart === false ? false
: strpos($openReinstall, ' return;', $reinstallBootstrapStart);
$reinstallFallback = strpos($openReinstall, 'api(`/api/v1/resources?vm_id=');
$assert($openReinstall !== ''
&& str_contains($openReinstall, 'Array.isArray(bootstrap.templates)')
&& str_contains($openReinstall, 'bootstrap.catalog_unavailable === true')
&& str_contains($openReinstall, "bootstrapItems(bootstrap, 'ssh_keys', 'keys')")
&& str_contains($openReinstall, "renderTemplateCatalog('reinstall-template')")
&& str_contains($openReinstall, "renderKeyCards('reinstall')")
&& $reinstallHydrationEnd !== false && $reinstallFallback !== false
&& $reinstallHydrationEnd < $reinstallFallback
&& !str_contains(substr($openReinstall, 0, $reinstallHydrationEnd), '/api/v1/resources'),
'Reinstall hydration must paint embedded choices before its compatibility fallback.');
$assert(str_contains($controller, 'JSON_HEX_TAG')
&& str_contains($controller, "'csrf_token' => AdminCsrf::token()")
&& str_contains($controller, "withHeader('Cache-Control', 'private, no-store')"),
'Embedded deployment data must remain script-safe, authenticated and non-cacheable.');
$assert(preg_match("/venix_asset_version value='[0-9]+'/", $layoutAssets) === 1
&& str_contains($cloudShell, "{include file='layout_assets.tpl'}")
&& str_contains($cloudShell, '?v={$venix_asset_version}')
&& str_contains($cloudShell, 'data-asset-version="{$venix_asset_version|escape:"html"}"')
&& str_contains($deployModule,
"const assetVersion = new URL(import.meta.url).searchParams.get('v');")
&& str_contains($deployModule, "runtimeUrl.searchParams.set('v', assetVersion)")
&& str_contains($reinstallModule,
"const assetVersion = new URL(import.meta.url).searchParams.get('v');")
&& str_contains($reinstallModule, "runtimeUrl.searchParams.set('v', assetVersion)"),
'Cloud page and dynamically imported runtime assets must share one cache-busting version.');
echo "Deployment and reinstall server-rendered first-paint contract passed.\n";