import assert from 'node:assert/strict'; import fs from 'node:fs'; import test from 'node:test'; import { printedCents, reconcileFinancialReference, } from '../../tools/financial-reference-reconciliation.mjs'; import { newAnnualReferencePlan } from '../../resources/js/lib/business-plan-guidance.ts'; const source = fs.readFileSync( new URL('./fixtures/financial-reference-2025.txt', import.meta.url), 'utf8', ); const report = reconcileFinancialReference(source); const item = (id) => { const record = report.checks.find((check) => check.id === id); assert.ok(record, id); return record; }; void test('printed amounts retain thousands, decimal cents and parentheses without floating-point rounding', () => { assert.equal(printedCents('1.234,56'), 123456n); assert.equal(printedCents('(416)'), -41600n); assert.equal(printedCents('0,01'), 1n); assert.equal(printedCents('501.475'), 50147500n); for (const invalid of ['', '1,234', '1.00', 'unknown', '17%']) assert.throws(() => printedCents(invalid)); }); void test('the one-unit imbalance is traced through the printed cash-spending subtotal without changing annual data', () => { const approvedBefore = JSON.stringify(newAnnualReferencePlan()); assert.deepEqual(report.oneUnitTrace, { approvedCashSpent: '15783.00', componentCashSpent: '15782.00', approvedClosingCash: '90756.00', componentOnlyClosingCash: '90757.00', approvedAssets: '501475.00', componentOnlyAssets: '501476.00', liabilitiesAndCapital: '501476.00', componentOnlyIsDiagnosticNotCorrection: true, }); assert.equal(item('cash.2025_spending_components').difference, '-1.00'); assert.equal( item('balance.2025_assets_vs_liabilities_capital').difference, '-1.00', ); assert.equal(report.annualTotalsChanged, false); assert.equal(report.applicationFormulasChanged, false); assert.equal(JSON.stringify(newAnnualReferencePlan()), approvedBefore); }); void test('sales, direct costs, gross margin and payroll reconcile at total level but listed revenue and cost lines are incomplete', () => { for (const id of [ 'sales.months_to_2025', 'direct_costs.months_to_2025', 'gross_margin.months_to_2025', 'payroll.months_to_2025', ]) assert.equal(item(id).matches, true); assert.equal(item('sales.listed_lines.2025').difference, '-3652.00'); assert.equal(item('costs.listed_lines.2025').difference, '-2243.00'); assert.equal(item('sales.listed_lines.2026').difference, '-8250.00'); assert.equal(item('sales.listed_lines.2027').difference, '-9350.00'); for (const record of report.checks.filter( (check) => check.id.startsWith('sales.line.') || check.id.startsWith('costs.line.'), )) assert.equal(record.matches, true); }); void test('the stated reference rate reconstructs cent precision and preserves annual payroll charges and profit', () => { const precise = report.payrollRecomposition; assert.equal(precise.rate, '15%'); assert.deepEqual(precise.monthlyCharges, Array(12).fill('26.25')); assert.deepEqual(precise.monthlyOperating, Array(12).fill('201.25')); assert.equal(precise.monthlyNetProfit[8], '-0.25'); assert.equal(precise.chargesAnnualCheck.calculated, '315.00'); assert.equal(precise.operatingAnnualCheck.calculated, '2415.00'); assert.equal(precise.profitAnnualCheck.calculated, '-416.00'); assert.ok( precise.chargesAnnualCheck.matches && precise.operatingAnnualCheck.matches && precise.profitAnnualCheck.matches, ); assert.equal(precise.scope, 'reference_arithmetic_only_not_a_fiscal_rule'); assert.equal( item('net_profit.printed_months_to_2025').calculated, '-413.00', ); }); void test('cash discrepancies and ambiguous duplicate labels remain explicit rather than being reclassified as investment', () => { assert.equal( item('cash.Caixa proveniente de contas a receber').difference, '2.00', ); assert.equal(item('cash.Subtotal de Dinheiro Gasto').difference, '-1.00'); assert.equal(item('cash.Fluxo de Caixa LĂ­quido').calculated, '755.00'); assert.equal(report.balanceMonths.at(-1).componentDifference, '-1.00'); assert.deepEqual( report.ambiguousCashLabel.map((row) => row.sum), ['0.00', '16538.00'], ); assert.equal(report.status, 'BLOCKED_SOURCE_PRECISION_OR_COMPLETENESS'); assert.equal(report.missing.length, 4); }); void test('normalised labels preserve the same numerical conclusions and missing source rows do not become zeros', () => { assert.deepEqual( reconcileFinancialReference(source.normalize('NFD')).checks, report.checks, ); assert.throws( () => reconcileFinancialReference( source.replaceAll( 'Caixa proveniente de contas a receber', 'Rubrica retirada', ), ), /Rubrica em falta/, ); });