212 lines
8.2 KiB
JavaScript
212 lines
8.2 KiB
JavaScript
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 { dayTermsFixture, operation } from './fixtures/financial-day-terms.mjs';
|
|
|
|
test('F20-A preserves explicitly classified independent other income when replacing sales collections', () => {
|
|
const p = dayTermsFixture();
|
|
p.monthlyCollections = '100';
|
|
p.reportAssumptions.manualCollectionsKind = 'other_income';
|
|
const r = forecastBusinessPlan(p);
|
|
assert.deepEqual(
|
|
r.rows.map((row) => row.otherCollections),
|
|
['100.00', '100.00', '100.00'],
|
|
);
|
|
assert.equal(r.totalCashCollections, '1300.00');
|
|
const tables = buildIntegralFinancialTables(p);
|
|
assert.deepEqual(
|
|
tables
|
|
.find((t) => t.id === 'profit-loss')
|
|
.rows.find((r) => r[0] === 'Outras receitas de caixa declaradas')
|
|
.slice(1),
|
|
['100.00', '100.00', '100.00', '300.00'],
|
|
);
|
|
assert.deepEqual(
|
|
tables
|
|
.find((t) => t.id === 'balance')
|
|
.rows.find((r) => r[0] === 'Diferença do balanço')
|
|
.slice(1),
|
|
['0.00', '0.00', '0.00', '0.00'],
|
|
);
|
|
});
|
|
|
|
test('F20-A replaces only opted-in cash agendas, preserves recognition and reconciles every monthly balance', () => {
|
|
const p = dayTermsFixture();
|
|
const before = structuredClone(p);
|
|
const r = forecastBusinessPlan(p);
|
|
assert.deepEqual(p, before);
|
|
assert.deepEqual(
|
|
r.rows.map((r) => r.salesCollections),
|
|
['0.00', '0.00', '1000.00'],
|
|
);
|
|
assert.deepEqual(
|
|
r.rows.map((r) => r.variableCostPayments),
|
|
['0.00', '0.00', '400.00'],
|
|
);
|
|
assert.deepEqual(
|
|
r.rows.map((r) => r.operatingPayments),
|
|
['0.00', '0.00', '100.00'],
|
|
);
|
|
assert.equal(r.totalCashCollections, '1000.00');
|
|
assert.equal(r.rows[0].revenue, '1000.00');
|
|
assert.equal(r.rows[0].inventoryReceipts, '400.00');
|
|
assert.equal(r.rows[0].operatingExpenses, '100.00');
|
|
assert.equal(r.salesReceivablesAtEnd, '0.00');
|
|
const tables = buildIntegralFinancialTables(p);
|
|
const balance = tables.find((t) => t.id === 'balance');
|
|
assert.deepEqual(
|
|
balance.rows.find((r) => r[0] === 'Diferença do balanço').slice(1),
|
|
['0.00', '0.00', '0.00', '0.00'],
|
|
);
|
|
assert.deepEqual(
|
|
balance.rows
|
|
.find((r) => r[0] === 'Obrigações operacionais líquidas')
|
|
.slice(1, 4),
|
|
['500.00', '500.00', '0.00'],
|
|
);
|
|
assert.ok(
|
|
tables.every((t) => t.issues.length === 0),
|
|
JSON.stringify(tables.map((t) => t.issues)),
|
|
);
|
|
assert.match(
|
|
tables.find((t) => t.id === 'cashflow').notes.join(' '),
|
|
/2027-03-02.*aproximação/,
|
|
);
|
|
assert.match(exportBusinessPlanCsv(p), /2027-03-02/);
|
|
});
|
|
|
|
test('F20-A calendar days cover zero, individual dates, leap February and year transition', () => {
|
|
const p = dayTermsFixture();
|
|
p.dayTerms.operations.forEach((o) => (o.termDays = '0'));
|
|
assert.equal(forecastBusinessPlan(p).rows[0].salesCollections, '1000.00');
|
|
const sale = p.dayTerms.operations[0];
|
|
Object.assign(sale, {
|
|
recognitionBasis: 'date',
|
|
recognitionDate: '2027-01-20',
|
|
termDays: '30',
|
|
});
|
|
let r = forecastBusinessPlan(p);
|
|
assert.equal(r.dayTermEvents[0].due, '2027-02-19');
|
|
assert.equal(r.dayTermEvents[0].approximate, false);
|
|
sale.recognitionBasis = 'monthly';
|
|
p.reportAssumptions.startYear = '2028';
|
|
assert.equal(forecastBusinessPlan(p).dayTermEvents[0].due, '2028-03-01');
|
|
p.reportAssumptions.startYear = '2027';
|
|
sale.termDays = '365';
|
|
assert.equal(forecastBusinessPlan(p).dayTermEvents[0].due, '2028-01-31');
|
|
});
|
|
|
|
test('F20-A excludes early and unknown payments including within the same month, never silently normalizes them', () => {
|
|
const p = dayTermsFixture();
|
|
const o = p.dayTerms.operations[1];
|
|
Object.assign(o, {
|
|
recognitionBasis: 'date',
|
|
recognitionDate: '2027-01-25',
|
|
originBasis: 'date',
|
|
originDate: '2027-01-01',
|
|
termDays: '0',
|
|
});
|
|
assert.throws(() => forecastBusinessPlan(p), /antecipação excluída/);
|
|
assert.equal(
|
|
restoreBusinessPlanSnapshot(serializeBusinessPlanSnapshot(p)),
|
|
null,
|
|
);
|
|
assert.ok(
|
|
restoreBusinessPlanSnapshot(serializeBusinessPlanSnapshot(p), {
|
|
editable: true,
|
|
}),
|
|
);
|
|
o.termDays = '30';
|
|
assert.equal(forecastBusinessPlan(p).dayTermEvents[1].due, '2027-01-31');
|
|
o.originBasis = 'unknown';
|
|
assert.throws(() => forecastBusinessPlan(p), /origem.*insuficiente/);
|
|
o.originBasis = 'recognition';
|
|
o.recognitionBasis = 'unknown';
|
|
assert.throws(() => forecastBusinessPlan(p), /reconhecimento insuficiente/);
|
|
});
|
|
|
|
test('F20-A parcels reconcile to recognition and reject duplicates without merging distinct equal payments', () => {
|
|
const p = dayTermsFixture();
|
|
const original = p.dayTerms.operations[1];
|
|
original.amount = '200';
|
|
const second = { ...original, parcelId: '2' };
|
|
p.dayTerms.operations.push(second);
|
|
assert.equal(
|
|
forecastBusinessPlan(p).rows[2].variableCostPayments,
|
|
'400.00',
|
|
);
|
|
second.parcelId = '1';
|
|
assert.throws(() => forecastBusinessPlan(p), /duplicada/);
|
|
second.originId = 'another';
|
|
assert.equal(
|
|
forecastBusinessPlan(p).rows[2].variableCostPayments,
|
|
'400.00',
|
|
);
|
|
second.amount = '201';
|
|
assert.throws(() => forecastBusinessPlan(p), /soma dos montantes/);
|
|
});
|
|
|
|
test('F20-A retains future receivables and payables at horizons 1 and 60 and restores manual schedules explicitly', () => {
|
|
for (const periods of [1, 60]) {
|
|
const p = dayTermsFixture(periods);
|
|
p.dayTerms.operations.forEach((o) => {
|
|
o.month = periods;
|
|
o.originId += '-last';
|
|
o.termDays = '365';
|
|
});
|
|
p.products[0].monthlyUnitsByPeriod = Array(periods).fill('0');
|
|
p.products[0].monthlyUnitsByPeriod[periods - 1] = '1';
|
|
p.inventoryPurchaseSchedule.forEach(
|
|
(o, i) => (o.amount = i === periods - 1 ? '400' : '0'),
|
|
);
|
|
p.monthlyOperatingExpensesByPeriod = Array(periods).fill('0');
|
|
p.monthlyOperatingExpensesByPeriod[periods - 1] = '100';
|
|
const r = forecastBusinessPlan(p);
|
|
assert.equal(r.totalCashCollections, '0.00');
|
|
assert.equal(r.supplierPaymentsOutstandingAtEnd, '400.00');
|
|
assert.equal(r.salesReceivablesAtEnd, '1000.00');
|
|
assert.ok(r.dayTermEvents.every((e) => e.outsideHorizon));
|
|
assert.equal(r.rows.at(-1).variableCostPayments, '0.00');
|
|
}
|
|
const p = dayTermsFixture();
|
|
const legacy = { ...p, dayTerms: undefined };
|
|
p.dayTerms.sales = p.dayTerms.purchases = p.dayTerms.services = false;
|
|
assert.deepEqual(forecastBusinessPlan(p), forecastBusinessPlan(legacy));
|
|
});
|
|
|
|
test('F20-A version 20 recovers dates, inactive manual inputs and outcomes; invalid terms and foreign versions are rejected', () => {
|
|
const p = dayTermsFixture();
|
|
const snapshot = serializeBusinessPlanSnapshot(p);
|
|
assert.equal(JSON.parse(snapshot).version, 20);
|
|
const recovered = restoreBusinessPlanSnapshot(snapshot);
|
|
assert.ok(recovered);
|
|
assert.deepEqual(recovered.dayTerms, p.dayTerms);
|
|
assert.equal(recovered.monthlyCollections, '777');
|
|
assert.deepEqual(forecastBusinessPlan(recovered), forecastBusinessPlan(p));
|
|
assert.equal(
|
|
restoreBusinessPlanSnapshot(
|
|
snapshot.replace('"version":20', '"version":19'),
|
|
),
|
|
null,
|
|
);
|
|
for (const termDays of ['', '-1', '366', '1.5', 'abc']) {
|
|
p.dayTerms.operations[0].termDays = termDays;
|
|
assert.throws(() => forecastBusinessPlan(p), /Prazo \(dias\)/);
|
|
}
|
|
p.dayTerms.operations[0] = operation('sales', '1000');
|
|
Object.assign(p.dayTerms.operations[0], {
|
|
recognitionBasis: 'date',
|
|
recognitionDate: '2027-02-30',
|
|
});
|
|
assert.throws(
|
|
() => forecastBusinessPlan(p),
|
|
/reconhecimento insuficiente ou inválida/,
|
|
);
|
|
});
|