create(); $run = ProvisioningRun::factory()->create([ 'subject_type' => Order::class, 'subject_id' => $order->id, 'pipeline' => 'customer', ]); expect($run->fresh()->subject->is($order))->toBeTrue() ->and($order->getRouteKeyName())->toBe('uuid'); }); it('marks the order failed via the ProvisioningSubject hook', function () { $order = Order::factory()->create(['status' => 'provisioning']); $order->onProvisioningFailed(); expect($order->fresh()->status)->toBe('failed'); }); it('enforces a unique stripe_event_id (idempotency)', function () { Order::factory()->create(['stripe_event_id' => 'evt_123']); Order::factory()->create(['stripe_event_id' => 'evt_123']); })->throws(QueryException::class); it('enforces a unique subdomain', function () { Instance::factory()->create(['subdomain' => 'kanzlei-berger']); Instance::factory()->create(['subdomain' => 'kanzlei-berger']); })->throws(QueryException::class); it('encrypts nc_admin_ref at rest', function () { $instance = Instance::factory()->create(['nc_admin_ref' => 'admin']); $raw = \DB::table('instances')->where('id', $instance->id)->value('nc_admin_ref'); expect($raw)->not->toBe('admin') ->and($instance->fresh()->nc_admin_ref)->toBe('admin'); }); it('computes host capacity from committed instance quotas', function () { $host = Host::factory()->active()->create(['total_gb' => 1000, 'reserve_pct' => 15]); // freeGb = 850 Instance::factory()->create(['host_id' => $host->id, 'quota_gb' => 100, 'status' => 'active']); Instance::factory()->create(['host_id' => $host->id, 'quota_gb' => 200, 'status' => 'provisioning']); Instance::factory()->create(['host_id' => $host->id, 'quota_gb' => 500, 'status' => 'failed']); // excluded expect($host->committedGb())->toBe(300) ->and($host->availableGb())->toBe(550); }); it('places an instance on an active host in the datacenter with room', function () { $full = Host::factory()->active()->create(['datacenter' => 'fsn', 'name' => 'pve-fsn-a', 'total_gb' => 100, 'reserve_pct' => 15]); Instance::factory()->create(['host_id' => $full->id, 'quota_gb' => 80, 'status' => 'active']); $room = Host::factory()->active()->create(['datacenter' => 'fsn', 'name' => 'pve-fsn-b', 'total_gb' => 1000, 'reserve_pct' => 15]); Host::factory()->active()->create(['datacenter' => 'hel', 'name' => 'pve-hel-a', 'total_gb' => 1000]); $picked = Host::placeableIn('fsn', 100); expect($picked?->is($room))->toBeTrue() ->and(Host::placeableIn('nowhere', 100))->toBeNull(); });