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

157 lines
7.9 KiB
PHP

<?php
$root = dirname(__DIR__);
$read = static function (string $path) use ($root): string {
$value = file_get_contents($root . '/' . $path);
if (!is_string($value)) {
throw new RuntimeException('Unable to read ' . $path);
}
return $value;
};
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$routes = $read('config/routes_cloud.php');
$controller = $read('app/Controllers/CloudApiController.php');
$options = $read(
'app/Services/Cloud/CloudNetworkChangeOptionsService.php'
);
$billing = $read('app/Services/Cloud/CloudIpChangeBillingService.php');
$jobs = $read('app/Services/Cloud/CloudJobService.php');
$worker = $read('app/Services/Cloud/CloudWorkerService.php');
$config = $read('app/Controllers/Admin/ConfigController.php');
$configView = $read('resources/views/venix/admin/config.tpl');
$detailView = $read('resources/views/venix/cloud/pages/detail.tpl');
$detailScript = $read('public/cloud/detail.js');
$invoiceService = $read('app/Services/Cloud/CloudInvoiceService.php');
$adminBilling = $read('app/Services/Cloud/CloudAdminBillingService.php');
$documentation = $read('../docs/MULTI_PUBLIC_IPV4_POOLS.md');
$assert(str_contains($routes,
"'/vms/{id:[0-9]+}/network-options'")
&& str_contains($routes, "'/vms/{id:[0-9]+}/network'")
&& str_contains($routes, 'CloudApiController:networkOptions')
&& str_contains($routes, 'CloudApiController:network'),
'Tenant paid IP-change routes are missing.');
$optionsStart = strpos($controller, 'public function networkOptions(');
$networkStart = strpos($controller, 'public function network(',
$optionsStart === false ? 0 : $optionsStart);
$networkEnd = strpos($controller, 'public function rescue(',
$networkStart === false ? 0 : $networkStart);
$optionsMethod = $optionsStart !== false && $networkStart !== false
? substr($controller, $optionsStart, $networkStart - $optionsStart) : '';
$networkMethod = $networkStart !== false && $networkEnd !== false
? substr($controller, $networkStart, $networkEnd - $networkStart) : '';
$assert($optionsMethod !== ''
&& str_contains($optionsMethod,
'(int) $instance->user_id !== (int) $actor[\'user_id\']')
&& str_contains($optionsMethod,
'CloudNetworkChangeOptionsService::forInstance($instance)')
&& str_contains($optionsMethod, 'CloudIpChangeBillingService::price()')
&& str_contains($optionsMethod, "'available_ip_options'")
&& !str_contains($optionsMethod, "'host_id'")
&& !str_contains($optionsMethod, "'gateway'")
&& !str_contains($optionsMethod, "'dns_servers'"),
'Tenant IP choices lost ownership, pricing, or safe projection boundaries.');
$assert($networkMethod !== ''
&& str_contains($networkMethod, "'request_idempotency_key'")
&& str_contains($networkMethod, "'expected_amount'")
&& str_contains($networkMethod, 'CloudIpChangeBillingService::normalizePrice')
&& str_contains($networkMethod, '$expectedAmount')
&& str_contains($networkMethod, "'task_id' => (string) \$job->id")
&& str_contains($networkMethod,
"'status_url' => '/api/v1/tasks/' . \$job->id")
&& !str_contains($networkMethod, "'invoice_id'")
&& !str_contains($networkMethod, "'balance' =>"),
'Paid IP submission lost its quote check, idempotency, or strict task receipt.');
$assert(str_contains($options, "->where('state', 'available')")
&& str_contains($options, "->whereNull('reservation_id')")
&& str_contains($options, "->whereNull('instance_id')")
&& str_contains($options, "->whereNull('operation_id')")
&& str_contains($options, "->where('state', 'active')")
&& str_contains($options, "->where('sync_state', 'applied')"),
'Tenant choices can include disabled, assigned, held, or unapplied IPs.');
$enqueueStart = strpos($jobs, 'public static function enqueue(');
$enqueueEnd = strpos($jobs, 'public static function enqueueExpiryDelete(',
$enqueueStart === false ? 0 : $enqueueStart);
$enqueue = $enqueueStart !== false && $enqueueEnd !== false
? substr($jobs, $enqueueStart, $enqueueEnd - $enqueueStart) : '';
$walletLock = strpos($enqueue, "User::where('id'");
$instanceLock = strpos($enqueue, "CloudInstance::where('id'");
$priorLookup = strpos($enqueue,
"->where('request_idempotency_key', \$requestKey)");
$priceLookup = strpos($enqueue,
'CloudIpChangeBillingService::priceCents()');
$assert($enqueue !== ''
&& str_contains($enqueue, 'CloudIpChangeBillingService::PURPOSE')
&& str_contains($enqueue, "\$requestValue['expected_amount']")
&& str_contains($enqueue, '$locked->serviceAvailable()')
&& str_contains($enqueue, 'CloudIpChangeBillingService::chargeLocked(')
&& $walletLock !== false && $instanceLock !== false
&& $walletLock < $instanceLock
&& $priorLookup !== false && $priceLookup !== false
&& $priorLookup < $priceLookup,
'Paid enqueue lost wallet-first locking or replay-before-charge semantics.');
$assert(str_contains($billing,
"public const PRICE_CONFIG_KEY = 'cloud_ip_change_price'")
&& str_contains($billing, "public const DEFAULT_PRICE = '0.00'")
&& str_contains($billing, "public const PURPOSE = 'ip_change'")
&& str_contains($billing, "\$invoice->status = 'paid'")
&& str_contains($billing, "\$invoice->paid_via = 'balance'")
&& str_contains($billing, '$job->invoice_id = (int) $invoice->id')
&& str_contains($billing, "if ((string) \$invoice->status === 'refunded')")
&& str_contains($billing, "\$invoice->status = 'refunded'"),
'IP-change charge, invoice linkage, or exactly-once refund is incomplete.');
$networkFinish = strpos($worker,
'CloudPlacementService::finishNetworkChange($instance');
$networkFinishEnd = strpos($worker,
"} elseif (\$status === 'succeeded' && \$job->kind === 'create')",
$networkFinish === false ? 0 : $networkFinish);
$networkSettlement = $networkFinish !== false && $networkFinishEnd !== false
? substr($worker, $networkFinish, $networkFinishEnd - $networkFinish)
: '';
$assert($networkSettlement !== ''
&& str_contains($networkSettlement,
"if (\$catalogStatus === 'failed')")
&& str_contains($networkSettlement,
'CloudIpChangeBillingService::refundNotAppliedLocked('),
'Network settlement no longer refunds only a conclusive not-applied result.');
$assert(str_contains($config,
'CloudIpChangeBillingService::PRICE_CONFIG_KEY')
&& str_contains($config,
'CloudIpChangeBillingService::normalizePrice(')
&& str_contains($configView, 'name="cloud_ip_change_price"')
&& str_contains($configView, '设置为 0.00 时关闭用户自助更换 IP'),
'Administrator price configuration or disabled default is missing.');
$assert(str_contains($detailView, 'data-instance-setting-tab="ip_change"')
&& str_contains($detailView, 'id="vm-ip-change-form"')
&& str_contains($detailView, '只有系统明确确认变更未生效时才会自动退回余额')
&& str_contains($detailScript, "headers: { 'Idempotency-Key': idempotencyKey }")
&& str_contains($detailScript, 'expected_amount: expectedAmount')
&& str_contains($detailScript, 'sessionStorage.setItem')
&& str_contains($detailScript,
'item.selected = item.dataset.poolKey === selectedPoolKey')
&& str_contains($detailScript,
'页面已保留原目标和同一请求标识')
&& str_contains($detailScript, "task.effect_resolution === 'not_applied'")
&& str_contains($detailScript, "task.effect_resolution === 'applied'"),
'Tenant settings UI lost explicit pricing, durable retry, or outcome guidance.');
$assert(substr_count($invoiceService, "'ip_change' => '更换公网 IP'") >= 2
&& str_contains($adminBilling, "'provision', 'renewal', 'ip_change'")
&& str_contains($documentation, '## 用户付费更换 IP')
&& str_contains($documentation, '钱包 → 实例 → 网络账本'),
'Billing presentation or operational documentation omitted paid IP changes.');
echo "Cloud paid IP-change contract tests passed.\n";