Files
Investidor-V2/tests/frontend/complementary-field-diagnostics.test.mjs
T

206 lines
7.1 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { complementaryFixture } from './fixtures/complementary-plan.mjs';
import { buildComplementaryTables } from '../../resources/js/lib/complementary-report.ts';
import {
correctionGroupMatches,
correctionFieldHints,
} from '../../resources/js/lib/field-corrections.ts';
void test('V09 field routing distinguishes Marco 2 from Necessidade 2 and preserves personnel aliases', () => {
assert.deepEqual(
correctionFieldHints(
'Função 2: «Departamento (mapa de pessoal)»: Preencha este campo.',
[
'Departamento (mapa de pessoal)',
'Quantidade de pessoas (inteiro)',
'Remuneração mensal por pessoa',
],
),
['Departamento (mapa de pessoal)'],
);
assert.equal(
correctionGroupMatches(
'Marcos / Marco 2 / «Prazo previsto»: Preencha este campo.',
'Marco 2',
),
true,
);
assert.equal(
correctionGroupMatches(
'Marcos / Marco 2 / «Prazo previsto»: Preencha este campo.',
'Necessidade 2',
),
false,
);
assert.equal(
correctionGroupMatches(
'Função 2: «Mês final no plano»: inválido.',
'Necessidade 2',
),
true,
);
assert.equal(
correctionGroupMatches('Função 2: inválido.', 'Marco 2'),
false,
);
assert.equal(
correctionGroupMatches('Segmento 2: inválido.', 'Segmento 1'),
false,
);
assert.equal(
correctionGroupMatches('Segmento 2: inválido.', 'Segmento 2'),
true,
);
});
void test('V09 milestone monetary diagnostics identify blank, format, negative and precision without changing acceptance or totals', () => {
const { plan, draft, milestones } = complementaryFixture();
for (const [value, message] of [
['', 'Preencha este campo'],
['abc', 'Introduza apenas números'],
['-1', 'Este valor não pode ser negativo'],
['1.234', 'até duas casas decimais'],
[' 1 ', 'até duas casas decimais'],
]) {
const input = {
...draft,
project_execution_snapshot: JSON.stringify([
{ ...milestones[0], planned_amount: value },
]),
};
const before = JSON.stringify(input);
const result = buildComplementaryTables(input, plan)[1];
assert.ok(
result.issues.some(
(s) =>
s.includes('Marco 1') &&
s.includes('«Montante planeado»') &&
s.includes(message),
),
);
assert.equal(result.rows.at(-1)[5], 'Por determinar');
assert.equal(JSON.stringify(input), before);
}
const optional = buildComplementaryTables(draft, plan)[1];
assert.ok(!optional.issues.length);
assert.equal(optional.rows.at(-1)[5], '100.25');
assert.equal(optional.rows.at(-1)[6], 'Por determinar');
const invalidActual = buildComplementaryTables(
{
...draft,
project_execution_snapshot: JSON.stringify([
{ ...milestones[0], actual_amount: '-1' },
]),
},
plan,
)[1];
assert.ok(
invalidActual.issues.some(
(s) =>
s.includes('«Montante realizado»') &&
s.includes('não pode ser negativo'),
),
);
});
void test('V09 required milestone fields and staff salary/months point to their real labels while valid rows stay unchanged', () => {
const { plan, draft, team, milestones } = complementaryFixture();
const fixed = buildComplementaryTables(draft, plan);
for (const [field, label] of [
['title', 'Marco ou resultado'],
['responsible', 'Responsável pelo marco'],
['department', 'Departamento do marco'],
['start_date', 'Data de início'],
['due_date', 'Prazo previsto'],
]) {
const result = buildComplementaryTables(
{
...draft,
project_execution_snapshot: JSON.stringify([
{ ...milestones[0], [field]: '' },
]),
},
plan,
)[1];
assert.ok(
result.issues.some(
(s) =>
s.includes(`«${label}»`) &&
s.includes('Preencha este campo'),
),
);
assert.deepEqual(result.rows.at(-1), fixed[1].rows.at(-1));
}
for (const [field, value, label] of [
['competency', '', 'Competência ou função'],
['department', '', 'Departamento (mapa de pessoal)'],
['monthly_salary', '-1', 'Remuneração mensal por pessoa'],
['start_month', '0', 'Mês inicial no plano'],
['end_month', '14', 'Mês final no plano'],
]) {
const input = {
...draft,
project_team_snapshot: JSON.stringify([
{ ...team[0], [field]: value },
]),
};
const result = buildComplementaryTables(input, plan)[2];
assert.ok(
result.issues.some(
(s) => s.includes('Função 1') && s.includes(label),
),
);
assert.equal(result.rows.at(-1)[1], 'Por determinar');
}
assert.deepEqual(buildComplementaryTables(draft, plan), fixed);
});
void test('V09 market fields use exact labels and retain existing growth and integer boundaries', () => {
const { plan, draft } = complementaryFixture();
for (const [key, value, label] of [
['baseYear', '', 'Ano-base de mercado'],
['years', '1', 'Horizonte de mercado (anos)'],
]) {
const result = buildComplementaryTables(draft, {
...plan,
marketAnalysis: { ...plan.marketAnalysis, [key]: value },
})[0];
assert.ok(result.issues.some((s) => s.includes(`«${label}»`)));
}
for (const [key, value, label] of [
['label', '', 'Nome do segmento'],
['basis', '', 'Fonte e data'],
['customers', '2.5', 'Clientes potenciais no ano-base'],
['growthPercent', '-101', 'Crescimento anual do segmento (%)'],
]) {
const changed = {
...plan,
marketAnalysis: {
...plan.marketAnalysis,
segments: [
{ ...plan.marketAnalysis.segments[0], [key]: value },
],
},
};
const before = JSON.stringify(changed),
result = buildComplementaryTables(draft, changed)[0];
assert.ok(
result.issues.some(
(s) => s.includes('Segmento 1') && s.includes(label),
),
);
assert.equal(JSON.stringify(changed), before);
}
const growth = {
...plan,
marketAnalysis: {
...plan.marketAnalysis,
segments: [
{ ...plan.marketAnalysis.segments[0], growthPercent: '101' },
],
},
};
assert.equal(buildComplementaryTables(draft, growth)[0].issues.length, 0);
});