httpStatus === 409, $message . ' Wrong status.'); return; } throw new RuntimeException($message); }; $capsule = new Capsule(); $capsule->addConnection([ 'driver' => 'sqlite', 'database' => ':memory:', ], 'default'); $capsule->setAsGlobal(); $capsule->bootEloquent(); $schema = $capsule->schema('default'); $schema->create('cloud_traffic_pool_groups', static function (Blueprint $t): void { $t->increments('id'); $t->string('code')->unique(); $t->string('name'); $t->string('mode'); $t->integer('revision')->default(1); $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_network_lines', static function (Blueprint $t): void { $t->increments('id'); $t->integer('region_id'); $t->integer('traffic_pool_group_id')->nullable(); $t->string('code'); $t->string('name'); $t->boolean('is_default')->default(false); $t->string('status'); $t->integer('sort')->default(0); $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_resource_pools', static function (Blueprint $t): void { $t->increments('id'); $t->integer('line_id'); }); $schema->create('cloud_orders', static function (Blueprint $t): void { $t->increments('id'); $t->integer('resource_pool_id')->nullable(); $t->string('status'); $t->text('spec_json'); }); $schema->create('cloud_instances', static function (Blueprint $t): void { $t->increments('id'); $t->integer('resource_pool_id')->nullable(); $t->string('status')->default('active'); $t->string('network_status')->default('active'); $t->integer('traffic_synced_at')->default(0); $t->integer('lifetime_download_bytes')->default(0); $t->integer('lifetime_upload_bytes')->default(0); }); $schema->create('cloud_service_periods', static function (Blueprint $t): void { $t->increments('id'); $t->integer('instance_id'); $t->integer('user_id'); $t->integer('traffic_pool_group_id')->nullable(); $t->string('traffic_quota_mode'); $t->integer('starts_at'); $t->integer('ends_at'); $t->integer('traffic_quota_bytes'); $t->string('status'); }); $schema->create('cloud_traffic_pools', static function (Blueprint $t): void { $t->increments('id'); $t->integer('user_id'); $t->integer('traffic_pool_group_id'); $t->string('state'); $t->integer('available_bytes')->default(0); $t->integer('overage_bytes')->default(0); $t->integer('version')->default(0); $t->integer('last_sequence')->default(0); $t->integer('last_event_id')->nullable(); $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_traffic_entitlements', static function (Blueprint $t): void { $t->increments('id'); $t->integer('pool_id'); $t->integer('user_id'); $t->integer('instance_id'); $t->integer('service_period_id')->unique(); $t->integer('starts_at'); $t->integer('ends_at'); $t->string('quota_mode'); $t->integer('granted_bytes'); $t->integer('consumed_bytes')->default(0); $t->integer('forfeited_bytes')->default(0); $t->string('state'); $t->integer('activated_at')->nullable(); $t->integer('closed_at')->nullable(); $t->integer('version')->default(0); $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_traffic_ledger_events', static function (Blueprint $t): void { $t->increments('id'); $t->integer('pool_id'); $t->integer('user_id'); $t->integer('pool_sequence'); $t->string('event_key'); $t->string('event_type'); $t->string('source_type'); $t->string('source_id'); $t->integer('source_revision'); $t->string('payload_hash'); $t->integer('instance_id')->nullable(); $t->integer('service_period_id')->nullable(); $t->integer('entitlement_id')->nullable(); $t->integer('amount_bytes'); $t->integer('effective_at'); $t->integer('observed_at')->nullable(); $t->text('payload_json'); $t->integer('created_at'); }); $schema->create('cloud_traffic_instance_cursors', static function (Blueprint $t): void { $t->integer('instance_id')->primary(); $t->integer('user_id'); $t->integer('pool_id'); $t->integer('accounted_download_bytes')->default(0); $t->integer('accounted_upload_bytes')->default(0); $t->integer('last_observed_at')->default(0); $t->integer('last_event_id')->nullable(); $t->string('state')->default('ready'); $t->string('attention_code')->default(''); $t->integer('version')->default(0); $t->integer('updated_at'); }); $schema->create('cloud_traffic_scan_failures', static function (Blueprint $t): void { $t->increments('id'); $t->string('scan_kind'); $t->integer('subject_id'); $t->integer('attempt_count')->default(1); $t->string('last_error_code'); $t->integer('retry_at'); $t->integer('first_failed_at'); $t->integer('last_failed_at'); }); $schema->create('cloud_instance_network_policy_states', static function (Blueprint $t): void { $t->integer('instance_id')->primary(); $t->integer('user_id'); $t->boolean('desired_suspended')->default(false); $t->boolean('applied_suspended')->default(false); $t->integer('revision')->default(1); $t->string('state')->default('applied'); $t->string('operation_id')->nullable(); $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_instance_network_restrictions', static function (Blueprint $t): void { $t->integer('instance_id'); $t->integer('user_id'); $t->string('reason_code'); $t->boolean('desired_active'); $t->integer('revision'); $t->string('source_type'); $t->string('source_id'); $t->integer('created_at'); $t->integer('updated_at'); }); $schema->create('cloud_jobs', static function (Blueprint $t): void { $t->increments('id'); $t->integer('order_id')->nullable(); $t->integer('instance_id')->nullable(); $t->integer('host_id')->nullable(); $t->string('kind'); $t->string('operation_id')->nullable(); $t->string('request_fingerprint')->nullable(); $t->string('request_idempotency_key')->nullable(); $t->string('phase'); $t->string('outcome')->nullable(); $t->string('status'); $t->string('error_code')->nullable(); $t->string('effect_resolution')->default('not_applicable'); $t->boolean('effect_may_have_applied')->nullable(); $t->boolean('safe_to_resubmit')->nullable(); $t->string('execution_mode')->nullable(); $t->integer('attempts')->nullable(); $t->integer('dispatch_attempts')->nullable(); $t->integer('dispatch_deadline_at')->nullable(); $t->integer('dispatch_started_at')->nullable(); $t->integer('agent_accepted_at')->nullable(); $t->integer('dispatched_at')->nullable(); $t->integer('receipt_deadline_at')->nullable(); $t->integer('terminal_at')->nullable(); $t->integer('completion_event_id')->nullable(); $t->text('result_json')->nullable(); $t->boolean('finalization_incident')->default(false); $t->string('finalization_error_code')->nullable(); }); $now = time(); Capsule::table('cloud_network_lines')->insert([ ['id' => 1, 'region_id' => 1, 'traffic_pool_group_id' => null, 'code' => 'premium', 'name' => 'Premium', 'is_default' => 1, 'status' => 'active', 'sort' => 0, 'created_at' => $now, 'updated_at' => $now], ['id' => 2, 'region_id' => 1, 'traffic_pool_group_id' => null, 'code' => 'standard', 'name' => 'Standard', 'is_default' => 0, 'status' => 'active', 'sort' => 1, 'created_at' => $now, 'updated_at' => $now], ['id' => 3, 'region_id' => 2, 'traffic_pool_group_id' => null, 'code' => 'premium', 'name' => 'Premium', 'is_default' => 0, 'status' => 'active', 'sort' => 0, 'created_at' => $now, 'updated_at' => $now], ]); $created = CloudAdminTrafficPoolService::create([ 'code' => 'premium_pool', 'name' => 'Premium Pool', ]); $groupId = (int) $created['item']['id']; $assert($created['item']['mode'] === 'disabled' && $created['item']['revision'] === 1, 'A new traffic-pool group did not start disabled at revision one.'); $mapped = CloudAdminTrafficPoolService::replaceLines($groupId, [ 'line_ids' => [1, 3], 'expected_revision' => 1, ]); $assert($mapped['item']['line_ids'] === [1, 3] && $mapped['item']['line_count'] === 2 && $mapped['item']['region_count'] === 2 && $mapped['item']['revision'] === 2, 'The cross-region line mapping or coverage summary was not saved atomically.'); $second = CloudAdminTrafficPoolService::create([ 'code' => 'standard_pool', 'name' => 'Standard Pool', ]); $secondId = (int) $second['item']['id']; CloudAdminTrafficPoolService::replaceLines($secondId, [ 'line_ids' => [2], 'expected_revision' => 1, ]); $expectConflict( static fn (): array => CloudAdminTrafficPoolService::replaceLines( $groupId, ['line_ids' => [1, 2, 3], 'expected_revision' => 2] ), 'A line owned by another group was partially attached.' ); $assert((int) Capsule::table('cloud_network_lines')->where('id', 1) ->value('traffic_pool_group_id') === $groupId && (int) Capsule::table('cloud_network_lines')->where('id', 3) ->value('traffic_pool_group_id') === $groupId && (int) Capsule::table('cloud_network_lines')->where('id', 2) ->value('traffic_pool_group_id') === $secondId, 'A failed mapping request left a partial catalog update.'); $expectConflict( static fn (): array => CloudAdminTrafficPoolService::update( $groupId, ['name' => 'Stale', 'expected_revision' => 1] ), 'A stale administrator revision overwrote the group.' ); $assert(CloudAdminTrafficPoolService::read($groupId)['item']['name'] === 'Premium Pool', 'A stale revision changed the group name.'); $shadow = CloudAdminTrafficPoolService::update($groupId, [ 'mode' => 'shadow', 'expected_revision' => 2, ]); $assert($shadow['item']['mode'] === 'shadow' && $shadow['item']['revision'] === 3, 'The disabled group did not advance to shadow mode.'); Capsule::table('cloud_resource_pools')->insert([ 'id' => 3, 'line_id' => 1, ]); Capsule::table('cloud_orders')->insert([ 'id' => 4, 'resource_pool_id' => 3, 'status' => 'failed', 'spec_json' => json_encode([ 'traffic_pool_group_id' => null, ], JSON_THROW_ON_ERROR), ]); Capsule::table('cloud_jobs')->insert([ 'id' => 40, 'order_id' => 4, 'instance_id' => null, 'host_id' => 1, 'kind' => 'create', 'operation_id' => str_repeat('c', 32), 'request_fingerprint' => str_repeat('d', 64), 'request_idempotency_key' => 'traffic-pool-test-40', 'phase' => 'terminal', 'outcome' => 'failed', 'status' => 'failed', 'error_code' => 'operation_effect_unknown', 'effect_resolution' => 'unknown', 'effect_may_have_applied' => 1, 'safe_to_resubmit' => 0, 'execution_mode' => 'agent', 'attempts' => 1, 'dispatch_attempts' => 1, 'dispatch_deadline_at' => $now + 30, 'dispatch_started_at' => $now - 2, 'dispatched_at' => $now - 2, 'receipt_deadline_at' => $now + 28, 'terminal_at' => $now, 'finalization_incident' => 0, ]); $frozenMismatch = CloudAdminTrafficPoolService::readiness($groupId, $now); $assert(in_array('line_frozen_identity_mismatch', array_column( $frozenMismatch['blockers'], 'code' ), true), 'A recoverable failed order with the old line identity did not block enforcement.'); Capsule::table('cloud_jobs')->where('id', 40)->update([ 'error_code' => 'create_not_applied', 'effect_resolution' => 'not_applied', 'effect_may_have_applied' => 0, 'safe_to_resubmit' => 1, ]); $resolvedFailure = CloudAdminTrafficPoolService::readiness($groupId, $now); $assert(!in_array('line_frozen_identity_mismatch', array_column( $resolvedFailure['blockers'], 'code' ), true), 'A definitively not-applied failed order permanently locked the group.'); Capsule::table('cloud_jobs')->where('id', 40)->delete(); Capsule::table('cloud_orders')->where('id', 4)->delete(); $periodStart = $now + 600; $periodEnd = $periodStart + 3600; Capsule::table('cloud_service_periods')->insert([ 'id' => 10, 'instance_id' => 100, 'user_id' => 50, 'traffic_pool_group_id' => $groupId, 'traffic_quota_mode' => 'finite', 'starts_at' => $periodStart, 'ends_at' => $periodEnd, 'traffic_quota_bytes' => 100, 'status' => 'scheduled', ]); $readiness = CloudAdminTrafficPoolService::readiness($groupId, $now); $assert(in_array('missing_entitlements', array_column( $readiness['blockers'], 'code' ), true), 'A scheduled period without an entitlement did not block enforcement.'); $expectConflict( static fn (): array => CloudAdminTrafficPoolService::update( $groupId, ['mode' => 'enforced', 'expected_revision' => 3] ), 'A group with a missing entitlement entered enforced mode.' ); $assert(CloudAdminTrafficPoolService::read($groupId)['item']['mode'] === 'shadow', 'A failed readiness gate changed the group mode.'); Capsule::table('cloud_traffic_pools')->insert([ 'id' => 20, 'user_id' => 50, 'traffic_pool_group_id' => $groupId, 'state' => 'available', 'available_bytes' => 0, 'overage_bytes' => 0, 'version' => 1, 'last_sequence' => 1, 'last_event_id' => 30, 'created_at' => $now, 'updated_at' => $now, ]); Capsule::table('cloud_traffic_entitlements')->insert([ 'id' => 25, 'pool_id' => 20, 'user_id' => 50, 'instance_id' => 100, 'service_period_id' => 10, 'starts_at' => $periodStart, 'ends_at' => $periodEnd, 'quota_mode' => 'finite', 'granted_bytes' => 100, 'consumed_bytes' => 0, 'forfeited_bytes' => 0, 'state' => 'scheduled', 'activated_at' => null, 'closed_at' => null, 'version' => 1, 'created_at' => $now, 'updated_at' => $now, ]); Capsule::table('cloud_traffic_ledger_events')->insert([ 'id' => 30, 'pool_id' => 20, 'user_id' => 50, 'pool_sequence' => 1, 'event_key' => 'scheduled:10', 'event_type' => 'entitlement_scheduled', 'source_type' => 'service_period', 'source_id' => '10', 'source_revision' => 1, 'payload_hash' => str_repeat('a', 64), 'instance_id' => 100, 'service_period_id' => 10, 'entitlement_id' => 25, 'amount_bytes' => 100, 'effective_at' => $periodStart, 'observed_at' => null, 'payload_json' => '{}', 'created_at' => $now, ]); $ready = CloudAdminTrafficPoolService::readiness($groupId, $now); $assert($ready['ready'] === true && $ready['blockers'] === [], 'A complete shadow projection did not pass readiness.'); Capsule::table('cloud_traffic_entitlements')->where('id', 25)->update([ 'state' => 'active', ]); $wrongEntitlementState = CloudAdminTrafficPoolService::readiness( $groupId, $now ); $assert(in_array('period_identity_mismatch', array_column( $wrongEntitlementState['blockers'], 'code' ), true), 'An active entitlement for a future scheduled period passed readiness.'); Capsule::table('cloud_traffic_entitlements')->where('id', 25)->update([ 'state' => 'scheduled', ]); Capsule::table('cloud_instances')->insert([ 'id' => 100, 'resource_pool_id' => 3, 'status' => 'active', 'network_status' => 'active', 'traffic_synced_at' => $now, 'lifetime_download_bytes' => 10, 'lifetime_upload_bytes' => 20, ]); Capsule::table('cloud_service_periods')->where('id', 10)->update([ 'starts_at' => $now - 60, 'ends_at' => $now + 3600, 'status' => 'active', ]); Capsule::table('cloud_traffic_entitlements')->where('id', 25)->update([ 'starts_at' => $now - 60, 'ends_at' => $now + 3600, 'consumed_bytes' => 30, 'state' => 'active', 'activated_at' => $now - 60, ]); Capsule::table('cloud_traffic_pools')->where('id', 20)->update([ 'available_bytes' => 70, 'last_sequence' => 2, 'last_event_id' => 31, ]); Capsule::table('cloud_traffic_ledger_events')->insert([ 'id' => 31, 'pool_id' => 20, 'user_id' => 50, 'pool_sequence' => 2, 'event_key' => 'usage:100', 'event_type' => 'usage_allocated', 'source_type' => 'traffic_sample', 'source_id' => '100', 'source_revision' => 1, 'payload_hash' => str_repeat('b', 64), 'instance_id' => 100, 'service_period_id' => 10, 'entitlement_id' => 25, 'amount_bytes' => 30, 'effective_at' => $now, 'observed_at' => $now, 'payload_json' => '{}', 'created_at' => $now, ]); Capsule::table('cloud_traffic_pools')->insert([ 'id' => 21, 'user_id' => 51, 'traffic_pool_group_id' => $groupId, 'state' => 'available', 'available_bytes' => 0, 'overage_bytes' => 0, 'version' => 1, 'last_sequence' => 0, 'last_event_id' => null, 'created_at' => $now, 'updated_at' => $now, ]); Capsule::table('cloud_traffic_instance_cursors')->insert([ 'instance_id' => 100, 'user_id' => 50, 'pool_id' => 21, 'accounted_download_bytes' => 10, 'accounted_upload_bytes' => 20, 'last_observed_at' => $now, 'last_event_id' => 31, 'state' => 'ready', 'attention_code' => '', 'version' => 1, 'updated_at' => $now, ]); $wrongCursor = CloudAdminTrafficPoolService::readiness($groupId, $now); $assert(in_array('cursor_missing', array_column( $wrongCursor['blockers'], 'code' ), true), 'A cursor pointing at another user pool passed readiness.'); Capsule::table('cloud_traffic_instance_cursors')->where( 'instance_id', 100 )->update([ 'pool_id' => 20, 'accounted_download_bytes' => 0, 'accounted_upload_bytes' => 0, ]); $unsettled = CloudAdminTrafficPoolService::readiness($groupId, $now); $assert(in_array('usage_unsettled', array_column( $unsettled['blockers'], 'code' ), true), 'A fresh but unaccounted traffic sample passed readiness.'); Capsule::table('cloud_traffic_instance_cursors')->where( 'instance_id', 100 )->update([ 'accounted_download_bytes' => 10, 'accounted_upload_bytes' => 20, ]); $ready = CloudAdminTrafficPoolService::readiness($groupId, $now); $assert($ready['ready'] === true && $ready['blockers'] === [], 'A fully settled active projection did not pass readiness.'); Capsule::table('cloud_instances')->insert([ 'id' => 101, 'resource_pool_id' => 3, 'status' => 'active', 'network_status' => 'active', 'traffic_synced_at' => $now, 'lifetime_download_bytes' => 5, 'lifetime_upload_bytes' => 0, ]); Capsule::table('cloud_service_periods')->insert([ 'id' => 11, 'instance_id' => 101, 'user_id' => 50, 'traffic_pool_group_id' => $groupId, 'traffic_quota_mode' => 'finite', 'starts_at' => $now - 7200, 'ends_at' => $now - 3600, 'traffic_quota_bytes' => 100, 'status' => 'finished', ]); Capsule::table('cloud_traffic_instance_cursors')->insert([ 'instance_id' => 101, 'user_id' => 50, 'pool_id' => 20, 'accounted_download_bytes' => 0, 'accounted_upload_bytes' => 0, 'last_observed_at' => $now, 'last_event_id' => 31, 'state' => 'ready', 'attention_code' => '', 'version' => 1, 'updated_at' => $now, ]); Capsule::table('cloud_traffic_scan_failures')->insert([ ['scan_kind' => 'period', 'subject_id' => 11, 'attempt_count' => 1, 'last_error_code' => 'period_failed', 'retry_at' => $now + 60, 'first_failed_at' => $now, 'last_failed_at' => $now], ['scan_kind' => 'instance', 'subject_id' => 101, 'attempt_count' => 1, 'last_error_code' => 'instance_failed', 'retry_at' => $now + 60, 'first_failed_at' => $now, 'last_failed_at' => $now], ]); $finishedDebt = CloudAdminTrafficPoolService::readiness($groupId, $now); $finishedCodes = array_column($finishedDebt['blockers'], 'code'); $assert(in_array('traffic_scan_failures', $finishedCodes, true) && in_array('usage_unsettled', $finishedCodes, true), 'Finished-period accounting debt was ignored by readiness.'); Capsule::table('cloud_traffic_scan_failures')->delete(); Capsule::table('cloud_traffic_instance_cursors')->where( 'instance_id', 101 )->delete(); Capsule::table('cloud_service_periods')->where('id', 11)->delete(); Capsule::table('cloud_instances')->where('id', 101)->delete(); $enforced = CloudAdminTrafficPoolService::update($groupId, [ 'mode' => 'enforced', 'expected_revision' => 3, ]); $assert($enforced['item']['mode'] === 'enforced' && $enforced['item']['revision'] === 4, 'A ready shadow group did not enter enforced mode.'); $expectConflict( static fn (): array => CloudAdminTrafficPoolService::update( $groupId, ['mode' => 'shadow', 'expected_revision' => 4] ), 'An enforced group was allowed to roll back.' ); echo "Cloud administrator traffic-pool service checks passed.\n";