Files
portal/app/tests/Feature/Admin/AdminDashboardBalancesTest.php
T
Дмитрий 88e816c576 feat(балансы): backend плитки балансов внешних сервисов
Ежедневный контроль баланса DaData/Поставщик/Yandex Cloud плиткой дашборда.

- Таблица external_service_balances (pgsql_supplier, BYPASSRLS, last-value upsert)
- BalanceHealth: чистая логика светофора (red <floor или <3д; amber <floor или <7д)
- BalanceProvider+DTO; провайдеры DaData(API)/YC(OAuth→IAM→billing)/Supplier(Playwright)
- RefreshExternalBalancesJob: изоляция провайдеров (try/catch), расписание 06:30 МСК
- AdminDashboardController::balances() + плитка в summary + topup_url (кнопка «Пополнить»)
- Тесты: BalanceHealth, 3 провайдера, джоба, endpoint (102 теста зелёные)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-28 07:12:14 +03:00

56 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;
uses(DatabaseTransactions::class);
beforeEach(function () {
DB::table('external_service_balances')->delete();
});
it('GET /api/admin/dashboard/balances возвращает строки сервисов + topup_url', function () {
config()->set('services.yandex_cloud.console_billing_url', 'https://console.yandex.cloud/billing/accounts');
config()->set('services.yandex_cloud.billing_account_id', 'dn2w7fcvynjxe6elljct');
DB::table('external_service_balances')->insert([
['service_key' => 'dadata', 'balance_amount' => 4500, 'currency' => 'RUB', 'daily_spend_estimate' => 100,
'days_left' => 9, 'light' => 'green', 'ok' => true, 'checked_at' => now(), 'created_at' => now(), 'updated_at' => now()],
['service_key' => 'yandex_cloud', 'balance_amount' => -540.48, 'currency' => 'RUB', 'daily_spend_estimate' => 600,
'days_left' => 0, 'light' => 'red', 'ok' => true, 'checked_at' => now(), 'created_at' => now(), 'updated_at' => now()],
]);
$res = $this->getJson('/api/admin/dashboard/balances');
$res->assertOk();
$res->assertJsonStructure([
'light',
'services' => [['service_key', 'balance_amount', 'currency', 'days_left', 'light', 'ok', 'checked_at', 'topup_url']],
]);
expect($res->json('light'))->toBe('red'); // худший из сервисов
$yc = collect($res->json('services'))->firstWhere('service_key', 'yandex_cloud');
expect($yc['topup_url'])->toBe('https://console.yandex.cloud/billing/accounts/dn2w7fcvynjxe6elljct/payments');
});
it('неуспешный сервис показывается серым (grey), не красным', function () {
DB::table('external_service_balances')->insert([
'service_key' => 'supplier', 'balance_amount' => 12000, 'currency' => 'RUB',
'light' => 'red', 'ok' => false, 'error' => 'кабинет недоступен',
'checked_at' => now(), 'created_at' => now(), 'updated_at' => now(),
]);
$res = $this->getJson('/api/admin/dashboard/balances');
$res->assertOk();
$svc = collect($res->json('services'))->firstWhere('service_key', 'supplier');
expect($svc['light'])->toBe('grey');
expect($svc['ok'])->toBeFalse();
});
it('summary включает плитку balances', function () {
$res = $this->getJson('/api/admin/dashboard?period=30d');
$res->assertOk();
$res->assertJsonStructure(['balances' => ['light', 'count', 'red']]);
});