Files
portal/app/tests/Feature/Autopodbor/RunAutopodborResolveJobTest.php
T
Дмитрий a42ee07c42
Accessibility (Pa11y live) / a11y (push) Has been cancelled
SAST — Semgrep / Semgrep SAST scan (push) Has been cancelled
fix(autopodbor): выставлять tenant-контекст ДО чтения прогона в джобах (прод-сбой под RLS)
Джобы Search/Study/Resolve делали AutopodborRun::findOrFail() ДО set_config
app.current_tenant_id. На проде роль crm_app_user под FORCE RLS (на боевом
кластере ни одна роль не BYPASSRLS) не видит строку без tenant-контекста →
ModelNotFound, все три кнопки автоподбора падали. На dev маскировалось
суперюзером postgres. Теперь tenantId передаётся в dispatch и контекст
выставляется первым. 3 регресс-теста на порядок (set до чтения runs).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-07 12:22:22 +03:00

84 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
use App\Jobs\Autopodbor\RunAutopodborResolveJob;
use App\Models\AutopodborCompetitor;
use App\Models\AutopodborRun;
use App\Models\Tenant;
use App\Services\Autopodbor\Agent\CompetitorAgent;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;
use Tests\Concerns\SharesSupplierPdo;
use Tests\Doubles\EmptyCompetitorAgent;
uses(DatabaseTransactions::class, SharesSupplierPdo::class);
it('резолв по названию: кандидаты с origin=resolve, status=done, без списания', function () {
$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' => 'Окна Комфорт'],
]);
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);
});