From 1f7a6d9a19919089d42dd18a428076e5fedbb95d Mon Sep 17 00:00:00 2001 From: codex Date: Sat, 31 Oct 2026 23:38:55 +0800 Subject: [PATCH] feat(traffic): freeze pool identity per service period --- web/app/Services/Cloud/CloudOrderService.php | 10 ++ .../Cloud/CloudPlanUpgradeService.php | 6 + .../Services/Cloud/CloudRenewalService.php | 6 + .../Cloud/CloudTrafficPoolPeriodBinding.php | 103 +++++++++++++ web/app/Services/Cloud/CloudWorkerService.php | 34 +++++ web/tests/CloudBillingLifecycleTest.php | 5 + web/tests/CloudFinancialWorkflowTest.php | 10 +- web/tests/CloudPlanUpgradeServiceTest.php | 4 + .../CloudTrafficPoolPeriodBindingTest.php | 137 ++++++++++++++++++ ...afficPoolPeriodIntegrationContractTest.php | 58 ++++++++ 10 files changed, 371 insertions(+), 2 deletions(-) create mode 100644 web/app/Services/Cloud/CloudTrafficPoolPeriodBinding.php create mode 100644 web/tests/CloudTrafficPoolPeriodBindingTest.php create mode 100644 web/tests/CloudTrafficPoolPeriodIntegrationContractTest.php diff --git a/web/app/Services/Cloud/CloudOrderService.php b/web/app/Services/Cloud/CloudOrderService.php index 8acf804e..3d73081b 100644 --- a/web/app/Services/Cloud/CloudOrderService.php +++ b/web/app/Services/Cloud/CloudOrderService.php @@ -139,6 +139,12 @@ final class CloudOrderService )) { throw new CloudError('该销售方案当前没有足够的可用资源或公网 IP', 409); } + $trafficPoolBinding = + CloudTrafficPoolPeriodBinding::purchaseSnapshot( + $pool, + $line, + (int) $plan->traffic_quota_bytes + ); $instancePrice = CloudMoney::cents($offer->price); $backupPrice = $automaticBackup ? CloudMoney::cents($offer->backup_price) : 0; @@ -231,6 +237,10 @@ final class CloudOrderService ? 'unlimited' : 'finite'; $safeSpec['validity_days'] = (int) $plan->validity_days; $safeSpec['traffic_quota_bytes'] = (int) $plan->traffic_quota_bytes; + $safeSpec['traffic_pool_group_id'] = + $trafficPoolBinding['traffic_pool_group_id']; + $safeSpec['traffic_quota_mode'] = + $trafficPoolBinding['traffic_quota_mode']; $safeSpec['instance_price'] = CloudMoney::decimal($instancePrice); $safeSpec['automatic_backup'] = $automaticBackup; $safeSpec['backup_price'] = CloudMoney::decimal($backupPrice); diff --git a/web/app/Services/Cloud/CloudPlanUpgradeService.php b/web/app/Services/Cloud/CloudPlanUpgradeService.php index f7d3c34d..a939ea20 100644 --- a/web/app/Services/Cloud/CloudPlanUpgradeService.php +++ b/web/app/Services/Cloud/CloudPlanUpgradeService.php @@ -558,6 +558,12 @@ final class CloudPlanUpgradeService $period->plan_id = (int) $target['plan_id']; $period->traffic_quota_bytes = (int) $target['traffic_quota_bytes']; + CloudTrafficPoolPeriodBinding::bindPeriod( + $period, + $period->traffic_pool_group_id === null + ? null : (int) $period->traffic_pool_group_id, + (int) $target['traffic_quota_bytes'] + ); $used = max(0, (int) $period->download_used_bytes) + max(0, (int) $period->upload_used_bytes); $quota = (int) $target['traffic_quota_bytes']; diff --git a/web/app/Services/Cloud/CloudRenewalService.php b/web/app/Services/Cloud/CloudRenewalService.php index 9bf6f6e7..0d2f0aca 100644 --- a/web/app/Services/Cloud/CloudRenewalService.php +++ b/web/app/Services/Cloud/CloudRenewalService.php @@ -149,6 +149,12 @@ final class CloudRenewalService $period->starts_at = $startsAt; $period->ends_at = $newExpiry; $period->traffic_quota_bytes = $addedTraffic; + CloudTrafficPoolPeriodBinding::bindPeriod( + $period, + $lastPeriod->traffic_pool_group_id === null + ? null : (int) $lastPeriod->traffic_pool_group_id, + $addedTraffic + ); $period->status = $startsAt <= $now ? 'active' : 'scheduled'; $period->activated_at = $startsAt <= $now ? $now : null; if ($period->status === 'active') { diff --git a/web/app/Services/Cloud/CloudTrafficPoolPeriodBinding.php b/web/app/Services/Cloud/CloudTrafficPoolPeriodBinding.php new file mode 100644 index 00000000..e5e7fb7c --- /dev/null +++ b/web/app/Services/Cloud/CloudTrafficPoolPeriodBinding.php @@ -0,0 +1,103 @@ +line_id !== (int) $line->id) { + throw new CloudError('资源池与线路配置不一致', 409); + } + $groupId = self::nullableGroupId( + $line->traffic_pool_group_id ?? null + ); + if ($groupId !== null + && CloudTrafficPoolGroup::find($groupId) === null) { + throw new CloudError('线路绑定的流量池分组不存在', 409); + } + return [ + 'traffic_pool_group_id' => $groupId, + 'traffic_quota_mode' => self::quotaMode($quotaBytes), + ]; + } + + /** @return array{traffic_pool_group_id:?int,traffic_quota_mode:string} */ + public static function orderSnapshot(array $spec, int $quotaBytes): array + { + if (!array_key_exists('traffic_pool_group_id', $spec) + || !array_key_exists('traffic_quota_mode', $spec)) { + throw new CloudError('订单缺少不可变的流量池计费快照', 409); + } + $snapshotGroup = $spec['traffic_pool_group_id']; + if ($snapshotGroup !== null + && (!is_int($snapshotGroup) || $snapshotGroup < 1)) { + throw new CloudError('订单流量池分组快照无效', 409); + } + $groupId = $snapshotGroup; + $expectedMode = self::quotaMode($quotaBytes); + $snapshotMode = $spec['traffic_quota_mode']; + if (!is_string($snapshotMode) + || !in_array($snapshotMode, ['finite', 'unlimited'], true) + || $snapshotMode !== $expectedMode) { + throw new CloudError('订单流量池计费快照不一致', 409); + } + return [ + 'traffic_pool_group_id' => $groupId, + 'traffic_quota_mode' => $snapshotMode, + ]; + } + + public static function bindPeriod( + CloudServicePeriod $period, + ?int $trafficPoolGroupId, + int $quotaBytes + ): void { + $groupId = self::nullableGroupId( + $trafficPoolGroupId + ); + $period->traffic_pool_group_id = $groupId; + $period->traffic_quota_mode = self::quotaMode($quotaBytes); + } + + public static function quotaMode(int $quotaBytes): string + { + if ($quotaBytes < 0) { + throw new CloudError('服务周期流量额度不能为负数', 409); + } + return $quotaBytes === 0 ? 'unlimited' : 'finite'; + } + + private static function nullableGroupId(mixed $value): ?int + { + if ($value === null || $value === '') { + return null; + } + if (is_int($value) && $value > 0) { + return $value; + } + if (is_string($value) + && preg_match('/^[1-9][0-9]*$/D', $value) === 1 + && (int) $value > 0) { + return (int) $value; + } + throw new CloudError('流量池分组快照无效', 409); + } +} diff --git a/web/app/Services/Cloud/CloudWorkerService.php b/web/app/Services/Cloud/CloudWorkerService.php index 7185633d..169578cf 100644 --- a/web/app/Services/Cloud/CloudWorkerService.php +++ b/web/app/Services/Cloud/CloudWorkerService.php @@ -342,6 +342,7 @@ final class CloudWorkerService return true; } if ($job->kind === 'create') { + self::assertCreateTrafficPoolSnapshot($job); $actor['can_provision'] = true; $actor['can_manage'] = true; } elseif ($vpcSystem) { @@ -2745,6 +2746,11 @@ final class CloudWorkerService if ($days === false || $traffic === false) { throw new CloudError('订单缺少不可变的计费周期快照', 409); } + $trafficPoolBinding = + CloudTrafficPoolPeriodBinding::orderSnapshot( + $spec, + $traffic + ); $instancePrice = CloudMoney::cents( $spec['instance_price'] ?? null ); @@ -2832,6 +2838,11 @@ final class CloudWorkerService $period->starts_at = $now; $period->ends_at = $instance->expires_at; $period->traffic_quota_bytes = $traffic; + CloudTrafficPoolPeriodBinding::bindPeriod( + $period, + $trafficPoolBinding['traffic_pool_group_id'], + $traffic + ); $period->download_base_bytes = 0; $period->upload_base_bytes = 0; $period->download_used_bytes = 0; @@ -3834,6 +3845,29 @@ final class CloudWorkerService } } + /** Fail a malformed clean-break order before any Agent create mutation. */ + private static function assertCreateTrafficPoolSnapshot(CloudJob $job): void + { + if ($job->order_id === null) { + throw new CloudError('开通任务缺少订单', 409); + } + $order = CloudOrder::find((int) $job->order_id); + $spec = $order === null + ? null : json_decode((string) $order->spec_json, true); + if (!is_array($spec) || array_is_list($spec)) { + throw new CloudError('订单计费快照损坏', 409); + } + $quota = filter_var( + $spec['traffic_quota_bytes'] ?? null, + FILTER_VALIDATE_INT, + ['options' => ['min_range' => 0]] + ); + if ($quota === false) { + throw new CloudError('订单缺少不可变的计费周期快照', 409); + } + CloudTrafficPoolPeriodBinding::orderSnapshot($spec, (int) $quota); + } + private static function exceptionDiagnostic(\Throwable $error, bool $dispatched, ?string $kind, bool $hasOrder, ?string $action): string { diff --git a/web/tests/CloudBillingLifecycleTest.php b/web/tests/CloudBillingLifecycleTest.php index b39f49d3..2f3f56a3 100644 --- a/web/tests/CloudBillingLifecycleTest.php +++ b/web/tests/CloudBillingLifecycleTest.php @@ -132,6 +132,8 @@ namespace { $table->integer('plan_id')->nullable(); $table->integer('order_id')->nullable(); $table->integer('renewal_id')->nullable(); + $table->integer('traffic_pool_group_id')->nullable(); + $table->string('traffic_quota_mode')->default('finite'); $table->integer('starts_at'); $table->integer('ends_at'); $table->integer('traffic_quota_bytes')->default(0); @@ -389,6 +391,9 @@ namespace { && User::find(1)->money === '90.00', 'Renewal idempotency did not debit exactly once.'); $assert($scheduled->status === 'scheduled' && (int) $scheduled->starts_at === $periodEndsAt, 'Early renewal did not create a future independent period.'); + $assert($scheduled->traffic_pool_group_id === $period->traffic_pool_group_id + && (string) $scheduled->traffic_quota_mode === 'finite', + 'Renewal changed the frozen pool group or quota mode.'); $assert((int) $instance->current_period_id === (int) $period->id && (int) $instance->traffic_download_bytes === 700 && (int) $instance->traffic_upload_bytes === 400 diff --git a/web/tests/CloudFinancialWorkflowTest.php b/web/tests/CloudFinancialWorkflowTest.php index 28d86722..f0aebc27 100644 --- a/web/tests/CloudFinancialWorkflowTest.php +++ b/web/tests/CloudFinancialWorkflowTest.php @@ -238,7 +238,7 @@ namespace { $schema->create('configurations', function (Blueprint $t) { $t->increments('id'); $t->string('name')->unique(); $t->text('value'); }); $schema->create('shop', function (Blueprint $t) { $t->increments('id'); $t->string('name'); foreach (['cpu_cores', 'memory_mib', 'bandwidth_mbps', 'disk_gib', 'validity_days', 'traffic_quota_bytes', 'status', 'sort'] as $v) { $t->integer($v)->default(0); } }); $schema->create('cloud_regions', function (Blueprint $t) { $t->increments('id'); $t->string('code'); $t->string('name'); $t->string('country_code'); $t->string('status'); $t->integer('sort')->default(0); $t->integer('created_at'); $t->integer('updated_at'); }); - $schema->create('cloud_network_lines', function (Blueprint $t) { $t->increments('id'); $t->integer('region_id'); $t->string('code'); $t->string('name'); $t->integer('is_default')->default(0); $t->string('status'); $t->integer('sort')->default(0); $t->integer('created_at'); $t->integer('updated_at'); }); + $schema->create('cloud_network_lines', function (Blueprint $t) { $t->increments('id'); $t->integer('region_id'); $t->integer('traffic_pool_group_id')->nullable(); $t->string('code'); $t->string('name'); $t->integer('is_default')->default(0); $t->string('status'); $t->integer('sort')->default(0); $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_resource_pools', function (Blueprint $t) { $t->increments('id'); $t->integer('line_id'); $t->string('code'); $t->string('name'); $t->string('status'); $t->string('scheduling_policy')->default('balanced'); $t->integer('sort')->default(0); $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_hosts', function (Blueprint $t) { $t->increments('id'); $t->string('name'); $t->string('region')->default(''); $t->string('country_code')->default(''); $t->integer('enabled'); $t->integer('resource_pool_id')->nullable(); $t->string('scheduler_state')->default('disabled'); $t->string('default_network'); $t->integer('created_at')->default(0); $t->integer('updated_at')->default(0); }); $schema->create('cloud_host_agents', function (Blueprint $t) { $t->integer('host_id')->primary(); $t->text('sealed_api_secret'); $t->integer('enabled')->default(0); $t->integer('revision')->default(1); $t->integer('tested_revision')->default(0); $t->string('last_test_status')->default('never'); $t->string('last_error_code')->default(''); $t->string('last_agent_version')->default(''); $t->integer('last_tested_at')->default(0); $t->integer('created_at'); $t->integer('updated_at'); }); @@ -326,7 +326,7 @@ namespace { $schema->create('cloud_vpc_ports', function (Blueprint $t) { $t->increments('id'); $t->integer('vpc_id'); $t->integer('instance_id'); $t->integer('user_id'); $t->integer('active_claim')->nullable(); $t->string('state'); }); $schema->create('cloud_instance_snapshots', function (Blueprint $t) { $t->increments('id'); $t->string('snapshot_key')->unique(); foreach (['instance_id', 'user_id', 'host_id'] as $v) { $t->integer($v); } $t->string('name'); $t->string('state'); $t->string('consistency'); $t->integer('size_bytes')->nullable(); $t->string('last_operation_id')->nullable(); foreach (['ready_at', 'last_restored_at', 'deleted_at'] as $v) { $t->integer($v)->nullable(); } $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_renewals', function (Blueprint $t) { $t->increments('id'); $t->integer('instance_id'); $t->integer('user_id'); $t->integer('plan_id')->nullable(); $t->integer('period_id')->nullable(); $t->string('idempotency_key'); $t->string('request_hash'); $t->string('amount'); $t->integer('validity_days'); $t->integer('traffic_bytes_added'); $t->integer('previous_expires_at'); $t->integer('new_expires_at'); $t->integer('created_at'); $t->unique(['user_id', 'idempotency_key']); }); - $schema->create('cloud_service_periods', function (Blueprint $t) { $t->increments('id'); foreach (['instance_id', 'user_id'] as $v) { $t->integer($v); } foreach (['plan_id', 'order_id', 'renewal_id', 'download_base_bytes', 'upload_base_bytes', 'activated_at'] as $v) { $t->integer($v)->nullable(); } foreach (['starts_at', 'ends_at', 'traffic_quota_bytes', 'download_used_bytes', 'upload_used_bytes', 'created_at', 'updated_at'] as $v) { $t->integer($v)->default(0); } $t->string('status'); $t->unique('order_id'); $t->unique('renewal_id'); }); + $schema->create('cloud_service_periods', function (Blueprint $t) { $t->increments('id'); foreach (['instance_id', 'user_id'] as $v) { $t->integer($v); } foreach (['plan_id', 'order_id', 'renewal_id', 'traffic_pool_group_id', 'download_base_bytes', 'upload_base_bytes', 'activated_at'] as $v) { $t->integer($v)->nullable(); } $t->string('traffic_quota_mode')->default('finite'); foreach (['starts_at', 'ends_at', 'traffic_quota_bytes', 'download_used_bytes', 'upload_used_bytes', 'created_at', 'updated_at'] as $v) { $t->integer($v)->default(0); } $t->string('status'); $t->unique('order_id'); $t->unique('renewal_id'); }); $schema->create('cloud_invoices', function (Blueprint $t) { $t->increments('id'); $t->string('invoice_no')->unique(); $t->string('purpose')->default('renewal'); $t->integer('instance_id')->nullable(); $t->integer('order_id')->nullable()->unique(); $t->integer('user_id'); $t->integer('plan_id')->nullable(); $t->integer('offer_id')->nullable(); $t->integer('renewal_id')->nullable(); $t->integer('renews_expires_at')->nullable(); $t->integer('due_at'); $t->string('amount'); $t->string('currency')->default('USD'); $t->integer('validity_days'); $t->integer('traffic_quota_bytes'); $t->string('status'); $t->string('paid_via')->nullable(); $t->string('payment_reference')->nullable(); $t->integer('paid_at')->nullable(); $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_invoice_items', function (Blueprint $t) { $t->increments('id'); $t->integer('invoice_id'); $t->integer('order_item_id')->nullable(); $t->string('code'); $t->string('label'); $t->string('amount'); $t->string('currency')->default('USD'); $t->text('snapshot_json'); $t->integer('created_at'); $t->unique(['invoice_id', 'code']); }); $schema->create('cloud_backup_subscriptions', function (Blueprint $t) { $t->increments('id'); $t->integer('instance_id')->unique(); $t->integer('user_id'); $t->integer('offer_id'); $t->boolean('enabled')->default(true); $t->boolean('renew_next_period')->default(true); $t->string('frequency')->default('daily'); $t->integer('weekday')->default(0); $t->integer('retention_count')->default(2); $t->string('timezone')->default('UTC'); $t->string('price'); $t->string('currency')->default('USD'); $t->integer('price_version'); $t->integer('state_revision')->default(1); $t->integer('next_run_at'); $t->integer('last_scheduled_at')->nullable(); $t->integer('last_backup_id')->nullable(); $t->integer('created_at'); $t->integer('updated_at'); }); @@ -534,6 +534,9 @@ namespace { && !array_key_exists('invoice_no', $openingDisplay) && ($openingDisplay['order_status'] ?? null) === 'awaiting_payment' && ($firstSpec['stock_mode'] ?? null) === 'finite' + && array_key_exists('traffic_pool_group_id', $firstSpec) + && $firstSpec['traffic_pool_group_id'] === null + && ($firstSpec['traffic_quota_mode'] ?? null) === 'finite' && ($firstSpec['automatic_backup'] ?? null) === false && ($firstSpec['instance_price'] ?? null) === '9.99' && ($firstSpec['backup_price'] ?? null) === '0.00' @@ -725,6 +728,9 @@ namespace { $openingJobDisplay = CloudJobService::display(CloudJob::where('order_id', $first->id)->first()); $assert(CloudOrder::find($first->id)->status === 'succeeded' && CloudInstance::count() === 1 && \App\Models\CloudServicePeriod::count() === 1 + && \App\Models\CloudServicePeriod::first()->traffic_pool_group_id === null + && (string) \App\Models\CloudServicePeriod::first()->traffic_quota_mode + === 'finite' && CloudResourceReservation::where('order_id', $first->id)->first()->state === 'committed' && CloudPublicIp::find($openingReservation->public_ip_id)->state === 'assigned' && CloudInvoice::find($openingInvoice->id)->instance_id !== null diff --git a/web/tests/CloudPlanUpgradeServiceTest.php b/web/tests/CloudPlanUpgradeServiceTest.php index 22a633dc..e9a48aef 100644 --- a/web/tests/CloudPlanUpgradeServiceTest.php +++ b/web/tests/CloudPlanUpgradeServiceTest.php @@ -282,6 +282,8 @@ namespace { $table->integer('plan_id')->nullable(); $table->integer('order_id')->nullable(); $table->integer('renewal_id')->nullable(); + $table->integer('traffic_pool_group_id')->nullable(); + $table->string('traffic_quota_mode')->default('finite'); $table->integer('starts_at'); $table->integer('ends_at'); $table->integer('traffic_quota_bytes'); @@ -827,6 +829,8 @@ namespace { && (string) $upgraded->renewal_price === '20.00' && (int) $upgraded->renewal_traffic_bytes === 2000 && (int) $period->traffic_quota_bytes === 2000 + && $period->traffic_pool_group_id === null + && (string) $period->traffic_quota_mode === 'finite' && (int) $period->download_used_bytes === 700 && (int) $period->upload_used_bytes === 600 && (string) $period->status === 'active' diff --git a/web/tests/CloudTrafficPoolPeriodBindingTest.php b/web/tests/CloudTrafficPoolPeriodBindingTest.php new file mode 100644 index 00000000..8c7784db --- /dev/null +++ b/web/tests/CloudTrafficPoolPeriodBindingTest.php @@ -0,0 +1,137 @@ +httpStatus === 409, $message . ' Wrong status.'); + return; + } + throw new RuntimeException($message); +}; + +$capsule = new Capsule(); +$capsule->addConnection([ + 'driver' => 'sqlite', + 'database' => ':memory:', +], 'default'); +$capsule->setAsGlobal(); +$capsule->bootEloquent(); +$capsule->schema('default')->create( + 'cloud_traffic_pool_groups', + static function (Blueprint $table): void { + $table->increments('id'); + $table->string('code')->unique(); + $table->string('name'); + $table->string('mode'); + $table->integer('created_at'); + $table->integer('updated_at'); + } +); +Capsule::table('cloud_traffic_pool_groups')->insert([ + 'id' => 7, + 'code' => 'premium', + 'name' => 'Premium', + 'mode' => 'disabled', + 'created_at' => 1, + 'updated_at' => 1, +]); + +$pool = new CloudResourcePool(); +$pool->id = 3; +$pool->line_id = 5; +$line = new CloudNetworkLine(); +$line->id = 5; +$line->traffic_pool_group_id = 7; +$snapshot = CloudTrafficPoolPeriodBinding::purchaseSnapshot( + $pool, + $line, + 1024 +); +$assert($snapshot === [ + 'traffic_pool_group_id' => 7, + 'traffic_quota_mode' => 'finite', +], 'Purchase did not freeze the configured line group.'); + +$line->traffic_pool_group_id = null; +$unbound = CloudTrafficPoolPeriodBinding::purchaseSnapshot($pool, $line, 0); +$assert($unbound === [ + 'traffic_pool_group_id' => null, + 'traffic_quota_mode' => 'unlimited', +], 'An unbound line did not preserve the disabled accounting path.'); + +$line->traffic_pool_group_id = 99; +$expectConflict( + static fn (): array => + CloudTrafficPoolPeriodBinding::purchaseSnapshot($pool, $line, 100), + 'A line referencing a missing traffic-pool group was accepted.' +); + +$orderBinding = CloudTrafficPoolPeriodBinding::orderSnapshot($snapshot, 1024); +$assert($orderBinding['traffic_pool_group_id'] === 7, + 'A live line change altered the immutable order snapshot.'); +$expectConflict( + static fn (): array => CloudTrafficPoolPeriodBinding::orderSnapshot([ + 'traffic_pool_group_id' => 7, + 'traffic_quota_mode' => 'unlimited', + ], 100), + 'A quota-mode mismatch in the order snapshot was accepted.' +); +$deletedCatalogBinding = CloudTrafficPoolPeriodBinding::orderSnapshot([ + 'traffic_pool_group_id' => 99, + 'traffic_quota_mode' => 'finite', +], 100); +$assert($deletedCatalogBinding['traffic_pool_group_id'] === 99, + 'Finalization incorrectly depended on the mutable group catalog.'); +$expectConflict( + static fn (): array => + CloudTrafficPoolPeriodBinding::orderSnapshot([], 100), + 'A missing clean-break order snapshot was accepted.' +); +$expectConflict( + static fn (): array => CloudTrafficPoolPeriodBinding::orderSnapshot([ + 'traffic_pool_group_id' => '', + 'traffic_quota_mode' => 'finite', + ], 100), + 'An empty-string order group bypassed the immutable mapping.' +); +$expectConflict( + static fn (): array => CloudTrafficPoolPeriodBinding::orderSnapshot([ + 'traffic_pool_group_id' => '7', + 'traffic_quota_mode' => 'finite', + ], 100), + 'A string order group bypassed the canonical snapshot type.' +); +$expectConflict( + static fn (): string => CloudTrafficPoolPeriodBinding::quotaMode(-1), + 'A negative period quota was silently converted to unlimited.' +); + +$period = new CloudServicePeriod(); +CloudTrafficPoolPeriodBinding::bindPeriod($period, 7, 0); +$assert((int) $period->traffic_pool_group_id === 7 + && (string) $period->traffic_quota_mode === 'unlimited', + 'A period did not retain its group while switching quota mode.'); +CloudTrafficPoolPeriodBinding::bindPeriod($period, 7, 2048); +$assert((int) $period->traffic_pool_group_id === 7 + && (string) $period->traffic_quota_mode === 'finite', + 'A finite upgrade changed the frozen group or quota mode.'); + +echo "Cloud traffic pool period binding checks passed.\n"; diff --git a/web/tests/CloudTrafficPoolPeriodIntegrationContractTest.php b/web/tests/CloudTrafficPoolPeriodIntegrationContractTest.php new file mode 100644 index 00000000..012569c0 --- /dev/null +++ b/web/tests/CloudTrafficPoolPeriodIntegrationContractTest.php @@ -0,0 +1,58 @@ + 'dispatching'"); +$assert($preDispatch !== false && $dispatchClaim !== false + && $preDispatch < $dispatchClaim, + 'Create snapshot validation no longer happens before Agent dispatch.'); +$assert(str_contains($renewal, + 'CloudTrafficPoolPeriodBinding::bindPeriod(') + && str_contains($renewal, + '$lastPeriod->traffic_pool_group_id'), + 'Renewal does not inherit the preceding service-period group.'); +$assert(str_contains($upgrade, + 'CloudTrafficPoolPeriodBinding::bindPeriod(') + && str_contains($upgrade, + '$period->traffic_pool_group_id'), + 'Upgrade no longer preserves the current service-period group.'); + +foreach ([$order, $worker, $renewal, $upgrade] as $source) { + $assert(!str_contains($source, 'CloudTrafficPoolAccounting::'), + 'A financial or provisioning transaction directly locks pool accounting.'); +} + +echo "Cloud traffic pool period integration contract passed.\n";