'11111111-1111-4111-8111-111111111111', 'user_id' => 42, 'user_name' => '演示客户', 'user_email' => 'd***@example.test', 'user_url' => '/admin/user/42/edit', 'host_id' => 7, 'host_name' => '香港宿主机 A', 'region' => 'HongKong', 'plan_name' => '标准云服务器', 'os_name' => 'Debian 12', 'vcpu' => 2, 'memory_mib' => 2048, 'disk_gib' => 40, 'instance_status' => 'active', 'service_status' => 'active', 'network_status' => 'active', 'service_expires_at' => $now + 4 * 86400, 'traffic_used_bytes' => 21474836480, 'traffic_quota_bytes' => 107374182400, 'traffic_synced_at' => $now - 60, 'created_at' => $now - 86400, 'active_job' => null, 'state' => 'unknown', 'can_manage' => true, 'display' => [ 'configuration' => '2 vCPU · 2 GiB · 40 GiB', 'traffic' => '20 GiB / 100 GiB', 'expiry' => '4 天后到期', 'service' => '服务正常', 'network' => '网络正常', 'lifecycle' => '在管', ], ]; $items = [ array_replace($common, ['id' => '101', 'name' => 'vm-production', 'public_ip' => '192.0.2.101', 'detail_url' => '/user/cloud/instances/101']), array_replace($common, ['id' => '102', 'name' => 'vm-network-limited', 'public_ip' => '192.0.2.102', 'detail_url' => '/user/cloud/instances/102', 'network_status' => 'blocked_traffic', 'traffic_used_bytes' => 107374182400, 'display' => array_replace($common['display'], [ 'traffic' => '100 GiB / 100 GiB', 'network' => '流量耗尽已断网', ])]), array_replace($common, ['id' => '103', 'name' => 'vm-task-failed', 'public_ip' => '192.0.2.103', 'detail_url' => '/user/cloud/instances/103', 'active_job' => ['id' => '61', 'kind' => 'reinstall', 'completed' => true, 'status' => 'failed', 'blocks_mutation' => true, 'effect_may_have_applied' => true, 'safe_to_resubmit' => false, 'recovery' => ['pending' => true, 'message' => '系统仍会继续自动核对。'], 'attempts' => 1, 'error' => '任务失败', 'phase' => null, 'created_at' => $now - 300, 'updated_at' => $now - 10]]), ]; $page = [ 'items' => $items, 'pagination' => ['page' => 1, 'per_page' => 20, 'total' => 43, 'total_pages' => 3, 'from' => 1, 'to' => 3], 'summary' => ['total' => 43, 'expiring' => 3, 'network_restricted' => 1, 'attention' => 1], 'filters' => ['q' => '', 'user_id' => null, 'host_id' => null, 'service_status' => '', 'expiry' => 'all', 'attention' => 'all', 'sort' => 'newest', 'page' => 1, 'per_page' => 20], 'hosts' => [['id' => 7, 'name' => '香港宿主机 A', 'region' => 'HongKong']], 'generated_at' => $now, ]; $detail = [ 'instance' => array_replace($items[0], [ 'public_ip_cidr' => '192.0.2.101/24', 'gateway' => '192.0.2.1', 'dns_servers' => ['1.1.1.1', '8.8.8.8'], 'engine_name' => 'vc-1111111111114111', 'bandwidth_mbps' => 100, 'os_source' => 'cloud_template', 'network_change_supported' => true, 'instance_settings_supported' => true, 'login_name' => 'root', 'current_period_started_at' => $now - 26 * 86400, 'current_period_ends_at' => $now + 4 * 86400, 'renewal_price' => '10.00', 'renewal_days' => 30, 'renewal_traffic_bytes' => 107374182400, 'traffic_download_bytes' => 10737418240, 'traffic_upload_bytes' => 10737418240, 'updated_at' => $now - 60, ]), 'jobs' => [['id' => '62', 'kind' => 'action', 'status' => 'succeeded', 'attempts' => 1, 'error' => null, 'phase' => 'complete', 'created_at' => $now - 3600, 'updated_at' => $now - 3500]], 'jobs_truncated' => false, 'invoices' => [['id' => '201', 'purpose' => 'provision', 'status' => 'paid', 'amount' => '10.00', 'created_at' => $now - 86400, 'detail_url' => '/admin/billing?detail=201']], 'invoices_truncated' => false, ]; $smarty = new Smarty(); $compileDir = sys_get_temp_dir() . '/admin-instance-view-' . bin2hex(random_bytes(6)); mkdir($compileDir); $smarty->setCompileDir($compileDir); $smarty->setTemplateDir(dirname(__DIR__) . '/resources/views/venix'); $smarty->assign('config', ['appName' => 'Venix Test']); $smarty->assign('cdn', ''); $smarty->assign('user', (object) ['isLogin' => false]); $smarty->assign('admin_can_manage', true); $smarty->assign('admin_csrf_token', 'fixture-not-a-real-token'); $render = static function (?array $payload, string $error = '') use ($smarty): string { $smarty->assign('instance_page', $payload); $smarty->assign('instance_error', $error); $smarty->assign('instance_bootstrap_json', json_encode($payload, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE)); return $smarty->fetch('admin/cloud/instances.tpl'); }; $assert = static function (bool $ok, string $message): void { if (!$ok) throw new RuntimeException($message); }; $html = $render($page); $clientScript = file_get_contents(dirname(__DIR__) . '/public/cloud/admin-instances.js'); $openDetailStart = strpos($clientScript, 'async function openDetail(id, opener)'); $openDetailEnd = $openDetailStart === false ? false : strpos($clientScript, 'async function copy(value)', $openDetailStart); $openDetailSource = $openDetailStart === false || $openDetailEnd === false ? '' : substr($clientScript, $openDetailStart, $openDetailEnd - $openDetailStart); $scrollReset = strpos($openDetailSource, 'drawerBody.scrollTop = 0;'); $drawerOpen = strpos($openDetailSource, 'window.VenixDrawerMotion.openDrawerWithMotion(dialog)'); $assert($scrollReset !== false && $drawerOpen !== false && $scrollReset < $drawerOpen, 'Opening or switching instance detail must reset the reusable drawer body to the top.'); $assert(str_contains($clientScript, 'if (vm.network_change_supported !== true)') && str_contains($clientScript, 'if (vm.instance_settings_supported !== true)') && str_contains($clientScript, "vm.instance_status !== 'active'") && str_contains($clientScript, 'function networkOptionsErrorView(') && !str_contains($clientScript, 'Older deployments can still use the existing detail fields'), 'Unsupported, inactive or failed administrator controls must not render guessed network constraints or actionable settings.'); if (in_array('--fixture', $argv ?? [], true)) { $smarty->assign('billing_page', null); $smarty->assign('billing_view', 'invoices'); $smarty->assign('billing_error', '账单预览未加载数据'); $smarty->assign('billing_bootstrap_json', 'null'); echo json_encode(['html' => $html, 'billing_html' => $smarty->fetch('admin/billing.tpl'), 'page' => $page, 'detail' => $detail], JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE); exit; } // The table is usable without waiting for JavaScript or any host connection. $dom = new DOMDocument(); libxml_use_internal_errors(true); $dom->loadHTML('' . $html); $xpath = new DOMXPath($dom); $assert($xpath->query('//tbody//tr')->length === 3, 'SSR did not render all fixture rows.'); $assert(str_contains($dom->textContent, '192.0.2.101') && str_contains($dom->textContent, '20 GiB / 100 GiB') && str_contains($dom->textContent, '香港宿主机 A') && str_contains($dom->textContent, '已失败') && str_contains($dom->textContent, '资源安全锁定,仍会继续自动核对'), 'Useful instance data or terminal safety-lock state is missing before scripts run.'); $assert(str_contains($clientScript, "task?.completed === false && task.status === 'active'") && str_contains($clientScript, "({ succeeded: '已成功', failed: '已失败' })") && str_contains($clientScript, 'task?.blocks_mutation !== true') && !str_contains($clientScript, 'needs_attention'), 'Administrator instance controls must use active/terminal receipts and retain failed-operation safety locks.'); $assert($xpath->query('//select[@name="per_page"]/option[@value="20"]')->length === 1 && $xpath->query('//select[@name="per_page"]/option[@selected and @value!="20"]')->length === 0, 'SSR did not retain the current page size.'); $unsafe = $page; $unsafe['items'][0]['name'] = ''; $unsafe['items'][0]['user_name'] = ''; $unsafe['items'][0]['host_name'] = '" onmouseover="alert(2)'; $unsafeHtml = $render($unsafe); $dom->loadHTML('' . $unsafeHtml); $xpath = new DOMXPath($dom); $assert($xpath->query('//*[@onerror or @onmouseover]')->length === 0, 'Untrusted row content escaped into an executable attribute.'); $assert(!str_contains($unsafeHtml, '') && str_contains($dom->textContent, ''), 'Untrusted names must render as text, not active markup.'); $empty = $page; $empty['items'] = []; $empty['pagination'] = ['page' => 1, 'per_page' => 20, 'total' => 0, 'total_pages' => 1, 'from' => 0, 'to' => 0]; $empty['summary'] = ['total' => 0, 'expiring' => 0, 'network_restricted' => 0, 'attention' => 0]; $emptyHtml = $render($empty); $assert(!str_contains($emptyHtml, 'vm-production'), 'Empty result retained an old instance.'); $failedHtml = $render(null, '实例信息暂不可用,请重试'); $assert(str_contains($failedHtml, '实例信息暂不可用,请重试') && !str_contains($failedHtml, 'vm-production'), 'Unavailable data was disguised as a successful list.'); echo "Administrator instance SSR, escaping and empty/error rendering passed.\n";