addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default'); $capsule->setAsGlobal(); $capsule->bootEloquent(); $schema = $capsule->schema('default'); $schema->create('cloud_regions', static function (Blueprint $table): void { $table->integer('id')->primary(); $table->string('name'); $table->string('country_code'); $table->string('status'); $table->integer('sort'); }); $schema->create('cloud_region_fabrics', static function (Blueprint $table): void { $table->integer('region_id')->primary(); $table->integer('fabric_revision'); $table->string('state'); }); $schema->create('cloud_instances', static function (Blueprint $table): void { $table->integer('id')->primary(); $table->integer('user_id'); $table->string('name'); $table->string('status'); $table->text('spec_json'); $table->string('service_status'); $table->integer('expires_at')->nullable(); }); $schema->create('cloud_vpcs', static function (Blueprint $table): void { $table->integer('id')->primary(); $table->integer('user_id'); $table->integer('region_id'); $table->string('vpc_key'); $table->string('name'); $table->string('state'); }); $schema->create('cloud_vpc_members', static function (Blueprint $table): void { $table->integer('id')->primary(); $table->integer('instance_id'); $table->integer('vpc_id'); $table->string('state'); }); $schema->create('cloud_vpc_private_ips', static function (Blueprint $table): void { $table->integer('instance_id'); $table->integer('member_id')->nullable(); $table->integer('vpc_id'); }); Capsule::table('cloud_regions')->insert([ ['id' => 1, 'name' => 'Region A', 'country_code' => 'hk', 'status' => 'active', 'sort' => 1], ['id' => 2, 'name' => 'Region B', 'country_code' => 'jp', 'status' => 'active', 'sort' => 0], ]); Capsule::table('cloud_region_fabrics')->insert([ ['region_id' => 1, 'fabric_revision' => 1, 'state' => 'active'], ['region_id' => 2, 'fabric_revision' => 0, 'state' => 'active'], ]); $key = static fn (int $id): string => str_pad(dechex($id), 32, '0', STR_PAD_LEFT); foreach ([1 => 'active', 2 => 'active', 3 => 'deleting', 4 => 'deleted', 5 => 'active'] as $id => $state) { Capsule::table('cloud_vpcs')->insert([ 'id' => $id, 'user_id' => $id === 5 ? 8 : 7, 'region_id' => 1, 'vpc_key' => $key($id), 'name' => $id === 5 ? 'foreign-secret-name' : 'VPC ' . $id, 'state' => $state, ]); } $seedInstance = static function (int $id, array $overrides = []): void { Capsule::table('cloud_instances')->insert(array_replace([ 'id' => $id, 'user_id' => 7, 'name' => sprintf('VM %04d', $id), 'status' => 'active', 'spec_json' => json_encode(['region_id' => 1, 'region' => 'Region A']), 'service_status' => 'active', 'expires_at' => time() + 3600, ], $overrides)); }; $seedMember = static function (int $instanceId, int $vpcId, string $state): void { Capsule::table('cloud_vpc_members')->insert([ 'id' => $instanceId, 'instance_id' => $instanceId, 'vpc_id' => $vpcId, 'state' => $state, ]); Capsule::table('cloud_vpc_private_ips')->insert([ 'instance_id' => $instanceId, 'member_id' => $instanceId, 'vpc_id' => $vpcId, ]); }; for ($id = 1; $id <= 25; $id++) { $seedInstance($id); } $seedInstance(90, ['user_id' => 8, 'name' => 'foreign-instance-name']); $seedMember(2, 1, 'active'); $seedMember(3, 2, 'active'); foreach ([4 => 'applying', 5 => 'uncertain', 6 => 'degraded', 7 => 'failed', 8 => 'pending'] as $id => $state) { $seedMember($id, 1, $state); } $seedMember(11, 1, 'deleting'); $seedMember(12, 1, 'deleted'); $seedMember(13, 3, 'active'); $seedMember(14, 4, 'active'); Capsule::table('cloud_vpc_private_ips')->insert(['instance_id' => 15, 'member_id' => null, 'vpc_id' => 1]); $seedMember(16, 5, 'active'); $seedMember(17, 999, 'active'); $seedMember(18, 1, 'active'); Capsule::table('cloud_vpc_private_ips')->where('instance_id', 18)->update(['vpc_id' => 5]); $seedMember(19, 1, 'active'); // Model a pre-existing corrupt database without relaxing the production unique key. Capsule::table('cloud_vpc_members')->insert(['id' => 190, 'instance_id' => 19, 'vpc_id' => 2, 'state' => 'active']); Capsule::table('cloud_instances')->where('id', 20)->update(['expires_at' => time() - 1]); Capsule::table('cloud_instances')->where('id', 21)->update(['service_status' => 'suspended']); Capsule::table('cloud_instances')->where('id', 22)->update(['service_status' => 'exempt', 'expires_at' => null]); Capsule::table('cloud_instances')->where('id', 23)->update(['status' => 'deleted']); Capsule::table('cloud_instances')->where('id', 24)->update([ 'spec_json' => json_encode(['region_id' => 2, 'region' => 'Region B']), ]); Capsule::table('cloud_instances')->where('id', 25)->update(['spec_json' => 'invalid json']); $actor = ['user_id' => 7, 'is_global' => true]; $read = static function () use ($actor): array { $data = CloudVpcPageService::bootstrap($actor); return array_column($data['instances'], null, 'id'); }; $connection = $capsule->getConnection('default'); $connection->enableQueryLog(); $rows = $read(); $queryCount = count($connection->getQueryLog()); $assert($rows[1]['vpc_attachment'] === ['state' => 'available', 'vpc_key' => null, 'vpc_name' => null], 'An unoccupied instance must remain selectable.'); foreach ([2 => 1, 3 => 2, 4 => 1, 5 => 1, 6 => 1, 7 => 1, 8 => 1] as $id => $vpcId) { $assert($rows[$id]['vpc_attachment'] === [ 'state' => 'attached', 'vpc_key' => $key($vpcId), 'vpc_name' => 'VPC ' . $vpcId, ], 'Every occupied member state must expose its owner-scoped VPC attachment.'); } $assert($rows[2]['region_id'] === $rows[3]['region_id'], 'The cross-VPC fixture must exercise two VPCs in the same region.'); foreach ([11, 12, 13, 14] as $id) { $assert($rows[$id]['vpc_attachment']['state'] === 'cleanup_pending', 'Member or root deletion must retain the instance reservation.'); } foreach ([15, 16, 17, 18, 19] as $id) { $assert($rows[$id]['vpc_attachment'] === ['state' => 'unavailable', 'vpc_key' => null, 'vpc_name' => null], 'Orphan, conflicting, duplicate or foreign ledgers must be unavailable without disclosing a VPC.'); } $assert(!isset($rows[90]) && !isset($rows[23]) && !str_contains(json_encode($rows), 'foreign-'), 'The tenant projection must not reveal other accounts or deleted instances.'); $assert(!$rows[20]['service_available'] && !$rows[21]['service_available'] && $rows[22]['service_available'] && $rows[24]['region_id'] === 2 && $rows[25]['region_id'] === null, 'Membership projection must preserve service lifecycle and region eligibility fields.'); $bootstrap = CloudVpcPageService::bootstrap($actor); $assert(count($bootstrap['regions']) === 1 && $bootstrap['regions'][0]['country_code'] === 'HK', 'The existing active Fabric region projection must remain unchanged.'); $assert(CloudVpcPageService::bootstrap(['user_id' => 0])['instances'] === [], 'An actor without an account must not receive instance candidates.'); // A reload must observe joins immediately; only normal physical cleanup frees // occupancy, never a terminal-looking member state or deletion of half a pair. $seedMember(1, 2, 'applying'); $assert($read()[1]['vpc_attachment']['state'] === 'attached', 'A refresh missed a newly attached instance.'); Capsule::table('cloud_vpc_members')->where('instance_id', 1)->update(['state' => 'deleted']); $assert($read()[1]['vpc_attachment']['state'] === 'cleanup_pending', 'Deleted membership was prematurely released.'); Capsule::table('cloud_vpc_members')->where('instance_id', 1)->delete(); $assert($read()[1]['vpc_attachment']['state'] === 'unavailable', 'An orphan lease was prematurely released.'); Capsule::table('cloud_vpc_private_ips')->where('instance_id', 1)->delete(); $assert($read()[1]['vpc_attachment']['state'] === 'available', 'Completed cleanup did not restore candidacy on refresh.'); $assertAllUnavailable = static function (string $reason) use ($read, $assert): void { $observed = $read(); $assert(count($observed) === 24, 'Read failure must keep the bounded instance display available.'); foreach ($observed as $row) { $assert($row['vpc_attachment'] === ['state' => 'unavailable', 'vpc_key' => null, 'vpc_name' => null], $reason); } }; foreach (['cloud_vpc_members', 'cloud_vpc_private_ips', 'cloud_vpcs'] as $table) { $schema->rename($table, $table . '_hidden'); $assertAllUnavailable('A missing ledger table was mistaken for no membership.'); $schema->rename($table . '_hidden', $table); } // Inject a read failure at the actual database execution boundary while all // table checks pass. An unavailable ledger must not reopen free candidates. $failMembershipRead = true; $connection->beforeExecuting(static function (string $query) use (&$failMembershipRead): void { if ($failMembershipRead && str_contains($query, 'from "cloud_vpc_members"')) { throw new RuntimeException('Simulated ledger read outage'); } }); $assertAllUnavailable('A failed ledger query was mistaken for no membership.'); $failMembershipRead = false; $assert($read()[1]['vpc_attachment']['state'] === 'available', 'Recovered reads must refresh candidacy.'); for ($id = 100; $id < 305; $id++) { $seedInstance($id); } $connection->flushQueryLog(); $bounded = $read(); $assert(count($bounded) === 200, 'The page lost its 200-instance bound.'); $assert(count($connection->getQueryLog()) === $queryCount, 'Membership projection introduced per-instance database queries.'); $assert(array_slice(array_keys($bounded), 0, 24) === array_keys($rows), 'Membership projection changed stable instance ordering.'); echo "VPC page membership runtime passed.\n";