Files
Investidor-V2/tests/Feature/InvestmentContextContinuityTest.php
T
Claude 2c03a0091e
tests / ci (push) Canceled after 0s
Criar Hotel: a mudança de sector feita pelo investidor no editor continua directa (C10)
O sector escolhido pelo próprio investidor no editor do dossier substitui a
actividade, como aprovado em C10; o que chega de outra referência ou da
página de entrada continua a ser só proposta. Retirar a actividade também
passa a exigir a escolha explícita.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MzJmxFy3izprXr5dSq91jM
2026-12-04 21:11:45 +02:00

153 lines
11 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\Sector;
use App\Models\User;
use App\Services\InvestmentZones;
use App\Services\InvestorPath;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Tests\TestCase;
class InvestmentContextContinuityTest extends TestCase
{
use DatabaseTransactions;
protected function setUp(): void
{
parent::setUp();
$this->withoutVite();
$db = DB::selectOne('select current_database() as name, inet_server_port() as port');
$this->assertSame('simulator_fiscal_delta_20261021_test', $db->name);
$this->assertSame(55441, (int) $db->port);
config(['investor-path.enabled' => true, 'suitecrm.enabled' => false, 'investmoz_ai.enabled' => false]);
}
private function dossier(User $user, string $title): string
{
return $this->actingAs($user)->postJson('/investimentos', ['title' => $title, 'client_key' => (string) Str::uuid(), 'reuse_consent' => true])->assertCreated()->json('id');
}
public function test_same_dossier_context_reuses_linked_draft_and_preserves_account_and_project_boundaries(): void
{
$owner = User::factory()->create();
$a = $this->dossier($owner, 'A');
$b = $this->dossier($owner, 'B');
foreach ([[$a, 'MZN', 'national'], [$b, 'USD', 'foreign']] as [$id,$currency,$origin]) {
$this->actingAs($owner)->postJson('/api/v1/simulation-drafts', ['context' => $id, 'personal_snapshot' => ['investor_origin' => $origin, 'tax_residency_country' => 'ZA', 'person_type' => 'collective'], 'economic_snapshot' => ['purpose' => 'simulate_only', 'project_title' => $id, 'currency' => $currency, 'expected_start_date' => '2026-01-01']], ['Idempotency-Key' => (string) Str::uuid()])->assertCreated();
$v = $this->getJson('/investimentos/'.$id.'/contexto')->assertOk();
$v->assertJsonPath('prefill.simulator.currency', $currency)->assertJsonPath('prefill.simulator.investor_origin', $origin)->assertJsonPath('prefill.simulator.tax_residency_country', 'ZA');
$this->assertSame('linked_simulation', $v->json('context.provenance.currency.kind'));
}
$this->actingAs(User::factory()->create())->getJson('/investimentos/'.$a.'/contexto')->assertForbidden();
$this->actingAs($owner)->getJson('/investimentos/'.$a.'/contexto')->assertOk()->assertJsonPath('prefill.simulator.currency', 'MZN');
}
public function test_context_change_is_previewed_versioned_and_idempotent_without_changing_the_source_draft(): void
{
$owner = User::factory()->create();
$id = $this->dossier($owner, 'Impacto');
$view = $this->getJson('/investimentos/'.$id.'/contexto')->assertOk()->json();
$body = ['version' => $view['case']['version'], 'selection' => [], 'declarations' => ['currency' => 'USD', 'investor_origin' => 'foreign', 'tax_residency_country' => 'MZ'], 'replace_fields' => []];
$plan = $this->postJson('/investimentos/'.$id.'/contexto/previa', $body)->assertOk()->json();
$this->assertNotEmpty($plan['updated']);
$this->assertTrue($plan['dependency_changed']);
$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->getJson('/investimentos/'.$id.'/contexto')->assertOk()->assertJsonPath('prefill.simulator.currency', 'USD')->assertJsonPath('prefill.simulator.tax_residency_country', 'MZ');
}
public function test_zone_selection_reuses_existing_reference_and_impact_workflow_without_granting_a_benefit(): void
{
$owner = User::factory()->create();
$id = $this->dossier($owner, 'Zonas QA');
$template = app(InvestmentZones::class)->visible()->where('category', 'industrial_park')->firstOrFail();
foreach (['zee', 'zfi', 'industrial_park'] as $category) {
// Synthetic catalogue metadata, rolled back. Never a legal source or published rule in homologation.
$zone = $template->replicate();
$zone->id = (string) Str::uuid();
$zone->code = 'SINT-CONTEXT-'.Str::random(12);
$zone->name = 'SINTÉTICO QA '.$category;
$zone->category = $category;
$zone->legal_regime = $category === 'industrial_park' ? 'pending' : $category;
$zone->source_evidence = [['source_type' => 'government', 'url' => 'https://example.test/synthetic-zone', 'supports' => 'Synthetic test only; no legal or eligibility conclusion']];
$zone->save();
$view = $this->getJson('/investimentos/'.$id.'/contexto')->assertOk()->json();
$body = ['version' => $view['case']['version'], 'selection' => ['park' => 'park:'.$zone->id]];
$plan = $this->postJson('/investimentos/'.$id.'/contexto/previa', $body)->assertOk()->json();
$this->assertSame($category, $plan['fields']['investment_regime']);
$this->assertContains('S17', $plan['review_steps']);
$this->postJson('/investimentos/'.$id.'/contexto/confirmar', [...$body, 'preview_hash' => $plan['preview_hash'], 'operation_key' => (string) Str::uuid(), 'confirm' => true])->assertOk();
$after = $this->getJson('/investimentos/'.$id.'/contexto')->assertOk();
$after->assertJsonPath('prefill.simulator.investment_regime', $category);
$snapshot = json_decode($after->json('prefill.simulator.context_applicability_snapshot'), true);
$this->assertSame($zone->id, $snapshot['zone']['id']);
$this->assertArrayNotHasKey('benefit_rate', $snapshot);
}
$view = $this->getJson('/investimentos/'.$id.'/contexto')->json();
$body = ['version' => $view['case']['version'], 'selection' => ['park' => null]];
$plan = $this->postJson('/investimentos/'.$id.'/contexto/previa', $body)->assertOk()->json();
$this->postJson('/investimentos/'.$id.'/contexto/confirmar', [...$body, 'preview_hash' => $plan['preview_hash'], 'operation_key' => (string) Str::uuid(), 'confirm' => true])->assertOk();
$this->getJson('/investimentos/'.$id.'/contexto')->assertOk()->assertJsonPath('prefill.simulator.investment_regime', 'general')->assertJsonPath('prefill.simulator.special_zone_id', '');
}
public function test_a_title_edit_does_not_invalidate_applicability_or_ask_for_irrelevant_reviews(): void
{
$owner = User::factory()->create();
$id = $this->dossier($owner, 'Título original');
$view = $this->getJson('/investimentos/'.$id.'/contexto')->assertOk()->json();
$this->postJson('/investimentos/'.$id.'/contexto/previa', ['version' => $view['case']['version'], 'selection' => [], 'declarations' => ['project_title' => 'Título revisto']])
->assertOk()->assertJsonPath('dependency_changed', false)->assertJsonPath('review_steps', []);
}
public function test_the_primary_activity_is_kept_until_the_investor_explicitly_changes_it(): void
{
// Mapa de erros, "Criar Hotel": a hotel dossier is never moved to another activity by a later reference.
$owner = User::factory()->create();
$id = $this->dossier($owner, 'Criar Hotel');
// Synthetic sectors, rolled back with the transaction; never catalogue data.
[$tourism, $agro] = array_map(fn (string $name) => Sector::forceCreate(['name' => 'SINTÉTICO QA '.$name, 'slug' => 'sintetico-qa-'.Str::lower(Str::random(12)), 'is_active' => true]), ['Hotelaria', 'Agronegócio']);
$apply = function (array $selection, array $replace = []) use ($id): array {
$view = $this->getJson('/investimentos/'.$id.'/contexto')->assertOk()->json();
$body = ['version' => $view['case']['version'], 'selection' => $selection, 'replace_fields' => $replace];
$plan = $this->postJson('/investimentos/'.$id.'/contexto/previa', $body)->assertOk()->json();
$this->postJson('/investimentos/'.$id.'/contexto/confirmar', [...$body, 'preview_hash' => $plan['preview_hash'], 'operation_key' => (string) Str::uuid(), 'confirm' => true])->assertOk();
return $plan;
};
$apply(['sector' => 'sector:'.$tourism->id]);
$this->getJson('/investimentos/'.$id.'/contexto')->assertJsonPath('context.fields.sector_id', (string) $tourism->id);
$plan = $apply(['sector' => 'sector:'.$agro->id]);
$this->assertSame((string) $tourism->id, $plan['fields']['sector_id']);
$this->assertContains(['field' => 'sector', 'value' => $tourism->name, 'proposed' => $agro->name], $plan['maintained']);
$this->getJson('/investimentos/'.$id.'/contexto')->assertJsonPath('context.fields.sector', $tourism->name);
$apply(['sector' => null]);
$this->getJson('/investimentos/'.$id.'/contexto')->assertJsonPath('context.fields.sector_id', (string) $tourism->id);
// The dossier choice shows each dossier's activity.
$listed = collect(app(InvestorPath::class)->contexts($owner))->firstWhere('id', $id);
$this->assertSame([(string) $tourism->id, $tourism->name], [$listed['sector_id'], $listed['sector']]);
$apply(['sector' => 'sector:'.$agro->id], ['sector_id', 'sector']);
$this->getJson('/investimentos/'.$id.'/contexto')->assertJsonPath('context.fields.sector_id', (string) $agro->id)->assertJsonPath('context.fields.sector', $agro->name);
// Removing the activity is also an explicit decision (C10 explicit changes keep working).
$apply(['sector' => null], ['sector_id', 'sector']);
$this->assertArrayNotHasKey('sector_id', $this->getJson('/investimentos/'.$id.'/contexto')->json('context.fields'));
}
public function test_the_business_plan_executive_summary_fields_are_saved_in_the_draft(): void
{
// Investor report model: objectives, mission and keys to success are requested from the investor.
$owner = User::factory()->create();
$id = $this->dossier($owner, 'Plano de negócio');
$economic = ['purpose' => 'simulate_only', 'journey_type' => 'investor', 'project_title' => 'Hotel', 'plan_objectives' => 'Lucro no segundo ano.', 'plan_mission' => 'Servir bem.', 'plan_success_keys' => 'Qualidade.'];
$this->actingAs($owner)->postJson('/api/v1/simulation-drafts', ['context' => $id, 'personal_snapshot' => [], 'economic_snapshot' => $economic], ['Idempotency-Key' => (string) Str::uuid()])->assertCreated();
$this->postJson('/api/v1/simulation-drafts', ['context' => $id, 'personal_snapshot' => [], 'economic_snapshot' => [...$economic, 'plan_mission' => str_repeat('a', 10001)]], ['Idempotency-Key' => (string) Str::uuid()])->assertUnprocessable();
}
}