tenant = Tenant::factory()->create(); $this->user = User::factory()->create(['tenant_id' => $this->tenant->id]); $this->actingAs($this->user); config(['client_tg.sandbox' => false]); }); it('<367 кандидатов в бою → launch 422, бронь НЕ сделана, статус остаётся draft', function () { app(AdWalletService::class)->topup($this->tenant->id, '5000.00', null, 'test'); for ($i = 0; $i < 10; $i++) { Contact::create([ 'tenant_id' => $this->tenant->id, 'phone' => sprintf('7999%07d', $i), 'name' => null, 'operator' => null, ]); } $campaign = Campaign::create([ 'tenant_id' => $this->tenant->id, 'status' => Campaign::STATUS_DRAFT, 'ad_text' => 'x', 'ad_link' => 'https://t.me/x', 'ord_category' => 'Размещение рекламы', 'budget_cap_rub' => '1000.00', 'audience_kind' => Campaign::AUDIENCE_BASE, 'planned_count' => 10, 'estimated_cost_rub' => '0.00', 'created_by' => $this->user->id, ]); Queue::fake(); $this->postJson("/api/telegram/campaigns/{$campaign->id}/launch")->assertStatus(422); expect(Campaign::find($campaign->id)->status)->toBe(Campaign::STATUS_DRAFT); $wallet = AdWallet::where('tenant_id', $this->tenant->id)->first(); expect((string) $wallet->frozen_rub)->toBe('0.00'); Queue::assertNothingPushed(); }); it('0 кандидатов в бою → launch 422, статус остаётся draft', function () { app(AdWalletService::class)->topup($this->tenant->id, '5000.00', null, 'test'); $campaign = Campaign::create([ 'tenant_id' => $this->tenant->id, 'status' => Campaign::STATUS_DRAFT, 'ad_text' => 'x', 'ad_link' => 'https://t.me/x', 'ord_category' => 'Размещение рекламы', 'budget_cap_rub' => '1000.00', 'audience_kind' => Campaign::AUDIENCE_BASE, 'planned_count' => 0, 'estimated_cost_rub' => '0.00', 'created_by' => $this->user->id, ]); Queue::fake(); $this->postJson("/api/telegram/campaigns/{$campaign->id}/launch")->assertStatus(422); expect(Campaign::find($campaign->id)->status)->toBe(Campaign::STATUS_DRAFT); Queue::assertNothingPushed(); }); it('≥367 кандидатов + денег хватает в бою → launch OK, draft→queued, джоб поставлен, бронь = budget', function () { app(AdWalletService::class)->topup($this->tenant->id, '5000.00', null, 'test'); for ($i = 0; $i < 400; $i++) { Contact::create([ 'tenant_id' => $this->tenant->id, 'phone' => sprintf('7999%07d', $i), 'name' => null, 'operator' => null, ]); } $campaign = Campaign::create([ 'tenant_id' => $this->tenant->id, 'status' => Campaign::STATUS_DRAFT, 'ad_text' => 'x', 'ad_link' => 'https://t.me/x', 'ord_category' => 'Размещение рекламы', 'budget_cap_rub' => '1000.00', 'audience_kind' => Campaign::AUDIENCE_BASE, 'planned_count' => 400, 'estimated_cost_rub' => '0.00', 'created_by' => $this->user->id, ]); Queue::fake(); $this->postJson("/api/telegram/campaigns/{$campaign->id}/launch") ->assertOk() ->assertJsonPath('status', Campaign::STATUS_QUEUED); Queue::assertPushed(RunTelegramCampaignJob::class); $wallet = AdWallet::where('tenant_id', $this->tenant->id)->first(); expect((string) $wallet->frozen_rub)->toBe('1000.00'); });