httpStatus === $status, $message); 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_hosts', static function (Blueprint $table): void { $table->increments('id'); $table->string('name'); $table->string('region')->default(''); }); $schema->create('cloud_public_ip_pools', static function (Blueprint $table): void { $table->increments('id'); $table->string('pool_key'); $table->integer('host_id'); $table->integer('address_family'); $table->string('name'); $table->string('cidr'); }); $schema->create('user', static function (Blueprint $table): void { $table->increments('id'); $table->string('email'); }); $schema->create('cloud_instances', static function (Blueprint $table): void { $table->increments('id'); $table->integer('user_id'); $table->integer('host_id'); $table->integer('reservation_id')->nullable(); $table->string('name'); $table->string('status'); $table->text('spec_json'); }); $schema->create('cloud_resource_reservations', static function (Blueprint $table): void { $table->increments('id'); $table->integer('instance_id')->nullable(); $table->integer('host_id'); $table->integer('user_id'); $table->string('state'); }); $schema->create('cloud_resource_reservation_ips', static function (Blueprint $table): void { $table->increments('id'); $table->integer('reservation_id'); $table->integer('address_family'); $table->integer('claim_generation'); $table->integer('active_claim')->nullable(); $table->integer('public_ip_pool_id'); $table->integer('public_ip_id'); $table->string('operation_id'); $table->string('state'); }); $schema->create('cloud_public_ips', static function (Blueprint $table): void { $table->increments('id'); $table->integer('host_id'); $table->integer('reservation_id')->nullable(); $table->integer('reservation_ip_claim_id')->nullable(); $table->integer('instance_id')->nullable(); $table->integer('public_ip_pool_id'); $table->integer('address_family'); $table->string('operation_id'); $table->string('state'); $table->string('address'); $table->integer('prefix_length'); }); Capsule::table('cloud_hosts')->insert([ ['id' => 1, 'name' => 'HKG-A', 'region' => 'HongKong'], ['id' => 2, 'name' => 'HKG-B', 'region' => 'HongKong'], ]); Capsule::table('cloud_public_ip_pools')->insert([ ['id' => 10, 'pool_key' => str_repeat('a', 32), 'host_id' => 1, 'address_family' => 4, 'name' => 'IPv4 A', 'cidr' => '8.8.8.0/24'], ['id' => 11, 'pool_key' => str_repeat('b', 32), 'host_id' => 1, 'address_family' => 6, 'name' => 'IPv6 A', 'cidr' => '2001:4860::/64'], ]); Capsule::table('user')->insert([ ['id' => 1, 'email' => 'OWNER@example.com'], ['id' => 2, 'email' => 'not-an-email'], ]); Capsule::table('cloud_instances')->insert([ ['id' => 101, 'user_id' => 1, 'host_id' => 1, 'reservation_id' => 201, 'name' => 'vm-one', 'status' => 'active', 'spec_json' => '{}'], ['id' => 102, 'user_id' => 1, 'host_id' => 1, 'reservation_id' => 202, 'name' => 'vm-two', 'status' => 'active', 'spec_json' => '{}'], ['id' => 103, 'user_id' => 2, 'host_id' => 1, 'reservation_id' => null, 'name' => 'vm-ledger-broken', 'status' => 'active', 'spec_json' => '{}'], ['id' => 104, 'user_id' => 1, 'host_id' => 1, 'reservation_id' => 204, 'name' => 'vm-deleted', 'status' => 'deleted', 'spec_json' => '{}'], ]); Capsule::table('cloud_resource_reservations')->insert([ ['id' => 201, 'instance_id' => 101, 'host_id' => 1, 'user_id' => 1, 'state' => 'committed'], ['id' => 202, 'instance_id' => 102, 'host_id' => 1, 'user_id' => 1, 'state' => 'committed'], ['id' => 204, 'instance_id' => 104, 'host_id' => 1, 'user_id' => 1, 'state' => 'committed'], ]); $insertNetwork = static function (int $reservationId, int $instanceId, int $claimBase, string $v4, string $v6): void { Capsule::table('cloud_resource_reservation_ips')->insert([ ['id' => $claimBase, 'reservation_id' => $reservationId, 'address_family' => 4, 'claim_generation' => 1, 'active_claim' => 1, 'public_ip_pool_id' => 10, 'public_ip_id' => $claimBase + 1000, 'operation_id' => str_repeat('a', 32), 'state' => 'committed'], ['id' => $claimBase + 1, 'reservation_id' => $reservationId, 'address_family' => 6, 'claim_generation' => 1, 'active_claim' => 1, 'public_ip_pool_id' => 11, 'public_ip_id' => $claimBase + 1001, 'operation_id' => str_repeat('b', 32), 'state' => 'committed'], ]); Capsule::table('cloud_public_ips')->insert([ ['id' => $claimBase + 1000, 'host_id' => 1, 'reservation_id' => $reservationId, 'reservation_ip_claim_id' => $claimBase, 'instance_id' => $instanceId, 'public_ip_pool_id' => 10, 'address_family' => 4, 'operation_id' => str_repeat('a', 32), 'state' => 'assigned', 'address' => $v4, 'prefix_length' => 24], ['id' => $claimBase + 1001, 'host_id' => 1, 'reservation_id' => $reservationId, 'reservation_ip_claim_id' => $claimBase + 1, 'instance_id' => $instanceId, 'public_ip_pool_id' => 11, 'address_family' => 6, 'operation_id' => str_repeat('b', 32), 'state' => 'assigned', 'address' => $v6, 'prefix_length' => 64], ]); }; $insertNetwork(201, 101, 301, '8.8.8.10', '2001:4860::10'); $insertNetwork(202, 102, 303, '8.8.8.11', '2001:4860::11'); $insertNetwork(204, 104, 305, '8.8.8.12', '2001:4860::12'); $affectedInstances = new CloudAffectedInstanceResolver(); $instanceScope = $affectedInstances->resolve('instance', 101); $assert(count($instanceScope) === 1 && (int) $instanceScope[0]->id === 101 && (int) $instanceScope[0]->user_id === 1, 'Instance scope must resolve exactly one live instance.'); $assert($affectedInstances->resolve('instance', 104) === [], 'Instance scope must exclude a deleted instance.'); $assert(array_map(static fn (object $row): int => (int) $row->id, $affectedInstances->resolve('host', 1)) === [101, 102, 103], 'Host scope must preserve the live-instance selection contract.'); $assert(array_map(static fn (object $row): int => (int) $row->id, $affectedInstances->resolve('public_ip_pool', 10)) === [101, 102], 'Pool scope must preserve the active committed-claim selection contract.'); $audience = new CloudMaintenanceNoticeAudience($affectedInstances); $host = $audience->preview('host', 1); $assert($host['instance_count'] === 3, 'Host audience must include every non-deleted instance.'); $assert($host['recipient_count'] === 1 && $host['skipped_count'] === 1, 'Host audience must separate deliverable and invalid recipients.'); $assert($host['recipients'][0]['email'] === 'owner@example.com' && $host['recipients'][0]['instance_ids'] === [101, 102], 'One owner must receive one grouped recipient row.'); $assert($host['instances'][2]['evidence']['kind'] === 'host_placement_only', 'Host scope must retain an affected VM even when its network ledger is incomplete.'); $assert($host['audience_digest'] === 'a35e34c7d963e63c6d1cb3ff10e6ddaace761f1a1daa8fd0857d7b10ef5ef843', 'Host audience DTO and digest must not drift after resolver extraction.'); $pool = $audience->preview('public_ip_pool', 10); $assert($pool['instance_count'] === 2 && $pool['recipient_count'] === 1, 'Pool audience must include only active committed claims.'); $assert($pool['instances'][0]['ipv4'] === '8.8.8.10' && $pool['instances'][0]['ipv6'] === '2001:4860::10', 'Pool audience must freeze both active addresses.'); $assert($pool['audience_digest'] === $audience ->preview('public_ip_pool', 10)['audience_digest'], 'Audience digest must be deterministic.'); $assert($pool['audience_digest'] === 'dc97bb59cca25dc7423e7e54e2b9877efb3b42485cb8dd03a3f150d368192c44', 'Pool audience DTO and digest must not drift after resolver extraction.'); $expectCloudError( static fn () => $audience->preview('instance', 101), 422, 'Maintenance notices must retain their host-or-pool scope contract.' ); Capsule::table('cloud_resource_reservation_ips') ->where('id', 302)->update(['active_claim' => null, 'state' => 'released']); $expectCloudError( static fn () => $audience->preview('public_ip_pool', 10), 409, 'Pool audience must reject an incomplete active dual-stack ledger.' ); echo "Maintenance notice audience tests passed.\n";