import test from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import {
formFieldGuidance,
formHelpPlacement,
normaliseHelpLabel,
readFormHelpAutomatic,
simulationStepGuidance,
} from '../../resources/js/lib/form-help.ts';
import { getSimulationSections } from '../../resources/js/lib/simulation-form.ts';
test('guidance explains actual fiscal/financial inputs without inventing rates or replacing periods', () => {
for (const [label, expected] of [
['Volume de negócios anterior', /período pedido/],
['Imposto liquidado', /documento correspondente/],
['Retenções na fonte', /documentado/],
['Receita mensal', /previsão/],
['Custo unitário', /unitário, mensal, anual ou total/],
['Moeda do projecto', /moeda/],
['Residência fiscal', /enquadramento/],
['NUIT', /documento/],
['Fonte documental', /recuperar e conferir/],
]) {
const result = formFieldGuidance({
label,
type: 'text',
required: false,
value: 'PRIVATE-INPUT-123',
});
assert.match(result.instruction, expected);
assert.doesNotMatch(JSON.stringify(result), /PRIVATE-INPUT|\d+%/);
}
});
test('native and accessible custom field types receive useful instructions', () => {
for (const type of [
'text',
'number',
'range',
'select',
'combobox',
'checkbox',
'radio',
'date',
'month',
'email',
'tel',
'url',
'password',
'file',
'textarea',
]) {
const result = formFieldGuidance({
label: 'Campo de ensaio',
type,
required: true,
});
assert.ok(result.instruction.length > 30, type);
}
assert.match(
formFieldGuidance({
label: 'Autorização',
type: 'checkbox',
required: false,
}).instruction,
/apenas se/,
);
assert.match(
formFieldGuidance({ label: 'Documento', type: 'file', required: false })
.instruction,
/formatos indicados/,
);
assert.equal(
normaliseHelpLabel('RETENÇÕES — MOÇAMBIQUE'),
'retencoes — mocambique',
);
});
test('all adaptive simulator sections have specific step guidance', () => {
const seen = new Set();
for (const draft of [
{},
{ purpose: 'simulate_only' },
{ journey_type: 'company' },
{ journey_type: 'personal' },
{ journey_type: 'investor' },
{ journey_type: 'small_business' },
{ purpose: 'prepare_investment', journey_type: 'investor' },
]) {
for (const step of getSimulationSections(draft)) {
seen.add(step.id);
assert.notEqual(
simulationStepGuidance(step.id),
simulationStepGuidance('unknown-section'),
step.id,
);
}
}
for (const code of [
'purpose',
'market',
'investment',
'financial',
'operating',
'cashflow',
'tax',
'review',
'team',
'scenarios',
'procedures',
]) {
assert.ok(simulationStepGuidance(code).length > 40);
}
assert.ok(seen.size >= 11);
});
test('browser help preference is optional and does not store field contents', () => {
assert.equal(readFormHelpAutomatic(null), true);
assert.equal(readFormHelpAutomatic('off'), false);
assert.equal(readFormHelpAutomatic('on'), true);
assert.equal(readFormHelpAutomatic('corrupt'), true);
for (const file of [
'components/form-field-help.tsx',
'lib/form-help-dom.ts',
]) {
const source = readFileSync(
new URL('../../resources/js/' + file, import.meta.url),
'utf8',
);
assert.doesNotMatch(
source,
/\.value\b|FormData|fetch\(|router\.|\.submit\(|\.requestSubmit\(/,
);
}
});
test('help is placed above or below the focused mobile field without covering it', () => {
for (const rect of [
{ left: 24, top: 180, bottom: 224 },
{ left: 24, top: 440, bottom: 484 },
{ left: 24, top: 700, bottom: 744 },
]) {
const layout = formHelpPlacement(rect, 390, 844);
assert.ok(layout.maxHeight >= 96 && layout.maxHeight <= 844 * 0.45);
if (layout.vertical === 'top')
assert.ok(80 + layout.maxHeight + 60 < rect.top);
else assert.ok(844 - 12 - layout.maxHeight - 60 > rect.bottom);
}
});
test('help mounts once across portal pages and the simulator publishes its current adaptive step', () => {
const app = readFileSync(
new URL('../../resources/js/app.tsx', import.meta.url),
'utf8',
);
assert.equal((app.match(//g) ?? []).length, 1);
const simulator = readFileSync(
new URL(
'../../resources/js/components/simulation-form.tsx',
import.meta.url,
),
'utf8',
);
assert.match(simulator, /data-form-help-step=\{current.id\}/);
assert.match(simulator, /data-form-help-title=\{current.title\}/);
assert.match(
simulator,
/