Files
portal/app/tests/Feature/Billing/BalancePreflightSweepJobTest.php
T
Дмитрий 53f9020653 feat(billing-v2-c): sweep-job заморозки + 4 mailable + cron 18:00 MSK
Task 1.4+1.5 Спека C. BalancePreflightSweepJob (chunkById всех тенантов,
переход active->frozen / frozen->active, идемпотентность, журнал balance_freeze_log
через pgsql_supplier) + BillingPreflightSweepCommand + cron billing:preflight-sweep
@18:00 MSK (SyncSupplierProjectsJob сдвинут 18:00->18:05). 4 Mailable
(Frozen/Reminder/Final/Unfrozen) + blade. Job шлёт Frozen/Unfrozen при переходах;
Reminder/Final (T+24h/T+72h) — классы готовы, рассылка по дате — следующий шаг.
11 Phase 1 billing-тестов GREEN. Адаптации под факт схемы: contact_email (не email),
organization_name (не name), is_active+daily_limit_target (не status+daily_limit).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-26 20:39:15 +03:00

63 lines
2.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
use App\Jobs\Billing\BalancePreflightSweepJob;
use App\Mail\BalanceFrozenMail;
use App\Models\PricingTier;
use App\Models\Project;
use App\Models\Tenant;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Tests\Concerns\SharesSupplierPdo;
// Изоляция: liderra_testing persistent (RefreshDatabase off). DatabaseTransactions
// откатывает default-pgsql после каждого теста; SharesSupplierPdo делает pgsql_supplier
// общим PDO с pgsql — иначе job-запись balance_freeze_log (pgsql_supplier) не видит
// незакоммиченного tenant и падает на FK (паттерн Спека B / AutoPauseFlowTest).
uses(DatabaseTransactions::class);
uses(SharesSupplierPdo::class);
beforeEach(function () {
// RLS-контекст (системный tenant 0) — паттерн supplier-тестов.
DB::statement("SELECT set_config('app.current_tenant_id', '0', true)");
PricingTier::query()->create(['tier_no' => 1, 'leads_in_tier' => null, 'price_per_lead_kopecks' => 5000, 'is_active' => true, 'effective_from' => now()]);
});
it('freezes tenant whose balance no longer covers daily limit', function () {
Mail::fake();
// 500₽ / 50₽ = 10 лидов; проекты хотят 25 → заморозка.
$tenant = Tenant::factory()->create(['balance_rub' => '500.00', 'frozen_by_balance_at' => null]);
Project::factory()->for($tenant)->create(['is_active' => true, 'daily_limit_target' => 25]);
(new BalancePreflightSweepJob)->handle();
expect($tenant->fresh()->frozen_by_balance_at)->not->toBeNull();
Mail::assertQueued(BalanceFrozenMail::class);
});
it('unfreezes tenant whose balance now covers daily limit', function () {
Mail::fake();
// 2000₽ / 50₽ = 40 лидов; хотят 25 → разморозка.
$tenant = Tenant::factory()->create(['balance_rub' => '2000.00', 'frozen_by_balance_at' => now()->subDay()]);
Project::factory()->for($tenant)->create(['is_active' => true, 'daily_limit_target' => 25]);
(new BalancePreflightSweepJob)->handle();
expect($tenant->fresh()->frozen_by_balance_at)->toBeNull();
});
it('is idempotent — does not re-freeze already frozen tenant', function () {
Mail::fake();
$frozenAt = now()->subDay();
$tenant = Tenant::factory()->create(['balance_rub' => '0.00', 'frozen_by_balance_at' => $frozenAt]);
Project::factory()->for($tenant)->create(['is_active' => true, 'daily_limit_target' => 25]);
(new BalancePreflightSweepJob)->handle();
// Дата заморозки не перезаписана, повторного письма нет.
expect($tenant->fresh()->frozen_by_balance_at->timestamp)->toBe($frozenAt->timestamp);
Mail::assertNotQueued(BalanceFrozenMail::class);
});