feat: persist investor warning limits and preserve financial snapshots in QA
This commit is contained in:
@@ -0,0 +1,260 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import {
|
||||
defaultPlanWarningLimits,
|
||||
isPlanWarningLimits,
|
||||
businessPlanWarnings,
|
||||
} from '../../resources/js/lib/business-plan-guidance.ts';
|
||||
import {
|
||||
restoreBusinessPlanSnapshot,
|
||||
serializeBusinessPlanSnapshot,
|
||||
forecastBusinessPlan,
|
||||
exportBusinessPlanCsv,
|
||||
} from '../../resources/js/lib/business-financial-plan.ts';
|
||||
import { buildIntegralFinancialTables } from '../../resources/js/lib/integral-financial-report.ts';
|
||||
import { financialPlanWarnings } from '../../resources/js/lib/financial-plan-warnings.ts';
|
||||
void test('V07 accepts finite warning thresholds without inventing percentage caps and rejects incomplete or invalid units', () => {
|
||||
assert.ok(isPlanWarningLimits(defaultPlanWarningLimits));
|
||||
assert.ok(
|
||||
isPlanWarningLimits({
|
||||
...defaultPlanWarningLimits,
|
||||
grossMarginPercent: -5,
|
||||
debtAssetsPercent: 200,
|
||||
}),
|
||||
);
|
||||
for (const v of [
|
||||
null,
|
||||
{},
|
||||
{ ...defaultPlanWarningLimits, creditDays: -1 },
|
||||
{ ...defaultPlanWarningLimits, cashExpenseMonths: -1 },
|
||||
{ ...defaultPlanWarningLimits, creditDays: NaN },
|
||||
{ ...defaultPlanWarningLimits, creditDays: Infinity },
|
||||
{ ...defaultPlanWarningLimits, extra: 1 },
|
||||
])
|
||||
assert.equal(isPlanWarningLimits(v), false);
|
||||
const metrics = {
|
||||
sales: 100,
|
||||
directCosts: 80,
|
||||
cash: 200,
|
||||
monthlyExpenses: 100,
|
||||
creditDays: 60,
|
||||
};
|
||||
assert.deepEqual(businessPlanWarnings(metrics), []);
|
||||
const adjusted = businessPlanWarnings(metrics, {
|
||||
...defaultPlanWarningLimits,
|
||||
grossMarginPercent: 25,
|
||||
cashExpenseMonths: 3,
|
||||
creditDays: 30,
|
||||
});
|
||||
assert.deepEqual(
|
||||
adjusted.map((w) => w.id),
|
||||
['margin', 'cash', 'credit'],
|
||||
);
|
||||
assert.match(adjusted.find((w) => w.id === 'cash').message, /3 meses/);
|
||||
});
|
||||
for (const source of [
|
||||
'e25-f20-a-20261121/saved.json',
|
||||
'e26-f20-b-20261121/saved-active.json',
|
||||
'e29-f20-b-nirf-20261121/saved-plan.json',
|
||||
'e32-f22-20261121/saved-plan.json',
|
||||
])
|
||||
void test(
|
||||
'V07 snapshot 24 roundtrip preserves financial schedules and amounts: ' +
|
||||
source,
|
||||
() => {
|
||||
const old = restoreBusinessPlanSnapshot(
|
||||
fs.readFileSync(
|
||||
'docs/investmoz/v2/auditoria-portal/' + source,
|
||||
'utf8',
|
||||
),
|
||||
);
|
||||
assert.ok(old);
|
||||
const before = serializeBusinessPlanSnapshot(old),
|
||||
configured = {
|
||||
...old,
|
||||
warningLimits: {
|
||||
...defaultPlanWarningLimits,
|
||||
creditDays: 200,
|
||||
},
|
||||
};
|
||||
const raw = serializeBusinessPlanSnapshot(configured);
|
||||
assert.equal(JSON.parse(raw).version, 24);
|
||||
const restored = restoreBusinessPlanSnapshot(raw);
|
||||
assert.ok(restored);
|
||||
assert.deepEqual(restored.warningLimits, configured.warningLimits);
|
||||
assert.deepEqual(
|
||||
forecastBusinessPlan(restored),
|
||||
forecastBusinessPlan(old),
|
||||
);
|
||||
assert.deepEqual(
|
||||
buildIntegralFinancialTables(restored),
|
||||
buildIntegralFinancialTables(old),
|
||||
);
|
||||
assert.equal(
|
||||
exportBusinessPlanCsv(restored, '2026-11-21T10:00:00Z'),
|
||||
exportBusinessPlanCsv(old, '2026-11-21T10:00:00Z'),
|
||||
);
|
||||
delete restored.warningLimits;
|
||||
assert.equal(serializeBusinessPlanSnapshot(restored), before);
|
||||
const missing = JSON.parse(raw);
|
||||
delete missing.plan.warningLimits;
|
||||
assert.equal(
|
||||
restoreBusinessPlanSnapshot(JSON.stringify(missing)),
|
||||
null,
|
||||
);
|
||||
},
|
||||
);
|
||||
void test('V07 financial warning consumer applies saved limits while V11 monthly negative cash stays visible', () => {
|
||||
const p = restoreBusinessPlanSnapshot(
|
||||
fs.readFileSync(
|
||||
'docs/investmoz/v2/auditoria-portal/e32-f22-20261121/saved-plan.json',
|
||||
'utf8',
|
||||
),
|
||||
);
|
||||
p.creditTermDays = '91';
|
||||
const tables = buildIntegralFinancialTables(p);
|
||||
assert.ok(
|
||||
financialPlanWarnings(p, tables).some((w) => w.id.endsWith('-credit')),
|
||||
);
|
||||
p.warningLimits = { ...defaultPlanWarningLimits, creditDays: 200 };
|
||||
assert.ok(
|
||||
!financialPlanWarnings(p, tables).some((w) => w.id.endsWith('-credit')),
|
||||
);
|
||||
const cash = [
|
||||
{
|
||||
id: 'cashflow',
|
||||
title: 'Synthetic cash',
|
||||
headers: [
|
||||
'Month',
|
||||
'2027-01',
|
||||
'2027-02',
|
||||
'2027-03',
|
||||
'2027-04',
|
||||
'2027-05',
|
||||
'2027-06',
|
||||
'2027-07',
|
||||
],
|
||||
rows: [
|
||||
[
|
||||
'Caixa final',
|
||||
'-1.00',
|
||||
'0.00',
|
||||
'0.00',
|
||||
'0.00',
|
||||
'0.00',
|
||||
'0.00',
|
||||
'0.00',
|
||||
],
|
||||
],
|
||||
notes: [],
|
||||
issues: [],
|
||||
},
|
||||
];
|
||||
assert.equal(
|
||||
financialPlanWarnings(p, cash).filter(
|
||||
(w) => w.id === 'cash-negative-2027-01',
|
||||
).length,
|
||||
1,
|
||||
);
|
||||
});
|
||||
|
||||
void test('V07 reset to snapshot 23 preserves valid periodized unit schedules when F22 data is retained', () => {
|
||||
const plan = restoreBusinessPlanSnapshot(
|
||||
fs.readFileSync(
|
||||
'docs/investmoz/v2/auditoria-portal/e25-f20-a-20261121/saved.json',
|
||||
'utf8',
|
||||
),
|
||||
);
|
||||
plan.periodInvestments = { enabled: false, financings: [], assets: [] };
|
||||
const raw = serializeBusinessPlanSnapshot(plan);
|
||||
assert.equal(JSON.parse(raw).version, 23);
|
||||
const restored = restoreBusinessPlanSnapshot(raw);
|
||||
assert.ok(restored);
|
||||
assert.deepEqual(
|
||||
restored.products[0].monthlyUnitsByPeriod,
|
||||
plan.products[0].monthlyUnitsByPeriod,
|
||||
);
|
||||
assert.deepEqual(
|
||||
forecastBusinessPlan(restored),
|
||||
forecastBusinessPlan(plan),
|
||||
);
|
||||
});
|
||||
import { createRequire } from 'node:module';
|
||||
import { compileFunction } from 'node:vm';
|
||||
import React from 'react';
|
||||
import ts from 'typescript';
|
||||
import * as guidance from '../../resources/js/lib/business-plan-guidance.ts';
|
||||
void test('V07 real limit editor applies only valid explicit settings, retains financial values and resets without mutating the plan', () => {
|
||||
const require = createRequire(import.meta.url),
|
||||
source = fs.readFileSync(
|
||||
new URL(
|
||||
'../../resources/js/components/warning-limits-editor.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 values = { ...defaultPlanWarningLimits, creditDays: 200 };
|
||||
let error = '',
|
||||
saved;
|
||||
const module = { exports: {} };
|
||||
compileFunction(code, ['require', 'module', 'exports'])(
|
||||
(id) =>
|
||||
id === 'react'
|
||||
? {
|
||||
...React,
|
||||
useRef: () => ({
|
||||
current: {
|
||||
querySelector: (selector) => ({
|
||||
valueAsNumber:
|
||||
values[
|
||||
/name="([^"]+)"/.exec(selector)[1]
|
||||
],
|
||||
}),
|
||||
},
|
||||
}),
|
||||
useState: () => ['', (value) => (error = value)],
|
||||
}
|
||||
: id === '@/lib/business-plan-guidance'
|
||||
? guidance
|
||||
: require(id),
|
||||
module,
|
||||
module.exports,
|
||||
);
|
||||
const plan = Object.freeze({
|
||||
currency: 'MZN',
|
||||
openingCash: '1000',
|
||||
warningLimits: Object.freeze({ ...defaultPlanWarningLimits }),
|
||||
});
|
||||
const before = JSON.stringify(plan);
|
||||
const tree = module.exports.WarningLimitsEditor({
|
||||
plan,
|
||||
onChange: (value) => (saved = value),
|
||||
});
|
||||
const nodes = (t) =>
|
||||
Array.isArray(t)
|
||||
? t.flatMap(nodes)
|
||||
: t && typeof t === 'object'
|
||||
? [t, ...nodes(t.props?.children)]
|
||||
: [];
|
||||
const buttons = nodes(tree).filter((n) => n.type === 'button');
|
||||
values.creditDays = NaN;
|
||||
buttons[0].props.onClick();
|
||||
assert.equal(saved, undefined);
|
||||
assert.ok(error);
|
||||
values.creditDays = 200;
|
||||
buttons[0].props.onClick();
|
||||
assert.equal(error, '');
|
||||
assert.equal(saved.warningLimits.creditDays, 200);
|
||||
assert.equal(saved.openingCash, '1000');
|
||||
buttons[1].props.onClick();
|
||||
assert.equal(saved.warningLimits, undefined);
|
||||
assert.equal(JSON.stringify(plan), before);
|
||||
});
|
||||
Reference in New Issue
Block a user