import test from 'node:test'; import assert from 'node:assert/strict'; import { forecastBusinessPlan, serializeBusinessPlanSnapshot, restoreBusinessPlanSnapshot, exportBusinessPlanCsv, } from '../../resources/js/lib/business-financial-plan.ts'; import { buildIntegralFinancialTables } from '../../resources/js/lib/integral-financial-report.ts'; import { buildFinancialFiscalBridge } from '../../resources/js/lib/financial-fiscal-bridge.ts'; import { periodInvestmentsFixture as fixture } from './fixtures/period-investments.mjs'; test('F22 unfinished regime selection remains editable without losing the new contract', () => { const p = fixture(); p.accountingRegime = undefined; const raw = serializeBusinessPlanSnapshot(p); assert.ok(restoreBusinessPlanSnapshot(raw, { editable: true })); assert.equal(restoreBusinessPlanSnapshot(raw), null); p.periodInvestments.enabled = false; const inactive = restoreBusinessPlanSnapshot( serializeBusinessPlanSnapshot(p), { editable: true }, ); assert.ok(inactive); assert.deepEqual(inactive.periodInvestments, p.periodInvestments); }); test('F22 year transition retains the asset before readiness and carries only the identified financing debt', () => { const p = fixture(14); const f = p.periodInvestments.financings[0]; p.periodInvestments.financings = [f]; f.drawdowns[0].date = '2027-12-15'; f.agenda.forEach((a) => Object.assign(a, { interestExpense: '0', interestPaid: '0', principal: '0', }), ); f.agenda[12].principal = '200'; f.agenda[13].principal = '300'; Object.assign(p.periodInvestments.assets[0], { recognitionDate: '2027-12-20', paymentDate: '2027-12-21', availableDate: '2028-01-16', lifeMonths: '1', }); const r = forecastBusinessPlan(p), a = r.periodInvestmentLedger.assets[0]; assert.equal(r.periodInvestmentLedger.monthly[11].bankBalance, '500.00'); assert.equal(r.periodInvestmentLedger.monthly[12].bankBalance, '300.00'); assert.equal(a.monthly[11].net, '100.00'); assert.equal(a.monthly[11].depreciation, '0.00'); assert.equal(a.monthly[12].depreciation, '51.61'); assert.equal(a.monthly[13].depreciation, '48.39'); const tables = buildIntegralFinancialTables(p); assert.ok(tables.every((t) => !t.issues.length)); }); test('F22 bank and shareholder funding remain separate, enter cash on disbursement, and never become sales', () => { const p = fixture(), r = forecastBusinessPlan(p), m = r.periodInvestmentLedger.monthly; assert.deepEqual( m.map((r) => r.borrowings), ['0.00', '500.00', '200.00', '0.00', '0.00', '0.00', '0.00'], ); assert.deepEqual( m.slice(0, 4).map((r) => r.bankBalance), ['0.00', '500.00', '400.00', '0.00'], ); assert.deepEqual( m.slice(0, 4).map((r) => r.shareholderBalance), ['0.00', '0.00', '200.00', '0.00'], ); assert.equal(r.totalRevenue, '0.00'); assert.equal(r.totalCashCollections, '0.00'); assert.equal(r.rows[1].closingCash, '1500.00'); assert.equal(r.rows[2].closingCash, '1590.00'); assert.equal(r.rows[3].closingCash, '890.00'); assert.equal( r.periodInvestmentLedger.financings[0].monthly[1].interestBalance, '5.00', ); const before = structuredClone(p); forecastBusinessPlan(p); assert.deepEqual(p, before); }); test('F22 recognition, cash, debt, interest and asset balances reconcile monthly and annually in both regimes', () => { for (const regime of ['PGC-PE', 'PGC-NIRF']) { const p = fixture(7, regime), r = forecastBusinessPlan(p), tables = buildIntegralFinancialTables(p); assert.ok( tables.every((t) => !t.issues.length), JSON.stringify(tables.map((t) => t.issues)), ); const balance = tables.find((t) => t.id === 'balance'); assert.deepEqual( balance.rows.find((r) => r[0] === 'Diferença do balanço').slice(1), Array(8).fill('0.00'), ); assert.equal(r.periodInvestmentLedger.assets[0].firstFraction, '15/30'); assert.deepEqual( r.periodInvestmentLedger.assets[0].monthly.map( (r) => r.depreciation, ), ['0.00', '0.00', '0.00', '16.67', '33.33', '33.33', '16.67'], ); assert.equal( r.periodInvestmentLedger.assets[0].remainingDepreciation, '0.00', ); assert.doesNotThrow(() => buildFinancialFiscalBridge(r)); assert.match(exportBusinessPlanCsv(p), /BANK-1/); assert.match(exportBusinessPlanCsv(p), /2027-04-16/); } }); test('F22 rejects cross-financing overpayments, duplicate origins, unknown agendas and invalid amounts', () => { const p = fixture(); p.periodInvestments.financings[0].agenda[2].principal = '501'; assert.throws( () => forecastBusinessPlan(p), /dívida disponível deste financiamento/, ); p.periodInvestments.financings[0].agenda[2].principal = '100'; p.periodInvestments.financings[1].id = 'BANK-1'; assert.throws(() => forecastBusinessPlan(p), /duplicado/); p.periodInvestments.financings[1].id = 'SHARE-1'; p.periodInvestments.financings[0].agenda.pop(); assert.throws(() => forecastBusinessPlan(p), /agenda completa/); p.periodInvestments.financings[0].agenda.push({ month: 7, interestExpense: '', interestPaid: '0', principal: '0', }); assert.throws(() => forecastBusinessPlan(p), /Juros reconhecidos/); }); test('F22 excludes advances even in the same month and excludes asset purchase credit', () => { const p = fixture(), a = p.periodInvestments.assets[0]; a.paymentDate = '2027-03-31'; assert.throws(() => forecastBusinessPlan(p), /antecipado excluído/); a.paymentDate = '2027-05-01'; assert.throws(() => forecastBusinessPlan(p), /mesmo período/); a.paymentDate = '2027-04-02'; a.recognitionDate = '2027-04-03'; assert.throws(() => forecastBusinessPlan(p), /antecipado excluído/); a.recognitionDate = '2027-04-01'; a.availableDate = ''; assert.throws(() => forecastBusinessPlan(p), /Disponibilidade para uso/); }); test('F22 explicit regime and linear justification are required, and non-depreciable cost is retained', () => { const p = fixture(7, 'PGC-NIRF'), a = p.periodInvestments.assets[0]; a.linearBasis = ''; assert.throws(() => forecastBusinessPlan(p), /Fundamentação linear/); p.accountingRegime = undefined; assert.throws(() => forecastBusinessPlan(p), /Regime/); p.accountingRegime = 'PGC-PE'; a.depreciable = false; a.base = '0'; a.lifeMonths = ''; a.availableDate = ''; const r = forecastBusinessPlan(p); assert.equal( r.periodInvestmentLedger.assets[0].monthly.at(-1).net, '100.00', ); assert.ok( r.periodInvestmentLedger.assets[0].monthly.every( (r) => r.depreciation === '0.00', ), ); }); test('F22 full first month, leap February, cent rounding and horizons 1/60 retain the approved closing rule', () => { for (const available of ['2027-04-01', '2028-02-29']) { const p = fixture(4); p.periodInvestments.financings = []; const a = p.periodInvestments.assets[0]; a.recognitionDate = available; a.paymentDate = available; a.availableDate = available; a.base = '0.01'; a.cost = '0.01'; a.lifeMonths = '1'; p.reportAssumptions.startYear = available.slice(0, 4); const r = forecastBusinessPlan(p); assert.equal( r.periodInvestmentLedger.assets[0].monthly.at(-1).accumulated, '0.01', ); } for (const periods of [1, 60]) { const p = fixture(periods); p.periodInvestments.financings = []; const a = p.periodInvestments.assets[0]; a.recognitionDate = '2027-01-01'; a.paymentDate = '2027-01-01'; a.availableDate = '2027-01-16'; a.lifeMonths = '600'; const r = forecastBusinessPlan(p), end = r.periodInvestmentLedger.assets[0]; assert.ok(Number(end.remainingDepreciation) > 0); assert.ok(Number(end.monthly.at(-1).net) > 0); } const future = fixture(1), r = forecastBusinessPlan(future); assert.ok(r.periodInvestmentLedger.assets[0].outsideHorizon); assert.equal(r.periodInvestmentLedger.monthly[0].acquisitions, '0.00'); assert.equal(r.periodInvestmentLedger.monthly[0].borrowings, '0.00'); }); test('F22 snapshot 23 recovers the complete contract and inactive mode preserves every legacy result', () => { const p = fixture(), snapshot = serializeBusinessPlanSnapshot(p); assert.equal(JSON.parse(snapshot).version, 23); const restored = restoreBusinessPlanSnapshot(snapshot); assert.ok(restored); assert.deepEqual(restored.periodInvestments, p.periodInvestments); assert.deepEqual(forecastBusinessPlan(restored), forecastBusinessPlan(p)); assert.equal( restoreBusinessPlanSnapshot( snapshot.replace('"version":23', '"version":22'), ), null, ); p.periodInvestments.enabled = false; assert.deepEqual( forecastBusinessPlan(p), forecastBusinessPlan({ ...p, periodInvestments: undefined }), ); p.periodInvestments.enabled = true; p.periodInvestments.assets[0].paymentDate = '2027-05-01'; assert.equal( restoreBusinessPlanSnapshot(serializeBusinessPlanSnapshot(p)), null, ); assert.ok( restoreBusinessPlanSnapshot(serializeBusinessPlanSnapshot(p), { editable: true, }), ); });