e8db184e99
LeadRouter SQL переписан на JOIN с project_routing_snapshots по active_slepok_date:
до 21:00 МСК = today, после 21:00 МСК = today+1. is_active / delivery_days_mask /
daily_limit / regions / signal_type / signal_identifier берутся из snapshot.
Из live projects — только delivered_today (счётчик остатка лимита). Из tenants —
balance_rub (live auto-pause при нулевом балансе).
Active snapshot date вычисляется в PHP (метод activeSnapshotDate()) и
передаётся в SQL как параметр — тестируемо через Carbon::setTestNow,
исключает дрейф между PHP- и DB-часами.
Fail-loud: Log::error('lead_router.no_snapshot_for_active_date', ...) если
по активной дате слепка вообще нет ни одной строки snapshot'а (cron не отработал).
Closes R-01, R-04, R-06, R-07, R-08, R-15.
Partial: R-02 (через шеринг), R-09 (race), R-10 (editable identifier) — закрываются Task 2.6+.
Plan: docs/superpowers/plans/2026-05-26-slepok-routing-protection.md §Task 2.5
Spec: docs/superpowers/specs/2026-05-26-slepok-routing-protection-design.md §4.2.3
Tests added:
- tests/Feature/LeadRouter/SnapshotRoutingTest.php (4 tests, all GREEN locally)
Tests patched (downstream — добавлен createRoutingSnapshotFromProject() helper):
- tests/Pest.php — global helper createRoutingSnapshotFromProject()
- tests/Feature/LeadRouter/BalanceFilterTest.php (2/2 GREEN)
- tests/Feature/Services/LeadRouterTest.php (10/10 GREEN)
- tests/Feature/Jobs/RouteSupplierLeadJobTest.php (14/14 GREEN)
- tests/Feature/Supplier/DirectPlatformTest.php (6/6 GREEN)
- tests/Feature/Supplier/RouteSupplierLeadJobBillingTest.php (3/3 GREEN)
- tests/Feature/Supplier/SupplierConnectionTest.php (5/5 GREEN)
- tests/Feature/Integration/SupplierLeadFlowTest.php (2/2 GREEN)
- tests/Feature/Pd/DealCreatePdLogTest.php (2/2 GREEN)
Each test file isolated regression: GREEN. Combined run 49/50 with 1 flake on
quirk #77 (Faker unique domainName + cross-connection pgsql/pgsql_supplier
DatabaseTransactions scope mismatch) — pre-existing, NOT regression от Task 2.5.
Patched via 7 parallel Sonnet subagents per Pravila §15.1; controller-verified
isolated + combined regression (latter caught 1 subagent over-application:
paused project in SupplierLeadFlowTest получил snapshot, что нарушило логику
теста — fixed inline, по semantic match with SnapshotBackfillCommand SQL
WHERE p.is_active = true).
222 lines
9.3 KiB
PHP
222 lines
9.3 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;
|
||
use Tests\Concerns\SharesSupplierPdo;
|
||
|
||
uses(DatabaseTransactions::class);
|
||
uses(SharesSupplierPdo::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)");
|
||
});
|
||
|
||
// `linkProjectToSupplier` helper now lives in tests/Pest.php — single source.
|
||
|
||
it('returns project linked via pivot to the supplier_project', function (): void {
|
||
$tenant = Tenant::factory()->create(['balance_leads' => 100, 'balance_rub' => '1000.00']);
|
||
$sp = SupplierProject::query()->create([
|
||
'platform' => 'B1', 'signal_type' => 'site', 'unique_key' => 'r.ru',
|
||
'subject_code' => 82, 'current_limit' => 0, 'sync_status' => 'ok',
|
||
]);
|
||
$project = Project::factory()->create([
|
||
'tenant_id' => $tenant->id, 'is_active' => true,
|
||
'daily_limit_target' => 10, 'delivered_today' => 0, 'delivery_days_mask' => 127,
|
||
]);
|
||
linkProjectToSupplier($project, $sp);
|
||
createRoutingSnapshotFromProject($project);
|
||
|
||
$matched = app(LeadRouter::class)->matchEligibleProjects($sp);
|
||
|
||
expect($matched)->toHaveCount(1)
|
||
->and($matched->first()->id)->toBe($project->id);
|
||
});
|
||
|
||
it('excludes project NOT linked to this supplier_project', function (): void {
|
||
$tenant = Tenant::factory()->create(['balance_leads' => 100]);
|
||
$sp = SupplierProject::query()->create([
|
||
'platform' => 'B1', 'signal_type' => 'site', 'unique_key' => 'r2.ru',
|
||
'subject_code' => 82, 'current_limit' => 0, 'sync_status' => 'ok',
|
||
]);
|
||
Project::factory()->create([
|
||
'tenant_id' => $tenant->id, 'is_active' => true,
|
||
'daily_limit_target' => 10, 'delivered_today' => 0, 'delivery_days_mask' => 127,
|
||
]); // не линкуем
|
||
|
||
expect(app(LeadRouter::class)->matchEligibleProjects($sp))->toHaveCount(0);
|
||
});
|
||
|
||
it('excludes inactive project, project at limit, and zero-balance tenant', function (): void {
|
||
$sp = SupplierProject::query()->create([
|
||
'platform' => 'B1', 'signal_type' => 'site', 'unique_key' => 'r3.ru',
|
||
'subject_code' => 82, 'current_limit' => 0, 'sync_status' => 'ok',
|
||
]);
|
||
|
||
$t1 = Tenant::factory()->create(['balance_leads' => 100]);
|
||
$inactive = Project::factory()->create(['tenant_id' => $t1->id, 'is_active' => false, 'daily_limit_target' => 10, 'delivered_today' => 0, 'delivery_days_mask' => 127]);
|
||
linkProjectToSupplier($inactive, $sp);
|
||
createRoutingSnapshotFromProject($inactive);
|
||
|
||
$atLimit = Project::factory()->create(['tenant_id' => $t1->id, 'is_active' => true, 'daily_limit_target' => 5, 'delivered_today' => 5, 'delivery_days_mask' => 127]);
|
||
linkProjectToSupplier($atLimit, $sp);
|
||
createRoutingSnapshotFromProject($atLimit);
|
||
|
||
$t0 = Tenant::factory()->create(['balance_leads' => 0, 'balance_rub' => 0]);
|
||
$broke = Project::factory()->create(['tenant_id' => $t0->id, 'is_active' => true, 'daily_limit_target' => 10, 'delivered_today' => 0, 'delivery_days_mask' => 127]);
|
||
linkProjectToSupplier($broke, $sp);
|
||
createRoutingSnapshotFromProject($broke);
|
||
|
||
expect(app(LeadRouter::class)->matchEligibleProjects($sp))->toHaveCount(0);
|
||
});
|
||
|
||
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 = Project::factory()->create([
|
||
'tenant_id' => $tenant->id,
|
||
'signal_type' => 'site',
|
||
'signal_identifier' => 'example.com',
|
||
'is_active' => false,
|
||
]);
|
||
linkProjectToSupplier($project, $supplier);
|
||
createRoutingSnapshotFromProject($project);
|
||
|
||
$router = app(LeadRouter::class);
|
||
expect($router->matchEligibleProjects($supplier))->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 = Project::factory()->create([
|
||
'tenant_id' => $tenant->id,
|
||
'signal_type' => 'site',
|
||
'signal_identifier' => 'example.com',
|
||
'is_active' => true,
|
||
'delivery_days_mask' => $maskWithoutToday,
|
||
]);
|
||
linkProjectToSupplier($project, $supplier);
|
||
createRoutingSnapshotFromProject($project);
|
||
|
||
$router = app(LeadRouter::class);
|
||
expect($router->matchEligibleProjects($supplier))->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 = Project::factory()->create([
|
||
'tenant_id' => $tenant->id,
|
||
'signal_type' => 'site',
|
||
'signal_identifier' => 'example.com',
|
||
'is_active' => true,
|
||
'effective_daily_limit_today' => 5,
|
||
'delivered_today' => 5,
|
||
]);
|
||
linkProjectToSupplier($project, $supplier);
|
||
createRoutingSnapshotFromProject($project);
|
||
|
||
$router = app(LeadRouter::class);
|
||
expect($router->matchEligibleProjects($supplier))->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, 'balance_rub' => '1000.00']);
|
||
|
||
$project = Project::factory()->create([
|
||
'tenant_id' => $tenant->id,
|
||
'signal_type' => 'site',
|
||
'signal_identifier' => 'example.com',
|
||
'is_active' => true,
|
||
'effective_daily_limit_today' => null,
|
||
'daily_limit_target' => 10,
|
||
'delivered_today' => 5,
|
||
]);
|
||
linkProjectToSupplier($project, $supplier);
|
||
createRoutingSnapshotFromProject($project, dailyLimit: $project->daily_limit_target);
|
||
|
||
$router = app(LeadRouter::class);
|
||
expect($router->matchEligibleProjects($supplier))->toHaveCount(1);
|
||
});
|
||
|
||
it('skips project where tenant has zero in BOTH balance_leads AND balance_rub (Plan 4 dual-balance)', function (): void {
|
||
// Plan 4 Task 4: фильтр расширен на (balance_leads > 0 OR balance_rub > 0).
|
||
// Tenant с обоими нулями — реально невыдачный, должен скипаться.
|
||
$supplier = SupplierProject::factory()->create(['platform' => 'B1', 'signal_type' => 'site']);
|
||
$tenant = Tenant::factory()->create(['balance_leads' => 0, 'balance_rub' => '0.00']);
|
||
|
||
$project = Project::factory()->create([
|
||
'tenant_id' => $tenant->id,
|
||
'signal_type' => 'site',
|
||
'signal_identifier' => 'example.com',
|
||
'is_active' => true,
|
||
]);
|
||
linkProjectToSupplier($project, $supplier);
|
||
createRoutingSnapshotFromProject($project);
|
||
|
||
$router = app(LeadRouter::class);
|
||
expect($router->matchEligibleProjects($supplier))->toHaveCount(0);
|
||
});
|
||
|
||
it('includes project when balance_leads=0 BUT balance_rub > 0 (Plan 4 dual-balance rub-only tenant)', function (): void {
|
||
// Plan 4 Task 4: rub-only tenant ДОЛЖЕН пройти LeadRouter; LedgerService
|
||
// сам резолвит prepaid/rub в transaction'е.
|
||
$supplier = SupplierProject::factory()->create(['platform' => 'B1', 'signal_type' => 'site']);
|
||
$tenant = Tenant::factory()->create(['balance_leads' => 0, 'balance_rub' => '1000.00']);
|
||
|
||
$project = Project::factory()->create([
|
||
'tenant_id' => $tenant->id,
|
||
'signal_type' => 'site',
|
||
'signal_identifier' => 'example.com',
|
||
'is_active' => true,
|
||
]);
|
||
linkProjectToSupplier($project, $supplier);
|
||
createRoutingSnapshotFromProject($project);
|
||
|
||
$router = app(LeadRouter::class);
|
||
$eligible = $router->matchEligibleProjects($supplier);
|
||
expect($eligible)->toHaveCount(1);
|
||
expect($eligible->first()->id)->toBe($project->id);
|
||
});
|
||
|
||
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, 'balance_rub' => '1000.00']);
|
||
$project = Project::factory()->create([
|
||
'tenant_id' => $tenant->id,
|
||
'signal_type' => 'site',
|
||
'signal_identifier' => 'example.com',
|
||
'is_active' => true,
|
||
'created_at' => now()->subDays(3 - $i),
|
||
]);
|
||
linkProjectToSupplier($project, $supplier);
|
||
createRoutingSnapshotFromProject($project);
|
||
$projectsCreated->push($project);
|
||
}
|
||
|
||
$router = app(LeadRouter::class);
|
||
$matched = $router->matchEligibleProjects($supplier);
|
||
|
||
expect($matched->pluck('id')->all())->toBe($projectsCreated->pluck('id')->all());
|
||
});
|