Files
portal/app/tests/Feature/RunwayCalculatorTest.php
T

58 lines
2.3 KiB
PHP
Raw Normal View History

<?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();
});