oportunidades 3
tests / ci (push) Canceled after 0s

This commit is contained in:
2026-10-09 15:00:37 +02:00
parent 9d6176f55b
commit e662230df7
32 changed files with 865 additions and 37 deletions
+78
View File
@@ -4,11 +4,14 @@ namespace Tests\Feature;
use App\Models\InterestRequest;
use App\Models\User;
use App\Services\CountryCatalogue;
use App\Services\DeclaredOriginStatistics;
use Database\Seeders\InvestmentReferenceSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Inertia\Testing\AssertableInertia;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Tests\TestCase;
class InvestmentWorkspaceTest extends TestCase
@@ -20,6 +23,81 @@ class InvestmentWorkspaceTest extends TestCase
return $this->actingAs($user)->postJson('/api/v1/investment/submissions')->assertCreated()->json('id');
}
public function test_country_meanings_are_persisted_separately_without_inference_and_recovered_by_account(): void
{
$user = User::factory()->create();
$this->actingAs($user)->putJson('/api/v1/investment/profile', ['version' => 0, 'payload' => ['country' => 'Valor histórico', 'residence_country_code' => 'PT']])->assertOk();
$company = $this->postJson('/api/v1/investment/companies', ['version' => 0, 'payload' => ['name' => 'Entidade QA', 'role' => 'Representante', 'incorporation_country_code' => 'MZ']])->assertOk()->json('id');
$draft = $this->postJson('/api/v1/investment/submissions', ['company_id' => $company])->assertCreated()->json('id');
$this->getJson('/api/v1/investment/submissions/'.$draft)->assertJsonPath('submission.payload.residence_country_code', 'PT')->assertJsonPath('submission.payload.incorporation_country_code', 'MZ')->assertJsonPath('submission.payload.country', 'Valor histórico')->assertJsonMissingPath('submission.payload.declared_origin_country_code');
$this->putJson('/api/v1/investment/submissions/'.$draft, ['version' => 1, 'payload' => ['residence_country_code' => 'PT', 'incorporation_country_code' => 'MZ', 'declared_origin_country_code' => 'ZA']])->assertOk();
// Discard the original authenticated context: recovery uses server data, not local storage.
$this->actingAs(User::factory()->create())->getJson('/api/v1/investment/submissions/'.$draft)->assertNotFound();
$this->actingAs(User::findOrFail($user->id))->getJson('/api/v1/investment/submissions/'.$draft)->assertJsonPath('submission.payload.declared_origin_country_code', 'ZA');
$this->putJson('/api/v1/investment/submissions/'.$draft, ['version' => 2, 'payload' => ['declared_origin_country_code' => 'ZZ']])->assertUnprocessable()->assertJsonValidationErrors('payload.declared_origin_country_code');
$this->getJson('/api/v1/investment/submissions/'.$draft)->assertJsonPath('submission.version', 2)->assertJsonPath('submission.payload.declared_origin_country_code', 'ZA');
$countries = app(CountryCatalogue::class)->all();
$this->assertCount(249, $countries);
$this->assertCount(249, array_unique(array_column($countries, 'code')));
}
public function test_declared_origin_is_snapshotted_on_submission_and_statistics_do_not_infer_old_country(): void
{
$this->seed(InvestmentReferenceSeeder::class);
$user = User::factory()->create();
$id = $this->draft($user);
$data = $this->payload();
$data['payload']['declared_origin_country_code'] = 'ZA';
$this->postJson('/api/v1/investment/submissions/'.$id.'/submit', $data)->assertOk();
$interest = InterestRequest::firstOrFail();
$this->assertSame('ZA', $interest->metadata['declared_origin_country_code']);
$this->assertSame('Moçambique', $interest->country);
$this->expectException(HttpException::class);
app(DeclaredOriginStatistics::class)->forManager($user);
}
public function test_authorized_origin_statistics_keep_historical_missing_as_not_informed(): void
{
$this->seed(InvestmentReferenceSeeder::class);
$user = User::factory()->create();
$id = $this->draft($user);
$this->postJson('/api/v1/investment/submissions/'.$id.'/submit', $this->payload())->assertOk();
config(['portal.managers' => [$user->email]]);
$stats = app(DeclaredOriginStatistics::class)->forManager($user);
$this->assertSame([['code' => null, 'name' => 'Não informado', 'count' => 1]], $stats['interests']);
$this->assertSame([], $stats['desk']);
}
public function test_crm_origin_filter_separates_explicit_missing_and_empty_results_without_writing(): void
{
$this->withoutVite();
$this->seed(InvestmentReferenceSeeder::class);
$user = User::factory()->create();
foreach ([null, 'PT', 'ZA'] as $origin) {
$id = $this->draft($user);
$data = $this->payload();
if ($origin !== null) {
$data['payload']['declared_origin_country_code'] = $origin;
}
$this->postJson('/api/v1/investment/submissions/'.$id.'/submit', $data)->assertOk();
}
$before = InterestRequest::orderBy('id')->get()->toArray();
config(['portal.managers' => [$user->email]]);
$this->get('/management/crm/leads?origin=PT')->assertOk()->assertInertia(fn (AssertableInertia $page) => $page
->where('filters.origin', 'PT')->has('originStatistics.interests', 1)
->where('originStatistics.interests.0.code', 'PT')->where('originStatistics.interests.0.count', 1));
$this->get('/management/crm/leads?origin=unknown')->assertOk()->assertInertia(fn (AssertableInertia $page) => $page
->has('originStatistics.interests', 1)->where('originStatistics.interests.0.code', null)
->where('originStatistics.interests.0.count', 1));
$this->get('/management/crm/leads?origin=MZ')->assertOk()->assertInertia(fn (AssertableInertia $page) => $page
->has('originStatistics.interests', 0)->has('originStatistics.desk', 0));
$this->get('/management/crm/leads')->assertOk()->assertInertia(fn (AssertableInertia $page) => $page
->has('originStatistics.interests', 3));
$this->getJson('/management/crm/leads?origin=ZZ')->assertUnprocessable()->assertJsonValidationErrors('origin');
$this->assertSame($before, InterestRequest::orderBy('id')->get()->toArray());
$this->actingAs(User::factory()->create())->get('/management/crm/leads?origin=PT')->assertForbidden();
}
private function payload(int $version = 1): array
{
return ['version' => $version, 'payload' => ['full_name' => 'Investidor sintético', 'email' => 'qa@example.test', 'country' => 'Moçambique',