create(['balance_rub' => '1000.00']); DB::statement("SET LOCAL app.current_tenant_id = ".$tenant->id); $run = AutopodborRun::create(['tenant_id' => $tenant->id, 'kind' => 'search', 'status' => 'running', 'params' => []]); $svc = app(AutopodborChargeService::class); $svc->chargeForRun($run, '300.00'); $svc->chargeForRun($run->fresh(), '300.00'); // повтор НЕ должен списать второй раз expect((string) $tenant->fresh()->balance_rub)->toBe('700.00') ->and($run->fresh()->price_rub_charged)->not->toBeNull() ->and(BalanceTransaction::where('type', 'autopodbor_charge')->where('related_id', $run->id)->count())->toBe(1); }); it('не списывает при нехватке баланса (бросает, баланс цел)', function () { $tenant = Tenant::factory()->create(['balance_rub' => '100.00']); DB::statement("SET LOCAL app.current_tenant_id = ".$tenant->id); $run = AutopodborRun::create(['tenant_id' => $tenant->id, 'kind' => 'study', 'status' => 'running', 'params' => []]); expect(fn () => app(AutopodborChargeService::class)->chargeForRun($run, '300.00')) ->toThrow(\App\Exceptions\Billing\InsufficientBalanceException::class); expect((string) $tenant->fresh()->balance_rub)->toBe('100.00') ->and(BalanceTransaction::where('related_id', $run->id)->count())->toBe(0); });