Files
portal/app/tests/Feature/Services/LeadRouterTest.php
T
Дмитрий 31929c9dd2 feat(services): LeadRouter — eligible Liderra projects matcher
Plan 2/5 Task 4 — sharing-model routing (spec §6): для входящего лида
возвращает Collection<Project> учитывая platform FK + active + workdays +
region (PhonePrefixService::phoneMatchesRegions) + delivered_today <
COALESCE(effective_daily_limit_today, daily_limit_target) +
tenant.balance_leads > 0. Сортировка created_at ASC, id ASC (детерминированно).

Параллельно расширил Project model fillable/casts на delivered_today
(колонка добавлена в schema v8.18 Plan 2 Task 1, но Project::class не
обновлён — без этого тесты Mass-Assignment'а ломались).

Покрытие: 9 it-blocks (sharing across tenants, paused, workdays, daily quota,
fallback to daily_limit_target, region filter, balance_leads zero, FK routing
по platform, deterministic sort). DatabaseTransactions context + set_config
(session-scoped) для очистки app.current_tenant_id — sharing-flow работает
поверх N tenant'ов, RLS bypass через postgres BYPASSRLS на dev.

PHPStan: 0 errors. Pint: clean. Pest: 9/9 PASS.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-10 18:50:28 +03:00

212 lines
7.8 KiB
PHP

<?php
declare(strict_types=1);
use App\Models\Project;
use App\Models\SupplierProject;
use App\Models\Tenant;
use App\Services\LeadRouter;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;
uses(DatabaseTransactions::class);
beforeEach(function (): void {
// Clear tenant context — LeadRouter operates without it (sharing across tenants).
// Use set_config (session-scoped, rolls back via DatabaseTransactions).
DB::statement("SELECT set_config('app.current_tenant_id', '0', true)");
});
it('returns matching active projects for B1 site supplier_project (sharing across tenants)', function (): void {
$supplier = SupplierProject::factory()->create([
'platform' => 'B1',
'signal_type' => 'site',
'unique_key' => 'vashinvestor.ru',
]);
$tenant1 = Tenant::factory()->create(['balance_leads' => 100]);
$tenant2 = Tenant::factory()->create(['balance_leads' => 100]);
$project1 = Project::factory()->create([
'tenant_id' => $tenant1->id,
'supplier_b1_project_id' => $supplier->id,
'signal_type' => 'site',
'signal_identifier' => 'vashinvestor.ru',
'is_active' => true,
'daily_limit_target' => 10,
'delivered_today' => 0,
'delivery_days_mask' => 127,
'region_mask' => 255,
'region_mode' => 'include',
]);
$project2 = Project::factory()->create([
'tenant_id' => $tenant2->id,
'supplier_b1_project_id' => $supplier->id,
'signal_type' => 'site',
'signal_identifier' => 'vashinvestor.ru',
'is_active' => true,
'daily_limit_target' => 10,
'delivered_today' => 0,
'delivery_days_mask' => 127,
'region_mask' => 255,
'region_mode' => 'include',
]);
$router = app(LeadRouter::class);
$matched = $router->matchEligibleProjects($supplier, '79991234567');
expect($matched)->toHaveCount(2);
expect($matched->pluck('id')->all())->toEqualCanonicalizing([$project1->id, $project2->id]);
});
it('skips paused project (is_active=false)', function (): void {
$supplier = SupplierProject::factory()->create(['platform' => 'B1', 'signal_type' => 'site']);
$tenant = Tenant::factory()->create(['balance_leads' => 100]);
Project::factory()->create([
'tenant_id' => $tenant->id,
'supplier_b1_project_id' => $supplier->id,
'signal_type' => 'site',
'signal_identifier' => 'example.com',
'is_active' => false,
]);
$router = app(LeadRouter::class);
expect($router->matchEligibleProjects($supplier, '79991234567'))->toHaveCount(0);
});
it('skips project where today is not in delivery_days_mask', function (): void {
$todayBit = 1 << (now()->isoWeekday() - 1);
$maskWithoutToday = 127 & ~$todayBit;
$supplier = SupplierProject::factory()->create(['platform' => 'B1', 'signal_type' => 'site']);
$tenant = Tenant::factory()->create(['balance_leads' => 100]);
Project::factory()->create([
'tenant_id' => $tenant->id,
'supplier_b1_project_id' => $supplier->id,
'signal_type' => 'site',
'signal_identifier' => 'example.com',
'is_active' => true,
'delivery_days_mask' => $maskWithoutToday,
]);
$router = app(LeadRouter::class);
expect($router->matchEligibleProjects($supplier, '79991234567'))->toHaveCount(0);
});
it('skips project where delivered_today >= effective_daily_limit_today', function (): void {
$supplier = SupplierProject::factory()->create(['platform' => 'B1', 'signal_type' => 'site']);
$tenant = Tenant::factory()->create(['balance_leads' => 100]);
Project::factory()->create([
'tenant_id' => $tenant->id,
'supplier_b1_project_id' => $supplier->id,
'signal_type' => 'site',
'signal_identifier' => 'example.com',
'is_active' => true,
'effective_daily_limit_today' => 5,
'delivered_today' => 5,
]);
$router = app(LeadRouter::class);
expect($router->matchEligibleProjects($supplier, '79991234567'))->toHaveCount(0);
});
it('falls back to daily_limit_target when effective_daily_limit_today is null', function (): void {
$supplier = SupplierProject::factory()->create(['platform' => 'B1', 'signal_type' => 'site']);
$tenant = Tenant::factory()->create(['balance_leads' => 100]);
Project::factory()->create([
'tenant_id' => $tenant->id,
'supplier_b1_project_id' => $supplier->id,
'signal_type' => 'site',
'signal_identifier' => 'example.com',
'is_active' => true,
'effective_daily_limit_today' => null,
'daily_limit_target' => 10,
'delivered_today' => 5,
]);
$router = app(LeadRouter::class);
expect($router->matchEligibleProjects($supplier, '79991234567'))->toHaveCount(1);
});
it('skips project where region_mode=include and region_mask does not include phone district', function (): void {
$supplier = SupplierProject::factory()->create(['platform' => 'B1', 'signal_type' => 'site']);
$tenant = Tenant::factory()->create(['balance_leads' => 100]);
Project::factory()->create([
'tenant_id' => $tenant->id,
'supplier_b1_project_id' => $supplier->id,
'signal_type' => 'site',
'signal_identifier' => 'example.com',
'is_active' => true,
'region_mask' => 1, // только Центральный округ
'region_mode' => 'include',
]);
$router = app(LeadRouter::class);
// 78121234567 = СПб (Северо-Западный, бит 2)
expect($router->matchEligibleProjects($supplier, '78121234567'))->toHaveCount(0);
});
it('skips project where tenant.balance_leads <= 0', function (): void {
$supplier = SupplierProject::factory()->create(['platform' => 'B1', 'signal_type' => 'site']);
$tenant = Tenant::factory()->create(['balance_leads' => 0]);
Project::factory()->create([
'tenant_id' => $tenant->id,
'supplier_b1_project_id' => $supplier->id,
'signal_type' => 'site',
'signal_identifier' => 'example.com',
'is_active' => true,
]);
$router = app(LeadRouter::class);
expect($router->matchEligibleProjects($supplier, '79991234567'))->toHaveCount(0);
});
it('routes through correct FK based on platform (B2 → supplier_b2_project_id)', function (): void {
$supplier = SupplierProject::factory()->create(['platform' => 'B2', 'signal_type' => 'site']);
$tenant = Tenant::factory()->create(['balance_leads' => 100]);
Project::factory()->create([
'tenant_id' => $tenant->id,
'supplier_b1_project_id' => null,
'supplier_b2_project_id' => $supplier->id,
'supplier_b3_project_id' => null,
'signal_type' => 'site',
'signal_identifier' => 'example.com',
'is_active' => true,
]);
$router = app(LeadRouter::class);
expect($router->matchEligibleProjects($supplier, '79991234567'))->toHaveCount(1);
});
it('orders results by created_at ASC (deterministic, spec §6 step 4)', function (): void {
$supplier = SupplierProject::factory()->create(['platform' => 'B1', 'signal_type' => 'site']);
$projectsCreated = collect();
for ($i = 0; $i < 3; $i++) {
$tenant = Tenant::factory()->create(['balance_leads' => 100]);
$projectsCreated->push(
Project::factory()->create([
'tenant_id' => $tenant->id,
'supplier_b1_project_id' => $supplier->id,
'signal_type' => 'site',
'signal_identifier' => 'example.com',
'is_active' => true,
'created_at' => now()->subDays(3 - $i),
])
);
}
$router = app(LeadRouter::class);
$matched = $router->matchEligibleProjects($supplier, '79991234567');
expect($matched->pluck('id')->all())->toBe($projectsCreated->pluck('id')->all());
});