Files
fotospiel-app/tests/Feature/Jobs/ProcessAiEditRequestTest.php
Codex Agent 8cc0918881
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled
tests / ui (push) Has been cancelled
feat(ai-edits): add output storage backfill flow and coverage
2026-02-07 10:10:45 +01:00

382 lines
14 KiB
PHP

<?php
namespace Tests\Feature\Jobs;
use App\Jobs\ProcessAiEditRequest;
use App\Models\AiEditingSetting;
use App\Models\AiEditRequest;
use App\Models\AiStyle;
use App\Models\AiUsageLedger;
use App\Models\Event;
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;
class ProcessAiEditRequestTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
AiEditingSetting::flushCache();
config(['ai-editing.outputs.enabled' => false]);
}
public function test_it_processes_ai_edit_request_with_fake_runware_provider(): void
{
AiEditingSetting::query()->create(array_merge(
AiEditingSetting::defaults(),
[
'runware_mode' => 'fake',
'queue_auto_dispatch' => false,
]
));
$event = Event::factory()->create(['status' => 'published']);
$photo = Photo::factory()->for($event)->create([
'tenant_id' => $event->tenant_id,
'status' => 'approved',
]);
$style = AiStyle::query()->create([
'key' => 'fake-style',
'name' => 'Fake 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-fake-1',
'queued_at' => now(),
]);
ProcessAiEditRequest::dispatchSync($request->id);
$request->refresh();
$this->assertSame(AiEditRequest::STATUS_SUCCEEDED, $request->status);
$this->assertNotNull($request->started_at);
$this->assertNotNull($request->completed_at);
$this->assertSame(1, $request->outputs()->count());
$this->assertSame(1, $request->providerRuns()->count());
$this->assertSame('succeeded', $request->providerRuns()->first()?->status);
$this->assertSame(1, $request->usageLedgers()->count());
$this->assertSame(AiUsageLedger::TYPE_DEBIT, $request->usageLedgers()->first()?->entry_type);
$this->assertSame('unentitled', $request->usageLedgers()->first()?->package_context);
ProcessAiEditRequest::dispatchSync($request->id);
$request->refresh();
$this->assertSame(1, $request->usageLedgers()->count());
}
public function test_it_marks_request_failed_when_runware_is_not_configured(): void
{
config([
'services.runware.api_key' => null,
]);
AiEditingSetting::query()->create(array_merge(
AiEditingSetting::defaults(),
[
'runware_mode' => 'live',
'queue_auto_dispatch' => false,
]
));
$event = Event::factory()->create(['status' => 'published']);
$photo = Photo::factory()->for($event)->create([
'tenant_id' => $event->tenant_id,
'status' => 'approved',
]);
$style = AiStyle::query()->create([
'key' => 'live-style',
'name' => 'Live 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-live-1',
'queued_at' => now(),
]);
ProcessAiEditRequest::dispatchSync($request->id);
$request->refresh();
$this->assertSame(AiEditRequest::STATUS_FAILED, $request->status);
$this->assertSame('provider_not_configured', $request->failure_code);
$this->assertNotNull($request->completed_at);
$this->assertSame(0, $request->outputs()->count());
$this->assertSame(1, $request->providerRuns()->count());
$this->assertSame('failed', $request->providerRuns()->first()?->status);
}
public function test_it_blocks_request_when_provider_flags_output_as_unsafe(): void
{
AiEditingSetting::query()->create(array_merge(
AiEditingSetting::defaults(),
[
'runware_mode' => 'fake',
'queue_auto_dispatch' => false,
]
));
$event = Event::factory()->create(['status' => 'published']);
$photo = Photo::factory()->for($event)->create([
'tenant_id' => $event->tenant_id,
'status' => 'approved',
]);
$style = AiStyle::query()->create([
'key' => 'unsafe-style',
'name' => 'Unsafe 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-fake-unsafe-1',
'queued_at' => now(),
'metadata' => ['fake_nsfw' => true],
]);
ProcessAiEditRequest::dispatchSync($request->id);
$request->refresh();
$this->assertSame(AiEditRequest::STATUS_BLOCKED, $request->status);
$this->assertSame('blocked', $request->safety_state);
$this->assertSame('output_policy_blocked', $request->failure_code);
$this->assertSame(['provider_nsfw_content'], $request->safety_reasons);
$this->assertNotNull($request->completed_at);
$this->assertSame(0, $request->outputs()->count());
$this->assertSame(1, $request->providerRuns()->count());
$this->assertIsArray($request->metadata);
$this->assertSame('output_block', $request->metadata['abuse']['type'] ?? null);
}
public function test_it_marks_request_failed_when_provider_returns_processing_without_task_id(): void
{
AiEditingSetting::query()->create(array_merge(
AiEditingSetting::defaults(),
[
'runware_mode' => 'live',
'queue_auto_dispatch' => false,
]
));
$event = Event::factory()->create(['status' => 'published']);
$photo = Photo::factory()->for($event)->create([
'tenant_id' => $event->tenant_id,
'status' => 'approved',
]);
$style = AiStyle::query()->create([
'key' => 'processing-no-task',
'name' => 'Processing Without Task ID',
'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-processing-no-task',
'queued_at' => now(),
]);
$provider = \Mockery::mock(RunwareAiImageProvider::class);
$provider->shouldReceive('submit')
->once()
->andReturn(new AiProviderResult(status: 'processing', providerTaskId: ''));
$this->app->instance(RunwareAiImageProvider::class, $provider);
ProcessAiEditRequest::dispatchSync($request->id);
$request->refresh();
$run = $request->providerRuns()->first();
$this->assertSame(AiEditRequest::STATUS_FAILED, $request->status);
$this->assertSame('provider_task_id_missing', $request->failure_code);
$this->assertNotNull($request->completed_at);
$this->assertNotNull($run);
$this->assertSame('failed', $run?->status);
$this->assertSame('Provider returned processing state without task identifier.', $run?->error_message);
}
public function test_process_job_failed_hook_marks_request_as_failed(): void
{
$event = Event::factory()->create(['status' => 'published']);
$photo = Photo::factory()->for($event)->create([
'tenant_id' => $event->tenant_id,
'status' => 'approved',
]);
$style = AiStyle::query()->create([
'key' => 'failed-hook-style',
'name' => 'Failed Hook 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' => 'job-failed-hook-1',
'queued_at' => now()->subMinute(),
'started_at' => now()->subSeconds(30),
]);
$job = new ProcessAiEditRequest($request->id);
$job->failed(new RuntimeException('Queue worker timeout'));
$request->refresh();
$this->assertSame(AiEditRequest::STATUS_FAILED, $request->status);
$this->assertSame('queue_job_failed', $request->failure_code);
$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=');
}
}