fix: complete InvesteMoz validation and error corrections

This commit is contained in:
2026-10-29 21:47:33 +02:00
parent 63147312e2
commit daf8623441
718 changed files with 127308 additions and 20959 deletions
+35 -22
View File
@@ -6,6 +6,8 @@ use App\Http\Controllers\InvestmentPortfolioController;
use App\Models\Opportunity;
use App\Models\User;
use App\Services\InvestmentComplements;
use App\Services\InvestmentCorridorContext;
use App\Services\OpportunityClassification;
use App\Services\PartnershipExchange;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;
@@ -52,29 +54,31 @@ final class InvestmentComplementsTest extends TestCase
public function test_complements_survive_draft_confirmation_and_consumer_snapshot_without_overwriting_title(): void
{
$actor = User::factory()->create(); $this->actingAs($actor);
$actor = User::factory()->create();
$this->actingAs($actor);
$origin = $this->source();
$option = collect(app(InvestmentComplements::class)->options($origin))->firstWhere('code', 'storage');
$target = $option['opportunities'][0]['key'];
$need = ['origin' => $origin, 'code' => 'spinning', 'target' => null];
$offer = ['origin' => $origin, 'code' => 'storage', 'target' => $target];
$query = http_build_query(['complement_origin' => $origin, 'complement_code' => 'storage', 'complement_target' => $target]);
$this->get('/investimentos/iniciar?'.$query)->assertOk()->assertInertia(fn(Assert $p) => $p->where('entryComplements.0.target.key', $target)->where('opportunityChoices.0.opportunity.key', $target));
$this->get('/investimentos/iniciar?'.$query)->assertOk()->assertInertia(fn (Assert $p) => $p->where('entryComplements.0.target.key', $target)->where('opportunityChoices.0.opportunity.key', $target));
$id = $this->postJson('/investimentos', ['title' => 'QA título deliberado dos complementos', 'client_key' => (string) Str::uuid(), 'reuse_consent' => true])->assertCreated()->json('id');
$version = $this->getJson('/investimentos/'.$id.'/contexto')->json('case.version');
$body = ['version' => $version, 'selection' => [], 'complements' => [$need, $offer], 'opportunity_choices' => [['source' => $target, 'portfolio_origins' => [substr($origin, 10)]]]];
$this->postJson('/investimentos/'.$id.'/contexto/rascunho', [...$body, 'draft_sequence' => 1])->assertOk();
$this->get('/investimentos/'.$id.'/contexto/editar')->assertOk()->assertInertia(fn(Assert $p) => $p->has('entryComplements', 2)->where('entryComplements.0.kind', 'need')->where('entryComplements.1.target.key', $target));
$this->get('/investimentos/'.$id.'/contexto/editar')->assertOk()->assertInertia(fn (Assert $p) => $p->has('entryComplements', 2)->where('entryComplements.0.kind', 'need')->where('entryComplements.1.target.key', $target));
$plan = $this->postJson('/investimentos/'.$id.'/contexto/previa', $body)->assertOk()->json();
$this->assertSame('QA título deliberado dos complementos', $plan['fields']['project_title']);
$control = [...$body, 'preview_hash' => $plan['preview_hash'], 'operation_key' => (string) Str::uuid(), 'confirm' => true];
$this->postJson('/investimentos/'.$id.'/contexto/confirmar', $control)->assertOk();
$this->postJson('/investimentos/'.$id.'/contexto/confirmar', $control)->assertOk()->assertJsonPath('replayed', true);
$this->flushSession(); $this->actingAs($actor);
$this->flushSession();
$this->actingAs($actor);
$view = $this->getJson('/investimentos/'.$id.'/contexto')->assertOk()->json();
$this->assertSame($plan['complements'], $view['context']['complements']);
$this->assertSame($plan['complements'], json_decode($view['prefill']['simulator']['context_applicability_snapshot'], true)['complements']);
$this->get('/investimentos/'.$id)->assertOk()->assertInertia(fn(Assert $p) => $p->has('investment_context.complements', 2));
$this->get('/investimentos/'.$id)->assertOk()->assertInertia(fn (Assert $p) => $p->has('investment_context.complements', 2));
$this->actingAs(User::factory()->create())->getJson('/investimentos/'.$id.'/contexto')->assertForbidden();
}
@@ -95,7 +99,8 @@ final class InvestmentComplementsTest extends TestCase
public function test_concrete_training_and_finance_services_are_reused_only_for_their_documented_origin(): void
{
$actor = User::factory()->create(); $this->actingAs($actor);
$actor = User::factory()->create();
$this->actingAs($actor);
$origin = $this->source();
foreach (['training' => 'Formação e certificação', 'finance' => 'Financiamento'] as $type => $title) {
$service = app(PartnershipExchange::class)->save($actor, ['client_key' => (string) Str::uuid(), 'title' => $title, 'kind' => 'offering', 'partnership_type' => $type,
@@ -110,15 +115,18 @@ final class InvestmentComplementsTest extends TestCase
$this->assertSame('service', $resolved[0]['kind']);
}
$other = InvestmentPortfolioController::visible()->where('content->code', 'COR-01')->firstOrFail();
$this->assertSame([], collect(app(InvestmentComplements::class)->options('portfolio:'.$other->id))->flatMap(fn($r) => $r['services'])->all());
$this->assertSame([], collect(app(InvestmentComplements::class)->options('portfolio:'.$other->id))->flatMap(fn ($r) => $r['services'])->all());
}
public function test_explicit_complementary_project_relationship_and_geography_control_the_available_fiche(): void
{
$records = Opportunity::published()->orderBy('id')->take(2)->get();
[$main, $target] = [$records[0], $records[1]];
$main->summary = 'QA investimento principal'; $main->save();
$target->title = 'Regadio QA relacionado'; $target->summary = 'Regadio para a cadeia de valor QA'; $target->save();
$main->summary = 'QA investimento principal';
$main->save();
$target->title = 'Regadio QA relacionado';
$target->summary = 'Regadio para a cadeia de valor QA';
$target->save();
$projects = InvestmentPortfolioController::visible()->whereNotNull('project_id')->take(2)->pluck('project_id');
DB::table('opportunity_projects')->whereIn('opportunity_id', [$main->id, $target->id])->delete();
DB::table('opportunity_projects')->insert([['opportunity_id' => $main->id, 'project_id' => $projects[0]], ['opportunity_id' => $target->id, 'project_id' => $projects[1]]]);
@@ -127,9 +135,10 @@ final class InvestmentComplementsTest extends TestCase
$service = app(InvestmentComplements::class);
$options = $service->options('opportunity:'.$main->id);
$this->assertSame('opportunity:'.$target->id, collect($options)->firstWhere('code', 'irrigation')['opportunities'][0]['key']);
$sofala = collect(app(\App\Services\InvestmentCorridorContext::class)->provinces('beira'))->firstWhere('source_code', '07')->id;
$gaza = app(\App\Services\InvestmentCorridorContext::class)->provinces('limpopo')[0]->id;
$target->primary_territory_id = $gaza; $target->save();
$sofala = collect(app(InvestmentCorridorContext::class)->provinces('beira'))->firstWhere('source_code', '07')->id;
$gaza = app(InvestmentCorridorContext::class)->provinces('limpopo')[0]->id;
$target->primary_territory_id = $gaza;
$target->save();
$filtered = $service->options('opportunity:'.$main->id, $sofala);
$this->assertSame([], collect($filtered)->firstWhere('code', 'irrigation')['opportunities']);
$batch = $this->postJson('/complementos-investimento', ['contexts' => [['origin' => 'opportunity:'.$main->id, 'territory_id' => $sofala]]])->assertOk()->json();
@@ -140,7 +149,8 @@ final class InvestmentComplementsTest extends TestCase
public function test_previous_relation_is_preserved_as_history_when_an_offer_is_withdrawn(): void
{
$service = app(InvestmentComplements::class); $origin = $this->source();
$service = app(InvestmentComplements::class);
$origin = $this->source();
$target = collect($service->options($origin))->firstWhere('code', 'storage')['opportunities'][0]['key'];
$input = [['origin' => $origin, 'code' => 'storage', 'target' => $target]];
$previous = $service->resolve($input);
@@ -159,28 +169,31 @@ final class InvestmentComplementsTest extends TestCase
{
$this->actingAs(User::factory()->create());
$id = $this->postJson('/investimentos', ['title' => 'QA retirada de complemento', 'client_key' => (string) Str::uuid(), 'reuse_consent' => true])->assertCreated()->json('id');
$origin = $this->source(); $selection = (string) Str::uuid();
$origin = $this->source();
$selection = (string) Str::uuid();
$entry = app(InvestmentComplements::class)->resolve([['origin' => $origin, 'code' => 'spinning']])[0];
$query = http_build_query(['complement_origin' => $origin, 'complement_code' => 'spinning', 'selection_key' => $selection]);
$tag = 'complement:'.hash('sha256', $entry['key'].'@'.$selection);
$this->get('/investimentos/'.$id.'/contexto/editar?'.$query)->assertOk()->assertInertia(fn(Assert $p) => $p->where('entrySources.0', $tag)->has('entryComplements', 1));
$this->get('/investimentos/'.$id.'/contexto/editar?'.$query)->assertOk()->assertInertia(fn (Assert $p) => $p->where('entrySources.0', $tag)->has('entryComplements', 1));
$this->postJson('/investimentos/'.$id.'/contexto/rascunho', ['version' => $this->getJson('/investimentos/'.$id.'/contexto')->json('case.version'),
'draft_sequence' => 1, 'selection' => [], 'complements' => [], 'entry_sources' => [$tag], 'declarations' => ['project_title' => 'QA edição a conservar']])->assertOk();
$this->get('/investimentos/'.$id.'/contexto/editar?'.$query)->assertOk()->assertInertia(fn(Assert $p) => $p->has('entryComplements', 0)->where('draft.declarations.project_title', 'QA edição a conservar'));
$this->get('/investimentos/'.$id.'/contexto/editar?'.$query)->assertOk()->assertInertia(fn (Assert $p) => $p->has('entryComplements', 0)->where('draft.declarations.project_title', 'QA edição a conservar'));
$query = http_build_query(['complement_origin' => $origin, 'complement_code' => 'spinning', 'selection_key' => (string) Str::uuid()]);
$this->get('/investimentos/'.$id.'/contexto/editar?'.$query)->assertOk()->assertInertia(fn(Assert $p) => $p->has('entryComplements', 1)->where('draft.declarations.project_title', 'QA edição a conservar'));
$this->get('/investimentos/'.$id.'/contexto/editar?'.$query)->assertOk()->assertInertia(fn (Assert $p) => $p->has('entryComplements', 1)->where('draft.declarations.project_title', 'QA edição a conservar'));
}
public function test_public_chain_contract_preserves_both_documentary_references_in_the_selected_relation(): void
{
$records = Opportunity::published()->orderBy('id')->take(2)->get();
[$main, $target] = [$records[0], $records[1]];
$main->summary = 'QA produção e processamento'; $main->save();
$target->summary = 'QA processamento da mesma cadeia'; $target->save();
$main->summary = 'QA produção e processamento';
$main->save();
$target->summary = 'QA processamento da mesma cadeia';
$target->save();
$row = ['target_type' => 'chain', 'target_id' => (string) Str::uuid(), 'label' => 'Cadeia sintética QA', 'state' => 'preliminary', 'source_reference' => 'Referência documental sintética QA'];
$classification = \Mockery::mock(\App\Services\OpportunityClassification::class);
$classification->shouldReceive('publicRows')->andReturnUsing(fn($record) => in_array($record->id, [$main->id, $target->id], true) ? [$row] : []);
$this->app->instance(\App\Services\OpportunityClassification::class, $classification);
$classification = \Mockery::mock(OpportunityClassification::class);
$classification->shouldReceive('publicRows')->andReturnUsing(fn ($record) => in_array($record->id, [$main->id, $target->id], true) ? [$row] : []);
$this->app->instance(OpportunityClassification::class, $classification);
$service = app(InvestmentComplements::class);
$options = $service->options('opportunity:'.$main->id);
$offer = collect(collect($options)->firstWhere('code', 'processing')['opportunities'])->firstWhere('key', 'opportunity:'.$target->id);