2026-06-23 09:17:21 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
use App\Jobs\SyncSupplierProjectJob;
|
2026-07-02 11:07:22 +03:00
|
|
|
use App\Models\PricingTier;
|
2026-06-23 09:17:21 +03:00
|
|
|
use App\Models\Project;
|
|
|
|
|
use App\Models\Tenant;
|
|
|
|
|
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);
|
|
|
|
|
uses(SharesSupplierPdo::class);
|
|
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
|
Queue::fake();
|
|
|
|
|
DB::statement("SELECT set_config('app.current_tenant_id', '0', true)");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// G: ВСЕ пути синхронизации должны уважать preflight_blocked_at — заблокированный
|
|
|
|
|
// (за нехваткой баланса) проект НЕ должен уезжать заказом к поставщику ни через
|
|
|
|
|
// одиночную правку, ни через ручную «Синхронизировать», ни через возобновление.
|
|
|
|
|
// Зеркалит create-гард (ProjectService::create) и фильтр ночного sweep.
|
|
|
|
|
|
|
|
|
|
// --- update ---
|
|
|
|
|
|
|
|
|
|
it('does not sync blocked project to supplier on update', function () {
|
|
|
|
|
$tenant = Tenant::factory()->create();
|
|
|
|
|
$user = User::factory()->create(['tenant_id' => $tenant->id]);
|
|
|
|
|
$project = Project::factory()->for($tenant)->create([
|
|
|
|
|
'is_active' => true,
|
|
|
|
|
'preflight_blocked_at' => now(),
|
|
|
|
|
'regions' => [77],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->actingAs($user)->patchJson("/api/projects/{$project->id}", [
|
|
|
|
|
'regions' => [78],
|
|
|
|
|
])->assertOk();
|
|
|
|
|
|
|
|
|
|
Queue::assertNotPushed(SyncSupplierProjectJob::class);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('syncs unblocked project to supplier on update', function () {
|
|
|
|
|
$tenant = Tenant::factory()->create();
|
|
|
|
|
$user = User::factory()->create(['tenant_id' => $tenant->id]);
|
|
|
|
|
$project = Project::factory()->for($tenant)->create([
|
|
|
|
|
'is_active' => true,
|
|
|
|
|
'preflight_blocked_at' => null,
|
|
|
|
|
'regions' => [77],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->actingAs($user)->patchJson("/api/projects/{$project->id}", [
|
|
|
|
|
'regions' => [78],
|
|
|
|
|
])->assertOk();
|
|
|
|
|
|
|
|
|
|
Queue::assertPushed(SyncSupplierProjectJob::class);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// --- toggle-active (возобновление) ---
|
|
|
|
|
|
2026-07-02 11:07:22 +03:00
|
|
|
it('does not launch or sync project on resume when balance insufficient', function () {
|
|
|
|
|
// Тариф: 50₽/лид. Баланс 100₽ → capacity 2; daily_limit_target=30 → 409.
|
|
|
|
|
PricingTier::create([
|
|
|
|
|
'tier_no' => 1,
|
|
|
|
|
'leads_in_tier' => null,
|
|
|
|
|
'price_per_lead_kopecks' => 5000,
|
|
|
|
|
'is_active' => true,
|
|
|
|
|
'effective_from' => now(),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$tenant = Tenant::factory()->create(['balance_rub' => '100.00', 'delivered_in_month' => 0]);
|
2026-06-23 09:17:21 +03:00
|
|
|
$user = User::factory()->create(['tenant_id' => $tenant->id]);
|
|
|
|
|
$project = Project::factory()->for($tenant)->create([
|
|
|
|
|
'is_active' => false,
|
2026-07-02 11:07:22 +03:00
|
|
|
'daily_limit_target' => 30,
|
2026-06-23 09:17:21 +03:00
|
|
|
'preflight_blocked_at' => now(),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->actingAs($user)->patchJson("/api/projects/{$project->id}/toggle-active", [
|
|
|
|
|
'is_active' => true,
|
2026-07-02 11:07:22 +03:00
|
|
|
])->assertStatus(409)->assertJsonPath('error', 'balance_insufficient');
|
2026-06-23 09:17:21 +03:00
|
|
|
|
|
|
|
|
Queue::assertNotPushed(SyncSupplierProjectJob::class);
|
2026-07-02 11:07:22 +03:00
|
|
|
expect((bool) $project->fresh()->is_active)->toBeFalse();
|
2026-06-23 09:17:21 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('syncs unblocked project to supplier on toggle-active resume', function () {
|
|
|
|
|
$tenant = Tenant::factory()->create();
|
|
|
|
|
$user = User::factory()->create(['tenant_id' => $tenant->id]);
|
|
|
|
|
$project = Project::factory()->for($tenant)->create([
|
|
|
|
|
'is_active' => false,
|
|
|
|
|
'preflight_blocked_at' => null,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->actingAs($user)->patchJson("/api/projects/{$project->id}/toggle-active", [
|
|
|
|
|
'is_active' => true,
|
|
|
|
|
])->assertOk();
|
|
|
|
|
|
|
|
|
|
Queue::assertPushed(SyncSupplierProjectJob::class);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// --- ручная «Синхронизировать» (triggerSync) ---
|
|
|
|
|
|
|
|
|
|
it('does not sync blocked project to supplier on manual sync', function () {
|
|
|
|
|
$tenant = Tenant::factory()->create();
|
|
|
|
|
$user = User::factory()->create(['tenant_id' => $tenant->id]);
|
|
|
|
|
$project = Project::factory()->for($tenant)->create([
|
|
|
|
|
'is_active' => true,
|
|
|
|
|
'preflight_blocked_at' => now(),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->actingAs($user)->postJson("/api/projects/{$project->id}/sync")->assertStatus(202);
|
|
|
|
|
|
|
|
|
|
Queue::assertNotPushed(SyncSupplierProjectJob::class);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('syncs unblocked project to supplier on manual sync', function () {
|
|
|
|
|
$tenant = Tenant::factory()->create();
|
|
|
|
|
$user = User::factory()->create(['tenant_id' => $tenant->id]);
|
|
|
|
|
$project = Project::factory()->for($tenant)->create([
|
|
|
|
|
'is_active' => true,
|
|
|
|
|
'preflight_blocked_at' => null,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->actingAs($user)->postJson("/api/projects/{$project->id}/sync")->assertStatus(202);
|
|
|
|
|
|
|
|
|
|
Queue::assertPushed(SyncSupplierProjectJob::class);
|
|
|
|
|
});
|