addConnection([ 'driver' => 'sqlite', 'database' => ':memory:', ], 'default'); $capsule->setAsGlobal(); $capsule->bootEloquent(); $schema = $capsule->schema('default'); $schema->create('cloud_template_import_intents', static function (Blueprint $table): void { $table->string('id')->primary(); $table->integer('user_id'); $table->string('request_idempotency_key'); $table->string('request_fingerprint'); $table->string('source_kind'); $table->string('name'); $table->string('os_family'); $table->string('os_version'); $table->string('source_filename')->default(''); $table->integer('source_size_bytes')->default(0); $table->integer('source_last_modified')->default(0); $table->string('source_sha256')->default(''); $table->text('sealed_source'); $table->string('template_id')->unique(); $table->string('catalog_key'); $table->string('version_label'); $table->string('generation_token')->unique(); $table->integer('storage_revision'); $table->text('sealed_storage_runtime'); $table->string('object_key')->unique(); $table->string('multipart_upload_id')->default(''); $table->integer('multipart_part_size_bytes')->default(0); $table->integer('multipart_part_count')->default(0); $table->string('object_version_id')->default(''); $table->string('object_etag')->default(''); $table->string('state'); $table->integer('active_job_id')->nullable(); $table->string('error_code')->default(''); $table->integer('expires_at'); $table->integer('completed_at')->nullable(); $table->integer('created_at'); $table->integer('updated_at'); $table->unique(['user_id', 'request_idempotency_key']); }); $schema->create('cloud_template_import_parts', static function (Blueprint $table): void { $table->string('intent_id'); $table->integer('part_number'); $table->integer('size_bytes'); $table->string('etag'); $table->integer('created_at'); $table->integer('updated_at'); $table->primary(['intent_id', 'part_number']); }); $runtime = [ 'config' => [ 'endpoint' => 'https://objects.example.test', 'region' => 'ewr', 'bucket' => 'templates', 'prefix' => 'virt-cloud', 'path_style' => true, ], 'credentials' => [ 'access_key_id' => 'INTENTACCESS', 'secret_access_key' => 'intent-secret', ], 'revision' => 9, ]; $uploads = new CloudTemplateMultipartAcquisition( static fn (): array => $runtime, static fn (): int => 2000000000 ); $readModel = new CloudTemplateImportReadModel( static fn (): array => $runtime, static fn (): int => 2000000000 ); $actor = [ 'user_id' => 7, 'is_admin' => true, 'is_super_admin' => true, 'can_manage' => true, 'request_idempotency_key' => 'template-upload-intent-0001', ]; $input = [ 'name' => 'Debian 13 本地版', 'os_family' => 'Debian', 'os_version' => '13', 'filename' => 'debian-13.qcow2', 'size_bytes' => 64 * 1024 * 1024, 'last_modified' => 1999999000000, ]; $first = $uploads->beginUpload($input, $actor); $second = $uploads->beginUpload($input, $actor); $assert($first['created'] === true && $second['created'] === false && $first['intent']['id'] === $second['intent']['id'] && $first['intent']['state'] === 'prepared' && $first['intent']['can_resume'] === true && $first['intent']['source_size_bytes'] === 64 * 1024 * 1024, 'A same-key replay did not return the original template import intent.'); $stored = CloudTemplateImportIntent::findOrFail( (string) $first['intent']['id'] ); $assert((int) $stored->storage_revision === 9 && str_starts_with((string) $stored->object_key, 'virt-cloud/cloud-templates/') && !str_contains(json_encode($first['intent'], JSON_THROW_ON_ERROR), 'intent-secret') && !array_key_exists('sealed_storage_runtime', $first['intent']) && !array_key_exists('multipart_upload_id', $first['intent']), 'The intent did not freeze its object identity or leaked secrets.'); try { $uploads->beginUpload( array_replace($input, ['name' => 'Different']), $actor ); throw new RuntimeException('A same-key different request was accepted.'); } catch (CloudError $error) { $assert($error->httpStatus === 409, 'A same-key fingerprint conflict returned the wrong status.'); } $listed = $readModel->active($actor); $one = $readModel->one((string) $stored->id, $actor); $assert(count($listed['intents']) === 1 && $one['intent']['id'] === (string) $stored->id, 'A refreshed administrator page cannot recover the active intent.'); CloudTemplateImportStateMachine::assertIntegrity(); $definitions = CloudTemplateImportStateMachine::definitions(); $mutated = $definitions; unset($mutated['prepared']['actions']['upload']); try { CloudTemplateImportStateMachine::assertDefinitions($mutated); throw new RuntimeException( 'A source without an executable import action was accepted.' ); } catch (LogicException) { // Expected: every supported source/state pair needs an action policy. } $wrongSource = clone $stored; $wrongSource->state = 'prepared'; $wrongSource->source_kind = 'upload'; try { CloudTemplateImportStateMachine::move($wrongSource, 'fetching'); throw new RuntimeException( 'An upload intent entered the remote-fetch state.' ); } catch (CloudError $error) { $assert($error->httpStatus === 409, 'A source/state mismatch returned the wrong status.'); } $assert(in_array('source_unknown', CloudTemplateImportStateMachine::cleanupStates('upload'), true) && !in_array('source_unknown', CloudTemplateImportStateMachine::cleanupStates('remote'), true), 'Cleanup eligibility is not derived from the executable definitions.'); CloudTemplateImportStateMachine::move($stored, 'uploading'); CloudTemplateImportStateMachine::move($stored, 'source_verifying'); CloudTemplateImportStateMachine::move($stored, 'source_ready'); CloudTemplateImportStateMachine::move($stored, 'distributing'); CloudTemplateImportStateMachine::move($stored, 'ready'); $assert($stored->completed_at !== null && CloudTemplateImportStateMachine::terminal('ready'), 'The import state machine did not mark a terminal intent.'); try { CloudTemplateImportStateMachine::move($stored, 'uploading'); throw new RuntimeException('A terminal import intent was reopened.'); } catch (CloudError $error) { $assert($error->httpStatus === 409, 'An invalid terminal transition returned the wrong status.'); } echo "Cloud-template import intent and state-machine behavior passed.\n"; }