feat(ai-edits): add output storage backfill flow and coverage
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled
tests / ui (push) Has been cancelled

This commit is contained in:
Codex Agent
2026-02-07 10:10:45 +01:00
parent fb45d1f6ab
commit 8cc0918881
18 changed files with 1610 additions and 18 deletions

View File

@@ -3,6 +3,7 @@
namespace Tests\Feature\Api\Event;
use App\Models\AiEditingSetting;
use App\Models\AiEditOutput;
use App\Models\AiEditRequest;
use App\Models\AiStyle;
use App\Models\AiUsageLedger;
@@ -13,6 +14,7 @@ use App\Models\Package;
use App\Models\Photo;
use App\Services\EventJoinTokenService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
class EventAiEditControllerTest extends TestCase
@@ -802,6 +804,67 @@ class EventAiEditControllerTest extends TestCase
->assertJsonPath('error.meta.allowed_style_keys.0', $allowed->key);
}
public function test_guest_show_serializes_local_output_url_when_storage_path_is_present(): void
{
$event = Event::factory()->create([
'status' => 'published',
]);
$this->attachEntitledEventPackage($event);
$photo = Photo::factory()->for($event)->create([
'tenant_id' => $event->tenant_id,
'status' => 'approved',
]);
$style = AiStyle::query()->create([
'key' => 'guest-output-url-style',
'name' => 'Guest Output URL',
'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_SUCCEEDED,
'safety_state' => 'passed',
'prompt' => 'Create edit.',
'idempotency_key' => 'guest-output-url-1',
'queued_at' => now()->subMinute(),
'completed_at' => now(),
]);
$storagePath = 'events/demo-event/ai-edits/output-1.jpg';
AiEditOutput::query()->create([
'request_id' => $request->id,
'provider_asset_id' => 'guest-output-asset-1',
'storage_disk' => 'public',
'storage_path' => $storagePath,
'provider_url' => 'https://provider.example/output.jpg',
'mime_type' => 'image/jpeg',
'is_primary' => true,
'safety_state' => 'passed',
'generated_at' => now(),
]);
$token = app(EventJoinTokenService::class)
->createToken($event, ['label' => 'guest-output-url'])
->getAttribute('plain_token');
$response = $this->withHeaders(['X-Device-Id' => 'guest-device-output-url'])
->getJson("/api/v1/events/{$token}/ai-edits/{$request->id}");
$response->assertOk()
->assertJsonPath('data.outputs.0.storage_path', $storagePath)
->assertJsonPath('data.outputs.0.url', Storage::disk('public')->url($storagePath));
}
private function attachEntitledEventPackage(Event $event): EventPackage
{
$package = Package::factory()->endcustomer()->create([

View File

@@ -3,6 +3,7 @@
namespace Tests\Feature\Api\Tenant;
use App\Models\AiEditingSetting;
use App\Models\AiEditOutput;
use App\Models\AiEditRequest;
use App\Models\AiStyle;
use App\Models\AiUsageLedger;
@@ -11,6 +12,7 @@ use App\Models\EventPackage;
use App\Models\EventPackageAddon;
use App\Models\Package;
use App\Models\Photo;
use Illuminate\Support\Facades\Storage;
use Tests\Feature\Tenant\TenantTestCase;
class TenantAiEditControllerTest extends TenantTestCase
@@ -835,6 +837,63 @@ class TenantAiEditControllerTest extends TenantTestCase
->assertJsonPath('data.budget.hard_stop_enabled', true);
}
public function test_tenant_show_serializes_local_output_url_when_storage_path_is_present(): void
{
$event = Event::factory()->create([
'tenant_id' => $this->tenant->id,
'status' => 'published',
]);
$this->attachEntitledEventPackage($event);
$photo = Photo::factory()->for($event)->create([
'tenant_id' => $this->tenant->id,
'status' => 'approved',
]);
$style = AiStyle::query()->create([
'key' => 'tenant-output-url-style',
'name' => 'Tenant Output URL',
'provider' => 'runware',
'provider_model' => 'runware-default',
'requires_source_image' => true,
'is_active' => true,
]);
$request = AiEditRequest::query()->create([
'tenant_id' => $this->tenant->id,
'event_id' => $event->id,
'photo_id' => $photo->id,
'style_id' => $style->id,
'provider' => 'runware',
'provider_model' => 'runware-default',
'status' => AiEditRequest::STATUS_SUCCEEDED,
'safety_state' => 'passed',
'prompt' => 'Create edit.',
'idempotency_key' => 'tenant-output-url-1',
'queued_at' => now()->subMinute(),
'completed_at' => now(),
]);
$storagePath = 'events/demo-event/ai-edits/output-tenant-1.jpg';
AiEditOutput::query()->create([
'request_id' => $request->id,
'provider_asset_id' => 'tenant-output-asset-1',
'storage_disk' => 'public',
'storage_path' => $storagePath,
'provider_url' => 'https://provider.example/output.jpg',
'mime_type' => 'image/jpeg',
'is_primary' => true,
'safety_state' => 'passed',
'generated_at' => now(),
]);
$response = $this->authenticatedRequest('GET', "/api/v1/tenant/events/{$event->slug}/ai-edits/{$request->id}");
$response->assertOk()
->assertJsonPath('data.outputs.0.storage_path', $storagePath)
->assertJsonPath('data.outputs.0.url', Storage::disk('public')->url($storagePath));
}
private function attachEntitledEventPackage(Event $event): EventPackage
{
$package = Package::factory()->endcustomer()->create([

View File

@@ -0,0 +1,121 @@
<?php
namespace Tests\Feature\Console;
use App\Models\AiEditOutput;
use App\Models\AiEditRequest;
use App\Models\AiStyle;
use App\Models\Event;
use App\Models\Photo;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
class AiEditsBackfillStorageCommandTest extends TestCase
{
use RefreshDatabase;
public function test_it_backfills_storage_for_outputs_without_local_path(): 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',
]),
]);
[$request, $output] = $this->createOutputWithoutStoragePath();
$this->artisan('ai-edits:backfill-storage --limit=50')
->assertExitCode(0);
$request->refresh();
$output->refresh();
$this->assertNotNull($output->storage_disk);
$this->assertNotNull($output->storage_path);
$this->assertNotSame('', trim((string) $output->storage_path));
$this->assertTrue(Storage::disk((string) $output->storage_disk)->exists((string) $output->storage_path));
}
public function test_it_supports_pretend_mode_without_writing_changes(): void
{
config([
'ai-editing.outputs.enabled' => true,
'ai-editing.outputs.allowed_hosts' => ['cdn.runware.ai'],
'filesystems.default' => 'public',
]);
[, $output] = $this->createOutputWithoutStoragePath();
$this->artisan('ai-edits:backfill-storage --pretend --limit=10')
->assertExitCode(0);
$output->refresh();
$this->assertNull($output->storage_disk);
$this->assertNull($output->storage_path);
}
/**
* @return array{0: AiEditRequest, 1: AiEditOutput}
*/
private function createOutputWithoutStoragePath(): array
{
$event = Event::factory()->create(['status' => 'published']);
$photo = Photo::factory()->for($event)->create([
'tenant_id' => $event->tenant_id,
'status' => 'approved',
]);
$style = AiStyle::query()->create([
'key' => 'backfill-style',
'name' => 'Backfill 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_SUCCEEDED,
'safety_state' => 'passed',
'prompt' => 'Transform image style.',
'idempotency_key' => 'backfill-request-1',
'queued_at' => now()->subMinutes(2),
'started_at' => now()->subMinute(),
'completed_at' => now()->subSeconds(20),
]);
$output = AiEditOutput::query()->create([
'request_id' => $request->id,
'provider_asset_id' => 'backfill-asset-1',
'provider_url' => 'https://cdn.runware.ai/outputs/backfill-image.png',
'is_primary' => true,
'safety_state' => 'passed',
'generated_at' => now(),
]);
return [$request, $output];
}
private function tinyPngBinary(): string
{
return (string) base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+XnV0AAAAASUVORK5CYII=');
}
}

View File

@@ -12,6 +12,8 @@ use App\Models\Photo;
use App\Services\AiEditing\AiProviderResult;
use App\Services\AiEditing\Providers\RunwareAiImageProvider;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use RuntimeException;
use Tests\TestCase;
@@ -24,6 +26,7 @@ class PollAiEditRequestTest extends TestCase
parent::setUp();
AiEditingSetting::flushCache();
config(['ai-editing.outputs.enabled' => false]);
}
public function test_it_marks_request_failed_when_poll_attempts_are_exhausted(): void
@@ -139,4 +142,91 @@ class PollAiEditRequestTest extends TestCase
$this->assertSame('Polling crashed', $request->failure_message);
$this->assertNotNull($request->completed_at);
}
public function test_it_persists_polled_output_to_local_storage_when_enabled(): 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());
AiEditingSetting::query()->create(array_merge(
AiEditingSetting::defaults(),
[
'runware_mode' => 'live',
'queue_auto_dispatch' => false,
'queue_max_polls' => 3,
]
));
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' => 'poll-storage-style',
'name' => 'Poll Storage',
'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' => 'poll-storage-1',
'queued_at' => now()->subMinutes(2),
'started_at' => now()->subMinute(),
]);
$provider = \Mockery::mock(RunwareAiImageProvider::class);
$provider->shouldReceive('poll')
->once()
->withArgs(function (AiEditRequest $polledRequest, string $taskId): bool {
return $polledRequest->id > 0 && $taskId === 'runware-task-storage';
})
->andReturn(AiProviderResult::succeeded(outputs: [[
'provider_url' => 'https://cdn.runware.ai/outputs/poll-image.png',
'provider_asset_id' => 'poll-asset-123',
'mime_type' => 'image/png',
]]));
$this->app->instance(RunwareAiImageProvider::class, $provider);
PollAiEditRequest::dispatchSync($request->id, 'runware-task-storage', 1);
$request->refresh();
$output = $request->outputs()->first();
$this->assertSame(AiEditRequest::STATUS_SUCCEEDED, $request->status);
$this->assertNotNull($output);
$this->assertNotNull($output?->storage_disk);
$this->assertNotNull($output?->storage_path);
$this->assertNotSame('', trim((string) $output?->storage_path));
$this->assertTrue(Storage::disk((string) $output?->storage_disk)->exists((string) $output?->storage_path));
}
private function tinyPngBinary(): string
{
return (string) base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+XnV0AAAAASUVORK5CYII=');
}
}

View File

@@ -12,6 +12,8 @@ use App\Models\Photo;
use App\Services\AiEditing\AiProviderResult;
use App\Services\AiEditing\Providers\RunwareAiImageProvider;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use RuntimeException;
use Tests\TestCase;
@@ -24,6 +26,7 @@ class ProcessAiEditRequestTest extends TestCase
parent::setUp();
AiEditingSetting::flushCache();
config(['ai-editing.outputs.enabled' => false]);
}
public function test_it_processes_ai_edit_request_with_fake_runware_provider(): void
@@ -292,4 +295,87 @@ class ProcessAiEditRequestTest extends TestCase
$this->assertSame('Queue worker timeout', $request->failure_message);
$this->assertNotNull($request->completed_at);
}
public function test_it_persists_provider_output_to_local_storage_when_enabled(): 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());
AiEditingSetting::query()->create(array_merge(
AiEditingSetting::defaults(),
[
'runware_mode' => 'live',
'queue_auto_dispatch' => false,
]
));
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' => 'storage-style',
'name' => 'Storage 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_QUEUED,
'safety_state' => 'pending',
'prompt' => 'Transform image style.',
'idempotency_key' => 'job-storage-enabled-1',
'queued_at' => now(),
]);
$provider = \Mockery::mock(RunwareAiImageProvider::class);
$provider->shouldReceive('submit')
->once()
->andReturn(AiProviderResult::succeeded(outputs: [[
'provider_url' => 'https://cdn.runware.ai/outputs/final-image.png',
'provider_asset_id' => 'asset-123',
'mime_type' => 'image/png',
]]));
$this->app->instance(RunwareAiImageProvider::class, $provider);
ProcessAiEditRequest::dispatchSync($request->id);
$request->refresh();
$output = $request->outputs()->first();
$this->assertSame(AiEditRequest::STATUS_SUCCEEDED, $request->status);
$this->assertNotNull($output);
$this->assertNotNull($output?->storage_disk);
$this->assertNotNull($output?->storage_path);
$this->assertNotSame('', trim((string) $output?->storage_path));
$this->assertTrue(Storage::disk((string) $output?->storage_disk)->exists((string) $output?->storage_path));
}
private function tinyPngBinary(): string
{
return (string) base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+XnV0AAAAASUVORK5CYII=');
}
}

View 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=');
}
}