Files
portal/app/tests/Feature/Console/IncidentsWatchFailuresTest.php
T
Дмитрий 9799c0eb0d
Accessibility (Pa11y live) / a11y (push) Has been cancelled
SAST — Semgrep / Semgrep SAST scan (push) Has been cancelled
feat(supplier): аварийный spike-детектор ручной очереди в incidents:watch-failures
Блок 6: если за окно (10 мин) в supplier_manual_sync_queue упала пачка
(>= threshold-manual-queue, дефолт 10) — вероятное падение кабинета поставщика.
Инцидент дедупится 60 мин, письмо шлётся каждый прогон на kdv1@bk.ru + ops@liderra.ru
(громко при реальной аварии, но одним сводным письмом). Флаг sendMail в createIncident.
phpstan-baseline: +4 TestCall::artisan() (5→9) от новых тестов.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
(cherry picked from commit 74d4d9f21d)
2026-07-07 19:19:26 +03:00

174 lines
6.3 KiB
PHP

<?php
declare(strict_types=1);
use App\Mail\IncidentDetectedMail;
use App\Models\Project;
use App\Models\Tenant;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Tests\Concerns\SharesSupplierPdo;
uses(DatabaseTransactions::class);
uses(SharesSupplierPdo::class);
// Helper: insert a failed_webhook_jobs row
function makeFailedWebhookJob(string $exception, ?DateTimeInterface $at = null): void
{
DB::table('failed_webhook_jobs')->insert([
'failed_at' => $at ?? now(),
'exception' => $exception,
'raw_payload' => '{}',
'retry_count' => 0,
]);
}
// Helper: ensure at least one saas_admin_users row exists for FK
function ensureSystemAdmin(): int
{
// ВАЖНО: фильтруем по контракту команды (IncidentsWatchFailures ищет
// is_active=true AND deleted_at IS NULL). Без фильтра протёкшие неактивные
// admin'ы от других тестов (PdErasureServiceTest вставляет is_active=false
// committed через pgsql_supplier) удовлетворяли бы value('id') → ранний
// return → активный admin не создавался → команда warn-only → 0 инцидентов.
$id = DB::table('saas_admin_users')->where('is_active', true)->whereNull('deleted_at')->value('id');
if ($id !== null) {
return (int) $id;
}
return (int) DB::table('saas_admin_users')->insertGetId([
'email' => 'system-cron@liderra.ru',
'full_name' => 'System Cron',
'password_hash' => '$2y$12$placeholder',
'role' => 'dev_oncall',
'is_active' => true,
'created_at' => now(),
]);
}
beforeEach(function () {
ensureSystemAdmin();
});
test('does not create incident when failures are below threshold', function () {
$now = Carbon::now();
// Insert 5 failures — below default threshold of 200
for ($i = 0; $i < 5; $i++) {
makeFailedWebhookJob('App\\Exceptions\\WebhookException: connection refused', $now);
}
$this->artisan('incidents:watch-failures')->assertSuccessful();
expect(DB::table('incidents_log')->count())->toBe(0);
});
test('creates incident when failures exceed threshold', function () {
$now = Carbon::now();
// Insert 201 failures with same exception signature
for ($i = 0; $i < 201; $i++) {
makeFailedWebhookJob('App\\Exceptions\\WebhookException: connection refused', $now);
}
$this->artisan('incidents:watch-failures')->assertSuccessful();
expect(DB::table('incidents_log')->count())->toBe(1);
$incident = DB::table('incidents_log')->first();
expect($incident->type)->toBe('other');
expect($incident->severity)->toBe('high');
expect($incident->summary)->toContain('201');
});
test('deduplicates: does not create second incident for same ongoing storm', function () {
$now = Carbon::now();
for ($i = 0; $i < 201; $i++) {
makeFailedWebhookJob('App\\Exceptions\\WebhookException: timeout', $now);
}
// First run creates incident
$this->artisan('incidents:watch-failures')->assertSuccessful();
expect(DB::table('incidents_log')->count())->toBe(1);
// Second run (storm still ongoing) should NOT create another incident
$this->artisan('incidents:watch-failures')->assertSuccessful();
expect(DB::table('incidents_log')->count())->toBe(1);
});
test('creates separate incidents for different exception signatures', function () {
$now = Carbon::now();
// 201 failures with exception A
for ($i = 0; $i < 201; $i++) {
makeFailedWebhookJob('App\\Exceptions\\WebhookException: connection refused', $now);
}
// 201 failures with exception B
for ($i = 0; $i < 201; $i++) {
makeFailedWebhookJob('App\\Exceptions\\CurlException: SSL handshake failed', $now);
}
$this->artisan('incidents:watch-failures')->assertSuccessful();
expect(DB::table('incidents_log')->count())->toBe(2);
});
// ===== Блок 6: supplier-manual-queue spike =====
// Helper: seed a project + N supplier_manual_sync_queue rows within the window
function seedManualQueue(int $n, ?DateTimeInterface $at = null): int
{
$project = Project::factory()->for(Tenant::factory())->create();
for ($i = 0; $i < $n; $i++) {
DB::table('supplier_manual_sync_queue')->insert([
'project_id' => $project->id,
'platform' => 'B1',
'operation' => 'create',
'external_id' => null,
'payload_snapshot' => '{}',
'failure_reason' => 'portal_unreachable',
'status' => 'pending',
'created_at' => $at ?? now(),
]);
}
return $project->id;
}
test('manual-queue spike below threshold: no incident, no mail', function () {
Mail::fake();
seedManualQueue(3, now()); // < default threshold 10
$this->artisan('incidents:watch-failures')->assertSuccessful();
expect(DB::table('incidents_log')->where('root_cause', 'supplier-manual-queue-spike')->count())->toBe(0);
Mail::assertNothingSent();
});
test('manual-queue spike at/above threshold: high incident + mail to both addresses', function () {
Mail::fake();
seedManualQueue(10, now()); // == default threshold 10
$this->artisan('incidents:watch-failures')->assertSuccessful();
$incident = DB::table('incidents_log')->where('root_cause', 'supplier-manual-queue-spike')->first();
expect($incident)->not->toBeNull();
expect($incident->severity)->toBe('high');
expect($incident->summary)->toContain('10');
Mail::assertSent(IncidentDetectedMail::class, function ($mail) {
return $mail->hasTo('kdv1@bk.ru') && $mail->hasTo('ops@liderra.ru');
});
});
test('manual-queue spike dedup: second run in window makes no new incident BUT re-sends mail', function () {
Mail::fake();
seedManualQueue(12, now());
$this->artisan('incidents:watch-failures')->assertSuccessful();
$this->artisan('incidents:watch-failures')->assertSuccessful();
// Инцидент один (дедуп 60 мин), а письмо ушло на каждом прогоне (громкость).
expect(DB::table('incidents_log')->where('root_cause', 'supplier-manual-queue-spike')->count())->toBe(1);
Mail::assertSent(IncidentDetectedMail::class, 2);
});