bind(CompetitorAgent::class, FakeCompetitorAgent::class); $tenant = Tenant::factory()->create(['balance_rub' => '100000.00']); DB::statement('SET app.current_tenant_id = '.$tenant->id); SystemSetting::updateOrCreate(['key' => 'autopodbor_price_study_rub'], ['value' => '900', 'type' => 'decimal']); $searchRun = AutopodborRun::create(['tenant_id' => $tenant->id, 'kind' => 'search', 'status' => 'done', 'region_code' => 16, 'params' => []]); $comp = AutopodborCompetitor::create(['tenant_id' => $tenant->id, 'search_run_id' => $searchRun->id, 'name' => 'Окна Комфорт', 'dedup_key' => 'site:okna-komfort-kzn.ru', 'site_url' => 'okna-komfort-kzn.ru']); $run = AutopodborRun::create(['tenant_id' => $tenant->id, 'kind' => 'study', 'status' => 'queued', 'region_code' => 16, 'competitor_id' => $comp->id, 'params' => []]); app()->call([new RunAutopodborStudyJob($run->id), 'handle']); expect($run->fresh()->status)->toBe('done') ->and($run->fresh()->price_rub_charged)->toBe('900.00') ->and($comp->fresh()->studied_at)->not->toBeNull() ->and($comp->fresh()->study_run_id)->toBe($run->id) ->and(AutopodborSource::where('competitor_id', $comp->id)->count())->toBeGreaterThan(0) ->and((string) $tenant->fresh()->balance_rub)->toBe('99100.00'); // источники нормализованы (телефоны 7xxxxxxxxxx) $phone = AutopodborSource::where('competitor_id', $comp->id)->where('signal_type', 'call')->first(); if ($phone) { expect($phone->identifier)->toMatch('/^7\d{10}$/') ->and(in_array($phone->phone_type, ['city', 'mobile', 'tollfree'], true))->toBeTrue(); // тип номера сохранён } }); it('пустой результат: status=empty, без списания', function () { app()->bind(CompetitorAgent::class, EmptyCompetitorAgent::class); $tenant = Tenant::factory()->create(['balance_rub' => '100000.00']); DB::statement('SET app.current_tenant_id = '.$tenant->id); SystemSetting::updateOrCreate(['key' => 'autopodbor_price_study_rub'], ['value' => '900', 'type' => 'decimal']); $searchRun = AutopodborRun::create(['tenant_id' => $tenant->id, 'kind' => 'search', 'status' => 'done', 'region_code' => 16, 'params' => []]); $comp = AutopodborCompetitor::create(['tenant_id' => $tenant->id, 'search_run_id' => $searchRun->id, 'name' => 'Пусто', 'dedup_key' => 'site:empty.ru', 'site_url' => 'empty.ru']); $run = AutopodborRun::create(['tenant_id' => $tenant->id, 'kind' => 'study', 'status' => 'queued', 'region_code' => 16, 'competitor_id' => $comp->id, 'params' => []]); app()->call([new RunAutopodborStudyJob($run->id), 'handle']); expect($run->fresh()->status)->toBe('empty') ->and($run->fresh()->price_rub_charged)->toBeNull() ->and((string) $tenant->fresh()->balance_rub)->toBe('100000.00'); }); it('джоба передаёт is_federal в StudyCompetitorRequest', function () { $captured = null; $spy = new class($captured) implements CompetitorAgent { public function __construct(public &$captured) {} public function findCompetitors(\App\Services\Autopodbor\Agent\Dto\FindCompetitorsRequest $r): \App\Services\Autopodbor\Agent\Dto\FindCompetitorsResult { throw new \LogicException('n/a'); } public function resolveByName(\App\Services\Autopodbor\Agent\Dto\ResolveByNameRequest $r): \App\Services\Autopodbor\Agent\Dto\ResolveByNameResult { throw new \LogicException('n/a'); } public function studyCompetitor(\App\Services\Autopodbor\Agent\Dto\StudyCompetitorRequest $r): \App\Services\Autopodbor\Agent\Dto\StudyCompetitorResult { $this->captured = $r->competitor; return new \App\Services\Autopodbor\Agent\Dto\StudyCompetitorResult([]); } }; app()->instance(CompetitorAgent::class, $spy); $tenant = Tenant::factory()->create(['balance_rub' => '100000.00']); DB::statement('SET app.current_tenant_id = '.$tenant->id); SystemSetting::updateOrCreate(['key' => 'autopodbor_price_study_rub'], ['value' => '900', 'type' => 'decimal']); $searchRun = AutopodborRun::create(['tenant_id' => $tenant->id, 'kind' => 'search', 'status' => 'done', 'region_code' => 29, 'params' => []]); $comp = AutopodborCompetitor::create(['tenant_id' => $tenant->id, 'search_run_id' => $searchRun->id, 'name' => 'Банк ДОМ.РФ', 'dedup_key' => 'site:domrfbank.ru', 'site_url' => 'domrfbank.ru', 'is_federal' => true]); $run = AutopodborRun::create(['tenant_id' => $tenant->id, 'kind' => 'study', 'status' => 'queued', 'region_code' => 29, 'competitor_id' => $comp->id, 'params' => []]); app()->call([new RunAutopodborStudyJob($run->id), 'handle']); expect($spy->captured['is_federal'])->toBeTrue(); });