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

63 lines
3.7 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 IvaSubstitutionDraftTest extends TestCase
{
use DatabaseTransactions;
public function test_substitution_recovers_frozen_predecessor_difference_payments_history_and_all_prior_maps(): 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);
config(['investor-path.enabled' => true, 'suitecrm.enabled' => false]);
$this->actingAs(User::factory()->create(['email_verified_at' => now()]));
$draft = json_decode(file_get_contents(base_path('outputs/iva-l3-c-20261025/calculated.json')), true, flags: JSON_THROW_ON_ERROR);
$id = $this->postJson('/api/v1/simulation-drafts', ['personal_snapshot' => [], 'economic_snapshot' => $draft], ['Idempotency-Key' => (string) Str::uuid()])->assertCreated()->json('data.id');
$recovered = $this->getJson('/api/v1/simulation-drafts/'.$id)->assertOk()->json('data.economic_snapshot');
$this->assertSame($draft['modelo_a_snapshot'], $recovered['modelo_a_snapshot']);
$s = json_decode($recovered['modelo_a_snapshot'], true, flags: JSON_THROW_ON_ERROR);
$b = $s['ivaSubstitutions']['opportunity:373'];
$this->assertCount(1, $b['rows']);
$this->assertCount(1, $b['history']);
$r = $b['result']['rows'][0];
$this->assertSame('determined', $r['status']);
foreach (['17' => '220', '18' => '0', '21' => '60', '22' => '60', '23' => '1', '24' => '61'] as $f => $v) {
$this->assertSame(['numerator' => $v, 'denominator' => '1'], $r['fields'][(string) $f]);
}
foreach (['19', '20', '25', '26', '27'] as $f) {
$this->assertNull($r['fields'][(string) $f]);
}
foreach (['paid' => '20', 'outstanding' => '41', 'totalPaid' => '50', 'totalOutstanding' => '83'] as $f => $v) {
$this->assertSame(['numerator' => $v, 'denominator' => '1'], $r[$f]);
}
$input = $b['result']['inputs'][0];
$this->assertSame('SYNTHETIC QA declaraçãoMarço', $input['priorReference']);
$this->assertSame('2026-04-15', $input['priorDate']);
$this->assertSame('SYNTHETIC QA reciboP2', $input['payments'][0]['reference']);
$prior = $s['ivaClosures']['opportunity:373']['result']['rows'][2];
$this->assertSame(['numerator' => '72', 'denominator' => '1'], $prior['fields']['24']);
$this->assertSame(['numerator' => '30', 'denominator' => '1'], $prior['paid']);
$this->assertSame(['numerator' => '42', 'denominator' => '1'], $prior['outstanding']);
$accepted = json_decode(file_get_contents(base_path('tests/frontend/fixtures/iva-l3c-preserved.json')), true, flags: JSON_THROW_ON_ERROR);
$accepted = json_decode($accepted['modelo_a_snapshot'], true, flags: JSON_THROW_ON_ERROR);
$this->assertSame($accepted['ivaClosures'], $s['ivaClosures']);
$frozen = json_decode(file_get_contents(base_path('tests/frontend/fixtures/iva-l3ab-preserved.json')), true, flags: JSON_THROW_ON_ERROR);
foreach (json_decode($frozen['modelo_a_snapshot'], true, flags: JSON_THROW_ON_ERROR) as $key => $map) {
if ($key === 'version') {
continue;
}foreach ($map as $context => $original) {
$this->assertSame($original, $s[$key][$context]);
}
}
file_put_contents(base_path('outputs/iva-l3-c-20261025/recovered.json'), json_encode($recovered, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE));
}
}