Files
portal/app/tests/Feature/External/ExternalServiceDownAlertTest.php
T
Дмитрий 21e1b7982f feat(external): email-алерт при переходе внешнего сервиса в красный
Edge-trigger: одно письмо на ops-адрес при первом покраснении (баланс на
исходе или сервис упал); повторное красное не спамит. Mailable + blade.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-02 09:03:53 +03:00

69 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
use App\Jobs\External\RefreshExternalBalancesJob;
use App\Mail\ExternalServiceDownMail;
use App\Services\External\BalanceReading;
use App\Services\External\DadataBalanceProvider;
use App\Services\External\LivenessReading;
use App\Services\External\SupplierBalanceProvider;
use App\Services\External\YandexCloudBalanceProvider;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Tests\Concerns\SharesSupplierPdo;
uses(DatabaseTransactions::class, SharesSupplierPdo::class);
afterEach(fn () => RefreshExternalBalancesJob::resetLivenessProbes());
function stubBalanceProvidersOk(): void
{
app()->instance(DadataBalanceProvider::class, fakeProvider('dadata', BalanceReading::ok('dadata', 9000, 'RUB', 100)));
app()->instance(SupplierBalanceProvider::class, fakeProvider('supplier', BalanceReading::ok('supplier', 90000, 'RUB', null)));
app()->instance(YandexCloudBalanceProvider::class, fakeProvider('yandex_cloud', BalanceReading::ok('yandex_cloud', 90000, 'RUB', 100)));
}
it('шлёт одно письмо, когда сервис впервые покраснел', function () {
Mail::fake();
stubBalanceProvidersOk();
RefreshExternalBalancesJob::useLivenessProbes([
fakeProbe('jivosite', LivenessReading::down('jivosite', 'HTTP 500')),
]);
(new RefreshExternalBalancesJob)->handle();
Mail::assertSent(ExternalServiceDownMail::class, 1);
});
it('не шлёт письмо, если сервис уже был красным (без спама)', function () {
Mail::fake();
stubBalanceProvidersOk();
// Предзаливаем строку: jivosite уже красный.
DB::connection('pgsql_supplier')->table('external_service_balances')->updateOrInsert(
['service_key' => 'jivosite'],
['light' => 'red', 'ok' => true, 'currency' => 'RUB', 'checked_at' => now(), 'updated_at' => now()],
);
RefreshExternalBalancesJob::useLivenessProbes([
fakeProbe('jivosite', LivenessReading::down('jivosite', 'HTTP 500')),
]);
(new RefreshExternalBalancesJob)->handle();
Mail::assertNothingSent();
});
it('не шлёт письмо, когда всё зелёное', function () {
Mail::fake();
stubBalanceProvidersOk();
RefreshExternalBalancesJob::useLivenessProbes([
fakeProbe('jivosite', LivenessReading::alive('jivosite', 'ок')),
]);
(new RefreshExternalBalancesJob)->handle();
Mail::assertNothingSent();
});