create(['balance_rub' => '100000.00']); DB::statement('SET app.current_tenant_id = '.$tenant->id); $run = AutopodborRun::create([ 'tenant_id' => $tenant->id, 'kind' => 'resolve', 'status' => 'queued', 'region_code' => 16, 'params' => ['name' => 'Окна Комфорт'], ]); app()->call([new RunAutopodborResolveJob($run->id), 'handle']); expect($run->fresh()->status)->toBe('done') ->and($run->fresh()->price_rub_charged)->toBeNull() ->and(AutopodborCompetitor::where('search_run_id', $run->id)->where('origin', 'resolve')->count())->toBeGreaterThan(0) ->and((string) $tenant->fresh()->balance_rub)->toBe('100000.00'); }); 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); $run = AutopodborRun::create([ 'tenant_id' => $tenant->id, 'kind' => 'resolve', 'status' => 'queued', 'region_code' => 16, 'params' => ['name' => 'Несуществующая Фирма XYZ'], ]); app()->call([new RunAutopodborResolveJob($run->id), 'handle']); expect($run->fresh()->status)->toBe('empty') ->and($run->fresh()->price_rub_charged)->toBeNull(); }); it('выставляет tenant-контекст ДО чтения прогона (иначе прод под RLS = ModelNotFound)', function () { // Регресс прод-сбоя 07.07.2026 (resolve). См. пояснение в RunAutopodborStudyJobTest. app()->bind(CompetitorAgent::class, EmptyCompetitorAgent::class); $tenant = Tenant::factory()->create(['balance_rub' => '100000.00']); DB::statement('SET app.current_tenant_id = '.$tenant->id); $run = AutopodborRun::create([ 'tenant_id' => $tenant->id, 'kind' => 'resolve', 'status' => 'queued', 'region_code' => 16, 'params' => ['name' => 'Окна Комфорт'], ]); $order = []; DB::listen(function ($q) use (&$order) { if (str_contains($q->sql, 'app.current_tenant_id')) { $order[] = 'set'; } elseif (preg_match('/from\s+"?autopodbor_runs"?/i', $q->sql)) { $order[] = 'read_run'; } }); app()->call([new RunAutopodborResolveJob($run->id, $tenant->id), 'handle']); $firstSet = array_search('set', $order, true); $firstRead = array_search('read_run', $order, true); expect($firstSet)->not->toBeFalse('джоба ни разу не выставила app.current_tenant_id'); expect($firstRead)->not->toBeFalse('джоба не читала autopodbor_runs'); expect($firstSet)->toBeLessThan($firstRead); });