9540090bba
Code-review Important: Carbon::now() resolved against process TZ (UTC quirk
из memory). Reset cron в 00:00 МСК — mismatch вызвал бы off-by-one на
рубеже полуночи. Тест синхронизирован (now('Europe/Moscow')) — иначе
mismatch test/service near midnight. + комментарий про unreachable default
match arm (защищён на DB-уровне через chk_supplier_projects_platform).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
214 lines
7.9 KiB
PHP
214 lines
7.9 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 {
|
|
// Mirror LeadRouter's МСК alignment to avoid off-by-one near midnight when
|
|
// process TZ (UTC) and Europe/Moscow disagree on ISO day-of-week.
|
|
$todayBit = 1 << (now('Europe/Moscow')->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());
|
|
});
|