Files
portal/app/tests/Feature/RunwayCalculatorTest.php
T
Дмитрий 2a6b476d6d fix(billing): единый источник runway — дашборд = биллинг (F3)
Дашборд считал «хватит на дни» от legacy balance_leads (≈0 для рублёвых тенантов)
и расходился с биллингом. Введён общий RunwayCalculator; оба контроллера считают
runway от affordable leads (рубли→лиды по тарифу, BalanceToLeadsConverter). Фронт
DashboardView больше не режет число дней до 7 сегментов полосы. TDD: 4 Pest нового
сервиса + обновлён DashboardSummary + 1 vitest.

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

58 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
use App\Models\Tenant;
use App\Services\Billing\RunwayCalculator;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;
/**
* F3 (17.06.2026): единый расчёт runway для биллинга и дашборда.
* daysLeft(tenantId, affordableLeads):
* affordable<=0 → 0; нет списаний за 30 дн → null;
* иначе floor(affordable / (lead_charges за 30 дн / 30)).
*/
uses(DatabaseTransactions::class);
/** Вставляет $count списаний с датой $chargedAt (deferred FK на deals — тест откатывается). */
function seedLeadCharges(int $tenantId, int $count, string $chargedAt): void
{
DB::statement('SET app.current_tenant_id = '.$tenantId);
for ($i = 0; $i < $count; $i++) {
DB::table('lead_charges')->insert([
'tenant_id' => $tenantId,
'deal_id' => 900000 + $i,
'deal_received_at' => $chargedAt,
'tier_no' => 1,
'price_per_lead_kopecks' => 50000,
'charge_source' => 'rub',
'charged_at' => $chargedAt,
'created_at' => now()->toDateTimeString(),
]);
}
}
test('daysLeft = 0 если affordableLeads <= 0', function () {
$tenant = Tenant::factory()->create();
expect(app(RunwayCalculator::class)->daysLeft($tenant->id, 0))->toBe(0);
expect(app(RunwayCalculator::class)->daysLeft($tenant->id, -5))->toBe(0);
});
test('daysLeft = null если нет списаний за 30 дней', function () {
$tenant = Tenant::factory()->create();
expect(app(RunwayCalculator::class)->daysLeft($tenant->id, 100))->toBeNull();
});
test('daysLeft = floor(affordable / средний дневной за 30 дней)', function () {
$tenant = Tenant::factory()->create();
seedLeadCharges($tenant->id, 30, now()->subDays(5)->toDateTimeString()); // 30/30 = 1 лид/день
expect(app(RunwayCalculator::class)->daysLeft($tenant->id, 90))->toBe(90);
});
test('daysLeft игнорирует списания старше 30 дней', function () {
$tenant = Tenant::factory()->create();
seedLeadCharges($tenant->id, 30, now()->subDays(40)->toDateTimeString()); // вне окна
expect(app(RunwayCalculator::class)->daysLeft($tenant->id, 90))->toBeNull();
});