$tenantId, 'enabled' => true, 'ad_text' => 'Заходите в наш канал', 'ad_link' => 'https://t.me/example_channel', 'ord_category' => 'Размещение рекламы', 'budget_cap_rub' => $cap, 'daily_limit_rub' => $dailyLimit, ]); } /** Скармливает n телефонов с заданным сдвигом (для разных пачек — разные номера). */ function afFeed(int $tenantId, int $from, int $to): void { for ($i = $from; $i <= $to; $i++) { app(TelegramAutoAccumulator::class)->accumulate($tenantId, '7999'.str_pad((string) $i, 7, '0', STR_PAD_LEFT)); } } beforeEach(function () { Carbon::setTestNow('2026-07-28 10:00:00'); Queue::fake(); config(['client_tg.auto_batch_threshold' => 3]); config(['client_tg.sandbox' => false]); $this->tenant = Tenant::factory()->create(['balance_rub' => '10000.00']); DB::statement('SET LOCAL app.current_tenant_id = '.$this->tenant->id); // Клиентская смета за пачку из 3 показов (то, что списывается с баланса). $this->cost = app(TelegramTariffService::class)->clientEstimateRub(3); }); afterEach(fn () => Carbon::setTestNow()); it('в бою авто-путь списывает клиентскую смету и ставит в очередь', function () { afRule($this->tenant->id, cap: '2500.00'); afFeed($this->tenant->id, 1, 3); Queue::assertPushed(RunTelegramCampaignJob::class, 1); expect(Campaign::where('tenant_id', $this->tenant->id)->first()->status)->toBe(Campaign::STATUS_QUEUED); // Баланс упал ровно на клиентскую смету, списание отражено проводкой. expect((string) $this->tenant->fresh()->balance_rub)->toBe(bcsub('10000.00', $this->cost, 2)) ->and(BalanceTransaction::where('tenant_id', $this->tenant->id) ->where('type', BalanceTransaction::TYPE_TG_AD_CHARGE)->count())->toBe(1); }); it('денег НЕ хватает на смету → держит черновиком, без списания', function () { // Баланс на копейку меньше сметы — списать нельзя. DB::table('tenants')->where('id', $this->tenant->id) ->update(['balance_rub' => bcsub((string) $this->cost, '0.01', 2)]); afRule($this->tenant->id, cap: '2500.00'); afFeed($this->tenant->id, 1, 3); Queue::assertNothingPushed(); expect(Campaign::where('tenant_id', $this->tenant->id)->first()->status)->toBe(Campaign::STATUS_DRAFT); // Баланс не тронут, ни одной проводки списания. expect((string) $this->tenant->fresh()->balance_rub)->toBe(bcsub((string) $this->cost, '0.01', 2)) ->and(BalanceTransaction::where('tenant_id', $this->tenant->id) ->where('type', BalanceTransaction::TYPE_TG_AD_CHARGE)->count())->toBe(0); }); it('две пачки за день — счётчик суммирует, две списанные сметы', function () { $rule = afRule($this->tenant->id, cap: '2500.00', dailyLimit: '1000.00'); afFeed($this->tenant->id, 1, 3); // пачка 1 → очередь, новая пачка откроется afFeed($this->tenant->id, 4, 6); // пачка 2 → очередь Queue::assertPushed(RunTelegramCampaignJob::class, 2); $rule->refresh(); expect((string) $rule->spent_today_rub)->toBe(bcadd($this->cost, $this->cost, 2)); // 2 сметы // Баланс упал на две сметы, два списания. expect((string) $this->tenant->fresh()->balance_rub)->toBe(bcsub('10000.00', bcadd($this->cost, $this->cost, 2), 2)) ->and(BalanceTransaction::where('tenant_id', $this->tenant->id) ->where('type', BalanceTransaction::TYPE_TG_AD_CHARGE)->count())->toBe(2); }); it('в песочнице авто-путь денег не касается', function () { config(['client_tg.sandbox' => true]); afRule($this->tenant->id, cap: '2500.00', dailyLimit: '0.00'); afFeed($this->tenant->id, 1, 3); Queue::assertPushed(RunTelegramCampaignJob::class, 1); // Ни списаний, ни движения баланса — денег в песочнице не трогаем. expect((string) $this->tenant->fresh()->balance_rub)->toBe('10000.00') ->and(BalanceTransaction::where('tenant_id', $this->tenant->id)->count())->toBe(0); });