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

137 lines
6.7 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
$root = dirname(__DIR__);
$read = static function (string $path) use ($root): string {
$source = file_get_contents($root . '/' . $path);
if (!is_string($source)) {
throw new RuntimeException('Unable to read ' . $path);
}
return $source;
};
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$readModel = $read('app/Services/Cloud/CloudTrafficPoolReadModel.php');
$pageService = $read('app/Services/Cloud/CloudInstancePageService.php');
$pageController = $read('app/Controllers/CloudPageController.php');
$apiController = $read('app/Controllers/CloudApiController.php');
$middleware = $read('app/Middleware/CloudApi.php');
$view = $read('app/Services/Cloud/CloudUserPageView.php');
$routes = $read('config/routes.php');
$apiRoutes = $read('config/routes_cloud.php');
$shell = $read('resources/views/venix/cloud/page.tpl');
$sidebar = $read('resources/views/venix/user/panel_sidebar.tpl');
$template = $read('resources/views/venix/cloud/pages/traffic.tpl');
$detail = $read('resources/views/venix/cloud/pages/detail.tpl');
$app = $read('public/cloud/app.js');
$script = $read('public/cloud/traffic.js');
$runtime = $read('public/cloud/runtime.js');
$styles = $read('public/cloud/styles.css');
$assets = $read('resources/views/venix/layout_assets.tpl');
$assert(str_contains($readModel,
"->where('traffic_pool.user_id', \$userId)")
&& str_contains($readModel, "->where('mode', 'enforced')")
&& str_contains($readModel, 'CloudTrafficPoolAccounting::snapshots(')
&& str_contains($readModel,
"CloudInstance::where('user_id', \$userId)")
&& str_contains($readModel,
"CloudServicePeriod::where('user_id', \$userId)")
&& str_contains($readModel,
"->where('traffic_quota_mode', 'finite')")
&& str_contains($readModel, "'projection_ready' => \$projectionReady")
&& str_contains($readModel, "'state' => \$projectionReady")
&& str_contains($readModel,
"'network_restriction_state' => \$networkState"),
'User traffic pools must be owner-scoped, enforced-only and derived from the canonical accounting projection.');
$assert(str_contains($readModel,
"'traffic_accounting_mode' => 'pooled'")
&& str_contains($readModel,
"'traffic_accounting_mode' => 'unlimited'")
&& str_contains($readModel,
"'traffic_accounting_mode' => 'legacy'")
&& substr_count($pageService,
'CloudTrafficPoolReadModel::forPeriod(') >= 2
&& str_contains($apiController,
'CloudTrafficPoolReadModel::forPeriods(')
&& str_contains($apiController,
"'traffic_accounting' => \$period === null ? null")
&& str_contains($runtime, "data.accountingMode === 'pooled'")
&& str_contains($runtime, 'data.progressUsed / data.quota'),
'SSR detail, lightweight status and fallback VM API must share one accounting-mode contract.');
$assert(str_contains($routes,
"'/cloud/traffic', 'App\\Controllers\\CloudPageController:trafficPools'")
&& str_contains($apiRoutes,
"'/traffic-pools', 'App\\Controllers\\CloudApiController:trafficPools'")
&& str_contains($pageController,
"return \$this->render(\$response, 'traffic'")
&& str_contains($pageController, "'cloud_traffic_bootstrap_json'")
&& str_contains($apiController,
"(int) \$actor['user_id']")
&& str_contains($middleware,
"\$path === '/api/v1/traffic-pools'")
&& !str_contains($apiController,
'public function trafficPools($request, $response, $args)'),
'The traffic-pool page and API must be reachable and derive identity only from the authenticated actor.');
$assert(str_contains($sidebar, 'href="/user/cloud/traffic"')
&& str_contains($shell, "cloud_page == 'traffic'")
&& str_contains($app, "traffic: '/cloud/traffic.js'")
&& str_contains($template, 'id="cloud-traffic-bootstrap"')
&& str_contains($template, 'id="cloud-traffic-list"')
&& str_contains($template, '下一批额度到期')
&& str_contains($template, '本实例已用')
&& str_contains($template, '累计总流量')
&& str_contains($template, '{$instance.lifetime_display|escape:')
&& str_contains($template, '“本实例已用”和 RX / TX 为当前周期采样')
&& str_contains($template, '“累计总流量”为实例生命周期累计值')
&& str_contains($template, '只显示已经进入强制执行的线路分组'),
'The user traffic-pool page lacks SSR first paint, navigation or clear shared-vs-instance copy.');
$assert(str_contains($script,
"api('/api/v1/traffic-pools', { signal: controller.signal })")
&& !str_contains($script, "method: 'POST'")
&& !str_contains($script, "method: 'PUT'")
&& !str_contains($script, "method: 'DELETE'")
&& !str_contains($script, '.reduce(')
&& !str_contains($script, '.innerHTML')
&& str_contains($script, 'pool.active_entitlement_bytes')
&& str_contains($script, 'pool.active_consumed_bytes')
&& str_contains($script, 'pool.available_bytes')
&& str_contains($script, 'pool.projection_ready === true')
&& str_contains($script,
"fact('累计总流量', formatBytes(instance.lifetime_traffic_bytes))")
&& str_contains($script, "preparing: ['额度准备中', 'warning']"),
'Browser code must only refresh the server projection and must not mutate, sum or inject traffic data.');
$assert(str_contains($view, 'function trafficPools(')
&& str_contains($view, "'network_restriction_state'")
&& str_contains($view, "'lifetime_display'")
&& str_contains($readModel, "'lifetime_traffic_bytes'")
&& str_contains($readModel, "'lifetime_download_bytes'")
&& str_contains($readModel, "'lifetime_upload_bytes'")
&& str_contains($detail, 'id="vm-detail-traffic-pool-link"')
&& str_contains($detail, '本实例本期下载(RX)')
&& str_contains($detail, '本实例本期上传(TX)')
&& str_contains($styles, '.cloud-traffic-facts')
&& str_contains($styles, '.cloud-traffic-instance')
&& str_contains($styles,
'.cloud-traffic-instance dl{display:grid;grid-template-columns:repeat(auto-fit,minmax(140px,1fr))')
&& str_contains($styles,
'.cloud-traffic-instance .state-pill{max-width:100%;white-space:normal')
&& str_contains($styles,
'@media(max-width:1100px){.cloud-traffic-instance{grid-template-columns:1fr}.cloud-traffic-instance dl{grid-template-columns:repeat(2,minmax(0,1fr))')
&& str_contains($styles, '@media(max-width:510px)')
&& preg_match(
"/venix_asset_version value='20261101[0-9]+'/",
$assets
) === 1,
'Traffic-pool formatting, detail semantics, responsive layout or asset revision is incomplete.');
echo "Cloud traffic-pool user UI contracts passed.\n";