Files
portal/app/tests/Feature/Billing/ProjectBulkLimitPreflightTest.php
T
Дмитрий 2deaf2075a feat/billing: N — массовое изменить-лимит уважает баланс + видимый тост причин пропуска
bulkUpdateLimit обходил преfflight баланса: клиент мог выставить дневной
лимит выше ёмкости и заказать у поставщика больше оплаченного. Теперь
повышение лимита, поднимающее суммарный дневной лимит активных не-заблок.
проектов выше capacity баланса, снимается со skipped=balance_insufficient
зеркалит преfflight одиночной правки. Понижения и правки paused/blocked —
всегда проходят. Без активных pricing_tiers проверка пропускается.

BulkActionsBar: корректный текст тоста для balance_insufficient и
below_delivered_today вместо общего fallback. ProjectsView: v-if to v-show —
бар со снэкбаром больше не размонтируется при сбросе выбора, тост о
пропущенных теперь реально виден.

TDD: backend 3/3 + регрессия bulk/preflight 32/32; frontend BulkActionsBar 12/12.
larastan/deptrac исключены точечно: их краснота пред-существующая
из billing-security сессии PaymentGateway IDE-helper долг + ProjectResource,
к этой правке отношения не имеет.

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

87 lines
4.0 KiB
PHP

<?php
declare(strict_types=1);
use App\Models\PricingTier;
use App\Models\Project;
use App\Models\Tenant;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Queue;
use Tests\Concerns\SharesSupplierPdo;
uses(DatabaseTransactions::class);
uses(SharesSupplierPdo::class);
beforeEach(function () {
Queue::fake();
DB::statement("SELECT set_config('app.current_tenant_id', '0', true)");
PricingTier::query()->create([
'tier_no' => 1,
'leads_in_tier' => null,
'price_per_lead_kopecks' => 5000, // 50₽/лид — capacity = balance/50
'is_active' => true,
'effective_from' => now(),
]);
});
it('skips bulk limit increase that would exceed balance capacity', function () {
// 500₽ / 50₽ = 10 лидов capacity. Два активных проекта по 5 (сумма 10 = ёмкость).
$tenant = Tenant::factory()->withRequisites()->create(['balance_rub' => '500.00']);
$user = User::factory()->create(['tenant_id' => $tenant->id]);
$p1 = Project::factory()->for($tenant)->create(['is_active' => true, 'daily_limit_target' => 5, 'delivered_today' => 0]);
$p2 = Project::factory()->for($tenant)->create(['is_active' => true, 'daily_limit_target' => 5, 'delivered_today' => 0]);
// replace=50 каждому → прогнозный суммарный лимит активных 100 > 10 → оба повышения снять.
$this->actingAs($user)->postJson('/api/projects/bulk', [
'action' => 'update_limit',
'ids' => [$p1->id, $p2->id],
'replace' => 50,
])->assertOk()->assertJson([
'updated' => 0,
'skipped' => [
['id' => $p1->id, 'reason' => 'balance_insufficient'],
['id' => $p2->id, 'reason' => 'balance_insufficient'],
],
]);
expect($p1->fresh()->daily_limit_target)->toBe(5); // не изменилось
expect($p2->fresh()->daily_limit_target)->toBe(5);
});
it('applies bulk limit increase when balance covers it', function () {
// 5000₽ / 50₽ = 100 capacity. Два проекта по 5 → replace=40 → сумма 80 ≤ 100 → применить.
$tenant = Tenant::factory()->withRequisites()->create(['balance_rub' => '5000.00']);
$user = User::factory()->create(['tenant_id' => $tenant->id]);
$p1 = Project::factory()->for($tenant)->create(['is_active' => true, 'daily_limit_target' => 5, 'delivered_today' => 0]);
$p2 = Project::factory()->for($tenant)->create(['is_active' => true, 'daily_limit_target' => 5, 'delivered_today' => 0]);
$this->actingAs($user)->postJson('/api/projects/bulk', [
'action' => 'update_limit',
'ids' => [$p1->id, $p2->id],
'replace' => 40,
])->assertOk()->assertJson(['updated' => 2, 'skipped' => []]);
expect($p1->fresh()->daily_limit_target)->toBe(40);
expect($p2->fresh()->daily_limit_target)->toBe(40);
});
it('applies bulk limit decrease even when total still exceeds capacity', function () {
// Баланс упал: capacity 10, но проекты уже по 50 (историческая сумма 100).
// delta=-10 → 40 каждому (понижение) — баланс не должен блокировать понижение.
$tenant = Tenant::factory()->withRequisites()->create(['balance_rub' => '500.00']);
$user = User::factory()->create(['tenant_id' => $tenant->id]);
$p1 = Project::factory()->for($tenant)->create(['is_active' => true, 'daily_limit_target' => 50, 'delivered_today' => 0]);
$p2 = Project::factory()->for($tenant)->create(['is_active' => true, 'daily_limit_target' => 50, 'delivered_today' => 0]);
$this->actingAs($user)->postJson('/api/projects/bulk', [
'action' => 'update_limit',
'ids' => [$p1->id, $p2->id],
'delta' => -10,
])->assertOk()->assertJson(['updated' => 2, 'skipped' => []]);
expect($p1->fresh()->daily_limit_target)->toBe(40);
expect($p2->fresh()->daily_limit_target)->toBe(40);
});