Files
fotospiel-app/tests/Feature/Jobs/ProcessAiEditRequestTest.php
Codex Agent 36bed12ff9
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled
tests / ui (push) Has been cancelled
feat: implement AI styling foundation and billing scope rework
2026-02-06 20:01:58 +01:00

192 lines
6.6 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 Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ProcessAiEditRequestTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
AiEditingSetting::flushCache();
}
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());
}
}