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

254 lines
10 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\CloudJob;
use App\Services\Cloud\CloudJobService;
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$job = static function (array $overrides = []): CloudJob {
$now = time();
$model = new CloudJob();
$model->setRawAttributes(array_replace([
'id' => 91,
'operation_id' => '11111111111111111111111111111111',
'request_idempotency_key' => 'resolved-task-fixture-91',
'request_fingerprint' => str_repeat('2', 64),
'kind' => 'action',
'execution_mode' => 'agent',
'parent_job_id' => null,
'host_id' => 7,
'phase' => 'terminal',
'outcome' => 'failed',
'status' => 'failed',
'dispatch_attempts' => 1,
'attempts' => 1,
'dispatch_deadline_at' => $now - 20,
'dispatch_started_at' => $now - 18,
'agent_accepted_at' => $now - 17,
'dispatched_at' => $now - 18,
'receipt_deadline_at' => $now + 900,
'error_code' => 'operation_failed_effect_unknown',
'result_json' => null,
'effect_may_have_applied' => 1,
'safe_to_resubmit' => 0,
'effect_resolution' => 'unknown',
'finalization_incident' => 1,
'finalization_error_code' => 'recovery_evidence_conflict',
'available_at' => $now + 20,
'terminal_at' => $now - 5,
'read_at' => null,
'sealed_payload' => '',
'order_id' => null,
'created_at' => $now - 30,
'updated_at' => $now,
], $overrides), true);
return $model;
};
$publicResultCases = [
'rescue' => [
'raw' => [
'active' => true,
'staged' => false,
'pending_manual_restart' => false,
'template_id' => '0123456789abcdef0123456789abcdef',
'network_suspended' => true,
],
'expected' => [
'active' => true,
'staged' => false,
'pending_manual_restart' => false,
'network_suspended' => true,
],
],
'network_suspend' => [
'raw' => ['network_enabled' => false],
'expected' => ['network_enabled' => false],
],
'network_resume' => [
'raw' => ['network_enabled' => true],
'expected' => ['network_enabled' => true],
],
];
foreach ($publicResultCases as $kind => $case) {
$display = CloudJobService::display($job([
'kind' => $kind,
'outcome' => 'succeeded',
'status' => 'succeeded',
'error_code' => null,
'result_json' => json_encode($case['raw'], JSON_THROW_ON_ERROR),
'effect_may_have_applied' => 0,
'safe_to_resubmit' => 0,
'effect_resolution' => 'applied',
'finalization_incident' => 0,
'finalization_error_code' => null,
]));
$assert(($display['result'] ?? null) === $case['expected'],
'Terminal task lost its public operation result: ' . $kind);
}
foreach (['applied', 'not_applied'] as $resolution) {
$display = CloudJobService::display($job([
'effect_resolution' => $resolution,
'finalization_incident' => 0,
'finalization_error_code' => null,
]));
$assert(($display['effect_resolution'] ?? null) === $resolution,
'Resolved terminal failure did not expose its bounded effect resolution.');
$assert(($display['status'] ?? null) === 'failed'
&& ($display['completed'] ?? null) === true
&& ($display['effect_may_have_applied'] ?? null) === true
&& ($display['safe_to_resubmit'] ?? null) === false,
'Effect convergence rewrote the original public failure contract.');
$assert(!array_key_exists('recovery', $display),
'A converged failure was still presented as awaiting recovery.');
}
$partialAggregate = $job([
'kind' => 'template_import',
'execution_mode' => 'aggregate',
'user_id' => 1,
'host_id' => null,
'attempts' => 0,
'dispatch_attempts' => 0,
'dispatch_started_at' => null,
'agent_accepted_at' => null,
'dispatched_at' => null,
'completion_event_id' => null,
'effect_resolution' => 'applied',
'effect_resolved_at' => time() - 1,
'effect_resolution_reason' => 'child_effects_resolved:93:mixed',
'finalization_incident' => 0,
'finalization_error_code' => null,
]);
$assert(CloudJobService::effectScope($partialAggregate) === 'partial',
'A converged mixed aggregate was exposed as an all-applied failure.');
$forgedLeafPartial = $job([
'effect_resolution' => 'applied',
'effect_resolved_at' => time() - 1,
'effect_resolution_reason' => 'child_effects_resolved:93:mixed',
'finalization_incident' => 0,
'finalization_error_code' => null,
]);
$assert(CloudJobService::effectScope($forgedLeafPartial) === null,
'A non-aggregate task forged the partial aggregate display marker.');
$stickyPartial = $job([
'kind' => 'template_delete',
'execution_mode' => 'aggregate',
'host_id' => null,
'effect_resolution' => 'applied',
'effect_resolved_at' => time() - 1,
'effect_resolution_reason' => 'legacy_child_effects_resolved:94:mixed',
]);
$assert(CloudJobService::effectScope($stickyPartial) === null,
'A sticky aggregate incident was hidden behind the partial-result notice.');
$stickyResolved = CloudJobService::display($job([
'effect_resolution' => 'applied',
]));
$assert(($stickyResolved['recovery']['pending'] ?? null) === true
&& str_contains(
(string) ($stickyResolved['recovery']['message'] ?? ''),
'Agent 回执与宿主机证据暂不一致'
),
'A resolved task with a sticky incident was removed from recovery.');
$recoveryMessages = [
'recovery_limit_reached' => '旧任务已重新进入持续自动核对',
'recovery_receipt_limit_reached' => '旧任务回执已重新进入持续自动核对',
'recovery_state_invalid' => '任务状态与恢复记录暂不一致',
'recovery_evidence_conflict' => 'Agent 回执与宿主机证据暂不一致',
'recovery_evidence_partial' => '只能确认部分操作结果',
'recovery_evidence_inconclusive' => '证据暂不足以确认操作结果',
'recovery_evidence_invalid' => '核对或栅栏证据格式不匹配',
'recovery_receipt_invalid' => 'Agent 回执未通过完整性核验',
'recovery_completion_outbox_invalid' => '完成回执队列状态损坏',
'recovery_completion_receipt_missing' => '完成回执内容缺失',
'recovery_completion_receipt_invalid' => '完成回执内容无效',
'recovery_completion_operation_missing' =>
'完成回执缺少对应原操作记录',
'recovery_completion_operation_invalid' =>
'完成回执对应原操作记录无效',
'recovery_completion_identity_mismatch' =>
'完成回执身份与原操作记录不匹配',
'recovery_payload_unavailable' => '恢复数据暂时无法安全读取',
'recovery_operation_missing' => '暂未找到原任务记录',
'recovery_authentication_failed' => '认证暂未通过',
'recovery_operation_conflict' => '任务编号与宿主机记录暂不一致',
'recovery_operation_identity_conflict' =>
'操作、资源或请求指纹与原任务不匹配',
'recovery_waiting_connection' => 'Agent 或网络暂不可用',
'recovery_instance_state_conflict' => '实例状态与任务证据暂不一致',
'recovery_ledger_conflict' => '面板账本暂未安全收敛',
'recovery_internal_error' => '自动核对暂时遇到内部异常',
];
foreach ($recoveryMessages as $code => $message) {
$recovering = $job(['finalization_error_code' => $code]);
$display = CloudJobService::display($recovering);
$assert(($display['recovery']['pending'] ?? null) === true
&& str_contains((string) ($display['recovery']['message'] ?? ''),
$message)
&& str_contains((string) ($display['recovery']['message'] ?? ''),
'仍会继续自动核对')
&& ($display['recovery']['last_checked_at'] ?? null)
=== (int) $recovering->updated_at
&& ($display['recovery']['next_check_at'] ?? null)
=== (int) $recovering->available_at,
'Recovering task did not expose a safe Chinese incident summary: '
. $code);
$assert(!array_key_exists('code', $display['recovery']),
'The ordinary task DTO exposed an internal recovery code.');
$adminDisplay = CloudJobService::display($recovering, true);
$assert(($adminDisplay['recovery']['code'] ?? null) === $code,
'The administrator DTO lost its bounded recovery diagnosis code.');
}
$unknownIncident = CloudJobService::display($job([
'finalization_error_code' => 'recovery_future_' . str_repeat('x', 200),
]), true);
$assert(($unknownIncident['recovery']['code'] ?? null) === 'recovery_pending'
&& !str_contains(json_encode($unknownIncident['recovery'],
JSON_UNESCAPED_UNICODE), str_repeat('x', 20)),
'An unrecognized server recovery error leaked through the safe DTO.');
$aggregate = CloudJobService::display($job([
'kind' => 'template_import',
'execution_mode' => 'aggregate',
'finalization_incident' => 0,
'finalization_error_code' => null,
]));
$assert(($aggregate['recovery']['pending'] ?? null) === true
&& str_contains((string) $aggregate['recovery']['message'], '子任务'),
'Aggregate task did not explain that child convergence remains active.');
foreach (['create', 'payment_create', 'host_storage_configure'] as $kind) {
$display = CloudJobService::display($job(['kind' => $kind]));
$assert(!array_key_exists('recovery', $display),
'A task with a dedicated recovery policy was mislabeled as generic recovery: '
. $kind);
}
foreach ([
['effect_resolution' => 'unknown'],
['effect_resolution' => 'not_applicable'],
['effect_resolution' => 'internal_reviewed'],
['effect_resolution' => 'applied', 'effect_may_have_applied' => 0],
['effect_resolution' => 'applied', 'outcome' => 'succeeded',
'status' => 'succeeded'],
] as $overrides) {
$display = CloudJobService::display($job($overrides));
$assert(!array_key_exists('effect_resolution', $display),
'Public task DTO exposed a non-applicable or untrusted effect resolution.');
}
echo "Cloud resolved task presentation checks passed.\n";