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

50 lines
2.9 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Tests\TestCase;
final class IvaOperationsDraftTest extends TestCase
{
use DatabaseTransactions;
public function test_l1_ui_payload_round_trips_without_duplicate_drafts_or_new_rules_and_other_owner_cannot_read(): void
{
$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, 'suitecrm.enabled' => false]);
$fingerprint = DB::selectOne("select md5(string_agg(row_to_json(r)::text,E'\\n' order by id)) as hash from simulation_tax_rule_versions r")->hash;
$this->actingAs(User::factory()->create(['email_verified_at' => now()]));
$path = getenv('IVA_L1_UI_INPUT') ?: base_path('tests/frontend/fixtures/iva-operations.json');
$draft = json_decode(file_get_contents($path), true, flags: JSON_THROW_ON_ERROR);
$draft['purpose'] = 'simulate_only';
$key = (string) Str::uuid();
$payload = ['personal_snapshot' => [], 'economic_snapshot' => $draft];
$created = $this->postJson('/api/v1/simulation-drafts', $payload, ['Idempotency-Key' => $key])->assertCreated();
$id = $created->json('data.id');
$retry = $this->postJson('/api/v1/simulation-drafts', $payload, ['Idempotency-Key' => $key]);
$this->assertSame($id, $retry->json('data.id'));
$saved = $this->getJson('/api/v1/simulation-drafts/'.$id)->assertOk();
$economic = $saved->json('data.economic_snapshot');
$this->assertSame($draft['modelo_a_snapshot'], $economic['modelo_a_snapshot']);
$this->assertSame($draft['context_opportunity'], $economic['context_opportunity']);
$store = json_decode($economic['modelo_a_snapshot'], true, flags: JSON_THROW_ON_ERROR);
$book = $store['salesOperations'][$draft['context_opportunity']];
$this->assertSame('iva-operations-l1/1', $book['result']['version']);
$this->assertCount(12, $book['result']['rows']);
$this->assertSame('160.00', $book['result']['rows'][0]['vat']);
$this->assertNull($book['result']['rows'][10]['vat']);
if (getenv('IVA_L1_RECOVERED_OUT')) {
file_put_contents(getenv('IVA_L1_RECOVERED_OUT'), json_encode($economic, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE));
}
$this->actingAs(User::factory()->create(['email_verified_at' => now()]));
$this->getJson('/api/v1/simulation-drafts/'.$id)->assertNotFound();
$this->assertSame($fingerprint, DB::selectOne("select md5(string_agg(row_to_json(r)::text,E'\\n' order by id)) as hash from simulation_tax_rule_versions r")->hash);
}
}