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/AdminFabricContractTest.php

125 lines
6.6 KiB
PHP

<?php
// Static contract coverage for the administrator region-readiness boundary.
// This test deliberately does not boot the application or contact a host.
$root = dirname(__DIR__);
$read = static function (string $relative) use ($root): string {
$value = file_get_contents($root . '/' . $relative);
if (!is_string($value)) {
throw new RuntimeException('Unable to read ' . $relative);
}
return $value;
};
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$routes = $read('config/routes.php');
$cloudRoutes = $read('config/routes_cloud.php');
$middleware = $read('app/Middleware/CloudApi.php');
$client = $read('app/Services/Cloud/CloudEngineClient.php');
$controller = $read('app/Controllers/CloudAdminFabricController.php');
$service = $read('app/Services/Cloud/CloudAdminFabricService.php');
$page = $read('app/Controllers/Admin/CloudPageController.php');
$template = $read('resources/views/venix/admin/cloud/fabric.tpl');
$script = $read('public/cloud/admin-fabric.js');
$header = $read('resources/views/venix/admin/static_header.tpl');
$assert(str_contains($routes, "\$this->get('/fabric', 'App\\Controllers\\Admin\\CloudPageController:fabric')")
&& str_contains($header, 'href="/admin/fabric"')
&& str_contains($header, '<span>地区私网</span>')
&& str_contains($page, "fetch('admin/cloud/fabric.tpl')"),
'Fabric administrator page route, navigation or renderer is missing.');
foreach (['probe', 'apply', 'reconcile'] as $operation) {
$assert(str_contains(
$cloudRoutes,
"/admin/fabric/regions/{region_id:[0-9]+}/{$operation}"
), 'Region Fabric route is missing: ' . $operation);
}
$assert(str_contains($cloudRoutes, "\$this->get('/admin/fabric'")
&& strpos($cloudRoutes, "/admin/fabric/regions/{region_id:[0-9]+}/apply")
< strpos($cloudRoutes, "\$this->get('/admin/fabric/{fabric_id:"),
'Region workflows must be registered before the dynamic Fabric detail route.');
$assert(str_contains($middleware, "/api/v1/admin/fabric(?:/|$)")
&& str_contains($client, 'function fabricStatuses')
&& str_contains($client, 'function fabricProbe')
&& str_contains($client, 'function fabricApply')
&& str_contains($client, 'function fabricReconcile')
&& str_contains($client, 'safeFabricErrorMessage')
&& str_contains($client,
"'fabric_probe_busy' => '已有地区宿主机检测正在运行,请稍后重试'"),
'Region Fabric actions must use the protected PHP boundary and bounded engine client.');
$assert(str_contains($controller, 'CloudAccess::manage')
&& str_contains($controller, "['is_super_admin']")
&& str_contains($controller, 'assertEmptyBody')
&& str_contains($controller, 'probeRegion')
&& str_contains($controller, 'applyRegion')
&& str_contains($controller, 'reconcileRegion'),
'Region Fabric actions lost the super-admin or empty-body boundary.');
$assert(str_contains($service, "'regions' => self::readinessRegions")
&& str_contains($service, 'function probeRegion')
&& str_contains($service, 'function applyRegion')
&& str_contains($service, 'function reconcileRegion')
&& str_contains($service, 'assertConfigurableProbes')
&& str_contains($service, 'automaticFabricId')
&& str_contains($service, 'automaticOverlay')
&& str_contains($service, 'automaticNodes')
&& str_contains($service, 'CloudAdminResourceService::saveRegionFabric')
&& str_contains($service, "'ready_host_count'")
&& str_contains($service, "'blocked_host_count'")
&& str_contains($service, "'network_manager_expected'")
&& str_contains($service, "'fabric_member'"),
'Fabric service must own automatic plans and project region/host readiness.');
$assert(!str_contains($service, "'private_key'")
&& !str_contains($service, "'ssh_command'")
&& !str_contains($service, "'raw_output'"),
'Fabric admin projection must not accept or expose private credentials or raw commands.');
preg_match_all('/<button\b[^>]*>/', $template, $allButtons);
preg_match_all('/<button\b[^>]*data-fabric-primary-action\b[^>]*>/', $template, $primaryButtons);
$assert(count($allButtons[0]) === 3 && count($primaryButtons[0]) === 3
&& str_contains($template, 'id="fabric-detect-hosts"')
&& str_contains($template, '>检测宿主机')
&& str_contains($template, 'id="fabric-auto-configure"')
&& str_contains($template, '>自动配置地区私网')
&& str_contains($template, 'id="fabric-reconcile"')
&& str_contains($template, '>重新核对'),
'The normal Fabric page must expose exactly the three approved operations.');
$assert(str_contains($template, 'id="fabric-region-select"')
&& str_contains($template, '地区宿主机 readiness 清单')
&& str_contains($template, 'Fabric 只读回读')
&& str_contains($template, 'data-fabric-readonly-notice')
&& str_contains($template, '{if !$fabric_can_manage|default:false}disabled{/if}')
&& !str_contains($template, 'fabric-apply-form')
&& !str_contains($template, 'fabric-mapping-editor')
&& !str_contains($template, '参与宿主机 JSON')
&& !str_contains($template, '地区到 Fabric 映射'),
'The Fabric page did not remove manual plans/mapping edits or lost readiness content.');
$assert(str_contains($script, "api('/api/v1/admin/fabric')")
&& str_contains($script, "'/api/v1/admin/fabric/regions/'")
&& str_contains($script, "{ method: 'POST', body: '{}' }")
&& str_contains($script, "headers['X-CSRF-Token']")
&& str_contains($script, 'state.selectionVersion')
&& str_contains($script, 'state.loadVersion')
&& str_contains($script, 'state.pendingAction')
&& str_contains($script, 'applyActionProjection')
&& str_contains($script, 'originSelectionVersion')
&& str_contains($script, 'button.disabled = !canManage')
&& str_contains($script, 'payload.engine_available === false')
&& preg_match('/payload\.engine_available === false\)[\s\S]{0,500}return false;/',
$script) === 1
&& str_contains($script, "method === 'GET' ? 30000 : 240000")
&& str_contains($script, 'network_manager_expected')
&& str_contains($script, 'transport_verified')
&& str_contains($script, 'next_stage_allowed')
&& str_contains($script, 'textContent')
&& !str_contains($script, 'innerHTML')
&& !str_contains($script, '/internal/v1/')
&& !str_contains($script, 'fabric-apply-form')
&& !str_contains($script, 'fabric-mapping-editor'),
'Fabric browser module lost CSRF, stale-response, duplicate-action or safe-DOM guards.');
echo "Administrator region Fabric readiness contracts passed.\n";