import test from 'node:test'; import assert from 'node:assert/strict'; import { internationalCase } from './helpers/irpc-international-case.mjs'; import { newPtMzRoyaltyPreparation } from '../../resources/js/lib/irpc-international-credit.ts'; import { calculateAnnual, recordAnnual, saveAnnualBook, annualBook, annualCurrent, annualHtml, } from '../../resources/js/lib/irpc-annual-settlement.ts'; import { createProjectDossierReport } from '../../resources/js/lib/project-dossier-report.ts'; function agreementCase() { const c = internationalCase(); Object.assign(c.b.internationalCredits.positions[0], { country: 'PT', qualificationCountry: 'PT', incomeAmount: '40', treaty: 'yes', treatyMethod: 'ptMzInterest', treatyReference: 'SYNTHETIC QA explicit PT-MZ11(3)b/23(3)a scope', treatyPreparation: { ...newPtMzRoyaltyPreparation(), gross: '60', from: '2026-01-01', to: '2026-12-31', reference: 'SYNTHETIC residence and interest contract conditions', residentMz: 'yes', beneficialOwner: 'yes', sourcePortugal: 'yes', payerResidentPt: 'yes', noLinkedEstablishment: 'yes', armLength: 'yes', capitalDefinition: 'yes', governmentInterestExemption: 'yes', governmentBasis: 'agreement', institutionRecipient: 'yes', financingGranted: 'yes', bilateralAgreement: 'yes', institutionReference: 'SYNTHETIC current subject123456789 is qualifying institution', financingReference: 'SYNTHETIC financing granted by this institution and linked to interest', agreementReference: 'SYNTHETIC PT-MZ intergovernmental agreement and applicable financing clause', }, }); return c; } void test('qualified institution financing under a PT-MZ governmental agreement gives zero source tax cap without altering L6', () => { const c = agreementCase(), before = c.d.modelo_a_snapshot, m = calculateAnnual(c.d, c.b), p = m.internationalCredit.positions[0]; assert.deepEqual(m.pending, []); assert.deepEqual(p.treatyLimit, { numerator: '0', denominator: '1' }); assert.equal(p.treatyDerivation.governmentExemption.basis, 'agreement'); assert.deepEqual(p.aboveLimit, { numerator: '10', denominator: '1' }); assert.deepEqual(m.arithmeticBalance, { numerator: '708', denominator: '1', }); assert.equal(m.internationalCredit.officialBalancesConsumed, false); assert.equal(c.d.modelo_a_snapshot, before); }); void test('institution identity alone does not prove a qualifying financing or governmental agreement', () => { for (const patch of [ { institutionRecipient: '' }, { financingGranted: '' }, { bilateralAgreement: 'no' }, { institutionReference: '' }, { financingReference: '' }, { agreementReference: '' }, { governmentBasis: '' }, ]) { const c = agreementCase(); Object.assign( c.b.internationalCredits.positions[0].treatyPreparation, patch, ); assert.equal(calculateAnnual(c.d, c.b).arithmeticBalance, null); } }); void test('changing or clearing the basis preserves the old result but cannot reuse the former exemption', () => { const c = agreementCase(), book = recordAnnual(c.d, c.b, '2026-11-10T18:20:00+02:00'), old = structuredClone(book.result); Object.assign(book.internationalCredits.positions[0].treatyPreparation, { governmentBasis: '', governmentPayer: 'state', governmentPayerReference: 'SYNTHETIC previous public-payer classification', }); assert.equal(annualCurrent(c.d, book), false); assert.equal(calculateAnnual(c.d, book).arithmeticBalance, null); assert.deepEqual(book.result, old); }); void test('agreement qualification and both documentary links survive recovery and reach both report consumers', () => { const c = agreementCase(), book = recordAnnual(c.d, c.b, '2026-11-10T18:20:00+02:00'), d = JSON.parse( JSON.stringify({ ...c.d, modelo_a_snapshot: saveAnnualBook(c.d, book), }), ), back = annualBook(d); assert.equal(annualCurrent(d, back), true); assert.deepEqual( calculateAnnual(d, back).internationalCredit, back.result.model.internationalCredit, ); const html = annualHtml(d); assert.match(html, /IRPC-PT-MZ-agreement-interest\/1/); assert.match(html, /SYNTHETIC financing granted/); assert.match(html, /SYNTHETIC PT-MZ intergovernmental agreement/); assert.match( JSON.stringify( createProjectDossierReport(d, { selectedSections: new Set(['finance']), generatedAt: '2026-11-10T18:20:00+02:00', }), ), /IRPC-PT-MZ-agreement-interest\/1/, ); });