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

92 lines
4.6 KiB
PHP

<?php
// Static contract test: PHP is not available in this development shell, so
// keep this check independent of Composer/DB while still making the CAS
// boundary reviewable in CI.
$root = dirname(__DIR__);
$repository = file_get_contents($root . '/app/Services/Cloud/CloudAgentOperationRepository.php');
$model = file_get_contents($root . '/app/Models/CloudAgentOperation.php');
$claim = file_get_contents($root . '/app/Services/Cloud/CloudAgentOperationClaim.php');
$exception = file_get_contents($root . '/app/Services/Cloud/CloudAgentOperationException.php');
$schema = file_get_contents($root . '/sql/cloud_agent_v1.sql');
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
foreach (['repository' => $repository, 'model' => $model, 'claim' => $claim, 'exception' => $exception, 'schema' => $schema] as $name => $source) {
$assert(is_string($source) && $source !== '', 'Missing Agent operation ' . $name . ' source.');
}
$assert(str_contains($model, "cloud_agent_operations")
&& str_contains($model, "desired_json")
&& str_contains($model, "lease_owner"),
'CloudAgentOperation model lost the fresh journal mapping.');
$assert(str_contains($repository, "private const TABLE = 'cloud_agent_operations'")
&& str_contains($repository, "function reserve")
&& str_contains($repository, "function claim")
&& str_contains($repository, "function finalize")
&& str_contains($repository, "function setCommitBoundary")
&& str_contains($repository, "function renew")
&& str_contains($repository, "function markUncertain")
&& str_contains($repository, "function get")
&& str_contains($repository, "function pruneReadOnly"),
'Agent operation repository is missing a required state transition.');
foreach ([
"->where('version'",
"->where('status'",
"->where('lease_owner'",
"->where('lease_expires_at'",
"'attempt_count' =>",
"'version' =>",
"lockForUpdate()",
'if ((int) $updated !== 1)',
"lease_lost",
"lease_expired",
"不确定操作禁止重新派发",
] as $required) {
$assert(str_contains($repository, $required),
'Agent repository lost CAS/lease guard: ' . $required);
}
$assert(str_contains($claim, 'must not be serialized')
&& str_contains($claim, '__unserialize')
&& str_contains($claim, 'dispatch\' && $this->status === \'running')
&& str_contains($claim, 'reconcile\' && $this->status === \'uncertain'),
'Agent operation claim can cross a queue serialization boundary.');
$assert(str_contains($exception, 'public readonly string $errorCode')
&& !str_contains($exception, 'public readonly string $code'),
'Agent operation exception must not redeclare Exception::$code.');
$assert(str_contains($repository, 'array_is_list($input[\'desired\'])')
&& str_contains($repository, 'validJsonKey')
&& str_contains($repository, 'isDigestValue')
&& str_contains($repository, "\$trimmed[0] !== '{'")
&& str_contains($repository, 'array_is_list($decoded)')
&& str_contains($repository, 'array_is_list($result) || $result === []')
&& str_contains($repository, 'if ($value === null)')
&& str_contains($repository, "Agent 操作 JSON 内容已损坏")
&& !str_contains($repository, "if (!is_string(\$value) || \$value === '') {\n return [];")
&& str_contains($repository, '成功操作必须有宿主 commit boundary'),
'Agent operation desired/result JSON needs an object and key-shape boundary.');
$assert(str_contains($schema, 'version BIGINT UNSIGNED NOT NULL DEFAULT 1')
&& str_contains($schema, 'attempt_count INT UNSIGNED')
&& str_contains($schema, 'host_id BIGINT UNSIGNED NOT NULL')
&& str_contains($schema, 'instance_id BIGINT UNSIGNED NULL')
&& str_contains($repository, '->where(\'instance_id\', $request[\'instance_id\'])')
&& str_contains($repository, "'host_id' => $hostId")
&& str_contains($repository, "'instance_id' => $instanceId")
&& str_contains($schema, 'UNIQUE KEY cloud_agent_instance_identity (agent_id, host_id, instance_id, instance_uuid)')
&& str_contains($schema, 'lease_owner VARCHAR(96)')
&& str_contains($schema, 'lease_expires_at BIGINT UNSIGNED'),
'Fresh schema lacks operation CAS/lease columns.');
foreach (['CloudJob', 'CloudEngineClient', 'ssh', 'curl_exec', 'shell_exec', 'CloudSecrets'] as $forbidden) {
$assert(!str_contains($repository, $forbidden),
'Fresh Agent repository crossed a legacy/transport boundary: ' . $forbidden);
}
echo "Cloud Agent operation repository contract checks passed.\n";