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), /]*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, /