feat(portal): deliver ordinary national credit fraction and education continuity
tests / ci (push) Canceled after 0s

This commit is contained in:
2026-11-10 12:47:57 +02:00
parent ff726e577f
commit c359364de3
14 changed files with 749 additions and 126 deletions
@@ -0,0 +1,52 @@
<?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 IrpcNationalFractionDraftTest extends TestCase
{
use DatabaseTransactions;
public function test_derived_national_fraction_round_trips_through_the_existing_private_draft_writer(): void
{
$target = DB::selectOne('select current_database() as name,inet_server_port() as port');
$this->assertSame('proactive_investment_20261027_test', $target->name);
$this->assertSame(55441, (int) $target->port);
config(['investor-path.enabled' => true, 'suitecrm.enabled' => false]);
$this->actingAs(User::findOrFail(1021));
$id = null;
$etag = null;
foreach (['ordinary', 'exact', 'missing'] as $name) {
$snapshot = json_decode(file_get_contents(base_path('outputs/irpc-national-fraction-20261110/'.$name.'.json')), true, flags: JSON_THROW_ON_ERROR);
$personal = ['person_type' => $snapshot['person_type']];
unset($snapshot['person_type']);
$payload = ['personal_snapshot' => $personal, 'economic_snapshot' => $snapshot];
if ($id === null) {
$id = $this->postJson('/api/v1/simulation-drafts', $payload, ['Idempotency-Key' => (string) Str::uuid()])->assertCreated()->json('data.id');
} else {
$this->putJson('/api/v1/simulation-drafts/'.$id, $payload, ['If-Match' => $etag])->assertOk();
}
$response = $this->getJson('/api/v1/simulation-drafts/'.$id)->assertOk();
$etag = $response->headers->get('ETag');
$back = $response->json('data.economic_snapshot');
$this->assertSame($snapshot['modelo_a_snapshot'], $back['modelo_a_snapshot']);
$this->assertSame($snapshot['business_plan_snapshot'], $back['business_plan_snapshot']);
$book = json_decode($back['modelo_a_snapshot'], true, flags: JSON_THROW_ON_ERROR)['irpcAnnual'][$snapshot['context_opportunity']];
$this->assertSame('proportional', $book['internationalCredits']['positions'][0]['nationalMethod']);
$model = $book['result']['model'];
if ($name === 'missing') {
$this->assertSame('indeterminate', $model['status']);
$this->assertNull($model['arithmeticBalance']);
} else {
$this->assertSame(['numerator' => $name === 'ordinary' ? '32' : '2', 'denominator' => $name === 'ordinary' ? '1' : '625'], $model['internationalCredit']['positions'][0]['nationalLimit']);
$this->assertSame('IRPC-national-fraction/1', $model['internationalCredit']['positions'][0]['nationalDerivation']['version']);
}
file_put_contents(base_path('outputs/irpc-national-fraction-20261110/recovered-'.$name.'.json'), json_encode($back, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE));
}
}
}
+63
View File
@@ -3,6 +3,69 @@ import assert from 'node:assert/strict';
import { harness, props, text } from './helpers/education-review.mjs';
const form = 'education-review';
function historyPage(page = 1) {
return {
data: [], total: 24, current_page: page, last_page: 2,
prev_page_url: page === 2 ? '/management/education/demands/one?scope=review&history_page=1' : null,
next_page_url: page === 1 ? '/management/education/demands/one?scope=review&history_page=2' : null,
};
}
test('history paging keeps the prepared review and does not ask to discard it', () => {
const h = harness(props({ history: historyPage() }));
h.change(form, reason, 'SYNTHETIC reasoning still being prepared.');
const next = h.all().find(node => node.type === 'a' && text(node) === 'Seguinte');
assert.equal(next.props.preserveState, true);
assert.equal(next.props.preserveScroll, true);
assert.match(next.props.href, /scope=review&history_page=2$/);
assert.equal(h.leave(next.props.preserveState), false);
assert.equal(h.confirmCalls, 0);
h.render({ ...h.props, history: historyPage(2) });
assert.equal(h.field(form, reason).props.value, 'SYNTHETIC reasoning still being prepared.');
const previous = h.all().find(node => node.type === 'a' && text(node) === 'Anterior');
assert.equal(previous.props.preserveState, true);
assert.equal(previous.props.preserveScroll, true);
assert.equal(h.sent.length, 0);
assert.equal(h.leave(), true); // Leaving the declaration still protects unsaved work.
});
test('history paging keeps an uncertain attempt recoverable under the same command key', () => {
const h = harness(props({ history: historyPage() }));
const request = prepared(h);
h.fail(request);
h.render({ ...h.props, history: historyPage(2) });
const retry = h.submit(form);
assert.deepEqual(retry.data, request.data);
assert.equal(h.sent.length, 2); // One explicit retry; paging itself sends no decision.
});
test('history paging cannot silently approve a newer declaration version', () => {
const h = harness(props({ history: historyPage() }));
h.change(form, reason, 'SYNTHETIC review prepared against version one.');
h.render({
...h.props, history: historyPage(2),
declaration: { ...h.props.declaration, version_no: 2, quantity: 31 },
});
h.submit(form);
assert.equal(h.sent.length, 0);
assert.equal(h.field(form, reason).props.value, 'SYNTHETIC review prepared against version one.');
assert.match(text(h.tree), /A declaração mudou/);
h.click('Confirmar a leitura desta versão');
const request = h.submit(form);
assert.equal(request.data.version_no, 2);
});
test('history paging with a different actor remounts instead of disclosing the prepared reason', () => {
const h = harness(props({ history: historyPage() }));
h.change(form, reason, 'SYNTHETIC private reasoning of the previous actor.');
h.render({
...h.props, actorId: 3, history: historyPage(2),
declaration: { ...h.props.declaration, review_context: { actor_id: 3, commands: [] } },
});
assert.equal(h.field(form, reason).props.value, '');
assert.equal(h.sent.length, 0);
});
test('the private detail renders preserved reasons, unavailable entries and contextual history pagination', () => {
const h = harness(
props({
@@ -0,0 +1,141 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { internationalCase } from './helpers/irpc-international-case.mjs';
import {
calculateAnnual,
annualBook,
annualCurrent,
recordAnnual,
saveAnnualBook,
annualHtml,
annualReportRows,
} from '../../resources/js/lib/irpc-annual-settlement.ts';
import { createProjectDossierReport } from '../../resources/js/lib/project-dossier-report.ts';
export function nationalCase(income = '100') {
const c = internationalCase('100');
Object.assign(c.b.internationalCredits.positions[0], {
nationalMethod: 'proportional',
incomeAmount: income,
nationalBasisReference:
'SYNTHETIC QA: net taxable foreign component, costs/losses/deductions allocated and included in this L6 base; not a real tax determination',
});
return c;
}
const position = (c) => c.b.internationalCredits.positions[0];
void test('national fraction aggregate cannot allocate more than the current base across countries', () => {
const c = nationalCase('3000');
const second = structuredClone(position(c));
Object.assign(second, {
id: 'country-pt',
country: 'PT',
qualificationCountry: 'PT',
});
Object.assign(second.payments[0], {
id: 'tax-pt',
document: 'SYNTHETIC foreign-tax-pt',
});
c.b.internationalCredits.positions.push(second);
const m = calculateAnnual(c.d, c.b);
assert.equal(m.arithmeticBalance, null);
assert.ok(m.pending.some((message) => message.includes('em conjunto')));
});
void test('national fraction derives 1408 x 100 / 4400 before credits and retains the manual entry', () => {
const c = nationalCase();
const m = calculateAnnual(c.d, c.b);
assert.deepEqual(m.pending, []);
const p = m.internationalCredit.positions[0];
assert.deepEqual(p.nationalLimit, { numerator: '32', denominator: '1' });
assert.equal(p.input.nationalLimit, '8');
assert.equal(
p.nationalDerivation.reference,
position(c).nationalBasisReference,
);
assert.deepEqual(m.payable, { numerator: '676', denominator: '1' });
assert.equal(m.internationalCredit.officialBalancesConsumed, false);
});
void test('national fraction calculation survives serialization, retrieval and both existing report renderers', () => {
const c = nationalCase('0.01');
const historical = structuredClone(c.b.result);
const b = recordAnnual(c.d, c.b, '2026-11-10T13:00:00+02:00');
const saved = { ...c.d, modelo_a_snapshot: saveAnnualBook(c.d, b) };
const back = JSON.parse(JSON.stringify(saved));
const recovered = annualBook(back);
assert.equal(annualCurrent(back, recovered), true);
assert.deepEqual(
recovered.result.model.internationalCredit.positions[0].nationalLimit,
{ numerator: '2', denominator: '625' },
);
assert.deepEqual(
calculateAnnual(back, recovered).internationalCredit,
recovered.result.model.internationalCredit,
);
assert.deepEqual(c.b.result, historical);
assert.match(annualHtml(back), /2\/625/);
assert.match(
JSON.stringify(annualReportRows(back)),
/IRPC-national-fraction\/1/,
);
assert.match(
JSON.stringify(
createProjectDossierReport(back, {
selectedSections: new Set(['finance']),
generatedAt: '2026-11-10T13:00:00+02:00',
}),
),
/2\/625/,
);
});
void test('national fraction leaves a case indeterminate without net-income correspondence or with too much income', () => {
for (const patch of [
{ nationalBasisReference: '' },
{ incomeAmount: '4400.01' },
{ nationalMethod: 'unknown' },
]) {
const c = nationalCase();
Object.assign(position(c), patch);
if (patch.nationalMethod)
assert.throws(() => calculateAnnual(c.d, c.b));
else {
const m = calculateAnnual(c.d, c.b);
assert.equal(m.status, 'indeterminate');
assert.equal(m.arithmeticBalance, null);
}
}
});
void test('national fraction rejects stale assessment binding and still applies a documented treaty ceiling', () => {
const c = nationalCase();
Object.assign(position(c), {
treaty: 'yes',
treatyLimit: '6',
treatyReference:
'SYNTHETIC QA case-specific ceiling, no actual treaty assertion',
});
assert.deepEqual(calculateAnnual(c.d, c.b).payable, {
numerator: '702',
denominator: '1',
});
position(c).assessmentKey = 'obsolete';
assert.equal(calculateAnnual(c.d, c.b).arithmeticBalance, null);
});
void test('national fraction keeps historical documented mode unchanged when the optional method is absent', () => {
const c = internationalCase();
const original = calculateAnnual(c.d, c.b);
position(c).nationalMethod = 'documented';
const m = calculateAnnual(c.d, c.b);
assert.deepEqual(
m.internationalCredit.deducted,
original.internationalCredit.deducted,
);
assert.deepEqual(m.payable, { numerator: '700', denominator: '1' });
assert.equal(
m.internationalCredit.positions[0].nationalDerivation,
undefined,
);
});