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

151 lines
8.8 KiB
PHP

<?php
$root = dirname(__DIR__);
$servicePath = $root . '/app/Services/Cloud/CloudAdminOverviewService.php';
$controllerPath = $root . '/app/Controllers/AdminController.php';
$templatePath = $root . '/resources/views/venix/admin/index.tpl';
$headerPath = $root . '/resources/views/venix/admin/static_header.tpl';
$footerPath = $root . '/resources/views/venix/admin/static_footer.tpl';
$assetsPath = $root . '/resources/views/venix/layout_assets.tpl';
$sharedStylePath = $root . '/public/theme/venix/css/admin-home.css';
$removedStylePath = $root . '/public/cloud/admin-overview.css';
$schemaPath = $root . '/sql/cloud.sql';
$baseSchemaPath = $root . '/sql/ss.sql';
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$assert(is_file($servicePath) && is_file($sharedStylePath) && !is_file($removedStylePath),
'Administrator overview must use the shared legacy administrator stylesheet.');
$service = file_get_contents($servicePath);
$controller = file_get_contents($controllerPath);
$template = file_get_contents($templatePath);
$header = file_get_contents($headerPath);
$footer = file_get_contents($footerPath);
$assets = file_get_contents($assetsPath);
$styles = file_get_contents($sharedStylePath);
$schema = file_get_contents($schemaPath);
$baseSchema = file_get_contents($baseSchemaPath);
$assert(str_contains($controller, 'CloudAdminOverviewService::snapshot()')
&& str_contains($controller, 'CloudAdminOverviewService::unavailable()')
&& str_contains($controller, "'overview_error' => \$overviewError"),
'Administrator landing page must render a database snapshot with a safe fallback.');
$assert(str_contains($service, 'CloudInvoice')
&& str_contains($service, 'CloudInstance')
&& str_contains($service, 'CloudJob')
&& str_contains($service, 'CloudHost')
&& !str_contains($service, 'App\\Models\\Code')
&& !str_contains($service, 'Bought')
&& !str_contains($service, 'CloudEngineClient')
&& !str_contains($service, 'CloudEngineSession'),
'Overview metrics must use cloud business tables without legacy ledgers or engine calls.');
$assert(str_contains($service, "where('status', 'paid')")
&& str_contains($service, "whereNotNull('paid_at')")
&& str_contains($service, "where('status', '<>', 'deleted')")
&& str_contains($service, "where('kind', 'create')")
&& str_contains($service, "whereNotNull('order_id')")
&& preg_match("/whereIn\\('status',\\s*\\[\\s*'queued',\\s*'running',\\s*'uncertain',\\s*'needs_attention',\\s*\\]\\s*\\)/s", $service) === 1
&& str_contains($service,
"status = 'needs_attention' THEN 1 ELSE 0 END) AS needs_attention_count")
&& str_contains($service,
"'needs_attention' => (int) (\$row?->needs_attention_count ?? 0)")
&& str_contains($service, "status = 'failed' AND updated_at")
&& str_contains($service, "status = 'active' AND (service_status = 'exempt'")
&& str_contains($service, 'updated_at >= {$todayStart}')
&& str_contains($service, "modify('+1 day')"),
'Financial, instance and job overview boundaries are incomplete.');
$assert(str_contains($service, "foreach (\$window['trend'] as \$index => \$day)")
&& substr_count($service, 'CloudInvoice::where') <= 3,
'Revenue trend must use bounded aggregate queries rather than one query per day.');
$assert(str_contains($schema, 'cloud_invoice_overview_paid (status, paid_at, purpose, plan_id)')
&& str_contains($schema, 'cloud_job_overview_status_updated (status, updated_at)')
&& str_contains($schema, 'cloud_job_overview_kind_created (kind, created_at)')
&& str_contains($baseSchema, '`ticket_overview_root_status` (`rootid`,`status`)'),
'Overview range queries must have bounded operational indexes.');
$assert(str_contains($template, 'card overflow-hidden admin-stat-card')
&& str_contains($template, 'row g-3 admin-overview-kpis')
&& substr_count($template, 'admin-stat-value d-block mb-0') === 4
&& substr_count($template, 'admin-stat-footer') === 4
&& !preg_match('/card overflow-hidden admin-stat-card h-100/', $template)
&& str_contains($template, 'admin-progress-list')
&& str_contains($template, 'admin-live-grid mb-0')
&& str_contains($template, 'admin-report-list')
&& str_contains($template, 'admin-log-links')
&& str_contains($template, 'admin-shortcuts')
&& str_contains($template, '数据库快照')
&& str_contains($template, '已启用宿主机配置')
&& str_contains($template, '不推断真实开关机状态')
&& str_contains($template, '退款后会从历史有效实收中移除')
&& str_contains($template, '/admin/billing?view=orders&amp;status=needs_attention')
&& str_contains($template, '自动核对已停止')
&& substr_count($template, 'card overflow-hidden admin-stat-card') === 4
&& !preg_match('/text-bg-(?:success|info|warning|danger)/', $template)
&& str_contains($template, 'bg-danger-subtle text-danger-emphasis')
&& str_contains($template, 'bg-warning-subtle text-warning-emphasis')
&& !str_contains($template, 'admin-overview.css')
&& !str_contains($template, 'class="vo-')
&& !str_contains($template, '$sts->')
&& !str_contains($template, '在线宿主机'),
'Overview template must reuse legacy administrator components, state its data source and avoid invented live state.');
foreach (['/admin/cloud/instances', '/admin/billing', '/admin/hosts', '/admin/ticket'] as $href) {
$assert(str_contains($template, 'href="' . $href),
'Overview shortcut missing: ' . $href);
}
$assert(str_contains($header, '/theme/venix/css/admin-home.css')
&& !str_contains($header, 'admin_extra_css_href')
&& str_contains($styles, '.admin-stat-card .card-body')
&& str_contains($styles, '.admin-stat-card .admin-stat-value')
&& str_contains($styles, '.admin-overview-kpis > [class*="col-"]')
&& str_contains($styles, '.admin-overview-kpis .admin-stat-link')
&& preg_match('/\.admin-live-grid\s*\{[^}]*grid-template-columns:\s*repeat\(2,\s*minmax\(0,\s*1fr\)\)/s', $styles)
&& str_contains($styles, '.admin-progress-list')
&& str_contains($styles, '.admin-shortcuts')
&& str_contains($styles, '.admin-log-links')
&& str_contains($styles, '@media (max-width: 991.98px)')
&& str_contains($styles, '@media (max-width: 575.98px)'),
'Overview must inherit the existing administrator component and responsive rules.');
$assert(preg_match('/\.admin-template-body\s*\{(?=[^}]*min-height:\s*100dvh)(?=[^}]*padding-bottom:\s*0\s*!important)[^}]*\}/s', $styles)
&& !str_contains($styles, '--admin-viewport-height')
&& preg_match('/\.admin-template#app-layout\s*\{(?=[^}]*height:\s*auto\s*!important)(?=[^}]*overflow:\s*visible\s*!important)[^}]*\}/s', $styles)
&& preg_match('/\.admin-template \.content-page\s*\{(?=[^}]*display:\s*flex)(?=[^}]*flex-direction:\s*column)[^}]*\}/s', $styles)
&& preg_match('/\.admin-template \.content\s*\{(?=[^}]*flex:\s*1 0 auto)[^}]*\}/s', $styles)
&& preg_match('/\.admin-template \.footer\s*\{(?=[^}]*position:\s*static)[^}]*\}/s', $styles)
&& preg_match('/\.admin-template \.app-sidebar-menu\s*\{(?=[^}]*position:\s*fixed)(?=[^}]*inset:\s*0 auto 0 0)(?=[^}]*height:\s*auto)(?=[^}]*overflow-y:\s*auto)[^}]*\}/s', $styles)
&& preg_match('/\.admin-template-body\.admin-sidebar-open \.admin-sidebar-backdrop\s*\{(?=[^}]*position:\s*fixed)(?=[^}]*inset:\s*0\s*;)(?=[^}]*height:\s*auto)[^}]*\}/s', $styles)
&& str_contains($styles, '.admin-template-body.admin-sidebar-no-transition .app-sidebar-menu')
&& !str_contains($footer, 'measureViewportHeight')
&& !str_contains($footer, 'scheduleViewportHeight')
&& !str_contains($footer, 'window.visualViewport')
&& !str_contains($footer, "window.addEventListener('pagehide', closeSidebar)")
&& str_contains($footer, "window.addEventListener('pageshow'")
&& str_contains($footer, 'event.persisted')
&& str_contains($footer, 'closeSidebarImmediately();')
&& substr_count($footer, 'window.requestAnimationFrame(function ()') >= 2
&& str_contains($footer, 'target.pathname === current.pathname')
&& str_contains($footer, 'target.search === current.search')
&& str_contains($footer, 'target.hash === current.hash')
&& str_contains($footer, 'event.preventDefault();')
&& !preg_match('/link\.addEventListener\(\'click\'.*?window\.innerWidth <= 991.*?closeSidebar\(\);/s', $footer),
'Mobile administrator navigation must cover the viewport and preserve same-page and BFCache behavior.');
$assetVersion = [];
$assert(preg_match("/venix_asset_version value='([0-9]+)'/", $assets, $assetVersion) === 1
&& (int) $assetVersion[1] >= 2026093004
&& str_contains($assets, 'cache key'),
'This administrator asset release must use cache key 2026093004 or newer.');
echo 'Administrator operational overview contracts passed.' . PHP_EOL;