feat(ai): finalize AI magic edits epic rollout and operations
This commit is contained in:
46
tests/Unit/Models/AiStyleVersioningTest.php
Normal file
46
tests/Unit/Models/AiStyleVersioningTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
126
tests/Unit/Services/AiBudgetGuardServiceTest.php
Normal file
126
tests/Unit/Services/AiBudgetGuardServiceTest.php
Normal 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()
|
||||
);
|
||||
}
|
||||
}
|
||||
96
tests/Unit/Services/AiObservabilityServiceTest.php
Normal file
96
tests/Unit/Services/AiObservabilityServiceTest.php
Normal 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),
|
||||
]);
|
||||
}
|
||||
}
|
||||
136
tests/Unit/Services/AiStatusNotificationServiceTest.php
Normal file
136
tests/Unit/Services/AiStatusNotificationServiceTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user