put( 'supplier:session', ['phpsessid' => 'test', 'csrf' => 'test'], now()->addHour(), ); } beforeEach(function (): void { Mail::fake(); Bus::fake([RouteSupplierLeadJob::class]); app()->bind(RefreshSupplierSessionJob::class, fn () => new class { public function handle(): void { putSupplierSession(); } }); Cache::store('redis')->forget('supplier:csv_reconcile'); putSupplierSession(); config(['services.supplier.portal_url' => 'https://crm.bp-gr.ru']); config(['services.supplier.alert_email' => 'ops@liderra.ru']); }); afterEach(function (): void { Cache::store('redis')->forget('supplier:csv_reconcile'); }); /** * 3-колоночный CSV «Запрос номеров»: Name;Tag;Phone. * * @param array $rows */ function csvBody(array $rows): string { $out = "Name;Tag;Phone\n"; foreach ($rows as $r) { $out .= "{$r['project']};tag;{$r['phone']}\n"; } return $out; } /** * Мокает весь async-флоу отчёта (реальные endpoint'ы — discovery T3 2026-05-19): * POST /admin/report/save-report → "OK" * GET /admin/report/load-reports → array [{id, title, status:"1", ...}] (id извлекается по title-match) * GET /admin/report/getfile?id=N → raw CSV * * Title включает фактически использованные dateFrom/dateTo — захватываем их из save-report body * и возвращаем тот же диапазон в load-reports, чтобы матч requestNumbersReport состоялся. */ function fakeReportFlow(string $csv): void { $captured = ['from' => '', 'to' => '']; Http::fake([ 'crm.bp-gr.ru/admin/report/save-report' => function (Request $r) use (&$captured) { $body = $r->data(); $captured['from'] = (string) ($body['reportFilter']['dateFrom'] ?? ''); $captured['to'] = (string) ($body['reportFilter']['dateTo'] ?? ''); return Http::response('OK', 200); }, 'crm.bp-gr.ru/admin/report/load-reports' => function () use (&$captured) { $title = sprintf('Запрос номеров с %s по %s', $captured['from'], $captured['to']); return Http::response([ ['id' => '700001', 'title' => $title, 'status' => '1', 'is_file' => '1', 'percent' => '100'], ], 200); }, 'crm.bp-gr.ru/admin/report/getfile*' => Http::response($csv, 200), ]); } function runCsvReconcile(): void { app(CsvReconcileJob::class)->handle( app(SupplierPortalClient::class), app(SupplierCsvParser::class), app(Mailer::class), ); } it('no missing leads — status=ok, no recovery, no alert', function (): void { for ($i = 0; $i < 10; $i++) { SupplierLead::create([ 'supplier_project_id' => null, 'platform' => 'B1', 'phone' => "7999000000{$i}", 'vid' => 800000 + $i, 'raw_payload' => ['project' => 'B1_a.com', 'phone' => "7999000000{$i}"], 'received_at' => now()->subHour(), 'source' => 'webhook', ]); } $rows = []; for ($i = 0; $i < 10; $i++) { $rows[] = ['project' => 'B1_a.com', 'phone' => "7999000000{$i}"]; } fakeReportFlow(csvBody($rows)); runCsvReconcile(); $log = DB::table('supplier_csv_reconcile_log')->latest('id')->first(); expect($log->status)->toBe('ok'); expect((int) $log->total_csv_rows)->toBe(10); expect((int) $log->matched_count)->toBe(10); expect((int) $log->recovered_count)->toBe(0); Mail::assertNothingSent(); Bus::assertNothingDispatched(); }); it('1 missing of 10 (drift 10%) — recovery + drift alert', function (): void { for ($i = 0; $i < 9; $i++) { SupplierLead::create([ 'supplier_project_id' => null, 'platform' => 'B1', 'phone' => "7999111000{$i}", 'vid' => 810000 + $i, 'raw_payload' => ['project' => 'B1_a.com', 'phone' => "7999111000{$i}"], 'received_at' => now()->subHour(), 'source' => 'webhook', ]); } $rows = []; for ($i = 0; $i < 10; $i++) { $rows[] = ['project' => 'B1_a.com', 'phone' => "7999111000{$i}"]; } fakeReportFlow(csvBody($rows)); runCsvReconcile(); $log = DB::table('supplier_csv_reconcile_log')->latest('id')->first(); expect($log->status)->toBe('drift_alert'); expect((float) $log->drift_ratio)->toBeGreaterThan(0.05); expect((int) $log->recovered_count)->toBe(1); $recovered = SupplierLead::where('source', 'csv_recovery')->first(); expect($recovered)->not->toBeNull(); expect($recovered->vid)->toBeNull(); expect($recovered->recovered_from_csv_at)->not->toBeNull(); Mail::assertSent(CsvDriftAlertMail::class, 1); Bus::assertDispatched(RouteSupplierLeadJob::class, 1); }); it('1 missing of 100 (drift 1%) — recovery without alert', function (): void { for ($i = 0; $i < 99; $i++) { SupplierLead::create([ 'supplier_project_id' => null, 'platform' => 'B1', 'phone' => '79992'.str_pad((string) $i, 6, '0', STR_PAD_LEFT), 'vid' => 820000 + $i, 'raw_payload' => ['project' => 'B1_a.com', 'phone' => '79992'.str_pad((string) $i, 6, '0', STR_PAD_LEFT)], 'received_at' => now()->subHour(), 'source' => 'webhook', ]); } $rows = []; for ($i = 0; $i < 100; $i++) { $rows[] = ['project' => 'B1_a.com', 'phone' => '79992'.str_pad((string) $i, 6, '0', STR_PAD_LEFT)]; } fakeReportFlow(csvBody($rows)); runCsvReconcile(); $log = DB::table('supplier_csv_reconcile_log')->latest('id')->first(); expect($log->status)->toBe('ok'); expect((int) $log->recovered_count)->toBe(1); Mail::assertNothingSent(); }); it('dedup is keyed by (phone, project) — same phone on different project is NOT a duplicate', function (): void { SupplierLead::create([ 'supplier_project_id' => null, 'platform' => 'B1', 'phone' => '79995550000', 'vid' => 830000, 'raw_payload' => ['project' => 'B1_a.com', 'phone' => '79995550000'], 'received_at' => now()->subHour(), 'source' => 'webhook', ]); fakeReportFlow(csvBody([ ['project' => 'B1_a.com', 'phone' => '79995550000'], ['project' => 'B2_b.com', 'phone' => '79995550000'], ])); runCsvReconcile(); $log = DB::table('supplier_csv_reconcile_log')->latest('id')->first(); expect((int) $log->matched_count)->toBe(1); expect((int) $log->recovered_count)->toBe(1); }); it('empty CSV — status=ok, drift=0', function (): void { fakeReportFlow("Name;Tag;Phone\n"); runCsvReconcile(); $log = DB::table('supplier_csv_reconcile_log')->latest('id')->first(); expect($log->status)->toBe('ok'); expect((int) $log->total_csv_rows)->toBe(0); }); it('overlap lock held — job skips, no log row', function (): void { $countBefore = DB::table('supplier_csv_reconcile_log')->count(); $lock = Cache::store('redis')->lock('supplier:csv_reconcile', 600); $lock->get(); try { runCsvReconcile(); } finally { $lock->release(); } expect(DB::table('supplier_csv_reconcile_log')->count())->toBe($countBefore); }); it('SupplierTransientException — status=failed, error recorded, rethrown', function (): void { Http::fake(['crm.bp-gr.ru/*' => Http::response('Server Error', 500)]); expect(fn () => runCsvReconcile())->toThrow(SupplierTransientException::class); $log = DB::table('supplier_csv_reconcile_log')->latest('id')->first(); expect($log->status)->toBe('failed'); expect($log->error_message)->toContain('500'); });