import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; 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 { advanceFixture, event } from './fixtures/supplier-advances.mjs'; function nirf() { const p = advanceFixture(); p.accountingRegime = 'PGC-NIRF'; p.supplierAdvances.regime = 'PGC-NIRF'; Object.assign(p.supplierAdvances.operations[0], { formalAgreement: { date: '2027-02-10', reference: 'ACORDO-SINTETICO-01', }, events: [ event('A', 'advance', '2027-01-10', '30000'), event('B', 'apply', '2027-02-15', '30000'), event('C', 'settle', '2027-04-01', '70000'), ], }); return p; } test('PGC-NIRF applies on recognized supply and dated formal agreement before invoice, no duplicate cash', () => { const p = nirf(), r = forecastBusinessPlan(p), l = r.supplierAdvanceLedger; assert.deepEqual( l.monthly.map((m) => [m.obligation, m.advance]), [ ['0.00', '30000.00'], ['70000.00', '0.00'], ['70000.00', '0.00'], ['0.00', '0.00'], ], ); assert.deepEqual( r.rows.map((m) => m.variableCostPayments), ['30000.00', '0.00', '0.00', '70000.00'], ); assert.deepEqual(l.operations[0].events[1].applicationBasis, { kind: 'formal_agreement', date: '2027-02-10', reference: 'ACORDO-SINTETICO-01', }); assert.equal( l.operations[0].monthly[1].documentaryStatus, 'formal_agreement', ); assert.equal(l.operations[0].monthly[2].documentaryStatus, 'invoice'); const tables = buildIntegralFinancialTables(p); assert.ok(tables.every((t) => t.issues.length === 0)); assert.ok( tables .find((t) => t.id === 'balance') .rows.find((r) => r[0] === 'Diferença do balanço') .slice(1) .every((v) => v === '0.00'), ); assert.match( tables.flatMap((t) => t.notes).join(' '), /ACORDO-SINTETICO-01/, ); assert.match(exportBusinessPlanCsv(p), /PGC-NIRF/); assert.match(exportBusinessPlanCsv(p), /ACORDO-SINTETICO-01/); assert.equal( buildFinancialFiscalBridge(r).accountingLines.find( (l) => l.code === 'revenue', ).total, '0.00', ); }); test('PGC-NIRF still requires recognition and sufficient dated basis; agreement is not a receipt', () => { const p = nirf(), o = p.supplierAdvances.operations[0]; o.events[1].date = '2027-02-09'; assert.throws( () => forecastBusinessPlan(p), /aplicação exige reconhecimento/, ); o.events[1].date = '2027-02-15'; o.formalAgreement.date = '2027-02-16'; assert.throws( () => forecastBusinessPlan(p), /acordo formal correspondente/, ); o.formalAgreement.date = '2027-02-10'; o.formalAgreement.reference = ' '; assert.throws(() => forecastBusinessPlan(p), /referência identificada/); o.formalAgreement.reference = 'A'; o.formalAgreement.date = '2027-02-30'; assert.throws(() => forecastBusinessPlan(p), /data válida/); delete o.formalAgreement; assert.throws( () => forecastBusinessPlan(p), /acordo formal correspondente/, ); o.events[1].date = '2027-03-01'; assert.equal( forecastBusinessPlan(p).supplierAdvanceLedger.operations[0].events[1] .applicationBasis.kind, 'invoice', ); }); test('PGC-PE never uses agreement to apply before invoice, even in explicit-regime plans', () => { const p = nirf(); p.accountingRegime = p.supplierAdvances.regime = 'PGC-PE'; assert.throws( () => forecastBusinessPlan(p), /aplicação exige reconhecimento e factura/, ); p.supplierAdvances.operations[0].events[1].date = '2027-03-01'; assert.equal( forecastBusinessPlan(p).supplierAdvanceLedger.monthly[1].advance, '30000.00', ); }); test('PGC-NIRF identity includes agreement across parcels; no shared balance or future settlement', () => { const p = nirf(), o = p.supplierAdvances.operations[0]; o.amount = '50000'; o.events[2].amount = '20000'; p.supplierAdvances.operations.push({ ...structuredClone(o), parcelId: 'P-2', events: [], formalAgreement: { date: '2027-02-10', reference: 'DIFFERENT' }, }); assert.throws(() => forecastBusinessPlan(p), /parcelas da mesma operação/); p.supplierAdvances.operations[1].formalAgreement = { reference: o.formalAgreement.reference, date: o.formalAgreement.date, }; assert.doesNotThrow(() => forecastBusinessPlan(p)); // JSON property order is not a different agreement. p.supplierAdvances.operations.pop(); o.amount = '100000'; o.events[1].amount = '30001'; assert.throws(() => forecastBusinessPlan(p), /excede o saldo/); o.events[1].amount = '30000'; o.events[2].date = '2027-02-20'; assert.throws( () => forecastBusinessPlan(p), /remanescente exige reconhecimento e factura/, ); }); test('snapshot 22 requires explicit consistent regime, carries agreement and permits editing incomplete basis', () => { const p = nirf(), s = serializeBusinessPlanSnapshot(p), r = restoreBusinessPlanSnapshot(s); assert.equal(JSON.parse(s).version, 22); assert.ok(r); assert.equal(r.accountingRegime, 'PGC-NIRF'); assert.deepEqual(r.supplierAdvances, p.supplierAdvances); assert.deepEqual(forecastBusinessPlan(r), forecastBusinessPlan(p)); assert.equal( restoreBusinessPlanSnapshot(s.replace('"version":22', '"version":21')), null, ); p.accountingRegime = 'PGC-PE'; assert.throws( () => forecastBusinessPlan(p), /identificação explícita|diverge/, ); assert.equal( restoreBusinessPlanSnapshot(serializeBusinessPlanSnapshot(p)), null, ); delete p.accountingRegime; assert.throws(() => forecastBusinessPlan(p), /identificação explícita/); p.accountingRegime = 'PGC-NIRF'; p.supplierAdvances.operations[0].formalAgreement.reference = ''; assert.equal( restoreBusinessPlanSnapshot(serializeBusinessPlanSnapshot(p)), null, ); assert.ok( restoreBusinessPlanSnapshot(serializeBusinessPlanSnapshot(p), { editable: true, }), ); }); test('accepted snapshot 21 stays PGC-PE locally without inferring plan regime or changing output/tables', () => { for (const name of ['saved-active.json', 'saved-refund.json']) { const s = JSON.parse( fs.readFileSync( new URL( '../../docs/investmoz/v2/auditoria-portal/e26-f20-b-20261121/' + name, import.meta.url, ), 'utf8', ), ); const r = restoreBusinessPlanSnapshot(JSON.stringify(s)); assert.ok(r); assert.equal(r.accountingRegime, undefined); assert.equal(r.supplierAdvances.regime, 'PGC-PE'); assert.equal(JSON.parse(serializeBusinessPlanSnapshot(r)).version, 21); const baselineFile = new URL( './fixtures/supplier-advances-pgc-pe-baseline.json', import.meta.url, ); { const b = JSON.parse(fs.readFileSync(baselineFile, 'utf8')).find( (b) => b.name === name, ); assert.deepEqual(forecastBusinessPlan(r), b.forecast); assert.deepEqual(buildIntegralFinancialTables(r), b.tables); } } });