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).
115 lines
4.9 KiB
PHP
115 lines
4.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 Carbon\Carbon;
|
|
|
|
it('uses snapshot before 21:00 MSK, snapshot_date = today', function () {
|
|
Carbon::setTestNow('2026-05-28 12:00:00', 'Europe/Moscow');
|
|
|
|
$tenant = Tenant::factory()->create(['balance_rub' => '500.00', 'frozen_by_balance_at' => null]);
|
|
$project = Project::factory()->for($tenant)->create([
|
|
'is_active' => false, // ЖИВОЕ состояние — paused
|
|
'delivery_days_mask' => 127,
|
|
'daily_limit_target' => 100,
|
|
'delivered_today' => 0,
|
|
]);
|
|
$sp = SupplierProject::factory()->create();
|
|
\DB::table('project_supplier_links')->insert([
|
|
'project_id' => $project->id, 'supplier_project_id' => $sp->id,
|
|
'platform' => $sp->platform, 'subject_code' => null,
|
|
]);
|
|
// SNAPSHOT за сегодня имеет проект → роутер должен вернуть, несмотря на is_active=false
|
|
\DB::table('project_routing_snapshots')->insert([
|
|
'snapshot_date' => '2026-05-28', 'project_id' => $project->id, 'tenant_id' => $tenant->id,
|
|
'daily_limit' => 10, 'delivery_days_mask' => 127, 'regions' => '{}',
|
|
'signal_type' => 'call', 'expected_volume' => 10, 'delivered_count' => 0,
|
|
'created_at' => now(),
|
|
]);
|
|
|
|
$matched = app(LeadRouter::class)->matchEligibleProjects($sp);
|
|
|
|
expect($matched)->toHaveCount(1); // ← это R-01 closure
|
|
Carbon::setTestNow();
|
|
});
|
|
|
|
it('uses snapshot after 21:00 MSK, snapshot_date = tomorrow', function () {
|
|
Carbon::setTestNow('2026-05-28 22:00:00', 'Europe/Moscow');
|
|
|
|
$tenant = Tenant::factory()->create(['balance_rub' => '500.00', 'frozen_by_balance_at' => null]);
|
|
$project = Project::factory()->for($tenant)->create([
|
|
'is_active' => true, 'delivery_days_mask' => 127,
|
|
'daily_limit_target' => 100, 'delivered_today' => 0,
|
|
]);
|
|
$sp = SupplierProject::factory()->create();
|
|
\DB::table('project_supplier_links')->insert([
|
|
'project_id' => $project->id, 'supplier_project_id' => $sp->id,
|
|
'platform' => $sp->platform, 'subject_code' => null,
|
|
]);
|
|
// Snapshot за СЕГОДНЯ (2026-05-28) НЕТ.
|
|
// Snapshot за ЗАВТРА (2026-05-29) есть.
|
|
\DB::table('project_routing_snapshots')->insert([
|
|
'snapshot_date' => '2026-05-29', 'project_id' => $project->id, 'tenant_id' => $tenant->id,
|
|
'daily_limit' => 10, 'delivery_days_mask' => 127, 'regions' => '{}',
|
|
'signal_type' => 'call', 'expected_volume' => 10, 'delivered_count' => 0,
|
|
'created_at' => now(),
|
|
]);
|
|
|
|
$matched = app(LeadRouter::class)->matchEligibleProjects($sp);
|
|
|
|
expect($matched)->toHaveCount(1); // после 21:00 МСК активен завтрашний snapshot
|
|
Carbon::setTestNow();
|
|
});
|
|
|
|
it('returns 0 if no snapshot exists for active date', function () {
|
|
Carbon::setTestNow('2026-05-28 12:00:00', 'Europe/Moscow');
|
|
|
|
$tenant = Tenant::factory()->create(['balance_rub' => '500.00']);
|
|
$project = Project::factory()->for($tenant)->create([
|
|
'is_active' => true, 'delivery_days_mask' => 127, 'daily_limit_target' => 10,
|
|
]);
|
|
$sp = SupplierProject::factory()->create();
|
|
\DB::table('project_supplier_links')->insert([
|
|
'project_id' => $project->id, 'supplier_project_id' => $sp->id,
|
|
'platform' => $sp->platform, 'subject_code' => null,
|
|
]);
|
|
// НЕТ snapshot за 2026-05-28.
|
|
|
|
$matched = app(LeadRouter::class)->matchEligibleProjects($sp);
|
|
|
|
expect($matched)->toHaveCount(0); // fail-loud, не fallback на live
|
|
Carbon::setTestNow();
|
|
});
|
|
|
|
it('limit comes from snapshot, not live projects.daily_limit_target', function () {
|
|
Carbon::setTestNow('2026-05-28 12:00:00', 'Europe/Moscow');
|
|
|
|
$tenant = Tenant::factory()->create(['balance_rub' => '500.00']);
|
|
$project = Project::factory()->for($tenant)->create([
|
|
'is_active' => true, 'delivery_days_mask' => 127,
|
|
'daily_limit_target' => 100, // живой лимит
|
|
'delivered_today' => 7,
|
|
]);
|
|
$sp = SupplierProject::factory()->create();
|
|
\DB::table('project_supplier_links')->insert([
|
|
'project_id' => $project->id, 'supplier_project_id' => $sp->id,
|
|
'platform' => $sp->platform, 'subject_code' => null,
|
|
]);
|
|
\DB::table('project_routing_snapshots')->insert([
|
|
'snapshot_date' => '2026-05-28', 'project_id' => $project->id, 'tenant_id' => $tenant->id,
|
|
'daily_limit' => 5, // ← snapshot лимит МЕНЬШЕ чем delivered_today=7
|
|
'delivery_days_mask' => 127, 'regions' => '{}',
|
|
'signal_type' => 'call', 'expected_volume' => 5, 'delivered_count' => 0,
|
|
'created_at' => now(),
|
|
]);
|
|
|
|
$matched = app(LeadRouter::class)->matchEligibleProjects($sp);
|
|
|
|
expect($matched)->toHaveCount(0); // delivered_today=7 >= snapshot.daily_limit=5
|
|
Carbon::setTestNow();
|
|
});
|