205 lines
7.4 KiB
JavaScript
205 lines
7.4 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import fs from 'node:fs';
|
|
import { createRequire } from 'node:module';
|
|
import { compileFunction } from 'node:vm';
|
|
import ts from 'typescript';
|
|
import React from 'react';
|
|
import { renderToStaticMarkup } from 'react-dom/server';
|
|
import * as business from '../../resources/js/lib/business-financial-plan.ts';
|
|
import * as integral from '../../resources/js/lib/integral-financial-report.ts';
|
|
import * as startup from '../../resources/js/lib/startup-asset-requirements.ts';
|
|
import * as presentation from '../../resources/js/lib/financial-issue-presentation.ts';
|
|
import * as annual from '../../resources/js/lib/annual-example-report.ts';
|
|
import * as warnings from '../../resources/js/lib/financial-plan-warnings.ts';
|
|
import { financialReportFixture } from './fixtures/integral-financial-plan.mjs';
|
|
|
|
const require = createRequire(import.meta.url);
|
|
const modules = {
|
|
'@/lib/business-financial-plan': business,
|
|
'@/lib/integral-financial-report': integral,
|
|
'@/lib/startup-asset-requirements': startup,
|
|
'@/lib/financial-issue-presentation': presentation,
|
|
'@/lib/annual-example-report': annual,
|
|
'@/lib/financial-plan-warnings': warnings,
|
|
'./business-plan-warnings': { BusinessPlanWarnings: () => null },
|
|
};
|
|
function component(name) {
|
|
if (modules[`./${name}`]) return modules[`./${name}`];
|
|
const code = ts.transpileModule(
|
|
fs.readFileSync(
|
|
new URL(
|
|
`../../resources/js/components/${name}.tsx`,
|
|
import.meta.url,
|
|
),
|
|
'utf8',
|
|
),
|
|
{
|
|
compilerOptions: {
|
|
module: ts.ModuleKind.CommonJS,
|
|
target: ts.ScriptTarget.ES2022,
|
|
jsx: ts.JsxEmit.ReactJSX,
|
|
},
|
|
},
|
|
).outputText;
|
|
const module = { exports: {} };
|
|
compileFunction(code, ['require', 'module', 'exports'])(
|
|
(id) =>
|
|
modules[id] ??
|
|
(id.startsWith('./') ? component(id.slice(2)) : require(id)),
|
|
module,
|
|
module.exports,
|
|
);
|
|
modules[`./${name}`] = module.exports;
|
|
return module.exports;
|
|
}
|
|
const { StartupAssetRequirements } = component('startup-asset-requirements');
|
|
const { IntegralFinancialPlan } = component('integral-financial-plan');
|
|
const planWithStartup = () => ({
|
|
...financialReportFixture(2),
|
|
startupAssetRequirements: {
|
|
cashRequired: '',
|
|
otherCurrentAssetsRequired: '0',
|
|
longTermAssetsRequired: '600',
|
|
priorAssetInvestment: '600',
|
|
},
|
|
});
|
|
|
|
test('D1: one startup diagnostic across real local and report components; standalone and unreadable report retain it', () => {
|
|
const plan = planWithStartup(),
|
|
error = presentation.startupErrorMessage(plan);
|
|
const render = () =>
|
|
renderToStaticMarkup(
|
|
React.createElement(
|
|
React.Fragment,
|
|
null,
|
|
React.createElement(StartupAssetRequirements, {
|
|
plan,
|
|
onChange() {},
|
|
showError: !presentation.forecastErrorShownInReport(
|
|
plan,
|
|
presentation.startupErrorMessage(plan),
|
|
),
|
|
}),
|
|
React.createElement(IntegralFinancialPlan, {
|
|
plan,
|
|
onChange() {},
|
|
}),
|
|
),
|
|
);
|
|
assert.ok(error.includes('caixa requerida'));
|
|
assert.equal(render().split(error).length - 1, 1);
|
|
assert.equal(
|
|
renderToStaticMarkup(
|
|
React.createElement(StartupAssetRequirements, {
|
|
plan,
|
|
onChange() {},
|
|
}),
|
|
).split(error).length - 1,
|
|
1,
|
|
);
|
|
plan.reportAssumptions.startupExpenses = 'invalid';
|
|
assert.equal(render().split(error).length - 1, 1);
|
|
plan.reportSource = 'annual_example';
|
|
assert.equal(presentation.forecastErrorShownInReport(plan, error), false);
|
|
plan.startupAssetRequirements.cashRequired = '0';
|
|
plan.reportAssumptions = financialReportFixture(2).reportAssumptions;
|
|
assert.equal(presentation.startupErrorMessage(plan), '');
|
|
delete plan.reportSource;
|
|
plan.reportAssumptions = financialReportFixture(2).reportAssumptions;
|
|
assert.ok(!render().includes(error));
|
|
});
|
|
|
|
test('D2: depreciation and principal diagnostics are independent, both retained, and disappear on correction', () => {
|
|
const plan = financialReportFixture(2);
|
|
plan.bankLoanProceeds = '100';
|
|
plan.depreciationLifeMonths = 1;
|
|
const issues = () =>
|
|
integral.buildIntegralFinancialTables(plan).flatMap((t) => t.issues);
|
|
plan.assetDepreciation = '700';
|
|
assert.equal(
|
|
issues().filter((x) => x.includes('Base depreciável contabilística'))
|
|
.length,
|
|
2,
|
|
);
|
|
assert.ok(!issues().some((x) => x.includes('Reembolso de capital')));
|
|
plan.assetDepreciation = '600';
|
|
plan.monthlyPrincipalRepayment = '200';
|
|
assert.equal(
|
|
issues().filter((x) => x.includes('Reembolso de capital')).length,
|
|
2,
|
|
);
|
|
assert.ok(
|
|
!issues().some((x) => x.includes('Base depreciável contabilística')),
|
|
);
|
|
plan.assetDepreciation = '700';
|
|
assert.equal(issues().length, 4);
|
|
plan.assetDepreciation = '600';
|
|
plan.monthlyPrincipalRepayment = '0';
|
|
assert.deepEqual(issues(), []);
|
|
const recovered = business.restoreBusinessPlanSnapshot(
|
|
business.serializeBusinessPlanSnapshot(plan),
|
|
);
|
|
assert.deepEqual(
|
|
business.forecastBusinessPlan(recovered),
|
|
business.forecastBusinessPlan(plan),
|
|
);
|
|
});
|
|
|
|
function supplier() {
|
|
const plan = financialReportFixture(2);
|
|
plan.useProductInventorySchedule = true;
|
|
plan.useSupplierPaymentTerms = true;
|
|
plan.products[0] = {
|
|
...plan.products[0],
|
|
label: 'Fornecedor sintético V12',
|
|
monthlyUnits: '0',
|
|
productInventoryOpening: '0',
|
|
productInventoryPurchasesByPeriod: [
|
|
{
|
|
amount: '200',
|
|
leadMonths: 0,
|
|
paymentInstallments: [
|
|
{ lagMonths: 0, percent: '60' },
|
|
{ lagMonths: 1, percent: '40' },
|
|
],
|
|
},
|
|
{ amount: '0', leadMonths: 0, paymentInstallments: [] },
|
|
],
|
|
};
|
|
return plan;
|
|
}
|
|
test('D3: 110% rejects with product, order month and percentage; 100% restores exact payments and unrelated validation remains', () => {
|
|
const plan = supplier(),
|
|
valid = business.forecastBusinessPlan(plan);
|
|
const parts =
|
|
plan.products[0].productInventoryPurchasesByPeriod[0]
|
|
.paymentInstallments;
|
|
parts[1].percent = '50';
|
|
assert.throws(
|
|
() => business.forecastBusinessPlan(plan),
|
|
/Fornecedor sintético V12 — encomenda do mês 1:.*excede 100%/,
|
|
);
|
|
assert.equal(
|
|
business.restoreBusinessPlanSnapshot(
|
|
business.serializeBusinessPlanSnapshot(plan),
|
|
),
|
|
null,
|
|
);
|
|
parts[1].percent = '40';
|
|
assert.deepEqual(business.forecastBusinessPlan(plan), valid);
|
|
assert.deepEqual(
|
|
business.forecastBusinessPlan(
|
|
business.restoreBusinessPlanSnapshot(
|
|
business.serializeBusinessPlanSnapshot(plan),
|
|
),
|
|
),
|
|
valid,
|
|
);
|
|
plan.products[0].productInventoryPurchasesByPeriod[0].leadMonths = 13;
|
|
assert.throws(
|
|
() => business.forecastBusinessPlan(plan),
|
|
/declare purchases and lead times/,
|
|
);
|
|
});
|