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

94 lines
4.6 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';
$stylePath = $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($stylePath),
'Administrator overview service or scoped stylesheet is missing.');
$service = file_get_contents($servicePath);
$controller = file_get_contents($controllerPath);
$template = file_get_contents($templatePath);
$styles = file_get_contents($stylePath);
$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')")
&& str_contains($service, "whereIn('status', ['queued', 'running', 'uncertain'])")
&& 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($schema, 'ticket_overview_root_status (rootid, status)')
&& str_contains($baseSchema, '`ticket_overview_root_status` (`rootid`,`status`)'),
'Overview range queries must have bounded operational indexes.');
$assert(str_contains($template, '<main class="admin-overview">')
&& str_contains($template, "admin_extra_css_href value='/cloud/admin-overview.css'")
&& str_contains($template, '数据库快照')
&& str_contains($template, '已启用宿主机配置')
&& str_contains($template, '不推断真实开关机状态')
&& str_contains($template, '退款后会从历史有效实收中移除')
&& substr_count($template, '<article class="vo-kpi') === 4
&& substr_count($template, '<h2>') >= 4
&& !str_contains($template, '$sts->')
&& !str_contains($template, '在线宿主机'),
'Overview template must 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($styles, 'min-height: 44px')
&& str_contains($styles, ':focus-visible')
&& str_contains($styles, '@media (max-width: 720px)')
&& str_contains($styles, '@media (max-width: 480px)')
&& str_contains($styles, '@media (prefers-reduced-motion: reduce)')
&& !str_contains($styles, 'font-size: 10px')
&& !str_contains($styles, 'font-size: 11px')
&& !str_contains($styles, '.vo-plan-row > span:nth-of-type(2)')
&& !str_contains($styles, 'min-width: 1120px'),
'Overview responsive, touch and keyboard accessibility contracts are incomplete.');
echo 'Administrator operational overview contracts passed.' . PHP_EOL;