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

120 lines
7.7 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\User;
use App\Services\SectorInvestment;
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 MiningReferenceTest 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, 'investor-path.private_preparation' => true, 'suitecrm.enabled' => false, 'investmoz_ai.enabled' => false]);
Http::preventStrayRequests();
}
private function choice(string $area = 'MIN-02', ?string $province = 'Cabo Delgado'): array
{
return ['area_id' => $area, 'location_label' => $province, 'source_sha256' => SectorResourceReferences::SOURCES['mineracao']];
}
public function test_complete_reference_has_source_counts_distinct_metrics_and_no_invented_coordinates(): void
{
$service = app(SectorResourceReferences::class);
$data = $service->data('mineracao');
$this->assertCount(11, $data['minerals']);
$this->assertCount(10, $data['areas']);
$this->assertCount(12, $data['indicators']);
$this->assertCount(6, $data['projects']);
$this->assertCount(13, $data['sources']);
foreach ([...$data['minerals'], ...$data['areas']] as $row) {
$this->assertNull($row['coordinates']);
}
$view = $service->view('mineracao');
$this->assertArrayNotHasKey('original_paragraphs', $view);
$this->assertArrayNotHasKey('source_fields', $view['areas'][0]);
$this->assertCount(6, $view['risks']);
file_put_contents(base_path('outputs/mining-reference-20261111/public-view.json'), json_encode($view, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE));
$this->get('/sectores/mineracao')->assertOk()->assertInertia(fn (AssertableInertia $page) => $page->component('portal/services')->has('sectorEntry.resource_reference.groups', 11));
$this->assertStringNotContainsString('Próximos passos', json_encode($view['risks']));
$selection = $service->selection('mineracao', $this->choice('MIN-06', 'Maputo'));
$this->assertSame('province_or_city_unspecified', $selection['territory_precision']);
$this->assertFalse($selection['promotion_ready']);
$this->assertNull($selection['coordinates']);
}
public function test_native_sector_start_save_retry_recovery_and_swot_keep_same_reference_without_creating_opportunities(): void
{
$actor = User::factory()->create();
$this->actingAs($actor);
$before = DB::table('opportunities')->count();
$choice = [...$this->choice(), 'group_id' => 'MINERAL-04'];
$start = $this->getJson('/investimentos/a-partir-do-setor/mineracao?'.http_build_query(['resource_area' => $choice['area_id'], 'resource_location' => $choice['location_label'], 'resource_group' => $choice['group_id'], 'resource_source' => $choice['source_sha256']]))->assertOk();
$this->assertSame('Grafite', $start->json('resourceSource.group_name'));
file_put_contents(base_path('outputs/mining-reference-20261111/start-page.json'), json_encode($start->json(), JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE));
$body = ['title' => 'SYNTHETIC mining value-chain preparation', 'client_key' => (string) Str::uuid(), 'reuse_consent' => true, 'sector_slug' => 'mineracao', 'sector_reference' => $choice];
$id = $this->postJson('/investimentos', $body)->assertCreated()->json('id');
$again = $this->postJson('/investimentos', $body)->assertCreated();
$this->assertSame($id, $again->json('id'));
$view = $this->getJson('/investimentos/'.$id)->assertOk()->json();
$this->assertCount(1, $view['sector_references']);
$this->assertSame('Cabo Delgado', $view['sector_references'][0]['location_label']);
$this->assertSame('MINERAL-04', $view['sector_references'][0]['group_id']);
$this->assertSame(SectorResourceReferences::SOURCES['mineracao'], $view['sector_references'][0]['source_sha256']);
$this->assertStringContainsString('Beneficiamento', $view['sector_references'][0]['value_chain']);
$this->assertCount(1, $view['sector_contexts']);
file_put_contents(base_path('outputs/mining-reference-20261111/recovered-dossier.json'), json_encode($view, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE));
$workspace = $this->getJson('/investimentos/'.$id.'/preparacao')->assertOk();
$this->assertStringContainsString('sector-resource:', $workspace->getContent());
$this->assertSame($before, DB::table('opportunities')->count());
$this->postJson('/investimentos', [...$body, 'sector_reference' => $this->choice('MIN-01', 'Tete')])->assertStatus(409);
$this->actingAs(User::factory()->create());
$this->getJson('/investimentos/'.$id)->assertForbidden();
}
public function test_existing_dossier_appends_distinct_area_and_preserves_manual_fields_and_previous_reference(): void
{
$actor = User::factory()->create();
$this->actingAs($actor);
$id = $this->postJson('/investimentos', ['title' => 'SYNTHETIC existing mining dossier', 'client_key' => (string) Str::uuid(), 'reuse_consent' => true])->assertCreated()->json('id');
$service = app(SectorInvestment::class);
$version = $this->getJson('/investimentos/'.$id)->assertOk()->json('case.version');
$service->attach($actor, $id, ['version' => $version, 'sector_slug' => 'mineracao', 'sector_reference' => $this->choice()]);
$one = $this->getJson('/investimentos/'.$id)->assertOk()->json();
$service->attach($actor, $id, ['version' => $one['case']['version'], 'sector_slug' => 'mineracao', 'sector_reference' => $this->choice('MIN-01', 'Tete')]);
$two = $this->getJson('/investimentos/'.$id)->assertOk()->json();
$this->assertCount(2, $two['sector_references']);
$this->assertSame($one['sector_references'][0], $two['sector_references'][0]);
$this->assertSame('SYNTHETIC existing mining dossier', $two['case']['title']);
$this->assertCount(1, $two['sector_contexts']);
$service->attach($actor, $id, ['version' => $version, 'sector_slug' => 'mineracao', 'sector_reference' => $this->choice()]);
$this->assertSame($two['case']['version'], $this->getJson('/investimentos/'.$id)->json('case.version'));
DB::table('desk_cases')->where('id', $id)->update(['closed_at' => now()]);
$this->postJson('/investimentos/'.$id.'/sectores', ['version' => $two['case']['version'], 'sector_slug' => 'mineracao', 'sector_reference' => $this->choice('MIN-05', 'Cabo Delgado')])->assertStatus(409);
}
public function test_mismatched_source_province_and_mineral_are_refused_before_dossier_creation(): void
{
$this->actingAs(User::factory()->create());
$count = DB::table('desk_cases')->count();
foreach ([['source_sha256' => str_repeat('0', 64)], ['location_label' => 'Gaza'], ['group_id' => 'MINERAL-01']] as $bad) {
$response = $this->postJson('/investimentos', ['title' => 'SYNTHETIC invalid mineral', 'client_key' => (string) Str::uuid(), 'reuse_consent' => true, 'sector_slug' => 'mineracao', 'sector_reference' => [...$this->choice(), ...$bad]]);
$this->assertContains($response->status(), [409, 422]);
}$this->assertSame($count, DB::table('desk_cases')->count());
}
}