addConnection([ 'driver' => 'sqlite', 'database' => ':memory:', ], 'default'); $capsule->setAsGlobal(); $capsule->bootEloquent(); $schema = $capsule->schema('default'); $schema->create('cloud_templates', static function (Blueprint $table): void { $table->string('id')->primary(); $table->string('name'); $table->string('state'); }); $schema->create('cloud_orders', static function (Blueprint $table): void { $table->increments('id'); $table->integer('user_id'); $table->integer('host_id')->nullable(); $table->integer('instance_id')->nullable(); $table->string('status'); $table->text('spec_json'); }); $schema->create('cloud_instances', static function (Blueprint $table): void { $table->increments('id'); $table->integer('host_id'); $table->integer('user_id'); $table->integer('order_id')->nullable(); $table->string('status'); $table->text('spec_json'); $table->integer('updated_at'); }); $schema->create('cloud_jobs', static function (Blueprint $table): void { $table->increments('id'); $table->integer('instance_id')->nullable(); $table->integer('host_id')->nullable(); $table->string('kind'); $table->string('execution_mode')->default('agent'); $table->string('phase'); $table->string('outcome')->nullable(); $table->string('status')->default('active'); $table->integer('attempts')->default(0); $table->integer('dispatch_attempts')->default(0); $table->string('operation_id')->nullable(); $table->string('request_fingerprint')->nullable(); $table->string('request_idempotency_key')->nullable(); $table->integer('dispatch_started_at')->nullable(); $table->integer('agent_accepted_at')->nullable(); $table->integer('dispatched_at')->nullable(); $table->integer('dispatch_deadline_at')->nullable(); $table->integer('receipt_deadline_at')->nullable(); $table->string('completion_event_id')->nullable(); $table->string('error_code')->nullable(); $table->boolean('effect_may_have_applied')->nullable(); $table->boolean('safe_to_resubmit')->nullable(); $table->string('effect_resolution')->default('not_applicable'); $table->boolean('finalization_incident')->default(false); $table->string('finalization_error_code')->nullable(); $table->text('result_json')->nullable(); $table->integer('terminal_at')->nullable(); }); Capsule::table('cloud_templates')->insert([ 'id' => 'debian-13-template', 'name' => 'Debian 13', 'state' => 'active', ]); $baseSpec = [ 'source' => 'cloud', 'template_id' => 'debian-13-template', 'public_ip_cidr' => '203.0.113.18/28', 'agent_metadata_version' => 1, ]; foreach ([ 1 => $baseSpec, 2 => array_replace($baseSpec, ['os_source' => 'iso']), 3 => $baseSpec, 4 => $baseSpec, ] as $id => $spec) { $orderSpec = $id === 4 ? array_replace($baseSpec, [ 'os_source' => 'cloud_template', 'os_name' => 'Debian 12 Snapshot', 'login_name' => 'root', ]) : $baseSpec; Capsule::table('cloud_orders')->insert([ 'id' => $id, 'user_id' => 10, 'host_id' => 7, 'instance_id' => $id, 'status' => 'succeeded', 'spec_json' => json_encode($orderSpec, JSON_THROW_ON_ERROR), ]); Capsule::table('cloud_instances')->insert([ 'id' => $id, 'host_id' => 7, 'user_id' => $id === 3 ? 99 : 10, 'order_id' => $id, 'status' => 'active', 'spec_json' => json_encode($spec, JSON_THROW_ON_ERROR), 'updated_at' => 1, ]); } $firstPage = CloudInstanceMetadataRepairService::run(false, 1); $assert($firstPage['scanned'] === 1 && $firstPage['would_repair'] === 1 && $firstPage['has_more'] === true && $firstPage['next_after_id'] === 1, 'Repair scan did not expose a stable next-page cursor.'); $lastPage = CloudInstanceMetadataRepairService::run(false, 1, 3); $assert($lastPage['after_id'] === 3 && $lastPage['scanned'] === 1 && $lastPage['would_repair'] === 1 && $lastPage['has_more'] === false && $lastPage['next_after_id'] === null, 'Repair scan did not honor the after-id cursor.'); $before = (string) Capsule::table('cloud_instances')->where('id', 1) ->value('spec_json'); $dryRun = CloudInstanceMetadataRepairService::run(false, 100); $assert($dryRun === [ 'mode' => 'dry-run', 'after_id' => 0, 'next_after_id' => null, 'has_more' => false, 'scanned' => 4, 'would_repair' => 2, 'repaired' => 0, 'unchanged' => 0, 'skipped' => 2, ] && Capsule::table('cloud_instances')->where('id', 1) ->value('spec_json') === $before, 'Dry-run changed data or did not report the one proven repair.'); Capsule::table('cloud_jobs')->insert([ 'instance_id' => 1, 'kind' => 'instance_upgrade', 'phase' => 'accepted', 'status' => 'accepted', ]); $applied = CloudInstanceMetadataRepairService::run(true, 100); $repaired = json_decode((string) Capsule::table('cloud_instances') ->where('id', 1)->value('spec_json'), true, flags: JSON_THROW_ON_ERROR); $conflict = json_decode((string) Capsule::table('cloud_instances') ->where('id', 2)->value('spec_json'), true, flags: JSON_THROW_ON_ERROR); $snapshot = json_decode((string) Capsule::table('cloud_instances') ->where('id', 4)->value('spec_json'), true, flags: JSON_THROW_ON_ERROR); $assert($applied['repaired'] === 1 && $applied['skipped'] === 3 && !isset($repaired['os_source'], $repaired['os_name'], $repaired['login_name'], $repaired['managed']) && ($repaired['public_ip_cidr'] ?? null) === '203.0.113.18/28' && ($repaired['agent_metadata_version'] ?? null) === 1 && ($conflict['os_source'] ?? null) === 'iso', 'Apply rewrote a source specification protected by an active upgrade.'); $assert(($snapshot['os_name'] ?? null) === 'Debian 12 Snapshot' && ($snapshot['managed'] ?? null) === true, 'Repair did not prefer the immutable paid-order metadata snapshot.'); $assert(Capsule::table('cloud_jobs')->delete() === 1, 'Upgrade fence fixture was not removed.'); $repeat = CloudInstanceMetadataRepairService::run(true, 100); $assert($repeat['repaired'] === 1 && $repeat['unchanged'] === 1 && $repeat['skipped'] === 2, 'Metadata repair did not resume after the upgrade fence cleared.'); $final = CloudInstanceMetadataRepairService::run(true, 100); $assert($final['repaired'] === 0 && $final['unchanged'] === 2 && $final['skipped'] === 2, 'Metadata repair was not idempotent.'); echo "Cloud instance metadata repair tests passed.\n";