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

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