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

112 lines
7.5 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\User;
use App\Services\SectorResourceReferences;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
use Inertia\Testing\AssertableInertia;
use Tests\TestCase;
final class EnergyReferenceTest extends TestCase
{
use DatabaseTransactions;
protected function setUp(): void
{
parent::setUp();
$this->withoutVite();
$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);
config(['investor-path.enabled' => true, 'investor-path.private_preparation' => true, 'suitecrm.enabled' => false, 'investmoz_ai.enabled' => false]);
Http::preventStrayRequests();
}
private function choice(): array
{
return ['area_id' => 'ENE-07', 'group_id' => 'EN-SOLAR', 'location_label' => 'Niassa', 'source_sha256' => SectorResourceReferences::SOURCES['energia-especifica']];
}
public function test_energy_source_preserves_named_places_solar_discrepancies_and_project_states_without_activating_rules(): void
{
$service = app(SectorResourceReferences::class);
$raw = $service->data('energia-especifica');
$view = $service->view('energia-especifica');
$this->assertCount(7, $view['groups']);
$this->assertCount(11, $view['areas']);
$this->assertCount(10, $view['projects']);
$this->assertCount(24, $view['sources']);
$this->assertCount(4, $view['risks']);
$this->assertCount(6, $raw['original_tables']);
$this->assertArrayNotHasKey('original_tables', $view);
foreach ($view['areas'] as $area) {
$this->assertNull($area['coordinates']);
$this->assertFalse($area['promotion_ready']);
}
$solar = implode(' ', $view['projects']);
$this->assertStringContainsString('47,3 milhões EUR', $solar);
$this->assertStringContainsString('56 milhões USD', $solar);
$this->assertStringContainsString('15 MWac', $solar);
$this->assertStringContainsString('após fecho financeiro', $solar);
$upstream = $service->selection('energia-especifica', ['area_id' => 'ENE-01', 'location_label' => 'Buzi', 'source_sha256' => $view['source_sha256']]);
$this->assertSame('Buzi', $upstream['location_label']);
$this->assertArrayNotHasKey('province_id', $upstream);
$unknown = $service->selection('energia-especifica', ['area_id' => 'ENE-09', 'source_sha256' => $view['source_sha256']]);
$this->assertNull($unknown['location_label']);
$this->assertSame('source_area_reference', $unknown['territory_precision']);
file_put_contents(base_path('outputs/energy-reference-20261111/public-view.json'), json_encode($view, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE));
$this->get('/sectores/energia-especifica')->assertOk()->assertInertia(fn (AssertableInertia $page) => $page->component('portal/services')->has('sectorEntry.resource_reference.groups', 7));
}
public function test_energy_new_dossier_and_existing_mining_dossier_keep_both_sources_and_recover_swot_evidence(): void
{
$this->actingAs(User::factory()->create());
$before = DB::table('opportunities')->count();
$selection = $this->choice();
$start = $this->getJson('/investimentos/a-partir-do-setor/energia-especifica?'.http_build_query(['resource_area' => $selection['area_id'], 'resource_location' => $selection['location_label'], 'resource_group' => $selection['group_id'], 'resource_source' => $selection['source_sha256']]))->assertOk();
$this->assertSame('EN-SOLAR', $start->json('resourceSource.group_id'));
file_put_contents(base_path('outputs/energy-reference-20261111/start-page.json'), json_encode($start->json(), JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE));
$body = ['title' => 'SYNTHETIC energy dossier', 'client_key' => (string) Str::uuid(), 'reuse_consent' => true, 'sector_slug' => 'energia-especifica', 'sector_reference' => $selection];
$id = $this->postJson('/investimentos', $body)->assertCreated()->json('id');
$this->assertSame($id, $this->postJson('/investimentos', $body)->assertCreated()->json('id'));
$one = $this->getJson('/investimentos/'.$id)->assertOk()->json();
$this->assertCount(1, $one['sector_references']);
$this->assertSame('Niassa', $one['sector_references'][0]['location_label']);
$mining = ['area_id' => 'MIN-02', 'location_label' => 'Cabo Delgado', 'group_id' => 'MINERAL-04', 'source_sha256' => SectorResourceReferences::SOURCES['mineracao']];
$this->postJson('/investimentos/'.$id.'/sectores', ['version' => $one['case']['version'], 'sector_slug' => 'mineracao', 'sector_reference' => $mining])->assertOk();
$two = $this->getJson('/investimentos/'.$id)->assertOk()->json();
$this->assertCount(2, $two['sector_references']);
$this->assertCount(2, $two['sector_contexts']);
$this->assertSame($one['sector_references'][0], $two['sector_references'][0]);
$this->assertSame('SYNTHETIC energy dossier', $two['case']['title']);
$version = $two['case']['version'];
$this->postJson('/investimentos/'.$id.'/sectores', ['version' => $version - 1, 'sector_slug' => 'energia-especifica', 'sector_reference' => $selection])->assertOk();
$this->assertSame($version, $this->getJson('/investimentos/'.$id)->assertOk()->json('case.version'));
$workspace = $this->getJson('/investimentos/'.$id.'/preparacao')->assertOk();
$this->assertStringContainsString('Solar, baterias', $workspace->getContent());
$this->assertStringContainsString('Grafite', $workspace->getContent());
$this->assertSame($before, DB::table('opportunities')->count());
file_put_contents(base_path('outputs/energy-reference-20261111/recovered-dossier.json'), json_encode($two, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE));
$this->actingAs(User::factory()->create());
$this->postJson('/investimentos/'.$id.'/sectores', ['version' => $version, 'sector_slug' => 'energia-especifica', 'sector_reference' => $selection])->assertForbidden();
}
public function test_energy_reference_rejects_wrong_sector_location_group_and_changed_creation_intent(): void
{
$this->actingAs(User::factory()->create());
$body = ['title' => 'SYNTHETIC guarded energy dossier', 'client_key' => (string) Str::uuid(), 'reuse_consent' => true, 'sector_slug' => 'energia-especifica', 'sector_reference' => $this->choice()];
$this->postJson('/investimentos', $body)->assertCreated();
$this->postJson('/investimentos', [...$body, 'sector_reference' => [...$this->choice(), 'location_label' => 'Cabo Delgado']])->assertStatus(409);
foreach ([['location_label' => 'Gaza'], ['group_id' => 'EN-WIND'], ['source_sha256' => str_repeat('a', 64)]] as $bad) {
$count = DB::table('desk_cases')->count();
$this->postJson('/investimentos', [...$body, 'client_key' => (string) Str::uuid(), 'sector_reference' => [...$this->choice(), ...$bad]])->assertStatus(isset($bad['source_sha256']) ? 409 : 422);
$this->assertSame($count, DB::table('desk_cases')->count());
}
$this->postJson('/investimentos', [...$body, 'client_key' => (string) Str::uuid(), 'sector_slug' => 'turismo'])->assertStatus(422);
}
}