Publish agribusiness lot through scoped management and extend existing dossier for multiple opportunities
tests / ci (push) Canceled after 0s
tests / ci (push) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Opportunity;
|
||||
use App\Models\User;
|
||||
use App\Services\CorridorAgribusinessOpportunities;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
use Tests\TestCase;
|
||||
|
||||
final class CorridorAgribusinessBatchTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
public function test_batch_uses_normal_scoped_session_preview_and_idempotent_disclosure_without_touching_other_opportunities(): void
|
||||
{
|
||||
$target = DB::selectOne('select current_database() as name, inet_server_port() as port');
|
||||
$this->assertSame('simulator_fiscal_delta_20261021_test', $target->name);
|
||||
$this->assertSame(55441, (int) $target->port);
|
||||
$this->withoutVite(); config(['portal.profile_access_enabled' => true]);
|
||||
$entries = app(CorridorAgribusinessOpportunities::class)->entries();
|
||||
$keys = array_column($entries, 'key');
|
||||
$others = Opportunity::withTrashed()->whereNotIn('management_creation_key', $keys)->orWhereNull('management_creation_key')->orderBy('id')->get()->map(fn ($r) => $r->getRawOriginal())->all();
|
||||
$count = Opportunity::withTrashed()->count();
|
||||
$actor = User::findOrFail(295);
|
||||
$this->actingAs($actor)->withSession(['portal.mfa_actor' => $actor->id, 'portal.mfa_at' => time(), 'portal.access_revision' => (int) $actor->portal_access_revision]);
|
||||
$base = '/minhas-oportunidades/lotes/agronegocio';
|
||||
$this->get($base)->assertOk();
|
||||
$this->postJson($base.'/cadastrar', ['manifest_hash' => str_repeat('0', 64), 'confirm' => true])->assertConflict();
|
||||
$this->postJson($base.'/cadastrar', ['manifest_hash' => hash('sha256', json_encode($entries, JSON_THROW_ON_ERROR)), 'confirm' => true])->assertOk();
|
||||
$this->assertSame($count, Opportunity::withTrashed()->count());
|
||||
$previews = $this->postJson($base.'/previa')->assertOk()->json('previews');
|
||||
$this->assertCount(34, $previews);
|
||||
$input = ['confirm' => true, 'reason' => 'QA: divulgação das fichas expressamente seleccionadas no lote de Agronegócio.',
|
||||
'previews' => array_map(fn ($p) => ['id' => $p['id'], 'token' => $p['token'], 'operation_key' => (string) Str::uuid()], $previews)];
|
||||
$bad = $input; array_pop($bad['previews']);
|
||||
$this->postJson($base.'/divulgar', $bad)->assertUnprocessable();
|
||||
$this->postJson($base.'/divulgar', $input)->assertOk();
|
||||
$this->postJson($base.'/divulgar', $input)->assertOk();
|
||||
$this->assertSame(34, Opportunity::whereIn('management_creation_key', $keys)->published()->count());
|
||||
$this->assertSame($others, Opportunity::withTrashed()->whereNotIn('management_creation_key', $keys)->orWhereNull('management_creation_key')->orderBy('id')->get()->map(fn ($r) => $r->getRawOriginal())->all());
|
||||
$this->withSession(['portal.mfa_at' => 0])->postJson($base.'/previa')->assertForbidden();
|
||||
$this->actingAs(User::factory()->create())->get($base)->assertForbidden();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Opportunity;
|
||||
use App\Models\User;
|
||||
use App\Services\CorridorAgribusinessOpportunities;
|
||||
use App\Services\InvestorContext;
|
||||
use App\Services\VirtualDesk;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
use Tests\TestCase;
|
||||
|
||||
final class InvestmentOpportunityChoicesTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
private User $investor;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$target = DB::selectOne('select current_database() as name, inet_server_port() as port');
|
||||
$this->assertSame('simulator_fiscal_delta_20261021_test', $target->name);
|
||||
$this->assertSame(55441, (int) $target->port);
|
||||
config(['investor-path.enabled' => true, 'investor-path.private_preparation' => true, 'suitecrm.enabled' => false, 'investmoz_ai.enabled' => false]);
|
||||
$this->withoutVite();
|
||||
$this->investor = User::factory()->create(); $this->actingAs($this->investor);
|
||||
}
|
||||
|
||||
private function dossier(): string
|
||||
{
|
||||
return $this->postJson('/investimentos', ['title' => 'QA: investimento conjunto', 'client_key' => (string) Str::uuid(), 'reuse_consent' => true])->assertCreated()->json('id');
|
||||
}
|
||||
|
||||
public function test_multiple_opportunities_keep_sites_sources_requirements_and_results_in_one_dossier(): void
|
||||
{
|
||||
$entries = array_slice(app(CorridorAgribusinessOpportunities::class)->entries(), 0, 2);
|
||||
$records = Opportunity::whereIn('management_creation_key', array_column($entries, 'key'))->orderBy('id')->get();
|
||||
$sites = DB::table('geo_locations')->where('location_type', 'province')->where('data_classification', 'public')->whereNull('deleted_at')->orderBy('id')->limit(2)->pluck('id');
|
||||
$this->get('/investimentos/iniciar?opportunities[]='.$records[0]->id.'&opportunities[]='.$records[1]->id)->assertOk();
|
||||
$id = $this->dossier();
|
||||
$before = $this->getJson('/investimentos/'.$id.'/contexto')->assertOk()->json();
|
||||
$input = ['version' => $before['case']['version'], 'selection' => [], 'opportunity_choices' => [
|
||||
['source' => 'opportunity:'.$records[0]->id, 'territory' => 'territory:'.$sites[0], 'activities' => ['production', 'processing']],
|
||||
['source' => 'opportunity:'.$records[1]->id, 'territory' => 'territory:'.$sites[1], 'activities' => ['trade']],
|
||||
]];
|
||||
$plan = $this->postJson('/investimentos/'.$id.'/contexto/previa', $input)->assertOk()->json();
|
||||
$this->assertCount(2, $plan['opportunity_choices']);
|
||||
$this->assertSame($sites[0], $plan['opportunity_choices'][0]['territory']['id']);
|
||||
$this->assertSame($sites[1], $plan['opportunity_choices'][1]['territory']['id']);
|
||||
$this->assertSame(CorridorAgribusinessOpportunities::SOURCE_HASH, $plan['opportunity_choices'][1]['source_sha256']);
|
||||
$this->assertSame('QA: investimento conjunto', $plan['fields']['project_title']);
|
||||
$this->assertSame($records[0]->summary, $plan['fields']['project_summary']);
|
||||
$this->assertContains('processing', array_column($plan['requirements_assessment']['requirements'], 'code'));
|
||||
$this->assertContains('trade', array_column($plan['requirements_assessment']['requirements'], 'code'));
|
||||
$this->assertSame($before['case']['version'], $this->getJson('/investimentos/'.$id.'/contexto')->json('case.version'));
|
||||
$control = [...$input, '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);
|
||||
$after = $this->getJson('/investimentos/'.$id.'/contexto')->assertOk()->json();
|
||||
$this->assertCount(2, $after['context']['opportunity_choices']);
|
||||
$snapshot = json_decode($after['prefill']['simulator']['context_applicability_snapshot'], true);
|
||||
$this->assertSame($after['context']['opportunity_choices'], $snapshot['opportunity_choices']);
|
||||
$this->assertSame('general', $snapshot['fields']['investment_regime']);
|
||||
$this->assertArrayNotHasKey('tax_benefit', $snapshot['fields']);
|
||||
$this->get('/investimentos/'.$id)->assertOk();
|
||||
|
||||
$desk = app(VirtualDesk::class); $case = $desk->case($this->investor, $id);
|
||||
$payload = $desk->decode($case->payload); $resultId = (string) Str::uuid();
|
||||
$payload['investor_path']['results'] = [['id' => $resultId, 'engine' => 'synthetic-qa-history', 'context_state' => 'current', 'value' => 'preserve']];
|
||||
DB::table('desk_cases')->where('id', $id)->update(['payload' => $desk->encode($payload)]);
|
||||
$next = ['version' => $after['case']['version'], 'selection' => [], 'opportunity_choices' => [
|
||||
['source' => 'opportunity:'.$records[0]->id, 'territory' => 'territory:'.$sites[0], 'activities' => ['production']],
|
||||
['source' => 'opportunity:'.$records[1]->id, 'territory' => 'territory:'.$sites[0], 'confirm_location_change' => true, 'location_reason' => 'Alternativa territorial escolhida expressamente para comparar o investimento.', 'activities' => ['trade']],
|
||||
]];
|
||||
$impact = $this->postJson('/investimentos/'.$id.'/contexto/previa', $next)->assertOk()->json();
|
||||
$this->assertSame($resultId, $impact['dependent_results'][0]['id']);
|
||||
$this->postJson('/investimentos/'.$id.'/contexto/confirmar', [...$next, 'preview_hash' => $impact['preview_hash'], 'operation_key' => (string) Str::uuid(), 'confirm' => true])->assertOk();
|
||||
$this->getJson('/investimentos/'.$id.'/contexto')->assertOk()->assertJsonPath('results.0.value', 'preserve')->assertJsonPath('results.0.context_state', 'review_required');
|
||||
$this->actingAs(User::factory()->create())->getJson('/investimentos/'.$id.'/contexto')->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_documented_location_cannot_change_silently_and_stale_source_cannot_be_confirmed(): void
|
||||
{
|
||||
$record = Opportunity::where('management_creation_key', app(CorridorAgribusinessOpportunities::class)->entries()[0]['key'])->firstOrFail();
|
||||
$sites = DB::table('geo_locations')->where('location_type', 'province')->where('data_classification', 'public')->whereNull('deleted_at')->orderBy('id')->limit(2)->pluck('id');
|
||||
$record->primary_territory_id = $sites[0]; $record->save();
|
||||
$id = $this->dossier(); $version = $this->getJson('/investimentos/'.$id.'/contexto')->json('case.version');
|
||||
$input = ['version' => $version, 'selection' => [], 'opportunity_choices' => [['source' => 'opportunity:'.$record->id, 'territory' => 'territory:'.$sites[1]]]];
|
||||
$this->postJson('/investimentos/'.$id.'/contexto/previa', $input)->assertUnprocessable();
|
||||
$this->postJson('/investimentos/'.$id.'/contexto/previa', ['version' => $version, 'selection' => ['opportunity' => 'opportunity:'.$record->id, 'territory' => 'territory:'.$sites[1]]])->assertUnprocessable();
|
||||
$input['opportunity_choices'][0]['confirm_location_change'] = true;
|
||||
$input['opportunity_choices'][0]['location_reason'] = 'Escolha explícita de implantação alternativa, sem alterar a origem.';
|
||||
$plan = $this->postJson('/investimentos/'.$id.'/contexto/previa', $input)->assertOk()->json();
|
||||
$this->assertSame($sites[0], $record->fresh()->primary_territory_id);
|
||||
$this->postJson('/investimentos/'.$id.'/contexto/confirmar', [...$input, 'preview_hash' => $plan['preview_hash'], 'operation_key' => (string) Str::uuid(), 'confirm' => true])->assertOk();
|
||||
$input['version'] = $this->getJson('/investimentos/'.$id.'/contexto')->json('case.version');
|
||||
unset($input['opportunity_choices'][0]['confirm_location_change']);
|
||||
$plan = $this->postJson('/investimentos/'.$id.'/contexto/previa', $input)->assertOk()->assertJsonPath('dependency_changed', false)->json();
|
||||
$record->title .= ' QA source changed'; $record->save();
|
||||
$this->postJson('/investimentos/'.$id.'/contexto/confirmar', [...$input, 'preview_hash' => $plan['preview_hash'], 'operation_key' => (string) Str::uuid(), 'confirm' => true])->assertConflict();
|
||||
$this->getJson('/investimentos/'.$id.'/contexto')->assertOk()->assertJsonPath('context.revision', 1);
|
||||
}
|
||||
|
||||
public function test_the_34_choices_and_sources_fit_the_existing_simulation_draft_without_reentry(): void
|
||||
{
|
||||
$records = Opportunity::whereIn('management_creation_key', array_column(app(CorridorAgribusinessOpportunities::class)->entries(), 'key'))->orderBy('id')->get();
|
||||
$this->assertCount(34, $records);
|
||||
$id = $this->dossier();
|
||||
$view = $this->getJson('/investimentos/'.$id.'/contexto')->assertOk()->json();
|
||||
$input = ['version' => $view['case']['version'], 'selection' => [], 'opportunity_choices' => $records->map(fn ($r) => ['source' => 'opportunity:'.$r->id])->all()];
|
||||
$plan = $this->postJson('/investimentos/'.$id.'/contexto/previa', $input)->assertOk()->json();
|
||||
$this->postJson('/investimentos/'.$id.'/contexto/confirmar', [...$input, 'preview_hash' => $plan['preview_hash'], 'operation_key' => (string) Str::uuid(), 'confirm' => true])->assertOk();
|
||||
$view = $this->getJson('/investimentos/'.$id.'/contexto')->assertOk()->json();
|
||||
$snapshot = $view['prefill']['simulator']['context_applicability_snapshot'];
|
||||
$this->assertCount(34, json_decode($snapshot, true)['opportunity_choices']);
|
||||
$this->assertLessThan(1000000, strlen($snapshot));
|
||||
$draft = $this->postJson('/api/v1/simulation-drafts', ['context' => $id, 'personal_snapshot' => [],
|
||||
'economic_snapshot' => [...$view['prefill']['simulator'], 'purpose' => 'simulate_only', 'currency' => 'MZN']], ['Idempotency-Key' => (string) Str::uuid()])->assertCreated()->json();
|
||||
$this->assertNotEmpty($draft);
|
||||
$this->getJson('/investimentos/'.$id.'/contexto')->assertOk()->assertJsonPath('context.opportunity_choices.33.opportunity.key', 'opportunity:'.$records[33]->id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user