feat(ai-edits): add output storage backfill flow and coverage
This commit is contained in:
132
tests/Unit/Services/AiEditOutputStorageServiceTest.php
Normal file
132
tests/Unit/Services/AiEditOutputStorageServiceTest.php
Normal file
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services;
|
||||
|
||||
use App\Models\AiEditRequest;
|
||||
use App\Models\AiStyle;
|
||||
use App\Models\Event;
|
||||
use App\Models\Photo;
|
||||
use App\Services\AiEditing\AiEditOutputStorageService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Tests\TestCase;
|
||||
|
||||
class AiEditOutputStorageServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_it_persists_provider_output_to_local_storage(): void
|
||||
{
|
||||
config([
|
||||
'ai-editing.outputs.enabled' => true,
|
||||
'ai-editing.outputs.allowed_hosts' => ['cdn.runware.ai'],
|
||||
'filesystems.default' => 'public',
|
||||
'watermark.base.asset' => 'branding/test-watermark.png',
|
||||
]);
|
||||
|
||||
Storage::fake('public');
|
||||
Storage::disk('public')->put('branding/test-watermark.png', $this->tinyPngBinary());
|
||||
|
||||
Http::fake([
|
||||
'https://cdn.runware.ai/*' => Http::response($this->tinyPngBinary(), 200, [
|
||||
'Content-Type' => 'image/png',
|
||||
]),
|
||||
]);
|
||||
|
||||
$event = Event::factory()->create(['status' => 'published']);
|
||||
$photo = Photo::factory()->for($event)->create([
|
||||
'tenant_id' => $event->tenant_id,
|
||||
'status' => 'approved',
|
||||
]);
|
||||
$style = AiStyle::query()->create([
|
||||
'key' => 'persist-style',
|
||||
'name' => 'Persist Style',
|
||||
'provider' => 'runware',
|
||||
'provider_model' => 'runware-default',
|
||||
'requires_source_image' => true,
|
||||
'is_active' => true,
|
||||
]);
|
||||
$request = AiEditRequest::query()->create([
|
||||
'tenant_id' => $event->tenant_id,
|
||||
'event_id' => $event->id,
|
||||
'photo_id' => $photo->id,
|
||||
'style_id' => $style->id,
|
||||
'provider' => 'runware',
|
||||
'provider_model' => 'runware-default',
|
||||
'status' => AiEditRequest::STATUS_PROCESSING,
|
||||
'safety_state' => 'pending',
|
||||
'prompt' => 'Transform image style.',
|
||||
'idempotency_key' => 'storage-service-1',
|
||||
'queued_at' => now()->subMinutes(2),
|
||||
'started_at' => now()->subMinute(),
|
||||
]);
|
||||
|
||||
$service = app(AiEditOutputStorageService::class);
|
||||
$persisted = $service->persist($request, [
|
||||
'provider_url' => 'https://cdn.runware.ai/outputs/image-1.png',
|
||||
'provider_asset_id' => 'asset-storage-1',
|
||||
'mime_type' => 'image/png',
|
||||
]);
|
||||
|
||||
$this->assertSame('public', $persisted['storage_disk']);
|
||||
$this->assertNotNull($persisted['storage_path']);
|
||||
$this->assertNotSame('', trim((string) $persisted['storage_path']));
|
||||
$this->assertTrue(Storage::disk('public')->exists((string) $persisted['storage_path']));
|
||||
$this->assertIsArray($persisted['metadata']);
|
||||
$this->assertIsArray($persisted['metadata']['storage'] ?? null);
|
||||
}
|
||||
|
||||
public function test_it_records_storage_failure_for_blocked_output_host(): void
|
||||
{
|
||||
config([
|
||||
'ai-editing.outputs.enabled' => true,
|
||||
'ai-editing.outputs.allowed_hosts' => ['cdn.runware.ai'],
|
||||
'filesystems.default' => 'public',
|
||||
]);
|
||||
|
||||
$event = Event::factory()->create(['status' => 'published']);
|
||||
$photo = Photo::factory()->for($event)->create([
|
||||
'tenant_id' => $event->tenant_id,
|
||||
'status' => 'approved',
|
||||
]);
|
||||
$style = AiStyle::query()->create([
|
||||
'key' => 'blocked-host-style',
|
||||
'name' => 'Blocked Host',
|
||||
'provider' => 'runware',
|
||||
'provider_model' => 'runware-default',
|
||||
'requires_source_image' => true,
|
||||
'is_active' => true,
|
||||
]);
|
||||
$request = AiEditRequest::query()->create([
|
||||
'tenant_id' => $event->tenant_id,
|
||||
'event_id' => $event->id,
|
||||
'photo_id' => $photo->id,
|
||||
'style_id' => $style->id,
|
||||
'provider' => 'runware',
|
||||
'provider_model' => 'runware-default',
|
||||
'status' => AiEditRequest::STATUS_PROCESSING,
|
||||
'safety_state' => 'pending',
|
||||
'prompt' => 'Transform image style.',
|
||||
'idempotency_key' => 'storage-service-blocked-host',
|
||||
'queued_at' => now()->subMinutes(2),
|
||||
'started_at' => now()->subMinute(),
|
||||
]);
|
||||
|
||||
$service = app(AiEditOutputStorageService::class);
|
||||
$persisted = $service->persist($request, [
|
||||
'provider_url' => 'https://example.invalid/fake-image.png',
|
||||
'provider_asset_id' => 'asset-storage-blocked',
|
||||
]);
|
||||
|
||||
$this->assertNull($persisted['storage_path']);
|
||||
$this->assertIsArray($persisted['metadata']);
|
||||
$this->assertTrue((bool) ($persisted['metadata']['storage']['failed'] ?? false));
|
||||
$this->assertSame('output_storage_failed', $persisted['metadata']['storage']['error_code'] ?? null);
|
||||
}
|
||||
|
||||
private function tinyPngBinary(): string
|
||||
{
|
||||
return (string) base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+XnV0AAAAASUVORK5CYII=');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user