fix: harmonizar pesquisa e navegacao dos catalogos em QA

This commit is contained in:
2026-10-20 18:53:41 +02:00
parent 1350b4df73
commit fd341b627e
34 changed files with 2311 additions and 1310 deletions
+92
View File
@@ -0,0 +1,92 @@
<?php
namespace Tests\Feature;
use App\Services\InvestmentCatalogue;
use App\Support\IsolatedQaDatabase;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\Support\OpportunitySearchFixture;
use Tests\TestCase;
class OpportunitySearchTest extends TestCase
{
use DatabaseTransactions;
public function createApplication(): Application
{
$app=parent::createApplication();
if (! IsolatedQaDatabase::allowsSchemaChanges()) throw new \LogicException('Isolated QA required before test transaction');
return $app;
}
public function test_one_park_selector_matches_public_associations_not_proximity_or_shared_territory(): void
{
$f=OpportunitySearchFixture::create();
$q=['park_id'=>$f['zone']->id,'q'=>$f['query']];
$this->getJson('/api/v1/investment/search?'.http_build_query($q))->assertOk()
->assertJsonPath('total',1)->assertJsonPath('data.0.id',$f['associated']->id);
$this->assertSame(1,app(InvestmentCatalogue::class)->map($q)['unlocated_count']??count(app(InvestmentCatalogue::class)->map($q)['unlocated']));
$this->getJson('/api/v1/investment/search?'.http_build_query([...$q,'currency'=>'MZN']))->assertOk()->assertJsonPath('total',0);
$this->getJson('/api/v1/investment/search?'.http_build_query(['association_type'=>'park','association_target'=>$f['zone']->id]))
->assertOk()->assertJsonPath('total',1)->assertJsonPath('data.0.id',$f['associated']->id);
}
public function test_historical_and_current_preparation_with_equal_numbers_are_not_equivalent(): void
{
$f=OpportunitySearchFixture::create();
$this->getJson('/api/v1/investment/search?'.http_build_query(['q'=>$f['query'],'preparation'=>'historical:2']))
->assertOk()->assertJsonPath('total',1)->assertJsonPath('data.0.id',$f['associated']->id);
$this->getJson('/api/v1/investment/search?'.http_build_query(['q'=>$f['query'],'preparation'=>'stage:concept']))
->assertOk()->assertJsonPath('total',1)->assertJsonPath('data.0.id',$f['nearby']->id);
}
public function test_amount_requires_currency_and_combines_with_sector_location_and_modality(): void
{
$f=OpportunitySearchFixture::create();
$this->getJson('/api/v1/investment/search?max_amount=500000')->assertUnprocessable()->assertJsonValidationErrors('currency');
$q=['q'=>$f['query'],'sector'=>'turismo','province_id'=>$f['province']->id,
'category'=>'investment_project','preparation'=>'historical:2','max_amount'=>'500000','currency'=>'USD'];
$this->getJson('/api/v1/investment/search?'.http_build_query($q))->assertOk()->assertJsonPath('total',1)->assertJsonPath('data.0.id',$f['associated']->id);
$this->getJson('/api/v1/investment/search?'.http_build_query([...$q,'max_amount'=>'1']))->assertOk()->assertJsonPath('total',0);
$this->getJson('/api/v1/investment/search?'.http_build_query([...$q,'category'=>'business_sale']))->assertOk()->assertJsonPath('total',0);
}
public function test_zone_options_are_not_filtered_by_opportunity_text_or_modality(): void
{
$f=OpportunitySearchFixture::create();
$this->getJson('/api/v1/investment/references?q=turismo&category=investment_project')->assertOk()
->assertJsonFragment(['id'=>$f['zone']->id,'name'=>$f['zone']->name,'category'=>'industrial_park']);
$this->getJson('/api/v1/investment/search?'.http_build_query(['park_id'=>$f['zone']->id,'association_type'=>'park','association_target'=>$f['other_zone']->id]))
->assertUnprocessable()->assertJsonValidationErrors('park_id');
}
public function test_list_map_and_export_keep_the_same_public_park_results(): void
{
$f = OpportunitySearchFixture::create();
$query = http_build_query(['q' => $f['query'], 'park_id' => $f['zone']->id]);
$this->get('/oportunidades?'.$query)->assertOk()->assertInertia(fn ($page) => $page
->component('investment/catalogue')->where('results.total', 1)
->where('results.data.0.id', $f['associated']->id));
$csv = $this->get('/api/v1/investment/export?'.$query)->assertOk()->streamedContent();
$this->assertStringContainsString($f['associated']->title, $csv);
$this->assertStringNotContainsString($f['nearby']->title, $csv);
$this->assertStringNotContainsString($f['private']->title, $csv);
}
public function test_public_catalogue_consumers_keep_their_own_result_collections(): void
{
foreach ([
'/carteira?q=SINTETICO-INEXISTENTE' => ['portfolio/index', 'records.total'],
'/zonas?q=SINTETICO-INEXISTENTE' => ['investment/zones', 'records.total'],
'/documentos?q=SINTETICO-INEXISTENTE' => ['portal/services', 'resources.total'],
'/legislacao?q=SINTETICO-INEXISTENTE' => ['portal/services', 'resources.total'],
'/educacao?q=SINTETICO-INEXISTENTE' => ['investment/catalogue', 'results.total'],
'/negocios?q=SINTETICO-INEXISTENTE' => ['investment/business-marketplace', 'results.meta.total'],
] as $url => [$component, $total]) {
$this->get($url)->assertOk()->assertInertia(fn ($page) => $page->component($component)->where($total, 0));
}
$this->get('/clusters')->assertRedirect('/carteira?kind=cluster');
$this->get('/corredores')->assertRedirect('/carteira?kind=corridor');
}
}
@@ -0,0 +1,49 @@
<?php
namespace Tests\Support;
use App\Models\InvestmentZone;
use App\Models\Opportunity;
use App\Models\Sector;
use App\Models\User;
use App\Services\InvestmentGeography;
use App\Services\OpportunityMaturity;
use App\Support\IsolatedQaDatabase;
use Illuminate\Support\Str;
final class OpportunitySearchFixture
{
public static function create(): array
{
if (! IsolatedQaDatabase::allowsSchemaChanges()) throw new \LogicException('Isolated QA only');
$province = app(InvestmentGeography::class)->publicReferences()->where('location_type', 'province')->where('name', 'Inhambane')->first();
if (! $province) throw new \LogicException('QA requires the existing public Inhambane reference');
$sector = Sector::where('slug', 'turismo')->firstOrFail();
$editor = User::factory()->create(); $reviewer = User::factory()->create();
$zone = InvestmentZone::query()->create(['id'=>(string)Str::uuid(), 'code'=>'SINT-QA-'.Str::random(8),
'name'=>'SINTÉTICO QA — Parque Industrial de Balama', 'category'=>'industrial_park', 'physical_category'=>'industrial_park',
'publication_status'=>'published','validation_status'=>'validated', 'editor_id'=>$editor->id,'reviewer_id'=>$reviewer->id,
'published_at'=>now(), 'source_evidence'=>[['source_type'=>'promoter','url'=>'https://example.invalid/synthetic-qa','supports'=>'SINTÉTICO QA: não constitui informação sobre o parque real.']]]);
$other = InvestmentZone::query()->create(['id'=>(string)Str::uuid(),'code'=>'SINT-QA-'.Str::random(8),'name'=>'SINTÉTICO QA — Outro parque',
'category'=>'industrial_park','physical_category'=>'industrial_park','publication_status'=>'published','validation_status'=>'validated','editor_id'=>$editor->id,'reviewer_id'=>$reviewer->id,
'published_at'=>now(),'source_evidence'=>[['source_type'=>'promoter','url'=>'https://example.invalid/synthetic-qa','supports'=>'SINTÉTICO QA: outra associação.']]]);
$prefix='SINTÉTICO QA turismo '.Str::upper(Str::random(8));
$create = function(string $name, string $purpose, string $currency, int $level, ?string $stage=null, ?string $target=null, bool $private=false) use ($sector,$province,$zone,$prefix) {
$row=['related_id'=>$target??$zone->id,'relation_type'=>'located_in','classification'=>['target_type'=>'park','purpose'=>$purpose,
'disclosure'=>'public','requested_state'=>'preliminary','source_reference'=>'SINTÉTICO QA — ensaio de pesquisa','citation'=>'Fixture local']];
return Opportunity::forceCreate(['reference'=>'SINT-QA-'.Str::random(10),'slug'=>'sintetico-qa-'.Str::uuid(),'title'=>$prefix.' '.$name,
'summary'=>'Dados sintéticos para ensaio exclusivo em QA.','sector_id'=>$sector->id,'province'=>'Inhambane','primary_territory_id'=>$province->id,
'maturity_level'=>$level,'maturity_label'=>$stage?OpportunityMaturity::STAGES[$stage]:OpportunityMaturity::LEGACY_LABELS[$level],
'estimated_investment_amount'=>'300000.00','estimated_investment_currency'=>$currency,'opportunity_category'=>'investment_project',
'status'=>$private?'draft':'published','published_at'=>$private?null:now(),
'workflow_context'=>['publication'=>['visibility'=>'public','classification'=>[$row],
'maturity'=>['catalogue_version'=>$stage?OpportunityMaturity::VERSION:OpportunityMaturity::LEGACY,'stage'=>$stage]]]]);
};
return ['query'=>$prefix,'zone'=>$zone,'other_zone'=>$other,'province'=>$province,
'associated'=>$create('associada','park_operation','USD',2),
'nearby'=>$create('apenas próxima','proximity','USD',2,'concept'),
'same_territory'=>$create('mesmo território','administrative_boundary','MZN',4,'feasibility'),
'other_park'=>$create('outro parque','park_operation','USD',4,'feasibility',$other->id),
'private'=>$create('privada','park_operation','USD',2,null,null,true)];
}
}
+63 -4
View File
@@ -1,5 +1,9 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import {
catalogueDetailHref,
catalogueReturnHref,
} from '../../resources/js/lib/catalogue-context.ts';
import {
portalNavigation,
legacyPortalNavigation,
@@ -51,7 +55,10 @@ void test('management and optional modules follow permissions and feature flags'
void test('detail URLs and filtered catalogue pages retain the current group', () => {
// Preserve the retained catalogue's detail/filter matching independently
// of the reconciled shell's new grouping names.
const groups = legacyPortalNavigation({ authenticated: true, manager: true });
const groups = legacyPortalNavigation({
authenticated: true,
manager: true,
});
for (const url of [
'/carteira/project/fixture',
'/zonas?category=zee',
@@ -87,8 +94,60 @@ void test('detail URLs and filtered catalogue pages retain the current group', (
void test('reconciled navigation selects discovery, realization and private support correctly', () => {
const groups = portalNavigation({ authenticated: false, manager: false });
assert.equal(currentNavigationGroup(groups, '/oportunidades/fixture'), 'discovery');
assert.equal(currentNavigationGroup(groups, '/simulador-fiscal'), 'realization');
assert.equal(currentNavigationGroup(groups, '/balcao-virtual/dossiers/fixture'), 'support');
assert.equal(
currentNavigationGroup(groups, '/oportunidades/fixture'),
'discovery',
);
assert.equal(
currentNavigationGroup(groups, '/simulador-fiscal'),
'realization',
);
assert.equal(
currentNavigationGroup(groups, '/balcao-virtual/dossiers/fixture'),
'support',
);
assert.equal(currentNavigationGroup(groups, '/management/crm/leads'), null);
});
void test('discovery keeps four entry points and catalogue destinations without duplicates', () => {
const groups = portalNavigation({
authenticated: false,
manager: false,
businesses: true,
education: true,
});
const links = [];
const collect = (items) =>
items.forEach((item) => {
if (item.href) links.push(item.href);
if (item.children) collect(item.children);
});
collect(groups);
assert.equal(new Set(links).size, links.length);
for (const href of [
'/investimentos/iniciar',
'/oportunidades',
'/sectores',
'/oportunidades?view=map',
'/carteira',
'/zonas',
'/negocios',
'/educacao',
'/documentos',
'/legislacao',
])
assert.ok(links.includes(href), href);
});
void test('catalogue detail and return preserve the search inside the local catalogue', () => {
const query = 'q=turismo+em+Inhambane&kind=cluster&province_id=fixture';
const href = catalogueDetailHref('/carteira/fixture', query);
assert.equal(catalogueReturnHref('/carteira', href), '/carteira?' + query);
assert.equal(
catalogueReturnHref(
'/zonas',
'/zonas/fixture?catalogue_query=https%3A%2F%2Foutside.invalid',
),
'/zonas?https%3A%2F%2Foutside.invalid=',
);
});