Files
portal/app/tests/Feature/Project/UpdateLimitGateTest.php
T

60 lines
2.4 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 Tests\Concerns\SharesSupplierPdo;
uses(DatabaseTransactions::class);
uses(SharesSupplierPdo::class);
beforeEach(function () {
PricingTier::create([
'tier_no' => 1,
'leads_in_tier' => null,
'price_per_lead_kopecks' => 10000,
'is_active' => true,
'effective_from' => now()->toDateString(),
]);
});
it('rejects limit raise beyond balance with unified payload', function () {
$t = Tenant::factory()->create(['balance_rub' => '300.00', 'delivered_in_month' => 0]); // 3 лида
$u = User::factory()->for($t)->create();
$p = Project::factory()->for($t)->create(['is_active' => true, 'daily_limit_target' => 2, 'preflight_blocked_at' => null]);
$resp = $this->actingAs($u)->patchJson("/api/projects/{$p->id}", ['daily_limit_target' => 10]);
$resp->assertStatus(409);
$resp->assertJsonPath('error', 'balance_insufficient');
$resp->assertJsonPath('balance.topup_rub', '700.00'); // не хватает 7 лидов × 100 ₽
expect($p->fresh()->daily_limit_target)->toBe(2); // лимит НЕ изменился
});
it('allows limit raise when balance sufficient', function () {
$t = Tenant::factory()->create(['balance_rub' => '1000.00', 'delivered_in_month' => 0]); // 10 лидов
$u = User::factory()->for($t)->create();
$p = Project::factory()->for($t)->create(['is_active' => true, 'daily_limit_target' => 2, 'preflight_blocked_at' => null]);
$resp = $this->actingAs($u)->patchJson("/api/projects/{$p->id}", ['daily_limit_target' => 5]);
$resp->assertOk();
expect($p->fresh()->daily_limit_target)->toBe(5);
});
it('allows limit raise on paused project without gate check', function () {
// Паузированный проект: не активный, гейт не нужен
$t = Tenant::factory()->create(['balance_rub' => '0.00', 'delivered_in_month' => 0]);
$u = User::factory()->for($t)->create();
$p = Project::factory()->for($t)->create(['is_active' => false, 'daily_limit_target' => 2, 'preflight_blocked_at' => null]);
$resp = $this->actingAs($u)->patchJson("/api/projects/{$p->id}", ['daily_limit_target' => 10]);
$resp->assertOk();
expect($p->fresh()->daily_limit_target)->toBe(10);
});