49 lines
1.7 KiB
PHP
49 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\System;
|
|
|
|
use App\Models\AiHealingLog;
|
|
use App\Models\User;
|
|
use App\Services\SystemConfig\SystemConfigService;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class AiCircuitBreakerTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$this->seed(\Database\Seeders\RoleAndPermissionSeeder::class);
|
|
}
|
|
|
|
public function test_circuit_breaker_blocks_excessive_healing_attempts()
|
|
{
|
|
// 1. Set limit to 2 fixes per hour
|
|
$config = app(SystemConfigService::class);
|
|
$config->update(['ai_healing_max_attempts_per_hour' => 2]);
|
|
|
|
// 2. Create 2 recent "resolved" logs
|
|
AiHealingLog::factory()->count(2)->create([
|
|
'status' => 'resolved',
|
|
'created_at' => now()->subMinutes(10),
|
|
]);
|
|
|
|
// 3. Verify count
|
|
$this->assertEquals(2, AiHealingLog::where('created_at', '>', now()->subHour())->count());
|
|
|
|
// 4. Simulate a new exception (we need to trigger the logic in bootstrap/app.php or simulate it)
|
|
// Since we can't easily trigger bootstrap/app.php in a feature test without throwing a real exception,
|
|
// we can test the logic directly or via a mock.
|
|
|
|
// Let's test if the circuit breaker logic would block.
|
|
$maxPerHour = (int) $config->get('ai_healing_max_attempts_per_hour', 5);
|
|
$recentCount = AiHealingLog::where('created_at', '>', now()->subHour())
|
|
->whereIn('status', ['resolved', 'pending', 'diagnosing'])
|
|
->count();
|
|
|
|
$this->assertTrue($recentCount >= $maxPerHour, "Circuit breaker should be tripped");
|
|
}
|
|
}
|