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

75 lines
3.7 KiB
PHP

<?php
// Source-level contract for the VM operation-log cutover. PHP/Composer are
// optional in the development shell, so the test verifies the boundary and
// DTO shape without booting the application or a database.
$root = dirname(__DIR__);
$read = static function (string $path) use ($root): string {
$value = file_get_contents($root . '/' . $path);
if (!is_string($value) || $value === '') {
throw new RuntimeException('Missing Agent operation-history source: ' . $path);
}
return $value;
};
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$service = $read('app/Services/Cloud/CloudAgentOperationHistoryService.php');
$repository = $read('app/Services/Cloud/CloudAgentOperationRepository.php');
$controller = $read('app/Controllers/CloudApiController.php');
$routes = $read('config/routes_cloud.php');
$middleware = $read('app/Middleware/CloudApi.php');
$start = strpos($controller, 'public function operationLogs(');
$end = strpos($controller, '/** Database-only read', $start === false ? 0 : $start);
$method = $start === false
? ''
: substr($controller, $start, $end === false ? null : $end - $start);
$assert(str_contains($repository, 'function historyForInstance(')
&& str_contains($repository, 'where(\'instance_uuid\', $instanceUuid)')
&& str_contains($repository, 'where(\'agent_id\', $agentId)')
&& str_contains($repository, 'where(\'host_id\', $hostId)')
&& str_contains($repository, 'where(\'instance_id\', $instanceId)')
&& str_contains($repository, 'whereIn(\'kind\', $normalizedKinds)')
&& str_contains($repository, 'self::publicRecord($row)'),
'Agent operation repository lost the validated per-instance history query.');
$assert(str_contains($repository, 'if ($page < 1 || $perPage < 1 || $perPage > 50)')
&& str_contains($repository, "->orderByDesc('created_at')->orderByDesc('id')"),
'Agent history query is not bounded or deterministically ordered.');
$assert(str_contains($service, "private const HISTORY_KINDS = ['domain_power']")
&& str_contains($service, 'historyForInstance(')
&& str_contains($service, '$agentId')
&& str_contains($service, '(int) ($instance->host_id ?? 0)')
&& str_contains($service, '(int) ($instance->id ?? 0)')
&& str_contains($service, 'return $kind === \'domain_power\' ? \'action\' : null')
&& str_contains($service, 'if ($action === \'destroy\')')
&& str_contains($service, 'CloudOperationErrorCatalog::result(')
&& str_contains($service, '\'kind_label\' => $labels[\'kind_label\']'),
'Agent history projection lost explicit domain_power/action mapping or result redaction.');
$assert($start !== false && $method !== ''
&& str_contains($method, 'CloudAccess::instance($args[\'id\'], $actor)')
&& str_contains($method, 'agentOperationHistoryService()')
&& !str_contains($method, 'CloudJobService::operationLogs')
&& !str_contains($method, 'CloudEngineClient')
&& !str_contains($method, 'shell_exec')
&& !str_contains($method, 'curl_exec')
&& !str_contains($method, 'ssh'),
'VM operation-log route must use the fresh Agent projection after owner/admin lookup, without legacy transport.');
$assert(str_contains($routes, "'/vms/{id:[0-9]+}/operations'")
&& str_contains($middleware, '/api/v1/vms/[0-9]+/operations$#D'),
'The existing VM operation-log route or database-only middleware boundary changed.');
foreach (['CloudJobService::', 'CloudEngineClient', 'shell_exec', 'curl_exec'] as $forbidden) {
$assert(!str_contains($service, $forbidden),
'Agent operation-history service crossed a legacy boundary: ' . $forbidden);
}
fwrite(STDOUT, "Cloud Agent operation-history contract passed.\n");