Files
portal/app/tests/Feature/Autopodbor/RunProgressBindingTest.php
T
Дмитрий 428b7b0f01 feat(autopodbor): прогресс прогона — очередь и этапы (бэкенд)
Клиент во время сбора видит место в общей очереди (пока ждёт) и текущий
этап (пока идёт). Очередь — длина общей Redis-очереди (честное ожидание,
без RLS, одинаково dev/прод). Этапы пишет сам движок через RunProgressChannel:
поиск (анализ→справочники→федералы→сбор→отбор), изучение — по каждому элементу.

- миграция progress (jsonb) на autopodbor_runs + cast
- RunResource отдаёт queue_position (AutopodborQueue) и progress
- RunProgressChannel (синглтон), джобы привязывают его к своему прогону
- SearchStages + инструментовка LiveFindCompetitors и RealCompetitorAgent
- 16 тестов (Feature+Unit), все зелёные

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-05 04:31:01 +03:00

99 lines
4.2 KiB
PHP

<?php
declare(strict_types=1);
use App\Jobs\Autopodbor\RunAutopodborSearchJob;
use App\Jobs\Autopodbor\RunAutopodborStudyJob;
use App\Models\AutopodborCompetitor;
use App\Models\AutopodborRun;
use App\Models\SystemSetting;
use App\Models\Tenant;
use App\Services\Autopodbor\Agent\CompetitorAgent;
use App\Services\Autopodbor\Agent\Dto\FindCompetitorsRequest;
use App\Services\Autopodbor\Agent\Dto\FindCompetitorsResult;
use App\Services\Autopodbor\Agent\Dto\ResolveByNameRequest;
use App\Services\Autopodbor\Agent\Dto\ResolveByNameResult;
use App\Services\Autopodbor\Agent\Dto\StudyCompetitorRequest;
use App\Services\Autopodbor\Agent\Dto\StudyCompetitorResult;
use App\Services\Autopodbor\RunProgressChannel;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;
use Tests\Concerns\SharesSupplierPdo;
uses(DatabaseTransactions::class, SharesSupplierPdo::class);
it('джоба ПОИСКА привязывает канал к своему прогону (отметка этапа попадает именно в этот прогон)', function () {
$tenant = Tenant::factory()->create(['balance_rub' => '100000.00']);
DB::statement('SET app.current_tenant_id = '.$tenant->id);
SystemSetting::updateOrCreate(['key' => 'autopodbor_price_search_rub'], ['value' => '0', 'type' => 'decimal']);
SystemSetting::updateOrCreate(['key' => 'autopodbor_max_competitors'], ['value' => '15', 'type' => 'int']);
// Агент по ходу поиска отмечает этап через общий канал (как живой движок).
app()->bind(CompetitorAgent::class, fn () => new class implements CompetitorAgent
{
public function findCompetitors(FindCompetitorsRequest $r): FindCompetitorsResult
{
app(RunProgressChannel::class)->stage(2, 4, 'Ищем фирмы в справочниках');
return new FindCompetitorsResult([]);
}
public function studyCompetitor(StudyCompetitorRequest $r): StudyCompetitorResult
{
return new StudyCompetitorResult([]);
}
public function resolveByName(ResolveByNameRequest $r): ResolveByNameResult
{
return new ResolveByNameResult([]);
}
});
$run = AutopodborRun::create([
'tenant_id' => $tenant->id, 'kind' => 'search', 'status' => 'queued',
'region_code' => 16, 'params' => ['examples' => [], 'about_self' => [], 'include_federal' => false],
]);
app()->call([new RunAutopodborSearchJob($run->id), 'handle']);
expect($run->fresh()->progress)->toEqual(['stage' => 2, 'total' => 4, 'label' => 'Ищем фирмы в справочниках']);
});
it('джоба ИЗУЧЕНИЯ привязывает канал к своему прогону', function () {
$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' => '0', 'type' => 'decimal']);
app()->bind(CompetitorAgent::class, fn () => new class implements CompetitorAgent
{
public function findCompetitors(FindCompetitorsRequest $r): FindCompetitorsResult
{
return new FindCompetitorsResult([]);
}
public function studyCompetitor(StudyCompetitorRequest $r): StudyCompetitorResult
{
app(RunProgressChannel::class)->stage(1, 1, 'Изучаю фирму');
return new StudyCompetitorResult([]);
}
public function resolveByName(ResolveByNameRequest $r): ResolveByNameResult
{
return new ResolveByNameResult([]);
}
});
$comp = AutopodborCompetitor::create([
'tenant_id' => $tenant->id, 'name' => 'Фирма', 'dedup_key' => 'firma',
]);
$run = AutopodborRun::create([
'tenant_id' => $tenant->id, 'kind' => 'study', 'status' => 'queued',
'region_code' => 16, 'params' => [], 'competitor_id' => $comp->id,
]);
app()->call([new RunAutopodborStudyJob($run->id), 'handle']);
expect($run->fresh()->progress)->toEqual(['stage' => 1, 'total' => 1, 'label' => 'Изучаю фирму']);
});