base64_encode(str_repeat('g', 32)), 'cloud_agent_gateway_url' => 'http://127.0.0.1:8001/internal/agent/request', 'cloud_agent_gateway_secret' => str_repeat('w', 32), ]; $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(''); $table->string('country_code', 2)->default(''); $table->integer('enabled')->default(0); $table->string('default_network')->default('public'); $table->integer('created_at'); $table->integer('updated_at'); }); $schema->create('cloud_host_agents', static function (Blueprint $table): void { $table->integer('host_id')->primary(); $table->text('sealed_api_secret'); $table->integer('enabled')->default(0); $table->integer('revision')->default(1); $table->integer('tested_revision')->default(0); $table->string('last_test_status')->default('never'); $table->string('last_error_code')->default(''); $table->string('last_agent_version')->default(''); $table->integer('last_tested_at')->default(0); $table->integer('created_at'); $table->integer('updated_at'); }); $now = time(); $host = new CloudHost(); $host->name = 'Activation Gate Host'; $host->region = 'test'; $host->country_code = 'US'; $host->enabled = true; $host->default_network = 'public'; $host->created_at = $now; $host->updated_at = $now; $host->save(); $saved = CloudAgentSettingsService::save((int) $host->id, [ 'enabled' => true, 'api_secret' => str_repeat('s', 32), 'expected_revision' => 0, ]); $assert($saved['enabled'] === true && $saved['revision'] === 1 && $saved['tested_revision'] === 0 && $saved['ready'] === false && CloudAgentSettingsService::enabledHostIds() === [], 'A newly saved enabled Agent revision became schedulable before testing.'); try { CloudAgentSettingsService::clientForHost((int) $host->id); throw new RuntimeException('Untested Agent client was issued.'); } catch (CloudAgentError $error) { $assert($error->agentCode === 'agent_not_tested', 'Untested Agent returned the wrong stable error code.'); } $recordTest = new ReflectionMethod(CloudAgentSettingsService::class, 'recordTest'); $recordTest->invoke(null, (int) $host->id, 1, 'healthy', '', 'test-1'); $ready = CloudAgentSettingsService::read((int) $host->id); $assert($ready['ready'] === true && $ready['tested_revision'] === 1 && CloudAgentSettingsService::enabledHostIds() === [(int) $host->id] && CloudAgentSettingsService::clientForHost((int) $host->id) instanceof CloudAgentClient, 'The exact successfully tested Agent revision did not become ready.'); CloudAgentSettingsService::invalidateForNetworkSelectorChange( (int) $host->id ); $invalidated = CloudAgentSettingsService::read((int) $host->id); $assert($invalidated['revision'] === 2 && $invalidated['tested_revision'] === 0 && $invalidated['last_test_status'] === 'never' && $invalidated['ready'] === false && CloudAgentSettingsService::enabledHostIds() === [], 'Changing the host selector did not revoke the prior Agent readiness.'); $recordTest->invoke(null, (int) $host->id, 2, 'healthy', '', 'test-2'); $changed = CloudAgentSettingsService::save((int) $host->id, [ 'enabled' => false, 'api_secret' => '', 'expected_revision' => 2, ]); $assert($changed['revision'] === 3 && $changed['enabled'] === false && $changed['tested_revision'] === 0 && $changed['last_test_status'] === 'never' && $changed['ready'] === false && CloudAgentSettingsService::enabledHostIds() === [], 'Changing an Agent setting retained the prior revision readiness.'); $recordTest->invoke(null, (int) $host->id, 3, 'healthy', '', 'test-3'); $recordTest->invoke(null, (int) $host->id, 3, 'unhealthy', 'agent_unavailable', ''); $failed = CloudHostAgent::find((int) $host->id); $assert($failed !== null && !$failed->isReady() && (int) $failed->tested_revision === 0 && CloudAgentSettingsService::enabledHostIds() === [], 'A failed retest did not revoke the current Agent revision readiness.'); echo "Agent current-revision activation gate passed.\n";