Files
Investidor-V2/tests/frontend/investment-assistance-controls.test.mjs
T
Claude 4745331ebd
tests / ci (push) Canceled after 0s
Simulador: os campos numéricos aceitam «25.000», «1 500» e «1500,50»
O campo numérico do navegador segue a língua do navegador: «25.000» era
lido como 25 e a vírgula decimal podia ser recusada, pelo que o investidor
via o valor mudar ou não conseguia escrever. Os campos de montantes do
simulador, do plano de negócio e do orçamento inicial passam a usar um
campo de texto decimal que lê os números como se escrevem em Moçambique,
mostra «Lido como …» quando reinterpreta e deixa o texto inválido chegar à
validação, que o explica.

Os campos inteiros (horizonte, prazo de entrega, vida útil, mês económico)
deixam de saltar para 1 quando se apaga para reescrever; se o valor escrito
não for aceite, ao sair do campo volta o valor em vigor.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MzJmxFy3izprXr5dSq91jM
2026-12-04 23:01:57 +02:00

175 lines
5.4 KiB
JavaScript

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 React from 'react';
import ts from 'typescript';
import {
decimalInputModule,
isNumberField,
typeNumber,
} from './helpers/decimal-input.mjs';
import * as finance from '../../resources/js/lib/financial-calculators.ts';
import * as operating from '../../resources/js/lib/investment-operating-assistance.ts';
const require = createRequire(import.meta.url);
const nodes = (tree) =>
Array.isArray(tree)
? tree.flatMap(nodes)
: tree && typeof tree === 'object'
? [tree, ...nodes(tree.props?.children)]
: [];
const text = (tree) =>
Array.isArray(tree)
? tree.map(text).join('')
: tree?.props
? text(tree.props.children)
: typeof tree === 'string'
? tree
: '';
function harness(file, snapshot = '') {
let cursor = 0,
tree;
const memory = [],
changes = [];
const module = { exports: {} };
const hooks = {
...React,
useState(initial) {
const id = cursor++;
if (!(id in memory))
memory[id] =
typeof initial === 'function' ? initial() : initial;
return [
memory[id],
(next) => {
memory[id] =
typeof next === 'function' ? next(memory[id]) : next;
},
];
},
};
compileFunction(
ts.transpileModule(
fs.readFileSync('resources/js/components/' + file, 'utf8'),
{
compilerOptions: {
module: ts.ModuleKind.CommonJS,
jsx: ts.JsxEmit.ReactJSX,
},
},
).outputText,
['require', 'module', 'exports'],
)(
(id) =>
id === 'react'
? hooks
: id === '@/lib/financial-calculators'
? finance
: id === '@/lib/investment-operating-assistance'
? operating
: id === '@/components/decimal-input'
? decimalInputModule
: require(id),
module,
module.exports,
);
const props = {
snapshot,
currency: 'MZN',
onChange(key, value) {
changes.push([key, value]);
},
};
const render = () => {
cursor = 0;
tree = module.exports.default(props);
};
render();
return {
render,
changes,
nodes: () => nodes(tree),
text: () => text(tree),
button: (label) =>
nodes(tree).find(
(node) =>
node.type === 'button' && text(node).startsWith(label),
),
};
}
test('initial budget has no sample amounts and adopts a sum only through the explicit action', () => {
const h = harness('investment-initial-budget.tsx');
const amounts = () => h.nodes().filter(isNumberField);
assert.deepEqual(
amounts().map((node) => node.props.value),
['', '', '', ''],
);
h.button('Usar a soma').props.onClick();
h.render();
assert.match(h.text(), /montantes em falta/);
assert.equal(
h.changes.some(([key]) => key === 'total_investment'),
false,
);
for (let i = 0; i < 4; i++) {
typeNumber(amounts()[i], (i + 1) * 1000);
h.render();
}
assert.equal(
h.changes.some(([key]) => key === 'total_investment'),
false,
);
h.button('Usar a soma').props.onClick();
h.render();
assert.deepEqual(h.changes.at(-1), ['total_investment', '10000.00']);
const saved = h.changes
.filter(([key]) => key === 'initial_budget_snapshot')
.at(-1)[1];
const restored = harness('investment-initial-budget.tsx', saved);
assert.deepEqual(
restored
.nodes()
.filter(isNumberField)
.map((node) => node.props.value),
['1000', '2000', '3000', '4000'],
);
});
test('hotel estimate is selected deliberately, saved as assumptions, then adopted without replacing another field', () => {
const h = harness('investment-operating-assistance.tsx');
h.nodes()
.find((node) => node.type === 'select')
.props.onChange({ target: { value: 'hotel' } });
h.render();
const amounts = () => h.nodes().filter(isNumberField);
for (const [i, value] of ['10', '5000', '50', '300'].entries()) {
typeNumber(amounts()[i], value);
h.render();
}
assert.equal(
h.changes.some(([key]) => key === 'annual_revenue'),
false,
);
h.button('Usar esta estimativa de receitas').props.onClick();
h.render();
assert.deepEqual(h.changes.at(-1), ['annual_revenue', '7500000.00']);
assert.equal(
h.changes.some(
([key]) =>
key === 'total_investment' || key === 'investment_budget_max',
),
false,
);
const saved = h.changes
.filter(([key]) => key === 'planning_assumptions_snapshot')
.at(-1)[1];
const restored = harness('investment-operating-assistance.tsx', saved);
assert.equal(
restored.nodes().find((node) => node.type === 'select').props.value,
'hotel',
);
assert.match(restored.text(), /ocupação/);
});