Files
Investidor-V2/tests/Feature/GlobalSearchTest.php
T

56 lines
2.4 KiB
PHP

<?php
namespace Tests\Feature;
use App\Http\Controllers\InvestmentPortfolioController;
use App\Services\FederatedSearch;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\DB;
use Inertia\Testing\AssertableInertia as Assert;
use Tests\TestCase;
final class GlobalSearchTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
$this->withoutVite();
}
public function test_empty_and_overlong_queries(): void
{
$this->get('/pesquisa')->assertOk()->assertInertia(fn (Assert $page) => $page->component('portal/search')->where('results.total', 0));
$this->get('/pesquisa?q='.str_repeat('a', 121))->assertSessionHasErrors('q');
}
public function test_source_failure_recalculates_the_page_without_hiding_the_other_results(): void
{
$sources = [
'Documentos' => fn () => ['count' => 1, 'fetch' => fn () => throw new QueryException('testing', 'select 1', [], new \RuntimeException('Source unavailable'))],
'Páginas' => fn () => ['count' => 1, 'fetch' => fn ($offset, $limit) => array_slice([['title' => 'Como investir', 'url' => '/como-investir']], $offset, $limit)],
];
$result = app(FederatedSearch::class)->page($sources, 1);
$this->assertSame(1, $result['total']);
$this->assertSame(['Documentos'], $result['unavailable']);
$this->assertSame('Como investir', $result['data'][0]['title']);
}
public function test_public_search_uses_the_existing_portfolio_visibility_scope(): void
{
if (getenv('GLOBAL_SEARCH_QA') !== '1') {
$this->markTestSkipped('A prova com dados exige a base QA explicitamente seleccionada.');
}
$identity = DB::selectOne('select current_database() as name, inet_server_port() as port');
$this->assertSame('simulator_fiscal_delta_20261021_test', $identity->name);
$this->assertSame(55441, (int) $identity->port);
$record = InvestmentPortfolioController::visible()->first();
if (! $record) {
$this->markTestSkipped('No published portfolio records in isolated QA.');
}
$term = mb_substr($record->title, 0, 15);
$this->get('/pesquisa?q='.urlencode($term))->assertOk()->assertInertia(fn (Assert $page) => $page
->component('portal/search')->where('query', $term)
->where('results.total', fn ($total) => $total >= 1));
}
}