2026-06-28 13:49:57 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
use App\Jobs\Autopodbor\RunAutopodborStudyJob;
|
2026-06-30 04:18:46 +03:00
|
|
|
use App\Models\AutopodborCompetitor;
|
|
|
|
|
use App\Models\AutopodborRun;
|
|
|
|
|
use App\Models\AutopodborSource;
|
|
|
|
|
use App\Models\SystemSetting;
|
|
|
|
|
use App\Models\Tenant;
|
2026-06-28 13:49:57 +03:00
|
|
|
use App\Services\Autopodbor\Agent\CompetitorAgent;
|
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
2026-06-30 04:18:46 +03:00
|
|
|
use Tests\Concerns\SharesSupplierPdo;
|
|
|
|
|
use Tests\Doubles\EmptyCompetitorAgent;
|
2026-06-28 13:49:57 +03:00
|
|
|
|
2026-06-30 04:18:46 +03:00
|
|
|
uses(DatabaseTransactions::class, SharesSupplierPdo::class);
|
2026-06-28 13:49:57 +03:00
|
|
|
|
|
|
|
|
it('успешное изучение: источники + конкурент изучен + списание', function () {
|
|
|
|
|
$tenant = Tenant::factory()->create(['balance_rub' => '100000.00']);
|
2026-06-30 04:18:46 +03:00
|
|
|
DB::statement('SET app.current_tenant_id = '.$tenant->id);
|
2026-06-28 13:49:57 +03:00
|
|
|
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) {
|
2026-06-30 04:18:46 +03:00
|
|
|
expect($phone->identifier)->toMatch('/^7\d{10}$/')
|
|
|
|
|
->and(in_array($phone->phone_type, ['city', 'mobile', 'tollfree'], true))->toBeTrue(); // тип номера сохранён
|
2026-06-28 13:49:57 +03:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('пустой результат: status=empty, без списания', function () {
|
2026-06-30 04:18:46 +03:00
|
|
|
app()->bind(CompetitorAgent::class, EmptyCompetitorAgent::class);
|
2026-06-28 13:49:57 +03:00
|
|
|
$tenant = Tenant::factory()->create(['balance_rub' => '100000.00']);
|
2026-06-30 04:18:46 +03:00
|
|
|
DB::statement('SET app.current_tenant_id = '.$tenant->id);
|
2026-06-28 13:49:57 +03:00
|
|
|
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');
|
|
|
|
|
});
|