feat(qa): separate startup resources and trace original example rows
This commit is contained in:
@@ -152,10 +152,10 @@ void test('the full example shares the saved snapshot and its eleven tables and
|
||||
recovered = reload(p);
|
||||
const tables = buildAnnualExampleTables(recovered);
|
||||
assert.equal(tables.length, 11);
|
||||
assert.equal(cell(tables, 'sales', 'Vendas totais', 1), '14537.25');
|
||||
assert.equal(cell(tables, 'sales', 'Vendas totais', 37), '14537.25');
|
||||
assert.equal(cell(tables, 'balance', 'Diferença do balanço', 1), '0.00');
|
||||
assert.equal(
|
||||
cell(tables, 'personnel', 'Folha de pagamento agregada', 1),
|
||||
cell(tables, 'personnel', 'Folha de pagamento agregada', 37),
|
||||
'2100.00',
|
||||
);
|
||||
assert.deepEqual(forecastBusinessPlan(recovered), before);
|
||||
|
||||
@@ -44,7 +44,9 @@ void test('one startup editor retains all rows and persists an edit without dupl
|
||||
? annual
|
||||
: name === './business-plan-warnings'
|
||||
? { BusinessPlanWarnings: () => null }
|
||||
: require(name),
|
||||
: name === './startup-asset-requirements'
|
||||
? { StartupAssetRequirements: () => null }
|
||||
: require(name),
|
||||
module,
|
||||
module.exports,
|
||||
);
|
||||
|
||||
@@ -0,0 +1,432 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import { compileFunction } from 'node:vm';
|
||||
import { createRequire } from 'node:module';
|
||||
import ts from 'typescript';
|
||||
import JSZip from 'jszip';
|
||||
import { financialReportFixture } from './fixtures/integral-financial-plan.mjs';
|
||||
import {
|
||||
forecastBusinessPlan,
|
||||
serializeBusinessPlanSnapshot,
|
||||
restoreBusinessPlanSnapshot,
|
||||
} from '../../resources/js/lib/business-financial-plan.ts';
|
||||
import {
|
||||
buildIntegralFinancialTables,
|
||||
newReportAssumptions,
|
||||
} from '../../resources/js/lib/integral-financial-report.ts';
|
||||
import * as requirements from '../../resources/js/lib/startup-asset-requirements.ts';
|
||||
import {
|
||||
newAnnualReferencePlan,
|
||||
serializeAnnualExampleSnapshot,
|
||||
calculateAnnualReferencePlan,
|
||||
} from '../../resources/js/lib/business-plan-guidance.ts';
|
||||
import { buildAnnualExampleTables } from '../../resources/js/lib/annual-example-report.ts';
|
||||
import { annualExampleOriginalDetail as original } from '../../resources/js/lib/annual-example-original-detail.ts';
|
||||
import { reconcileOriginalExample } from '../../resources/js/lib/annual-example-reconciliation.ts';
|
||||
import {
|
||||
createIntegralFinancialReport,
|
||||
createProjectDossierPrintHtml,
|
||||
generateProjectDossierDocx,
|
||||
} from '../../resources/js/lib/project-dossier-report.ts';
|
||||
import { selectPlanCae } from '../../resources/js/lib/cae-mozambique.ts';
|
||||
const cell = (ts, id, label, col) =>
|
||||
ts.find((t) => t.id === id).rows.find((r) => r[0] === label)[col];
|
||||
const cents = (s) => BigInt(s.replace('.', ''));
|
||||
const reload = (p) =>
|
||||
restoreBusinessPlanSnapshot(serializeBusinessPlanSnapshot(p));
|
||||
function startup() {
|
||||
const p = financialReportFixture(12);
|
||||
for (const key of [
|
||||
'monthlyPayrollExpense',
|
||||
'monthlyPayrollPayments',
|
||||
'monthlyOperatingExpenses',
|
||||
'monthlyOperatingPayments',
|
||||
'monthlyInventoryPurchases',
|
||||
'monthlyVariableCostPayments',
|
||||
'assetDepreciation',
|
||||
'monthlyInterestExpense',
|
||||
'monthlyInterestPayment',
|
||||
'monthlyPrincipalRepayment',
|
||||
])
|
||||
p[key] = '0';
|
||||
Object.assign(p, {
|
||||
openingCash: '20000',
|
||||
inventory: '10000',
|
||||
receivables: '5000',
|
||||
assetInvestment: '600',
|
||||
equity: '0',
|
||||
economicClassification: selectPlanCae('55111'),
|
||||
startupAssetRequirements: {
|
||||
cashRequired: '90000',
|
||||
otherCurrentAssetsRequired: '150000',
|
||||
longTermAssetsRequired: '260250',
|
||||
priorAssetInvestment: '600',
|
||||
},
|
||||
});
|
||||
p.products = [
|
||||
{
|
||||
label: 'Sem vendas no ensaio de arranque',
|
||||
monthlyUnits: '0',
|
||||
unitPrice: '1',
|
||||
unitVariableCost: '0',
|
||||
},
|
||||
];
|
||||
Object.assign(p.reportAssumptions, {
|
||||
openingOtherAssets: '10000',
|
||||
openingFixedAssets: '55000',
|
||||
openingEquity: '85000',
|
||||
startYear: '2025',
|
||||
startupExpenses: newAnnualReferencePlan().startupExpenses,
|
||||
taxExpenseByMonth: Array(12).fill('0'),
|
||||
taxPaymentByMonth: Array(12).fill('0'),
|
||||
});
|
||||
return p;
|
||||
}
|
||||
void test('F16 distinguishes required and available cash/assets without treating planned equity as received funding', () => {
|
||||
const p = startup(),
|
||||
budget = requirements.calculateStartupAssetRequirements(p);
|
||||
assert.equal(
|
||||
requirements.startupAmount(budget.grossRequirements),
|
||||
'1825250.00',
|
||||
);
|
||||
assert.equal(
|
||||
requirements.startupAmount(budget.currentAcquisition),
|
||||
'125000.00',
|
||||
);
|
||||
assert.equal(
|
||||
requirements.startupAmount(budget.fixedAcquisition),
|
||||
'205250.00',
|
||||
);
|
||||
assert.throws(() => forecastBusinessPlan(p), /aquisições/);
|
||||
p.assetInvestment = requirements.startupAmount(budget.assetInvestment);
|
||||
let tables = buildIntegralFinancialTables(p);
|
||||
assert.ok(
|
||||
tables.every((t) => !t.issues.length),
|
||||
JSON.stringify(tables.flatMap((t) => t.issues)),
|
||||
);
|
||||
assert.equal(cell(tables, 'startup', 'Caixa', 1), '90000.00');
|
||||
assert.equal(cell(tables, 'startup', 'Caixa', 2), '20000.00');
|
||||
assert.equal(
|
||||
cell(
|
||||
tables,
|
||||
'funding',
|
||||
'Falta para a reserva requerida (projecção)',
|
||||
1,
|
||||
),
|
||||
'1740250.00',
|
||||
);
|
||||
p.equity = '1740250';
|
||||
tables = buildIntegralFinancialTables(p);
|
||||
assert.equal(
|
||||
cell(
|
||||
tables,
|
||||
'funding',
|
||||
'Caixa projectada após arranque com fontes planeadas',
|
||||
1,
|
||||
),
|
||||
'90000.00',
|
||||
);
|
||||
assert.equal(
|
||||
cell(tables, 'funding', 'Caixa disponível antes de financiamento', 1),
|
||||
'20000.00',
|
||||
);
|
||||
assert.equal(
|
||||
cell(tables, 'balance', 'Activos fixos líquidos', 1),
|
||||
'260250.00',
|
||||
);
|
||||
assert.equal(cell(tables, 'balance', 'Total do activo', 1), '500250.00');
|
||||
for (const v of tables
|
||||
.find((t) => t.id === 'balance')
|
||||
.rows.find((r) => r[0] === 'Diferença do balanço')
|
||||
.slice(1))
|
||||
assert.equal(v, '0.00');
|
||||
assert.match(
|
||||
tables.find((t) => t.id === 'funding').notes.join(' '),
|
||||
/não financiamento realizado/,
|
||||
);
|
||||
assert.equal(
|
||||
cell(tables, 'funding', 'Total de fontes planeadas', 1),
|
||||
'1740250.00',
|
||||
);
|
||||
assert.ok(
|
||||
!tables
|
||||
.find((t) => t.id === 'funding')
|
||||
.rows.some((r) => r[0] === 'Capital introduzido'),
|
||||
);
|
||||
});
|
||||
void test('F16 preserves legacy formulas and guards missing data, double counting, snapshot version and CAE', () => {
|
||||
const old = financialReportFixture(12),
|
||||
before = forecastBusinessPlan(old);
|
||||
assert.deepEqual(forecastBusinessPlan(reload(old)), before);
|
||||
const p = startup();
|
||||
p.assetInvestment = requirements.startupAmount(
|
||||
requirements.calculateStartupAssetRequirements(p).assetInvestment,
|
||||
);
|
||||
const s = serializeBusinessPlanSnapshot(p);
|
||||
assert.equal(JSON.parse(s).version, 19);
|
||||
const saved = reload(p);
|
||||
assert.deepEqual(saved.economicClassification, p.economicClassification);
|
||||
assert.deepEqual(
|
||||
saved.startupAssetRequirements,
|
||||
p.startupAssetRequirements,
|
||||
);
|
||||
assert.deepEqual(
|
||||
buildIntegralFinancialTables(saved),
|
||||
buildIntegralFinancialTables(p),
|
||||
);
|
||||
const bad = JSON.parse(s);
|
||||
bad.version = 18;
|
||||
assert.equal(restoreBusinessPlanSnapshot(JSON.stringify(bad)), null);
|
||||
p.startupAssetRequirements.cashRequired = '';
|
||||
assert.throws(
|
||||
() => requirements.calculateStartupAssetRequirements(p),
|
||||
/caixa requerida/,
|
||||
);
|
||||
p.startupAssetRequirements.cashRequired = '90000';
|
||||
p.reportAssumptions.openingOtherAssets = '';
|
||||
assert.throws(
|
||||
() => requirements.calculateStartupAssetRequirements(p),
|
||||
/disponíveis/,
|
||||
);
|
||||
});
|
||||
void test('F16 real editor adopts only acquisitions on explicit action and can restore the earlier investment', () => {
|
||||
const source = fs.readFileSync(
|
||||
new URL(
|
||||
'../../resources/js/components/startup-asset-requirements.tsx',
|
||||
import.meta.url,
|
||||
),
|
||||
'utf8',
|
||||
);
|
||||
const code = ts.transpileModule(source, {
|
||||
compilerOptions: {
|
||||
module: ts.ModuleKind.CommonJS,
|
||||
target: ts.ScriptTarget.ES2022,
|
||||
jsx: ts.JsxEmit.ReactJSX,
|
||||
},
|
||||
}).outputText;
|
||||
const module = { exports: {} },
|
||||
require = createRequire(import.meta.url);
|
||||
compileFunction(code, ['require', 'module', 'exports'])(
|
||||
(name) =>
|
||||
name === '@/lib/startup-asset-requirements'
|
||||
? requirements
|
||||
: name === '@/lib/integral-financial-report'
|
||||
? { newReportAssumptions }
|
||||
: require(name),
|
||||
module,
|
||||
module.exports,
|
||||
);
|
||||
let p = startup();
|
||||
delete p.startupAssetRequirements;
|
||||
const nodes = (n) =>
|
||||
Array.isArray(n)
|
||||
? n.flatMap(nodes)
|
||||
: n && typeof n === 'object'
|
||||
? [n, ...nodes(n.props?.children)]
|
||||
: [];
|
||||
const text = (n) =>
|
||||
typeof n === 'string'
|
||||
? n
|
||||
: Array.isArray(n)
|
||||
? n.map(text).join('')
|
||||
: n && typeof n === 'object'
|
||||
? text(n.props?.children)
|
||||
: '';
|
||||
const render = () =>
|
||||
module.exports.StartupAssetRequirements({
|
||||
plan: p,
|
||||
onChange: (n) => {
|
||||
p = reload(n);
|
||||
assert.ok(p);
|
||||
},
|
||||
});
|
||||
nodes(render())
|
||||
.find((n) => n.type === 'button')
|
||||
.props.onClick();
|
||||
assert.equal(p.assetInvestment, '600');
|
||||
for (const [label, value] of [
|
||||
['Caixa requerida após o arranque', '90000'],
|
||||
['Outros activos correntes requeridos', '150000'],
|
||||
['Activos de longo prazo requeridos', '260250'],
|
||||
]) {
|
||||
const l = nodes(render()).find(
|
||||
(n) => n.type === 'label' && text(n).startsWith(label),
|
||||
);
|
||||
nodes(l)
|
||||
.find((n) => n.type === 'input')
|
||||
.props.onChange({ target: { value } });
|
||||
}
|
||||
nodes(render())
|
||||
.find((n) => n.type === 'button' && text(n).includes('Adoptar'))
|
||||
.props.onClick();
|
||||
assert.equal(p.assetInvestment, '330250.00');
|
||||
assert.equal(p.equity, '0');
|
||||
assert.equal(p.openingCash, '20000');
|
||||
nodes(render())
|
||||
.find((n) => n.type === 'button' && text(n).includes('Retirar'))
|
||||
.props.onClick();
|
||||
assert.equal(p.assetInvestment, '600');
|
||||
assert.equal(p.startupAssetRequirements, undefined);
|
||||
});
|
||||
void test('F31 recovers seven documented departments and identifies the exact unsolved source discrepancies', () => {
|
||||
const reconciled = reconcileOriginalExample(newAnnualReferencePlan());
|
||||
assert.equal(original.sales.length, 4);
|
||||
assert.equal(original.costs.length, 5);
|
||||
assert.equal(original.departments.length, 7);
|
||||
assert.deepEqual(
|
||||
reconciled.years.map((y) => y.salesDifference),
|
||||
['3652.00', '8250.00', '9350.00'],
|
||||
);
|
||||
assert.deepEqual(
|
||||
reconciled.years.map((y) => y.costDifference),
|
||||
['2243.00', '2900.00', '3650.00'],
|
||||
);
|
||||
assert.equal(reconciled.months[0].knownSales, '510.00');
|
||||
assert.equal(reconciled.months[0].sourceSales, '680.00');
|
||||
assert.equal(reconciled.months[0].knownCosts, '431.00');
|
||||
assert.equal(reconciled.months[0].sourceCosts, '586.00');
|
||||
assert.equal(reconciled.years[0].payrollDifference, '0.00');
|
||||
assert.equal(reconciled.years[0].missingDepartments.length, 0);
|
||||
for (const y of reconciled.years.slice(1)) {
|
||||
assert.equal(y.missingDepartments.length, 4);
|
||||
assert.equal(y.payrollDifference, '1600.00');
|
||||
}
|
||||
assert.equal(original.departments[3].annual[1], null);
|
||||
});
|
||||
void test('F31 carries all 36 calculated months, original service/department rows and unchanged annual totals into one eleven-table report', () => {
|
||||
const annual = newAnnualReferencePlan(),
|
||||
p = financialReportFixture(12);
|
||||
p.annualExampleSnapshot = serializeAnnualExampleSnapshot(annual);
|
||||
p.reportSource = 'annual_example';
|
||||
const source = calculateAnnualReferencePlan(annual),
|
||||
tables = buildAnnualExampleTables(reload(p));
|
||||
assert.equal(tables.length, 11);
|
||||
const sales = tables.find((t) => t.id === 'sales');
|
||||
assert.equal(sales.headers.length, 40);
|
||||
assert.equal(sales.headers[1], '2025-01');
|
||||
assert.equal(sales.headers[36], '2027-12');
|
||||
for (let year = 0; year < 3; year++) {
|
||||
const start = year * 12 + 1,
|
||||
end = start + 12,
|
||||
col = 37 + year;
|
||||
for (const [id, label] of [
|
||||
['sales', 'Vendas totais'],
|
||||
['sales', 'Custo directo'],
|
||||
['profit-loss', 'Lucro líquido'],
|
||||
['cashflow', 'Caixa recebida'],
|
||||
['cashflow', 'Caixa gasta'],
|
||||
['cashflow', 'Fluxo líquido'],
|
||||
['personnel', 'Folha de pagamento agregada'],
|
||||
]) {
|
||||
const row = tables
|
||||
.find((t) => t.id === id)
|
||||
.rows.find((r) => r[0] === label);
|
||||
assert.equal(
|
||||
row.slice(start, end).reduce((sum, v) => sum + cents(v), 0n),
|
||||
cents(row[col]),
|
||||
`${id}/${label}/${year}`,
|
||||
);
|
||||
}
|
||||
assert.equal(
|
||||
cell(tables, 'cashflow', 'Caixa final', col),
|
||||
cell(tables, 'cashflow', 'Caixa final', end - 1),
|
||||
);
|
||||
}
|
||||
for (const amount of tables
|
||||
.find((t) => t.id === 'balance')
|
||||
.rows.find((r) => r[0] === 'Diferença do balanço')
|
||||
.slice(1))
|
||||
assert.equal(amount, '0.00');
|
||||
assert.equal(
|
||||
cell(
|
||||
tables,
|
||||
'sales',
|
||||
'Vendas documentadas no original — Publicidade',
|
||||
1,
|
||||
),
|
||||
'100.00',
|
||||
);
|
||||
assert.equal(
|
||||
cell(
|
||||
tables,
|
||||
'sales',
|
||||
'Vendas documentadas no original — Publicidade',
|
||||
37,
|
||||
),
|
||||
'2137.00',
|
||||
);
|
||||
assert.equal(
|
||||
cell(tables, 'sales', 'Custo documentado no original — Marketing', 1),
|
||||
'Não documentado',
|
||||
);
|
||||
assert.equal(
|
||||
cell(tables, 'personnel', 'Departamento documentado — Marketing', 37),
|
||||
'300.00',
|
||||
);
|
||||
assert.equal(
|
||||
cell(tables, 'personnel', 'Departamento documentado — Marketing', 38),
|
||||
'Não documentado',
|
||||
);
|
||||
assert.equal(source.years[0].closingAssets, '501475.00');
|
||||
assert.equal(source.years[0].closingLiabilities, '1641.00');
|
||||
assert.equal(source.years[0].closingCapital, '499834.00');
|
||||
assert.equal(
|
||||
cell(
|
||||
tables,
|
||||
'funding',
|
||||
'Caixa efectivamente disponível antes do financiamento',
|
||||
1,
|
||||
),
|
||||
'Não documentado no exemplo original',
|
||||
);
|
||||
});
|
||||
void test('F16/F31 persistence and existing Word/print exports preserve the same requirements, all months and source gaps', async () => {
|
||||
const p = startup();
|
||||
p.assetInvestment = requirements.startupAmount(
|
||||
requirements.calculateStartupAssetRequirements(p).assetInvestment,
|
||||
);
|
||||
p.equity = '1740250';
|
||||
const annual = newAnnualReferencePlan();
|
||||
annual.years[0].sales = '14537.25';
|
||||
p.annualExampleSnapshot = serializeAnnualExampleSnapshot(annual);
|
||||
for (const mode of ['investor', 'annual_example']) {
|
||||
p.reportSource = mode;
|
||||
const restored = reload(p),
|
||||
before = JSON.stringify(p);
|
||||
const report = createIntegralFinancialReport(
|
||||
{
|
||||
business_plan_snapshot: serializeBusinessPlanSnapshot(restored),
|
||||
project_title: 'QA F16/F31',
|
||||
},
|
||||
{ generatedAt: '2026-11-20T12:00:00Z' },
|
||||
);
|
||||
assert.equal(report.sections[0].tables.length, 11);
|
||||
assert.equal(JSON.stringify(p), before);
|
||||
const html = await createProjectDossierPrintHtml(report, []),
|
||||
doc = await generateProjectDossierDocx(report, []),
|
||||
zip = await JSZip.loadAsync(
|
||||
doc instanceof Blob ? await doc.arrayBuffer() : doc,
|
||||
),
|
||||
xml = await zip.file('word/document.xml').async('string');
|
||||
for (const s of mode === 'investor'
|
||||
? [
|
||||
'Disponível antes de financiamento',
|
||||
'Caixa requerida',
|
||||
'não financiamento realizado',
|
||||
]
|
||||
: [
|
||||
'2025-01',
|
||||
'2027-12',
|
||||
'Vendas documentadas no original',
|
||||
'Departamento documentado',
|
||||
'Não documentado',
|
||||
]) {
|
||||
assert.ok((html + xml).includes(s), s);
|
||||
}
|
||||
if (mode === 'annual_example') {
|
||||
assert.match(xml, /14 537,25/);
|
||||
assert.match(xml, /documental/);
|
||||
}
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user