Files
portal/app/tests/Feature/Billing/ProjectPreflightTest.php
T

108 lines
4.0 KiB
PHP
Raw Normal View History

<?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); // не изменилось
});