Files
portal/app/tests/Feature/Billing/ProjectPreflightTest.php
T
Дмитрий 16105cae5c feat(billing-v2-c): ProjectController preflight — 409 при перегрузке баланса
Task 1.7 плана 2026-05-24-billing-v2-spec-c-preflight-vtb.

store/update проверяют преfflight перед созданием/изменением проекта:
- если сумма daily_limit_target всех активных не-blocked проектов
  превышает capacity баланса (через BalancePreflightService) и не
  передан force_save_blocked=true → возврат 409 с JSON-телом:
  {error, current_balance_rub, current_capacity_leads,
   would_be_required_leads, deficit_leads}
- если force_save_blocked=true → проект создаётся/обновляется с
  preflight_blocked_at=now() (точечная заморозка одного проекта,
  не блокирует остальные).

Safe fallback: без активных pricing_tiers — преfflight skipped
(legacy-окружения без настроенного биллинга).

TDD: 4 теста GREEN (409 store / 409 update / force_save_blocked
создаёт blocked / norm pass через capacity).

Регрессия: 0 регрессий на Plan5 ProjectsStoreTest+ProjectsUpdateTest
(37/37 GREEN после safe fallback).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-25 04:41:57 +03:00

108 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('returns 409 when new project would overload balance', function () {
// 1000₽ / 50₽ = 20 лидов capacity; запрашиваем daily_limit_target=30 → дефицит 10.
$tenant = Tenant::factory()->create(['balance_rub' => '1000.00']);
$user = User::factory()->create(['tenant_id' => $tenant->id]);
$response = $this->actingAs($user)->postJson('/api/projects', [
'name' => 'Перегрузка',
'signal_type' => 'site',
'signal_identifier' => 'overload.ru',
'daily_limit_target' => 30,
'regions' => [],
'delivery_days_mask' => 127,
]);
$response->assertStatus(409);
$response->assertJsonPath('error', 'balance_insufficient');
$response->assertJsonPath('deficit_leads', 10);
$response->assertJsonPath('current_capacity_leads', 20);
$response->assertJsonPath('would_be_required_leads', 30);
expect(Project::where('signal_identifier', 'overload.ru')->exists())->toBeFalse();
});
it('creates blocked project when force_save_blocked=true', function () {
$tenant = Tenant::factory()->create(['balance_rub' => '1000.00']);
$user = User::factory()->create(['tenant_id' => $tenant->id]);
$response = $this->actingAs($user)->postJson('/api/projects', [
'name' => 'Заблокированный',
'signal_type' => 'site',
'signal_identifier' => 'force-blocked.ru',
'daily_limit_target' => 30,
'regions' => [],
'delivery_days_mask' => 127,
'force_save_blocked' => true,
]);
$response->assertCreated();
$project = Project::where('signal_identifier', 'force-blocked.ru')->first();
expect($project)->not->toBeNull();
expect($project->preflight_blocked_at)->not->toBeNull();
});
it('creates normally when within balance', function () {
// 2000₽ / 50₽ = 40 лидов capacity; daily_limit_target=30 — passes.
$tenant = Tenant::factory()->create(['balance_rub' => '2000.00']);
$user = User::factory()->create(['tenant_id' => $tenant->id]);
$response = $this->actingAs($user)->postJson('/api/projects', [
'name' => 'Норма',
'signal_type' => 'site',
'signal_identifier' => 'ok-balance.ru',
'daily_limit_target' => 30,
'regions' => [],
'delivery_days_mask' => 127,
]);
$response->assertCreated();
$project = Project::where('signal_identifier', 'ok-balance.ru')->first();
expect($project->preflight_blocked_at)->toBeNull();
});
it('returns 409 on update when increased limit overloads balance', function () {
// существующий проект на 15 лидов, всё ок (capacity 20).
$tenant = Tenant::factory()->create(['balance_rub' => '1000.00']);
$user = User::factory()->create(['tenant_id' => $tenant->id]);
$project = Project::factory()->for($tenant)->create([
'is_active' => true,
'daily_limit_target' => 15,
]);
// UPDATE до 30 → suma 30 > capacity 20 → 409.
$response = $this->actingAs($user)->patchJson("/api/projects/{$project->id}", [
'daily_limit_target' => 30,
]);
$response->assertStatus(409);
$response->assertJsonPath('error', 'balance_insufficient');
expect($project->fresh()->daily_limit_target)->toBe(15); // не изменилось
});