fix: remove demonstration scenario from investor simulator and exports

This commit is contained in:
2026-11-23 09:11:12 +02:00
parent 8ffb0f1bc5
commit e929632e42
16 changed files with 565 additions and 150 deletions
@@ -0,0 +1,99 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { createRequire } from 'node:module';
import { compileFunction } from 'node:vm';
import ts from 'typescript';
import React from 'react';
import { renderToStaticMarkup } from 'react-dom/server';
const root = path.resolve('resources/js');
const nativeRequire = createRequire(import.meta.url);
const cache = new Map();
function load(file) {
if (cache.has(file)) return cache.get(file).exports;
const module = { exports: {} };
cache.set(file, module);
const code = ts.transpileModule(fs.readFileSync(file, 'utf8'), {
compilerOptions: {
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES2022,
jsx: ts.JsxEmit.ReactJSX,
},
}).outputText;
compileFunction(code, ['require', 'module', 'exports'])(
(name) => {
if (!name.startsWith('@/') && !name.startsWith('.'))
return nativeRequire(name);
const base = name.startsWith('@/')
? path.join(root, name.slice(2))
: path.resolve(path.dirname(file), name);
const stem = base.replace(/\.(js|ts|tsx)$/, '');
const resolved = [base, stem + '.ts', stem + '.tsx'].find(
(f) => fs.existsSync(f) && fs.statSync(f).isFile(),
);
assert.ok(resolved, name);
return load(resolved);
},
module,
module.exports,
);
return module.exports;
}
void test('financial editor exposes no demonstration loader or source; legacy selection has explicit recovery without overwriting data', () => {
const { SimulationBusinessPlan } = load(
path.join(root, 'components/simulation-business-plan.tsx'),
);
const html = renderToStaticMarkup(
React.createElement(SimulationBusinessPlan, {
currency: 'MZN',
onCurrencyChange() {},
onSnapshotChange() {},
}),
);
assert.doesNotMatch(
html,
/Carregar cenário anual|Cenário anual de exemplo|Exemplo sintético anual editável/,
);
const { FinancialPlanDetails } = load(
path.join(root, 'components/financial-plan-details.tsx'),
);
const plan = {
reportSource: 'annual_example',
annualExampleSnapshot: 'preserved',
currency: 'MZN',
};
let next;
const node = FinancialPlanDetails({
plan,
onChange: (p) => {
next = p;
},
});
const nodes = (n) =>
Array.isArray(n)
? n.flatMap(nodes)
: n && typeof n === 'object'
? [n, ...nodes(n.props?.children)]
: [];
const button = nodes(node).find((n) => n.type === 'button');
assert.ok(button);
button.props.onClick();
assert.equal(next.reportSource, 'investor');
assert.equal(next.annualExampleSnapshot, 'preserved');
assert.equal(plan.reportSource, 'annual_example');
assert.doesNotMatch(
renderToStaticMarkup(node),
/<option[^>]*value="annual_example"/,
);
const { IntegralFinancialPlan } = load(
path.join(root, 'components/integral-financial-plan.tsx'),
);
const report = renderToStaticMarkup(
React.createElement(IntegralFinancialPlan, { plan, onChange() {} }),
);
assert.match(report, /Seleccione o plano do investidor/);
assert.doesNotMatch(report, /<table|Relatório do exemplo/);
});