feat(ai): finalize AI magic edits epic rollout and operations
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-06 22:41:51 +01:00
parent 36bed12ff9
commit 1d2242fb4d
33 changed files with 2621 additions and 18 deletions

View File

@@ -29,6 +29,7 @@ class AiEditingDataModelTest extends TestCase
'key',
'provider',
'provider_model',
'version',
'requires_source_image',
'is_premium',
] as $column) {

View File

@@ -5,6 +5,7 @@ namespace Tests\Feature\Api\Event;
use App\Models\AiEditingSetting;
use App\Models\AiEditRequest;
use App\Models\AiStyle;
use App\Models\AiUsageLedger;
use App\Models\Event;
use App\Models\EventPackage;
use App\Models\EventPackageAddon;
@@ -302,6 +303,7 @@ class EventAiEditControllerTest extends TestCase
$response->assertOk()
->assertJsonPath('data.0.id', $allowed->id)
->assertJsonPath('data.0.version', 1)
->assertJsonCount(1, 'data')
->assertJsonPath('meta.required_feature', 'ai_styling')
->assertJsonPath('meta.allow_custom_prompt', false)
@@ -594,6 +596,129 @@ class EventAiEditControllerTest extends TestCase
$second->assertStatus(429);
}
public function test_guest_submit_endpoint_enforces_event_scope_rate_limit_across_devices(): void
{
config([
'ai-editing.abuse.guest_submit_per_minute' => 10,
'ai-editing.abuse.guest_submit_per_hour' => 100,
'ai-editing.abuse.guest_submit_per_event_per_minute' => 1,
]);
$event = Event::factory()->create([
'status' => 'published',
]);
$this->attachEntitledEventPackage($event);
$photo = Photo::factory()->for($event)->create([
'tenant_id' => $event->tenant_id,
'status' => 'approved',
]);
$token = app(EventJoinTokenService::class)
->createToken($event, ['label' => 'guest-ai-event-rate-limit'])
->getAttribute('plain_token');
$first = $this->withHeaders(['X-Device-Id' => 'guest-device-event-limit-a'])
->postJson("/api/v1/events/{$token}/photos/{$photo->id}/ai-edits", [
'prompt' => 'First request.',
'idempotency_key' => 'guest-event-rate-limit-1',
]);
$first->assertCreated();
$second = $this->withHeaders(['X-Device-Id' => 'guest-device-event-limit-b'])
->postJson("/api/v1/events/{$token}/photos/{$photo->id}/ai-edits", [
'prompt' => 'Second request.',
'idempotency_key' => 'guest-event-rate-limit-2',
]);
$second->assertStatus(429);
}
public function test_guest_cannot_create_ai_edit_when_hard_budget_cap_is_reached(): void
{
$event = Event::factory()->create([
'status' => 'published',
]);
$this->attachEntitledEventPackage($event);
$tenantSettings = (array) ($event->tenant->settings ?? []);
data_set($tenantSettings, 'ai_editing.budget.hard_cap_usd', 0.01);
$event->tenant->update(['settings' => $tenantSettings]);
$photo = Photo::factory()->for($event)->create([
'tenant_id' => $event->tenant_id,
'status' => 'approved',
]);
AiUsageLedger::query()->create([
'tenant_id' => $event->tenant_id,
'event_id' => $event->id,
'entry_type' => AiUsageLedger::TYPE_DEBIT,
'quantity' => 1,
'unit_cost_usd' => 0.02,
'amount_usd' => 0.02,
'currency' => 'USD',
'recorded_at' => now(),
]);
$token = app(EventJoinTokenService::class)
->createToken($event, ['label' => 'guest-ai-budget-hard-cap'])
->getAttribute('plain_token');
$response = $this->withHeaders(['X-Device-Id' => 'guest-device-budget-cap'])
->postJson("/api/v1/events/{$token}/photos/{$photo->id}/ai-edits", [
'prompt' => 'Stylize image.',
'idempotency_key' => 'guest-budget-cap-1',
]);
$response->assertForbidden()
->assertJsonPath('error.code', 'budget_hard_cap_reached')
->assertJsonPath('error.meta.budget.hard_reached', true);
}
public function test_guest_prompt_block_records_abuse_metadata_when_escalation_threshold_is_reached(): void
{
config([
'ai-editing.abuse.escalation_threshold_per_hour' => 1,
'ai-editing.abuse.escalation_cooldown_minutes' => 1,
]);
AiEditingSetting::flushCache();
AiEditingSetting::query()->create(array_merge(
AiEditingSetting::defaults(),
['blocked_terms' => ['explicit']]
));
$event = Event::factory()->create([
'status' => 'published',
]);
$this->attachEntitledEventPackage($event);
$photo = Photo::factory()->for($event)->create([
'tenant_id' => $event->tenant_id,
'status' => 'approved',
]);
$token = app(EventJoinTokenService::class)
->createToken($event, ['label' => 'guest-ai-escalation'])
->getAttribute('plain_token');
$response = $this->withHeaders(['X-Device-Id' => 'guest-device-escalation'])
->postJson("/api/v1/events/{$token}/photos/{$photo->id}/ai-edits", [
'prompt' => 'Create an explicit style image.',
'idempotency_key' => 'guest-escalation-1',
]);
$response->assertCreated()
->assertJsonPath('data.status', AiEditRequest::STATUS_BLOCKED)
->assertJsonFragment(['abuse_escalation_threshold_reached']);
$requestId = (int) $response->json('data.id');
$editRequest = AiEditRequest::query()->find($requestId);
$this->assertNotNull($editRequest);
$this->assertIsArray($editRequest?->metadata);
$this->assertSame('prompt_block', $editRequest?->metadata['abuse']['type'] ?? null);
$this->assertTrue((bool) ($editRequest?->metadata['abuse']['escalated'] ?? false));
}
public function test_guest_cannot_create_ai_edit_when_event_ai_feature_is_disabled(): void
{
$event = Event::factory()->create([

View File

@@ -5,6 +5,7 @@ namespace Tests\Feature\Api\Tenant;
use App\Models\AiEditingSetting;
use App\Models\AiEditRequest;
use App\Models\AiStyle;
use App\Models\AiUsageLedger;
use App\Models\Event;
use App\Models\EventPackage;
use App\Models\EventPackageAddon;
@@ -305,6 +306,7 @@ class TenantAiEditControllerTest extends TenantTestCase
$response->assertOk()
->assertJsonPath('data.0.id', $active->id)
->assertJsonPath('data.0.version', 1)
->assertJsonCount(1, 'data')
->assertJsonPath('meta.required_feature', 'ai_styling')
->assertJsonPath('meta.event_enabled', false)
@@ -540,6 +542,148 @@ class TenantAiEditControllerTest extends TenantTestCase
$second->assertStatus(429);
}
public function test_tenant_submit_endpoint_enforces_event_scope_rate_limit(): void
{
config([
'ai-editing.abuse.tenant_submit_per_minute' => 10,
'ai-editing.abuse.tenant_submit_per_hour' => 100,
'ai-editing.abuse.tenant_submit_per_event_per_minute' => 1,
]);
$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-event-rate-limit-style',
'name' => 'Tenant Event Rate Limit',
'provider' => 'runware',
'provider_model' => 'runware-default',
'requires_source_image' => true,
'is_active' => true,
]);
$first = $this->authenticatedRequest('POST', "/api/v1/tenant/events/{$event->slug}/ai-edits", [
'photo_id' => $photo->id,
'style_id' => $style->id,
'prompt' => 'First request.',
'idempotency_key' => 'tenant-event-rate-limit-1',
]);
$first->assertCreated();
$second = $this->authenticatedRequest('POST', "/api/v1/tenant/events/{$event->slug}/ai-edits", [
'photo_id' => $photo->id,
'style_id' => $style->id,
'prompt' => 'Second request.',
'idempotency_key' => 'tenant-event-rate-limit-2',
]);
$second->assertStatus(429);
}
public function test_tenant_cannot_create_ai_edit_when_hard_budget_cap_is_reached(): void
{
$event = Event::factory()->create([
'tenant_id' => $this->tenant->id,
'status' => 'published',
]);
$this->attachEntitledEventPackage($event);
$tenantSettings = (array) ($this->tenant->settings ?? []);
data_set($tenantSettings, 'ai_editing.budget.hard_cap_usd', 0.01);
$this->tenant->update(['settings' => $tenantSettings]);
$photo = Photo::factory()->for($event)->create([
'tenant_id' => $this->tenant->id,
'status' => 'approved',
]);
$style = AiStyle::query()->create([
'key' => 'tenant-budget-cap-style',
'name' => 'Tenant Budget Cap',
'provider' => 'runware',
'provider_model' => 'runware-default',
'requires_source_image' => true,
'is_active' => true,
]);
AiUsageLedger::query()->create([
'tenant_id' => $this->tenant->id,
'event_id' => $event->id,
'entry_type' => AiUsageLedger::TYPE_DEBIT,
'quantity' => 1,
'unit_cost_usd' => 0.02,
'amount_usd' => 0.02,
'currency' => 'USD',
'recorded_at' => now(),
]);
$response = $this->authenticatedRequest('POST', "/api/v1/tenant/events/{$event->slug}/ai-edits", [
'photo_id' => $photo->id,
'style_id' => $style->id,
'prompt' => 'Apply AI style.',
'idempotency_key' => 'tenant-budget-cap-1',
]);
$response->assertForbidden()
->assertJsonPath('error.code', 'budget_hard_cap_reached')
->assertJsonPath('error.meta.budget.hard_reached', true);
}
public function test_tenant_prompt_block_records_abuse_metadata_when_escalation_threshold_is_reached(): void
{
config([
'ai-editing.abuse.escalation_threshold_per_hour' => 1,
'ai-editing.abuse.escalation_cooldown_minutes' => 1,
]);
AiEditingSetting::flushCache();
AiEditingSetting::query()->create(array_merge(
AiEditingSetting::defaults(),
['blocked_terms' => ['weapon']]
));
$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-escalation-style',
'name' => 'Tenant Escalation',
'provider' => 'runware',
'provider_model' => 'runware-default',
'requires_source_image' => true,
'is_active' => true,
]);
$response = $this->authenticatedRequest('POST', "/api/v1/tenant/events/{$event->slug}/ai-edits", [
'photo_id' => $photo->id,
'style_id' => $style->id,
'prompt' => 'Add weapon effects.',
'idempotency_key' => 'tenant-escalation-1',
]);
$response->assertCreated()
->assertJsonPath('data.status', AiEditRequest::STATUS_BLOCKED)
->assertJsonFragment(['abuse_escalation_threshold_reached']);
$requestId = (int) $response->json('data.id');
$editRequest = AiEditRequest::query()->find($requestId);
$this->assertNotNull($editRequest);
$this->assertIsArray($editRequest?->metadata);
$this->assertSame('prompt_block', $editRequest?->metadata['abuse']['type'] ?? null);
$this->assertTrue((bool) ($editRequest?->metadata['abuse']['escalated'] ?? false));
}
public function test_tenant_cannot_create_ai_edit_when_event_ai_feature_is_disabled(): void
{
$event = Event::factory()->create([
@@ -685,7 +829,10 @@ class TenantAiEditControllerTest extends TenantTestCase
->assertJsonPath('data.total', 2)
->assertJsonPath('data.status_counts.succeeded', 1)
->assertJsonPath('data.status_counts.failed', 1)
->assertJsonPath('data.failed_total', 1);
->assertJsonPath('data.failed_total', 1)
->assertJsonPath('data.usage.debit_count', 0)
->assertJsonPath('data.observability.provider_runs_total', 0)
->assertJsonPath('data.budget.hard_stop_enabled', true);
}
private function attachEntitledEventPackage(Event $event): EventPackage

View File

@@ -0,0 +1,172 @@
<?php
namespace Tests\Feature\Console;
use App\Models\AiEditOutput;
use App\Models\AiEditRequest;
use App\Models\AiProviderRun;
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 AiEditsPruneCommandTest extends TestCase
{
use RefreshDatabase;
public function test_command_prunes_stale_requests_and_ledgers(): void
{
config([
'ai-editing.retention.request_days' => 90,
'ai-editing.retention.usage_ledger_days' => 365,
]);
[$oldRequest, $recentRequest] = $this->createRequestsForPruning();
[$oldLedger, $recentLedger] = $this->createLedgerEntriesForPruning($oldRequest, $recentRequest);
$this->artisan('ai-edits:prune')
->expectsOutputToContain('AI prune candidates')
->expectsOutputToContain('Pruned AI data')
->assertExitCode(0);
$this->assertDatabaseMissing('ai_edit_requests', ['id' => $oldRequest->id]);
$this->assertDatabaseMissing('ai_provider_runs', ['request_id' => $oldRequest->id]);
$this->assertDatabaseMissing('ai_edit_outputs', ['request_id' => $oldRequest->id]);
$this->assertDatabaseHas('ai_edit_requests', ['id' => $recentRequest->id]);
$this->assertDatabaseMissing('ai_usage_ledgers', ['id' => $oldLedger->id]);
$this->assertNotNull($recentLedger);
if ($recentLedger) {
$this->assertDatabaseHas('ai_usage_ledgers', ['id' => $recentLedger->id]);
}
}
public function test_command_pretend_mode_does_not_delete_records(): void
{
config([
'ai-editing.retention.request_days' => 90,
'ai-editing.retention.usage_ledger_days' => 365,
]);
[$oldRequest] = $this->createRequestsForPruning();
[$oldLedger] = $this->createLedgerEntriesForPruning($oldRequest, null);
$this->artisan('ai-edits:prune', ['--pretend' => true])
->expectsOutputToContain('AI prune candidates')
->expectsOutput('Pretend mode enabled. No records were deleted.')
->assertExitCode(0);
$this->assertDatabaseHas('ai_edit_requests', ['id' => $oldRequest->id]);
$this->assertDatabaseHas('ai_usage_ledgers', ['id' => $oldLedger->id]);
}
/**
* @return array{0: AiEditRequest, 1: AiEditRequest}
*/
private function createRequestsForPruning(): 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' => 'prune-style',
'name' => 'Prune Style',
'provider' => 'runware',
'provider_model' => 'runware-default',
'requires_source_image' => true,
'is_active' => true,
]);
$oldRequest = 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' => 'Old request',
'idempotency_key' => 'old-prune-request',
'queued_at' => now()->subDays(121),
'started_at' => now()->subDays(120),
'completed_at' => now()->subDays(120),
'expires_at' => now()->subDays(30),
]);
AiProviderRun::query()->create([
'request_id' => $oldRequest->id,
'provider' => 'runware',
'attempt' => 1,
'provider_task_id' => 'old-task-id',
'status' => AiProviderRun::STATUS_SUCCEEDED,
'started_at' => now()->subDays(120),
'finished_at' => now()->subDays(120),
]);
AiEditOutput::query()->create([
'request_id' => $oldRequest->id,
'provider_asset_id' => 'old-asset-id',
'provider_url' => 'https://cdn.example.invalid/old.jpg',
'safety_state' => 'passed',
'generated_at' => now()->subDays(120),
]);
$recentRequest = 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' => 'Recent request',
'idempotency_key' => 'recent-prune-request',
'queued_at' => now()->subDays(11),
'started_at' => now()->subDays(10),
'completed_at' => now()->subDays(10),
]);
return [$oldRequest, $recentRequest];
}
/**
* @return array{0: AiUsageLedger, 1: ?AiUsageLedger}
*/
private function createLedgerEntriesForPruning(AiEditRequest $oldRequest, ?AiEditRequest $recentRequest): array
{
$oldLedger = AiUsageLedger::query()->create([
'tenant_id' => $oldRequest->tenant_id,
'event_id' => $oldRequest->event_id,
'request_id' => $oldRequest->id,
'entry_type' => AiUsageLedger::TYPE_DEBIT,
'quantity' => 1,
'unit_cost_usd' => 0.01,
'amount_usd' => 0.01,
'currency' => 'USD',
'recorded_at' => now()->subDays(400),
]);
if (! $recentRequest) {
return [$oldLedger, null];
}
$recentLedger = AiUsageLedger::query()->create([
'tenant_id' => $recentRequest->tenant_id,
'event_id' => $recentRequest->event_id,
'request_id' => $recentRequest->id,
'entry_type' => AiUsageLedger::TYPE_DEBIT,
'quantity' => 1,
'unit_cost_usd' => 0.01,
'amount_usd' => 0.01,
'currency' => 'USD',
'recorded_at' => now()->subDays(30),
]);
return [$oldLedger, $recentLedger];
}
}

View File

@@ -0,0 +1,125 @@
<?php
namespace Tests\Feature\Console;
use App\Jobs\PollAiEditRequest;
use App\Jobs\ProcessAiEditRequest;
use App\Models\AiEditRequest;
use App\Models\AiProviderRun;
use App\Models\AiStyle;
use App\Models\Event;
use App\Models\Photo;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;
class AiEditsRecoverStuckCommandTest extends TestCase
{
use RefreshDatabase;
public function test_command_requeues_stuck_requests_with_scope_aware_job_selection(): void
{
[$queuedRequest, $processingRequest] = $this->createStuckRequests();
Queue::fake();
$this->artisan('ai-edits:recover-stuck', [
'--minutes' => 10,
'--requeue' => true,
])->assertExitCode(0);
Queue::assertPushed(ProcessAiEditRequest::class, 1);
Queue::assertPushed(PollAiEditRequest::class, 1);
$this->assertDatabaseHas('ai_edit_requests', [
'id' => $queuedRequest->id,
'status' => AiEditRequest::STATUS_QUEUED,
]);
$this->assertDatabaseHas('ai_edit_requests', [
'id' => $processingRequest->id,
'status' => AiEditRequest::STATUS_PROCESSING,
]);
}
public function test_command_can_mark_stuck_requests_as_failed(): void
{
[$queuedRequest, $processingRequest] = $this->createStuckRequests();
$this->artisan('ai-edits:recover-stuck', [
'--minutes' => 10,
'--fail' => true,
])->assertExitCode(0);
$this->assertDatabaseHas('ai_edit_requests', [
'id' => $queuedRequest->id,
'status' => AiEditRequest::STATUS_FAILED,
'failure_code' => 'operator_recovery_marked_failed',
]);
$this->assertDatabaseHas('ai_edit_requests', [
'id' => $processingRequest->id,
'status' => AiEditRequest::STATUS_FAILED,
'failure_code' => 'operator_recovery_marked_failed',
]);
}
/**
* @return array{0: AiEditRequest, 1: AiEditRequest}
*/
private function createStuckRequests(): 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' => 'recovery-style',
'name' => 'Recovery Style',
'provider' => 'runware',
'provider_model' => 'runware-default',
'requires_source_image' => true,
'is_active' => true,
]);
$queued = 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' => 'stuck-queued-1',
'queued_at' => now()->subMinutes(45),
'updated_at' => now()->subMinutes(45),
]);
$processing = 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' => 'stuck-processing-1',
'queued_at' => now()->subMinutes(50),
'started_at' => now()->subMinutes(40),
'updated_at' => now()->subMinutes(40),
]);
AiProviderRun::query()->create([
'request_id' => $processing->id,
'provider' => 'runware',
'attempt' => 1,
'provider_task_id' => 'runware-task-recovery-1',
'status' => AiProviderRun::STATUS_RUNNING,
'started_at' => now()->subMinutes(39),
]);
return [$queued, $processing];
}
}

View File

@@ -0,0 +1,142 @@
<?php
namespace Tests\Feature\Jobs;
use App\Jobs\PollAiEditRequest;
use App\Models\AiEditingSetting;
use App\Models\AiEditRequest;
use App\Models\AiProviderRun;
use App\Models\AiStyle;
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 RuntimeException;
use Tests\TestCase;
class PollAiEditRequestTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
AiEditingSetting::flushCache();
}
public function test_it_marks_request_failed_when_poll_attempts_are_exhausted(): void
{
AiEditingSetting::query()->create(array_merge(
AiEditingSetting::defaults(),
[
'runware_mode' => 'live',
'queue_auto_dispatch' => false,
'queue_max_polls' => 1,
]
));
$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-exhaust-style',
'name' => 'Poll Exhaust',
'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-exhaust-1',
'queued_at' => now()->subMinutes(3),
'started_at' => now()->subMinutes(2),
]);
$providerRun = AiProviderRun::query()->create([
'request_id' => $request->id,
'provider' => 'runware',
'attempt' => 1,
'provider_task_id' => 'runware-task-1',
'status' => AiProviderRun::STATUS_RUNNING,
'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-1';
})
->andReturn(AiProviderResult::processing('runware-task-1'));
$this->app->instance(RunwareAiImageProvider::class, $provider);
PollAiEditRequest::dispatchSync($request->id, 'runware-task-1', 1);
$request->refresh();
$providerRun->refresh();
$latestRun = $request->providerRuns()->latest('attempt')->first();
$this->assertSame(AiEditRequest::STATUS_FAILED, $request->status);
$this->assertSame('provider_poll_timeout', $request->failure_code);
$this->assertNotNull($request->completed_at);
$this->assertNotNull($latestRun);
$this->assertSame(AiProviderRun::STATUS_FAILED, $latestRun?->status);
$this->assertSame('Polling exhausted after 1 attempt(s).', $latestRun?->error_message);
}
public function test_poll_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' => 'poll-failed-hook-style',
'name' => 'Poll Failed Hook',
'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-failed-hook-1',
'queued_at' => now()->subMinute(),
'started_at' => now()->subSeconds(30),
]);
$job = new PollAiEditRequest($request->id, 'runware-task-2', 2);
$job->failed(new RuntimeException('Polling crashed'));
$request->refresh();
$this->assertSame(AiEditRequest::STATUS_FAILED, $request->status);
$this->assertSame('queue_job_failed', $request->failure_code);
$this->assertSame('Polling crashed', $request->failure_message);
$this->assertNotNull($request->completed_at);
}
}

View File

@@ -9,7 +9,10 @@ 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 RuntimeException;
use Tests\TestCase;
class ProcessAiEditRequestTest extends TestCase
@@ -187,5 +190,106 @@ class ProcessAiEditRequestTest extends TestCase
$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);
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace Tests\Unit\Models;
use App\Models\AiStyle;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class AiStyleVersioningTest extends TestCase
{
use RefreshDatabase;
public function test_style_defaults_to_version_one_on_create(): void
{
$style = AiStyle::query()->create([
'key' => 'style-version-default',
'name' => 'Version Default',
'provider' => 'runware',
'provider_model' => 'runware-default',
'is_active' => true,
]);
$this->assertSame(1, $style->version);
}
public function test_style_version_increments_when_core_style_fields_change(): void
{
$style = AiStyle::query()->create([
'key' => 'style-version-increment',
'name' => 'Version Increment',
'provider' => 'runware',
'provider_model' => 'runware-default',
'prompt_template' => 'Initial prompt',
'is_active' => true,
]);
$this->assertSame(1, $style->version);
$style->update([
'prompt_template' => 'Updated prompt',
]);
$style->refresh();
$this->assertSame(2, $style->version);
}
}

View File

@@ -0,0 +1,126 @@
<?php
namespace Tests\Unit\Services;
use App\Models\AiUsageLedger;
use App\Models\Event;
use App\Models\User;
use App\Services\AiEditing\AiBudgetGuardService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Tests\TestCase;
class AiBudgetGuardServiceTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
Cache::flush();
}
public function test_it_allows_requests_when_spend_is_below_caps(): void
{
$event = Event::factory()->create(['status' => 'published']);
$settings = (array) ($event->tenant->settings ?? []);
data_set($settings, 'ai_editing.budget.soft_cap_usd', 10.0);
data_set($settings, 'ai_editing.budget.hard_cap_usd', 20.0);
$event->tenant->update(['settings' => $settings]);
AiUsageLedger::query()->create([
'tenant_id' => $event->tenant_id,
'event_id' => $event->id,
'entry_type' => AiUsageLedger::TYPE_DEBIT,
'quantity' => 1,
'unit_cost_usd' => 3.0,
'amount_usd' => 3.0,
'currency' => 'USD',
'recorded_at' => now(),
]);
$decision = app(AiBudgetGuardService::class)->evaluateForEvent($event->fresh('tenant'));
$this->assertTrue($decision['allowed']);
$this->assertFalse($decision['budget']['soft_reached']);
$this->assertFalse($decision['budget']['hard_reached']);
$this->assertSame(3.0, $decision['budget']['current_spend_usd']);
}
public function test_it_blocks_requests_when_hard_cap_is_reached_without_override(): void
{
$event = Event::factory()->create(['status' => 'published']);
$owner = User::factory()->create();
$event->tenant->update(['user_id' => $owner->id]);
$settings = (array) ($event->tenant->settings ?? []);
data_set($settings, 'ai_editing.budget.hard_cap_usd', 5.0);
$event->tenant->update(['settings' => $settings]);
AiUsageLedger::query()->create([
'tenant_id' => $event->tenant_id,
'event_id' => $event->id,
'entry_type' => AiUsageLedger::TYPE_DEBIT,
'quantity' => 1,
'unit_cost_usd' => 5.0,
'amount_usd' => 5.0,
'currency' => 'USD',
'recorded_at' => now(),
]);
$decision = app(AiBudgetGuardService::class)->evaluateForEvent($event->fresh('tenant'));
$this->assertFalse($decision['allowed']);
$this->assertSame('budget_hard_cap_reached', $decision['reason_code']);
$this->assertTrue($decision['budget']['hard_reached']);
$this->assertFalse($decision['budget']['override_active']);
$this->assertDatabaseHas('tenant_notification_logs', [
'tenant_id' => $event->tenant_id,
'type' => 'ai_budget_hard_cap',
'channel' => 'system',
'status' => 'sent',
]);
$this->assertDatabaseHas('tenant_notification_receipts', [
'tenant_id' => $event->tenant_id,
'user_id' => $owner->id,
'status' => 'delivered',
]);
}
public function test_it_throttles_soft_cap_notifications_with_cooldown(): void
{
$event = Event::factory()->create(['status' => 'published']);
$settings = (array) ($event->tenant->settings ?? []);
data_set($settings, 'ai_editing.budget.soft_cap_usd', 2.0);
data_set($settings, 'ai_editing.budget.hard_cap_usd', 100.0);
$event->tenant->update(['settings' => $settings]);
AiUsageLedger::query()->create([
'tenant_id' => $event->tenant_id,
'event_id' => $event->id,
'entry_type' => AiUsageLedger::TYPE_DEBIT,
'quantity' => 1,
'unit_cost_usd' => 3.0,
'amount_usd' => 3.0,
'currency' => 'USD',
'recorded_at' => now(),
]);
$service = app(AiBudgetGuardService::class);
$service->evaluateForEvent($event->fresh('tenant'));
$service->evaluateForEvent($event->fresh('tenant'));
$this->assertSame(
1,
\App\Models\TenantNotificationLog::query()
->where('tenant_id', $event->tenant_id)
->where('type', 'ai_budget_soft_cap')
->count()
);
}
}

View File

@@ -0,0 +1,96 @@
<?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\AiObservabilityService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Tests\TestCase;
class AiObservabilityServiceTest extends TestCase
{
use RefreshDatabase;
public function test_it_records_terminal_outcomes_in_cache(): void
{
$request = $this->makeRequest(AiEditRequest::STATUS_PROCESSING);
app(AiObservabilityService::class)->recordTerminalOutcome(
$request,
AiEditRequest::STATUS_SUCCEEDED,
1200,
false,
'process'
);
$bucket = now()->format('YmdH');
$prefix = sprintf('ai-editing:obs:tenant:%d:event:%d:hour:%s', $request->tenant_id, $request->event_id, $bucket);
$this->assertSame(1, (int) Cache::get($prefix.':total'));
$this->assertSame(1, (int) Cache::get($prefix.':succeeded'));
$this->assertSame(1200, (int) Cache::get($prefix.':duration_total_ms'));
}
public function test_it_logs_failure_rate_alert_when_threshold_is_reached(): void
{
config([
'ai-editing.observability.failure_rate_alert_threshold' => 0.5,
'ai-editing.observability.failure_rate_min_samples' => 1,
]);
$request = $this->makeRequest(AiEditRequest::STATUS_PROCESSING);
Log::spy();
app(AiObservabilityService::class)->recordTerminalOutcome(
$request,
AiEditRequest::STATUS_FAILED,
500,
false,
'poll'
);
Log::shouldHaveReceived('warning')
->withArgs(function (string $message, array $context): bool {
return $message === 'AI failure-rate alert threshold reached'
&& isset($context['failure_rate'])
&& $context['failure_rate'] >= 0.5;
})
->once();
}
private function makeRequest(string $status): AiEditRequest
{
$event = Event::factory()->create(['status' => 'published']);
$photo = Photo::factory()->for($event)->create([
'tenant_id' => $event->tenant_id,
'status' => 'approved',
]);
$style = AiStyle::query()->create([
'key' => 'obs-style',
'name' => 'Observability Style',
'provider' => 'runware',
'provider_model' => 'runware-default',
'is_active' => true,
]);
return 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' => $status,
'safety_state' => 'pending',
'prompt' => 'Observability',
'idempotency_key' => 'obs-'.uniqid('', true),
'queued_at' => now()->subMinute(),
'started_at' => now()->subSeconds(30),
]);
}
}

View File

@@ -0,0 +1,136 @@
<?php
namespace Tests\Unit\Services;
use App\Models\AiEditRequest;
use App\Models\Event;
use App\Models\Photo;
use App\Models\User;
use App\Services\AiEditing\AiStatusNotificationService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache;
use Tests\TestCase;
class AiStatusNotificationServiceTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
Cache::flush();
}
public function test_it_creates_guest_and_tenant_notifications_for_guest_requests(): void
{
$event = Event::factory()->create(['status' => 'published']);
$owner = User::factory()->create();
$event->tenant->update(['user_id' => $owner->id]);
$photo = Photo::factory()->for($event)->create([
'tenant_id' => $event->tenant_id,
'status' => 'approved',
]);
$request = AiEditRequest::query()->create([
'tenant_id' => $event->tenant_id,
'event_id' => $event->id,
'photo_id' => $photo->id,
'status' => AiEditRequest::STATUS_SUCCEEDED,
'safety_state' => 'passed',
'requested_by_device_id' => 'device-ai-1',
'idempotency_key' => 'notify-guest-success-1',
'queued_at' => now()->subMinute(),
'completed_at' => now(),
]);
app(AiStatusNotificationService::class)->notifyTerminalOutcome($request);
$this->assertDatabaseHas('guest_notifications', [
'event_id' => $event->id,
'type' => 'upload_alert',
'audience_scope' => 'guest',
'target_identifier' => 'device-ai-1',
]);
$this->assertDatabaseHas('tenant_notification_logs', [
'tenant_id' => $event->tenant_id,
'type' => 'ai_edit_succeeded',
'channel' => 'system',
'status' => 'sent',
]);
$this->assertDatabaseHas('tenant_notification_receipts', [
'tenant_id' => $event->tenant_id,
'user_id' => $owner->id,
'status' => 'delivered',
]);
}
public function test_it_creates_only_tenant_notification_for_tenant_admin_requests(): void
{
$event = Event::factory()->create(['status' => 'published']);
$owner = User::factory()->create();
$event->tenant->update(['user_id' => $owner->id]);
$photo = Photo::factory()->for($event)->create([
'tenant_id' => $event->tenant_id,
'status' => 'approved',
]);
$request = AiEditRequest::query()->create([
'tenant_id' => $event->tenant_id,
'event_id' => $event->id,
'photo_id' => $photo->id,
'requested_by_user_id' => $owner->id,
'status' => AiEditRequest::STATUS_FAILED,
'safety_state' => 'pending',
'failure_code' => 'provider_timeout',
'idempotency_key' => 'notify-tenant-failed-1',
'queued_at' => now()->subMinute(),
'completed_at' => now(),
]);
app(AiStatusNotificationService::class)->notifyTerminalOutcome($request);
$this->assertDatabaseCount('guest_notifications', 0);
$this->assertDatabaseHas('tenant_notification_logs', [
'tenant_id' => $event->tenant_id,
'type' => 'ai_edit_failed',
'channel' => 'system',
'status' => 'sent',
]);
}
public function test_it_deduplicates_terminal_notifications_per_request_and_status(): void
{
$event = Event::factory()->create(['status' => 'published']);
$owner = User::factory()->create();
$event->tenant->update(['user_id' => $owner->id]);
$photo = Photo::factory()->for($event)->create([
'tenant_id' => $event->tenant_id,
'status' => 'approved',
]);
$request = AiEditRequest::query()->create([
'tenant_id' => $event->tenant_id,
'event_id' => $event->id,
'photo_id' => $photo->id,
'status' => AiEditRequest::STATUS_BLOCKED,
'safety_state' => 'blocked',
'requested_by_device_id' => 'device-ai-dup',
'idempotency_key' => 'notify-dedupe-1',
'queued_at' => now()->subMinute(),
'completed_at' => now(),
]);
$service = app(AiStatusNotificationService::class);
$service->notifyTerminalOutcome($request);
$service->notifyTerminalOutcome($request->fresh());
$this->assertDatabaseCount('guest_notifications', 1);
$this->assertDatabaseCount('tenant_notification_logs', 1);
}
}