154 lines
5.1 KiB
JavaScript
154 lines
5.1 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import {
|
|
investmentLandPreparation,
|
|
landStatusVisible,
|
|
} from '../../resources/js/lib/investment-land-preparation.ts';
|
|
import {
|
|
simulationFieldGroups,
|
|
validateSimulationDraft,
|
|
partitionSimulationDraft,
|
|
restoreSimulationDraft,
|
|
} from '../../resources/js/lib/simulation-form.ts';
|
|
import { inheritCompatibleFields } from '../../resources/js/lib/context-inheritance.ts';
|
|
import {
|
|
buildProjectDossierRows,
|
|
exportProjectDossierCsv,
|
|
} from '../../resources/js/lib/project-dossier.ts';
|
|
import {
|
|
createProjectDossierReport,
|
|
createProjectDossierPrintHtml,
|
|
} from '../../resources/js/lib/project-dossier-report.ts';
|
|
|
|
const snapshot = (need) =>
|
|
JSON.stringify({
|
|
version: 1,
|
|
context_revision: 2,
|
|
land_preparation: { need, reason: 'Decisão guardada no dossier' },
|
|
});
|
|
const draft = (need) => ({
|
|
purpose: 'simulate_only',
|
|
journey_type: 'investor',
|
|
land_status: 'duat',
|
|
land_status_label: 'DUAT',
|
|
context_applicability_snapshot: snapshot(need),
|
|
});
|
|
|
|
void test('no hides land status and yes restores the same saved value; unknown is a pending decision without a required answer', () => {
|
|
const field = simulationFieldGroups.location.find(
|
|
(field) => field.key === 'land_status',
|
|
);
|
|
for (const need of ['no', 'yes', 'unknown']) {
|
|
const input = draft(need);
|
|
const before = JSON.stringify(input);
|
|
assert.equal(field.showWhen(input), need === 'yes');
|
|
assert.equal(
|
|
Boolean(
|
|
validateSimulationDraft(
|
|
{ ...input, land_status: '' },
|
|
'location',
|
|
).land_status,
|
|
),
|
|
false,
|
|
);
|
|
const saved = partitionSimulationDraft(input);
|
|
const recovered = restoreSimulationDraft(
|
|
saved.personal_snapshot,
|
|
saved.economic_snapshot,
|
|
);
|
|
assert.equal(recovered.land_status, 'duat');
|
|
assert.equal(recovered.land_status_label, 'DUAT');
|
|
assert.equal(
|
|
investmentLandPreparation(recovered.context_applicability_snapshot)
|
|
.need,
|
|
need,
|
|
);
|
|
assert.equal(JSON.stringify(input), before);
|
|
}
|
|
});
|
|
|
|
void test('old snapshots use S10 and absent or malformed decisions never imply yes or no', () => {
|
|
for (const [applicability, need] of [
|
|
['applicable', 'yes'],
|
|
['not_needed_for_preparation', 'no'],
|
|
['undetermined', 'unknown'],
|
|
])
|
|
assert.equal(
|
|
investmentLandPreparation(
|
|
JSON.stringify({
|
|
version: 1,
|
|
path: { steps: [{ code: 'S10', applicability }] },
|
|
}),
|
|
).need,
|
|
need,
|
|
);
|
|
for (const raw of [
|
|
'',
|
|
'invalid',
|
|
'{}',
|
|
'{"version":1,"land_preparation":{"need":"invalid"}}',
|
|
])
|
|
assert.equal(investmentLandPreparation(raw).need, 'unknown');
|
|
assert.equal(landStatusVisible({}), true); // Standalone simulator preserves its previous behaviour.
|
|
});
|
|
|
|
void test('current dossier assessment refreshes after server recovery without overwriting the saved land answer', () => {
|
|
const scope = 'dossier:A:simulator';
|
|
const inherited = inheritCompatibleFields(
|
|
scope,
|
|
{},
|
|
{
|
|
context_applicability_snapshot: snapshot('yes'),
|
|
land_status: 'duat',
|
|
},
|
|
null,
|
|
);
|
|
const recovered = {
|
|
...inherited.patch,
|
|
context_reuse: inherited.metadata,
|
|
context_applicability_snapshot: snapshot('no'),
|
|
};
|
|
const restored = inheritCompatibleFields(
|
|
scope,
|
|
recovered,
|
|
{ context_applicability_snapshot: snapshot('yes') },
|
|
recovered.context_reuse,
|
|
);
|
|
assert.equal(
|
|
restored.patch.context_applicability_snapshot,
|
|
snapshot('yes'),
|
|
);
|
|
assert.equal(Object.hasOwn(restored.patch, 'land_status'), false);
|
|
});
|
|
|
|
void test('report, CSV and print reflect yes, no and unknown without treating historical DUAT as current when hidden', async () => {
|
|
for (const need of ['yes', 'no', 'unknown']) {
|
|
const input = draft(need);
|
|
const before = JSON.stringify(input);
|
|
const rows = buildProjectDossierRows(input);
|
|
assert.equal(
|
|
rows.some((row) => row[1] === 'Situação do terreno'),
|
|
need === 'yes',
|
|
);
|
|
const report = createProjectDossierReport(input, {
|
|
selectedSections: new Set(['location']),
|
|
});
|
|
const print = await createProjectDossierPrintHtml(report, [], false);
|
|
const csv = exportProjectDossierCsv(input);
|
|
if (need === 'yes') {
|
|
assert.match(print, /DUAT/);
|
|
assert.match(csv, /DUAT/);
|
|
} else {
|
|
assert.doesNotMatch(print, /DUAT/);
|
|
assert.doesNotMatch(csv, /DUAT/);
|
|
assert.match(
|
|
print,
|
|
need === 'no'
|
|
? /valores anteriores conservados/
|
|
: /Definir se precisa de terra/,
|
|
);
|
|
}
|
|
assert.equal(JSON.stringify(input), before);
|
|
}
|
|
});
|