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.
124 lines
7.4 KiB
PHTML
124 lines
7.4 KiB
PHTML
|
1 month ago
|
<?php
|
||
|
|
|
||
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
||
|
|
set_error_handler(static function (int $severity, string $message, string $file, int $line): bool {
|
||
|
|
if ((error_reporting() & $severity) && ($severity & (E_WARNING | E_USER_WARNING | E_DEPRECATED))) {
|
||
|
|
throw new ErrorException($message, 0, $severity, $file, $line);
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
});
|
||
|
|
$assert = static function (bool $ok, string $message): void {
|
||
|
|
if (!$ok) throw new RuntimeException($message);
|
||
|
|
};
|
||
|
|
$now = time();
|
||
|
|
$attempt = [
|
||
|
|
'attempt_id' => str_repeat('a', 32), 'host_id' => 7, 'host_name' => '示例宿主机 A',
|
||
|
|
'source' => 'manual', 'source_label' => '管理员手动同步',
|
||
|
|
'started_at' => $now - 20, 'finished_at' => $now - 12, 'duration_ms' => 8010,
|
||
|
|
'duration_is_final' => true, 'outcome' => 'timeout', 'outcome_label' => '超时',
|
||
|
|
'stage' => 'capacity_request', 'stage_label' => '请求容量数据',
|
||
|
|
'error_code' => 'engine_http_timeout', 'error_message' => '请求超时;引擎内部阶段尚未确认',
|
||
|
|
'applied' => false, 'timeout_ms' => 8000, 'panel_version' => 'inventory-diagnostics-v1',
|
||
|
|
'php_version' => '8.5.0', 'http_status' => null, 'generation' => 12,
|
||
|
|
'stages' => [['stage' => 'capacity_request', 'label' => '请求容量数据', 'duration_ms' => 8000]],
|
||
|
|
];
|
||
|
|
$page = [
|
||
|
|
'items' => [$attempt],
|
||
|
|
'pagination' => ['page' => 1, 'per_page' => 20, 'total' => 21, 'total_pages' => 2, 'from' => 1, 'to' => 1],
|
||
|
|
'filters' => ['host_id' => '7', 'outcome' => 'timeout', 'page' => 1, 'per_page' => 20],
|
||
|
|
'hosts' => [['id' => '7', 'name' => '示例宿主机 A']],
|
||
|
|
'summary' => ['last_attempt_at' => $now - 20, 'last_success_at' => $now - 100,
|
||
|
|
'last_failure_at' => $now - 12, 'in_progress' => 0, 'consecutive_failures' => 1],
|
||
|
|
];
|
||
|
|
$smarty = new Smarty();
|
||
|
|
$compileDir = sys_get_temp_dir() . '/admin-inventory-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', false);
|
||
|
|
$smarty->assign('admin_csrf_token', 'fixture-not-a-real-token');
|
||
|
|
$render = static function (?array $payload, string $error = '') use ($smarty): string {
|
||
|
|
$smarty->assign('inventory_page', $payload);
|
||
|
|
$smarty->assign('inventory_error', $error);
|
||
|
|
$smarty->assign('inventory_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/host_inventory.tpl');
|
||
|
|
};
|
||
|
|
$html = $render($page);
|
||
|
|
if (in_array('--fixture', $argv ?? [], true)) {
|
||
|
|
echo json_encode(['html' => $html, 'page' => $page,
|
||
|
|
'detail' => ['attempt' => $attempt, 'notes' => []]], JSON_UNESCAPED_UNICODE);
|
||
|
|
exit;
|
||
|
|
}
|
||
|
|
$dom = new DOMDocument();
|
||
|
|
libxml_use_internal_errors(true);
|
||
|
|
$dom->loadHTML('<?xml encoding="UTF-8">' . $html);
|
||
|
|
$xpath = new DOMXPath($dom);
|
||
|
|
$assert($xpath->query('//tbody[@id="inventory-body"]/tr')->length === 1,
|
||
|
|
'Inventory history must render on the server.');
|
||
|
|
$assert(str_contains($dom->textContent, 'engine_http_timeout')
|
||
|
|
&& str_contains($dom->textContent, '容量未更新') && str_contains($dom->textContent, '管理员手动同步'),
|
||
|
|
'Initial rows must expose diagnostic results without JavaScript.');
|
||
|
|
$assert($xpath->query('//select[@name="host_id"]/option[@value="7" and @selected]')->length === 1
|
||
|
|
&& $xpath->query('//select[@name="outcome"]/option[@value="timeout" and @selected]')->length === 1,
|
||
|
|
'Server-rendered filters must preserve the requested host and outcome.');
|
||
|
|
$assert($xpath->query('//dialog[@data-admin-instance-drawer and @id="inventory-detail"]')->length === 1,
|
||
|
|
'Diagnostic details must reuse the Venix drawer.');
|
||
|
|
$assert($xpath->query('//section[@id="inventory-engine" and @hidden]')->length === 1,
|
||
|
|
'Engine evidence must not pretend to be loaded during server rendering.');
|
||
|
|
$assert($xpath->query('//main[@data-timezone="UTC"]')->length === 1
|
||
|
|
&& str_contains($html, '时间采用面板时区 UTC'),
|
||
|
|
'SSR and JavaScript timestamps must share an explicit server time zone.');
|
||
|
|
$assert($xpath->query('//button[contains(@id,"sync") or contains(@id,"retry") or contains(@id,"delete")]')->length === 0,
|
||
|
|
'The diagnostic page must not expose mutating controls.');
|
||
|
|
|
||
|
|
$unsafe = $page;
|
||
|
|
$unsafe['items'][0]['host_name'] = '</script><img src=x onerror=alert(1)>';
|
||
|
|
$unsafe['items'][0]['error_message'] = '<script>window.unsafe=1</script>';
|
||
|
|
$unsafe['hosts'][0]['name'] = '" onmouseover="alert(2)';
|
||
|
|
$unsafeHtml = $render($unsafe);
|
||
|
|
$dom->loadHTML('<?xml encoding="UTF-8">' . $unsafeHtml);
|
||
|
|
$xpath = new DOMXPath($dom);
|
||
|
|
$assert($xpath->query('//*[@onerror or @onmouseover]')->length === 0
|
||
|
|
&& !str_contains($unsafeHtml, '<script>window.unsafe=1</script>')
|
||
|
|
&& str_contains($dom->textContent, '</script><img src=x onerror=alert(1)>'),
|
||
|
|
'Database labels must be escaped in both templates and JSON bootstraps.');
|
||
|
|
$empty = $page;
|
||
|
|
$empty['items'] = [];
|
||
|
|
$empty['pagination']['total'] = 0;
|
||
|
|
$assert(str_contains($render($empty), '新功能只记录启用后的盘点尝试'), 'Empty history requires an honest explanation.');
|
||
|
|
$failedHtml = $render(null, '数据库暂不可读');
|
||
|
|
$assert(str_contains($failedHtml, '数据库暂不可读') && !str_contains($failedHtml, '已读取 0 条'),
|
||
|
|
'Unavailable records must not be rendered as an empty successful list.');
|
||
|
|
$unknown = $page;
|
||
|
|
$unknown['items'][0] = array_replace($attempt, ['outcome' => 'interrupted',
|
||
|
|
'outcome_label' => '中断 / 未知', 'finished_at' => null, 'duration_is_final' => false]);
|
||
|
|
$assert(str_contains($render($unknown), '是否生效待核对') && str_contains($render($unknown), '已记录 8010 ms'),
|
||
|
|
'Interrupted attempts must not claim final duration or definite non-application.');
|
||
|
|
|
||
|
|
$root = dirname(__DIR__);
|
||
|
|
$controller = file_get_contents($root . '/app/Controllers/CloudAdminHostInventoryController.php');
|
||
|
|
$pageController = file_get_contents($root . '/app/Controllers/Admin/CloudPageController.php');
|
||
|
|
$routes = file_get_contents($root . '/config/routes_cloud.php');
|
||
|
|
$middleware = file_get_contents($root . '/app/Middleware/CloudApi.php');
|
||
|
|
$js = file_get_contents($root . '/public/cloud/admin-host-inventory.js');
|
||
|
|
$assert(str_contains($routes, "'/admin/host-inventory/{id:[0-9a-f]{32}}/engine'")
|
||
|
|
&& str_contains($middleware, '^/api/v1/admin/host-inventory(?:/|$)')
|
||
|
|
&& str_contains($controller, 'CloudAccess::actor(false)'),
|
||
|
|
'Diagnostic APIs need signed-in admin identity without creating engine sessions.');
|
||
|
|
$assert(str_contains($pageController, "'inventory_bootstrap_json'")
|
||
|
|
&& str_contains($controller, 'CloudHostInventoryDiagnostics::detail($actor, $attemptId)'),
|
||
|
|
'Engine evidence must authorize and bind host identity through a persisted attempt.');
|
||
|
|
$assert(str_contains($controller, 'new CloudEngineClient(null, 1.0)')
|
||
|
|
&& str_contains($controller, '->inventoryDiagnostics(')
|
||
|
|
&& !str_contains($controller, '->inventory(') && !str_contains($controller, 'syncOne('),
|
||
|
|
'Evidence retrieval must be a bounded read-only lookup, never another inventory.');
|
||
|
|
$assert(!str_contains($js, 'innerHTML') && !str_contains($js, "method: 'POST'")
|
||
|
|
&& !str_contains($js, '/capacity-sync') && !str_contains($js, 'setInterval('),
|
||
|
|
'UI must be text-only and must not poll or run inventory.');
|
||
|
|
echo "Administrator inventory SSR, escaping, read-only routes and diagnostic boundaries passed.\n";
|