Files
portal/app/tests/Feature/Autopodbor/CreateProjectsGateTest.php
T
Дмитрий f78ab4af3d feat(биллинг): баланс блокирует запуск проекта, а не создание — единый гейт
Создавать проекты можно всегда; баланс проверяется в момент ЗАПУСКА
(создать-и-запустить / запустить / возобновить одиночно и пачкой / автоподбор)
под замком на клиента (без гонок). Не хватает — проект остаётся на паузе с
меткой preflight_blocked_at, клиенту сообщение в рублях (сколько пополнить).
Групповой запуск «сколько влезло». Нет активного тарифа на дату → запуск
запрещён (fail-closed). Гейт реквизитов первого проекта добавлен и в автоподбор.

- LaunchBalanceGate — единый гейт вместо 3 копий preflight (ProjectController
  store/update, AutopodborController), под DB::transaction + lockForUpdate(Tenant).
- ProjectService::create($launch) + новый setActive(); bulk resume «сколько влезло».
- AutopodborProjectCreator: пачка в транзакции через общий ProjectService::create.
- Идемпотентность box/phone_type миграций автоподбора (Schema::hasColumn guard).
- Тест-инфра: afterRefreshingDatabase восстанавливает месячные партиции.

Тесты фичи 40/40 зелёные. Спека и план — docs/superpowers.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-02 11:07:22 +03:00

130 lines
4.4 KiB
PHP

<?php
declare(strict_types=1);
use App\Models\AutopodborCompetitor;
use App\Models\AutopodborRun;
use App\Models\AutopodborSource;
use App\Models\PricingTier;
use App\Models\Tenant;
use App\Models\TenantRequisites;
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, 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(),
]);
});
/**
* Создаёт tenant + user + run + competitor + 2 источника типа site.
* Возвращает [$tenant, $user, $s1, $s2].
*/
function setupGateScene(array $tenantAttrs = []): array
{
$tenant = Tenant::factory()->create($tenantAttrs);
$user = User::factory()->create(['tenant_id' => $tenant->id]);
DB::statement('SET app.current_tenant_id = '.$tenant->id);
$run = AutopodborRun::create([
'tenant_id' => $tenant->id,
'kind' => 'search',
'status' => 'done',
'region_code' => 16,
'params' => [],
]);
$comp = AutopodborCompetitor::create([
'tenant_id' => $tenant->id,
'search_run_id' => $run->id,
'study_run_id' => $run->id,
'studied_at' => now(),
'name' => 'Gate-конкурент',
'box' => 'field',
'site_url' => 'gatetest1.ru',
'dedup_key' => 'site:gatetest1.ru',
]);
$s1 = AutopodborSource::create([
'tenant_id' => $tenant->id,
'competitor_id' => $comp->id,
'study_run_id' => $run->id,
'signal_type' => 'site',
'identifier' => 'gatetest1.ru',
'box' => 'field',
'dedup_key' => 'site:gatetest1.ru',
]);
$s2 = AutopodborSource::create([
'tenant_id' => $tenant->id,
'competitor_id' => $comp->id,
'study_run_id' => $run->id,
'signal_type' => 'site',
'identifier' => 'gatetest2.ru',
'box' => 'field',
'dedup_key' => 'site:gatetest2.ru',
]);
return [$tenant, $user, $s1, $s2];
}
it('creates all held with summary when balance insufficient (201)', function () {
Queue::fake();
// Баланс на 1 лид, запрашиваем 5 лидов × 2 источника — оба удержаны
[$tenant, $user, $s1, $s2] = setupGateScene(['balance_rub' => '100.00', 'delivered_in_month' => 0]);
// Реквизиты чтобы пройти гейт реквизитов (tenant новый, проектов 0, нужны реквизиты)
TenantRequisites::create([
'tenant_id' => $tenant->id,
'subject_type' => 'individual',
'contact_name' => 'Тест Тестов',
'contact_phone' => '+79991234567',
]);
$resp = $this->actingAs($user)->postJson('/api/autopodbor/projects', [
'source_ids' => [$s1->id, $s2->id],
'daily_limit_target' => 5,
'delivery_days_mask' => 127,
'launch' => true,
]);
$resp->assertCreated();
$resp->assertJsonPath('launch.launched', 0);
$resp->assertJsonPath('launch.deferred', 2);
// balance payload должен быть от первого удержанного
expect($resp->json('launch.balance'))->not->toBeNull();
expect($resp->json('launch.balance.topup_rub'))->not->toBeNull();
// оба проекта созданы (не заблокировано), но не запущены
expect($resp->json('data'))->toHaveCount(2);
expect(collect($resp->json('data'))->filter(fn ($p) => $p['is_active'])->count())->toBe(0);
});
it('blocks first project without requisites (422 requisites_required)', function () {
Queue::fake();
// Tenant без реквизитов, проектов 0
[$tenant, $user, $s1, $s2] = setupGateScene(['balance_rub' => '10000.00', 'delivered_in_month' => 0]);
// Реквизиты НЕ создаём намеренно
$resp = $this->actingAs($user)->postJson('/api/autopodbor/projects', [
'source_ids' => [$s1->id, $s2->id],
'daily_limit_target' => 5,
'delivery_days_mask' => 127,
'launch' => true,
]);
$resp->assertStatus(422);
$resp->assertJsonPath('error', 'requisites_required');
});