$value) { if (is_string($key) && (str_contains($key, 'credential') || str_contains($key, 'operation') || in_array($key, [ 'host', 'host_id', 'resource_pool', 'resource_pool_id', 'pool_id', 'user_id', ], true))) { throw new RuntimeException($message . ': ' . $key); } if (is_array($value)) { $assertNoInternalFields($value, $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 $table): void { $table->increments('id'); $table->string('code')->unique(); $table->string('name'); $table->string('mode'); $table->integer('revision')->default(1); $table->integer('created_at'); $table->integer('updated_at'); }); $schema->create('cloud_regions', static function (Blueprint $table): void { $table->increments('id'); $table->string('code')->unique(); $table->string('name'); $table->string('country_code'); $table->string('status'); $table->integer('sort')->default(0); $table->integer('created_at'); $table->integer('updated_at'); }); $schema->create('cloud_network_lines', static function (Blueprint $table): void { $table->increments('id'); $table->integer('region_id'); $table->integer('traffic_pool_group_id')->nullable(); $table->string('code'); $table->string('name'); $table->boolean('is_default')->default(false); $table->string('status'); $table->integer('sort')->default(0); $table->integer('created_at'); $table->integer('updated_at'); }); $schema->create('cloud_traffic_pools', static function (Blueprint $table): void { $table->increments('id'); $table->integer('user_id'); $table->integer('traffic_pool_group_id'); $table->string('state'); $table->integer('available_bytes')->default(0); $table->integer('overage_bytes')->default(0); $table->integer('version')->default(1); $table->integer('last_sequence')->default(0); $table->integer('last_event_id')->nullable(); $table->integer('created_at'); $table->integer('updated_at'); $table->unique(['user_id', 'traffic_pool_group_id']); }); $schema->create('cloud_resource_pools', static function (Blueprint $table): void { $table->increments('id'); $table->integer('line_id'); }); $schema->create('cloud_instances', static function (Blueprint $table): void { $table->increments('id'); $table->integer('user_id'); $table->integer('resource_pool_id')->nullable(); $table->integer('current_period_id')->nullable(); $table->string('name'); $table->string('status'); $table->string('service_status'); $table->string('network_status'); $table->integer('expires_at')->nullable(); $table->integer('traffic_download_bytes')->default(0); $table->integer('traffic_upload_bytes')->default(0); }); $schema->create('cloud_service_periods', static function (Blueprint $table): void { $table->increments('id'); $table->integer('instance_id'); $table->integer('user_id'); $table->integer('traffic_pool_group_id')->nullable(); $table->string('traffic_quota_mode')->default('finite'); $table->integer('traffic_quota_bytes')->default(0); $table->integer('starts_at'); $table->integer('ends_at'); $table->string('status'); }); $schema->create('cloud_traffic_entitlements', static function (Blueprint $table): void { $table->increments('id'); $table->integer('pool_id'); $table->integer('user_id'); $table->integer('instance_id'); $table->integer('service_period_id')->unique(); $table->integer('starts_at'); $table->integer('ends_at'); $table->string('quota_mode'); $table->integer('granted_bytes')->default(0); $table->integer('consumed_bytes')->default(0); $table->integer('forfeited_bytes')->default(0); $table->string('state'); $table->integer('activated_at')->nullable(); $table->integer('closed_at')->nullable(); $table->integer('version')->default(1); $table->integer('created_at'); $table->integer('updated_at'); }); $schema->create('cloud_traffic_instance_cursors', static function (Blueprint $table): void { $table->integer('instance_id')->primary(); $table->integer('user_id'); $table->integer('pool_id'); $table->integer('accounted_download_bytes')->default(0); $table->integer('accounted_upload_bytes')->default(0); $table->integer('last_observed_at')->default(0); $table->integer('last_event_id')->nullable(); $table->string('state')->default('ready'); $table->string('attention_code')->default(''); $table->integer('version')->default(1); $table->integer('updated_at'); }); $schema->create('cloud_instance_network_restrictions', static function (Blueprint $table): void { $table->increments('id'); $table->integer('instance_id'); $table->integer('user_id'); $table->string('reason_code'); $table->boolean('desired_active'); $table->integer('revision')->default(1); $table->integer('created_at'); $table->integer('updated_at'); }); $schema->create('cloud_instance_network_policy_states', static function (Blueprint $table): void { $table->integer('instance_id')->primary(); $table->integer('user_id'); $table->boolean('desired_suspended'); $table->boolean('applied_suspended'); $table->string('state'); $table->integer('revision')->default(1); $table->integer('created_at'); $table->integer('updated_at'); }); $now = 2_000_000; $groups = []; foreach ([ [1, 'line-a', '线路 A', 'enforced'], [2, 'line-b', '线路 B', 'shadow'], [3, 'line-c', '线路 C', 'enforced'], [4, 'line-d', '线路 D', 'enforced'], [5, 'line-e', '线路 E', 'enforced'], [6, 'line-f', '线路 F', 'enforced'], [7, 'line-g', '线路 G', 'enforced'], ] as [$id, $code, $name, $mode]) { $groups[] = compact('id', 'code', 'name', 'mode') + [ 'revision' => 1, 'created_at' => $now, 'updated_at' => $now, ]; } Capsule::table('cloud_traffic_pool_groups')->insert($groups); Capsule::table('cloud_regions')->insert([ ['id' => 10, 'code' => 'hkg', 'name' => '香港', 'country_code' => 'HK', 'status' => 'active', 'sort' => 10, 'created_at' => $now, 'updated_at' => $now], ['id' => 20, 'code' => 'sgp', 'name' => '新加坡', 'country_code' => 'SG', 'status' => 'active', 'sort' => 10, 'created_at' => $now, 'updated_at' => $now], ['id' => 30, 'code' => 'tyo', 'name' => '东京', 'country_code' => 'JP', 'status' => 'active', 'sort' => 30, 'created_at' => $now, 'updated_at' => $now], ]); $lines = []; foreach ([ [1, 10, 1, 'hkg-a', '香港线路 A'], [2, 10, 1, 'hkg-a2', '香港线路 A2'], [3, 10, 2, 'hkg-shadow', '香港影子线路'], [4, 20, 3, 'sgp-c', '新加坡线路 C'], [5, 20, 5, 'sgp-e', '新加坡线路 E'], [6, 10, 6, 'hkg-f', '香港跨区线路'], [7, 20, 6, 'sgp-f', '新加坡跨区线路'], [8, 20, 7, 'sgp-g', '新加坡线路 G'], [9, 30, 3, 'tyo-c', '东京线路 C'], ] as [$id, $regionId, $groupId, $code, $name]) { $lines[] = [ 'id' => $id, 'region_id' => $regionId, 'traffic_pool_group_id' => $groupId, 'code' => $code, 'name' => $name, 'is_default' => 0, 'status' => 'active', 'sort' => $id, 'created_at' => $now, 'updated_at' => $now, ]; } Capsule::table('cloud_network_lines')->insert($lines); Capsule::table('cloud_resource_pools')->insert([ ['id' => 1, 'line_id' => 1], ['id' => 2, 'line_id' => 3], ['id' => 3, 'line_id' => 1], ['id' => 4, 'line_id' => 2], ['id' => 5, 'line_id' => 4], ['id' => 6, 'line_id' => 5], ['id' => 7, 'line_id' => 6], ]); Capsule::table('cloud_traffic_pools')->insert([ ['id' => 11, 'user_id' => 1, 'traffic_pool_group_id' => 1, 'state' => 'available', 'available_bytes' => 1100, 'overage_bytes' => 0, 'version' => 3, 'last_sequence' => 0, 'created_at' => $now, 'updated_at' => $now], ['id' => 12, 'user_id' => 1, 'traffic_pool_group_id' => 2, 'state' => 'available', 'available_bytes' => 200, 'overage_bytes' => 0, 'version' => 1, 'last_sequence' => 0, 'created_at' => $now, 'updated_at' => $now], ['id' => 13, 'user_id' => 1, 'traffic_pool_group_id' => 3, 'state' => 'exhausted', 'available_bytes' => 0, 'overage_bytes' => 20, 'version' => 4, 'last_sequence' => 0, 'created_at' => $now, 'updated_at' => $now], ['id' => 14, 'user_id' => 1, 'traffic_pool_group_id' => 4, 'state' => 'attention', 'available_bytes' => 300, 'overage_bytes' => 0, 'version' => 1, 'last_sequence' => 0, 'created_at' => $now, 'updated_at' => $now], ['id' => 21, 'user_id' => 2, 'traffic_pool_group_id' => 1, 'state' => 'available', 'available_bytes' => 9999, 'overage_bytes' => 0, 'version' => 1, 'last_sequence' => 0, 'created_at' => $now, 'updated_at' => $now], ['id' => 16, 'user_id' => 1, 'traffic_pool_group_id' => 6, 'state' => 'available', 'available_bytes' => 500, 'overage_bytes' => 0, 'version' => 2, 'last_sequence' => 0, 'created_at' => $now, 'updated_at' => $now], ['id' => 17, 'user_id' => 1, 'traffic_pool_group_id' => 7, 'state' => 'available', 'available_bytes' => 0, 'overage_bytes' => 0, 'version' => 2, 'last_sequence' => 0, 'created_at' => $now, 'updated_at' => $now], ['id' => 31, 'user_id' => 3, 'traffic_pool_group_id' => 1, 'state' => 'available', 'available_bytes' => 100, 'overage_bytes' => 0, 'version' => 1, 'last_sequence' => 0, 'created_at' => $now, 'updated_at' => $now], ['id' => 33, 'user_id' => 3, 'traffic_pool_group_id' => 5, 'state' => 'available', 'available_bytes' => 100, 'overage_bytes' => 0, 'version' => 1, 'last_sequence' => 0, 'created_at' => $now, 'updated_at' => $now], ]); $instances = [ [1, 1, 1, 101, 'owner-a', 'active', 'active', 'active', 100, 50], [2, 1, 2, 102, 'shadow', 'active', 'active', 'active', 30, 20], [3, 2, 3, 103, 'foreign', 'active', 'active', 'active', 8000, 1000], [4, 1, 4, 104, 'unlimited', 'active', 'active', 'active', 9000, 9000], [5, 1, 5, 105, 'owner-c', 'active', 'active', 'blocked_traffic', 140, 80], [6, 1, 6, 106, 'preparing', 'active', 'active', 'restoring', 0, 0], [7, 1, 7, 107, 'existing-pool-pending', 'active', 'active', 'active', 0, 0], ]; foreach ($instances as [$id, $userId, $resourcePoolId, $periodId, $name, $status, $serviceStatus, $networkStatus, $download, $upload]) { Capsule::table('cloud_instances')->insert([ 'id' => $id, 'user_id' => $userId, 'resource_pool_id' => $resourcePoolId, 'current_period_id' => $periodId, 'name' => $name, 'status' => $status, 'service_status' => $serviceStatus, 'network_status' => $networkStatus, 'expires_at' => $now + 900, 'traffic_download_bytes' => $download, 'traffic_upload_bytes' => $upload, ]); } $periods = [ [101, 1, 1, 1, 'finite', 1000], [102, 2, 1, 2, 'finite', 200], [103, 3, 2, 1, 'finite', 9999], [104, 4, 1, 1, 'unlimited', 0], [105, 5, 1, 3, 'finite', 200], [106, 6, 1, 5, 'finite', 700], [107, 7, 1, 6, 'finite', 900], ]; foreach ($periods as [$id, $instanceId, $userId, $groupId, $mode, $quota]) { Capsule::table('cloud_service_periods')->insert([ 'id' => $id, 'instance_id' => $instanceId, 'user_id' => $userId, 'traffic_pool_group_id' => $groupId, 'traffic_quota_mode' => $mode, 'traffic_quota_bytes' => $quota, 'starts_at' => $now - 100, 'ends_at' => $now + 900, 'status' => 'active', ]); } $entitlements = [ [1, 11, 1, 1, 101, 1000, 300, $now + 900, 'active'], [2, 11, 1, 99, 199, 500, 100, $now + 300, 'active'], [3, 12, 1, 2, 102, 200, 0, $now + 900, 'active'], [4, 13, 1, 5, 105, 200, 200, $now + 900, 'exhausted'], [5, 14, 1, 98, 198, 300, 0, $now + 700, 'active'], [6, 21, 2, 3, 103, 9999, 0, $now + 900, 'active'], [7, 16, 1, 97, 197, 500, 0, $now + 600, 'active'], [8, 17, 1, 96, 196, 400, 100, $now - 1, 'expired'], [9, 31, 3, 301, 301, 100, 0, $now + 900, 'active'], [10, 33, 3, 303, 303, 100, 0, $now + 900, 'active'], ]; foreach ($entitlements as [$id, $poolId, $userId, $instanceId, $periodId, $granted, $consumed, $endsAt, $state]) { Capsule::table('cloud_traffic_entitlements')->insert([ 'id' => $id, 'pool_id' => $poolId, 'user_id' => $userId, 'instance_id' => $instanceId, 'service_period_id' => $periodId, 'starts_at' => $now - 100, 'ends_at' => $endsAt, 'quota_mode' => 'finite', 'granted_bytes' => $granted, 'consumed_bytes' => $consumed, 'forfeited_bytes' => 0, 'state' => $state, 'version' => 1, 'created_at' => $now, 'updated_at' => $now, ]); } Capsule::table('cloud_traffic_instance_cursors')->insert([ ['instance_id' => 1, 'user_id' => 1, 'pool_id' => 11, 'last_observed_at' => $now - 5, 'updated_at' => $now], ['instance_id' => 5, 'user_id' => 1, 'pool_id' => 13, 'last_observed_at' => $now - 4, 'updated_at' => $now], ]); Capsule::table('cloud_instance_network_restrictions')->insert([ ['instance_id' => 1, 'user_id' => 1, 'reason_code' => 'traffic_pool_exhausted', 'desired_active' => 0, 'revision' => 2, 'created_at' => $now, 'updated_at' => $now], ['instance_id' => 1, 'user_id' => 1, 'reason_code' => 'admin_hold', 'desired_active' => 1, 'revision' => 1, 'created_at' => $now, 'updated_at' => $now], ['instance_id' => 5, 'user_id' => 1, 'reason_code' => 'traffic_pool_exhausted', 'desired_active' => 1, 'revision' => 1, 'created_at' => $now, 'updated_at' => $now], ['instance_id' => 6, 'user_id' => 1, 'reason_code' => 'traffic_pool_exhausted', 'desired_active' => 0, 'revision' => 2, 'created_at' => $now, 'updated_at' => $now], ]); Capsule::table('cloud_instance_network_policy_states')->insert([ ['instance_id' => 1, 'user_id' => 1, 'desired_suspended' => 1, 'applied_suspended' => 0, 'state' => 'failed', 'revision' => 3, 'created_at' => $now, 'updated_at' => $now], ['instance_id' => 5, 'user_id' => 1, 'desired_suspended' => 1, 'applied_suspended' => 1, 'state' => 'applied', 'revision' => 1, 'created_at' => $now, 'updated_at' => $now], ['instance_id' => 6, 'user_id' => 1, 'desired_suspended' => 0, 'applied_suspended' => 1, 'state' => 'applying', 'revision' => 2, 'created_at' => $now, 'updated_at' => $now], ]); Capsule::connection('default')->flushQueryLog(); Capsule::connection('default')->enableQueryLog(); $payload = CloudTrafficPoolReadModel::forUser(1, $now); $userProjectionQueries = count(Capsule::connection('default')->getQueryLog()); Capsule::connection('default')->disableQueryLog(); $assert($userProjectionQueries <= 13, 'The line-pool projection added a query loop per pool or instance: ' . $userProjectionQueries); $assert(array_keys($payload) === [ 'line_pools', 'line_pool_count', 'generated_at', ], 'User projection retained a duplicate region or raw-item interface.'); $linePools = $payload['line_pools']; $assert(array_column($linePools, 'key') === [ 'line-group:1', 'line-group:3', 'line-group:4', 'line-group:5', 'line-group:6', ] && array_column($linePools, 'group_id') === [1, 3, 4, 5, 6] && $payload['line_pool_count'] === 5, 'Commercial line pools were not projected once in stable group order.'); $assert(array_column($linePools[1]['coverage_regions'], 'name') === [ '新加坡', '东京', ] && $linePools[1]['coverage_region_count'] === 2, 'A cross-region commercial line lost or duplicated its coverage regions.'); $assert(array_column($linePools[4]['coverage_regions'], 'name') === [ '香港', '新加坡', ] && $linePools[4]['instance_count'] === 1, 'Another cross-region line was merged with a different pool identity.'); $assert($linePools[2]['coverage_regions'] === [] && $linePools[2]['summary_state'] === 'attention' && $linePools[2]['default_open'] === true, 'A line pool without a valid region was not surfaced for attention.'); $singleRegion = CloudTrafficPoolReadModel::forUser(2, $now); $assert($singleRegion['line_pool_count'] === 1 && $singleRegion['line_pools'][0]['key'] === 'line-group:1' && $singleRegion['line_pools'][0]['default_open'] === true && $singleRegion['line_pools'][0]['instances'][0]['region'] === [ 'code' => 'hkg', 'name' => '香港', ], 'A single commercial line did not expose its instance placement region.'); Capsule::table('cloud_instances')->where('id', 3)->update([ 'resource_pool_id' => 5, ]); $placementMismatch = CloudTrafficPoolReadModel::forUser(2, $now); $assert($placementMismatch['line_pools'][0]['summary_state'] === 'attention' && $placementMismatch['line_pools'][0]['requires_attention'] === true && $placementMismatch['line_pools'][0]['instances'][0][ 'placement_scope_state'] === 'attention', 'A placement line outside the frozen commercial group was presented as healthy.'); Capsule::table('cloud_instances')->where('id', 3)->update([ 'resource_pool_id' => 3, ]); Capsule::table('cloud_traffic_pools')->where('id', 21)->update([ 'state' => 'unexpected_state', ]); $unknownState = CloudTrafficPoolReadModel::forUser(2, $now); $assert($unknownState['line_pools'][0]['summary_state'] === 'attention' && $unknownState['line_pools'][0]['default_open'] === true, 'An unknown pool state was presented as a healthy commercial line.'); Capsule::table('cloud_traffic_pools')->where('id', 21)->update([ 'state' => 'available', ]); $healthyLines = CloudTrafficPoolReadModel::forUser(3, $now); $assert(array_column($healthyLines['line_pools'], 'key') === [ 'line-group:1', 'line-group:5', ] && array_column( $healthyLines['line_pools'], 'default_open' ) === [true, false], 'Multiple healthy commercial lines should open only the first by default.'); $assertNoInternalFields($payload, 'User traffic-pool DTO exposed an internal identifier'); $lineA = $linePools[0]; $assert($lineA['active_entitlement_bytes'] === 1500 && $lineA['active_consumed_bytes'] === 400 && $lineA['available_bytes'] === 1100 && $lineA['next_expiry_at'] === $now + 300 && $lineA['next_expiry_bytes'] === 400 && $lineA['last_observed_at'] === $now - 5, 'User projection did not preserve the canonical FEFO snapshot.'); $assert(count($lineA['instances']) === 1 && $lineA['instances'][0]['id'] === '1' && $lineA['instances'][0]['traffic_used_bytes'] === 150 && $lineA['instances'][0]['period_traffic_quota_bytes'] === 1000 && $lineA['network_restriction_state'] === 'clear', 'Current-instance observations crossed user, group, deletion or unlimited boundaries.'); $assert($linePools[1]['network_restriction_state'] === 'restricted', 'Applied aggregate traffic restriction was not projected to the user.'); $assert($linePools[2]['instances'] === [] && $linePools[2]['available_bytes'] === 300 && $linePools[2]['network_restriction_state'] === 'attention', 'Deleting an instance incorrectly removed its unexpired pool entitlement.'); $assert($linePools[3]['state'] === 'preparing' && $linePools[3]['projection_ready'] === false && $linePools[3]['network_restriction_state'] === 'applying' && $linePools[3]['instances'][0]['id'] === '6', 'A newly active enforced period disappeared before its pool projection was materialized.'); $assert($linePools[4]['state'] === 'preparing' && $linePools[4]['projection_ready'] === false && $linePools[4]['active_entitlement_bytes'] === 500, 'An existing pool concealed a newly active period whose entitlement was not materialized.'); $batchPeriods = [ CloudServicePeriod::find(101), CloudServicePeriod::find(103), CloudServicePeriod::find(105), CloudServicePeriod::find(107), ]; Capsule::connection('default')->flushQueryLog(); Capsule::connection('default')->enableQueryLog(); $batch = CloudTrafficPoolReadModel::forPeriods($batchPeriods, $now); $batchQueries = count(Capsule::connection('default')->getQueryLog()); Capsule::connection('default')->disableQueryLog(); $assert($batchQueries <= 5 && count($batch) === 4, 'Multi-user period projection regressed to a query loop per pool.'); $pooled = CloudTrafficPoolReadModel::forPeriod( 1, CloudServicePeriod::find(101), $now ); $shadow = CloudTrafficPoolReadModel::forPeriod( 1, CloudServicePeriod::find(102), $now ); $unlimited = CloudTrafficPoolReadModel::forPeriod( 1, CloudServicePeriod::find(104), $now ); $foreign = CloudTrafficPoolReadModel::forPeriod( 1, CloudServicePeriod::find(103), $now ); $preparing = CloudTrafficPoolReadModel::forPeriod( 1, CloudServicePeriod::find(106), $now ); $existingPoolPending = CloudTrafficPoolReadModel::forPeriod( 1, CloudServicePeriod::find(107), $now ); $assert($pooled['traffic_accounting_mode'] === 'pooled' && $pooled['traffic_pool']['group_id'] === 1 && $pooled['traffic_pool']['instances'] === [], 'Enforced finite period did not resolve through the canonical pool model.'); $assert($shadow['traffic_accounting_mode'] === 'legacy' && $shadow['traffic_pool'] === null, 'Shadow accounting leaked into the user-facing shared-pool vocabulary.'); $assert($unlimited['traffic_accounting_mode'] === 'unlimited' && $unlimited['traffic_pool'] === null, 'Unlimited traffic was not kept outside the finite shared pool.'); $assert($foreign['traffic_accounting_mode'] === 'legacy', 'A foreign service period crossed the user boundary.'); $assert($preparing['traffic_accounting_mode'] === 'pooled' && $preparing['traffic_pool']['projection_ready'] === false && $preparing['traffic_pool']['state'] === 'preparing', 'An enforced period without a materialized pool was misreported as zero quota.'); $assert($existingPoolPending['traffic_accounting_mode'] === 'pooled' && $existingPoolPending['traffic_pool']['projection_ready'] === false && $existingPoolPending['traffic_pool']['state'] === 'preparing', 'An existing pool misreported a missing current entitlement as ready.'); Capsule::table('cloud_service_periods')->where('id', 101)->update([ 'traffic_quota_bytes' => 1200, ]); $upgradePending = CloudTrafficPoolReadModel::forPeriod( 1, CloudServicePeriod::find(101), $now ); $assert($upgradePending['traffic_accounting_mode'] === 'pooled' && $upgradePending['traffic_pool']['projection_ready'] === false && $upgradePending['traffic_pool']['state'] === 'preparing' && $upgradePending['traffic_pool']['active_entitlement_bytes'] === 1500, 'A paid quota increase exposed the stale entitlement total as ready.'); echo "Cloud traffic-pool user read-model tests passed.\n";