Files
Investidor-V2/tests/frontend/presentation-corrections.test.mjs
T

134 lines
4.5 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {
correctionFieldHints,
fieldLabelMatches,
} from '../../resources/js/lib/field-corrections.ts';
import {
publicReference,
publicSourceHref,
publicLocation,
uniquePresentation,
publicPortfolioText,
} from '../../resources/js/lib/public-presentation.ts';
import {
catalogueFilterHref,
catalogueDetailHref,
catalogueReturnHref,
} from '../../resources/js/lib/catalogue-context.ts';
test('existing diagnostics point to matching labels without inventing fields for unknown messages', () => {
const hints = correctionFieldHints(
'O capital discriminado não coincide com o capital próprio.',
);
assert.ok(
hints.some((h) => fieldLabelMatches('Capital do proprietário', h)),
);
assert.ok(hints.some((h) => fieldLabelMatches('Capital próprio (MZN)', h)));
assert.ok(
correctionFieldHints(
'Confira «Preço por unidade»: valor em falta.',
).some((h) => fieldLabelMatches('Preço por unidade', h)),
);
assert.equal(
correctionFieldHints('Situação não prevista').some((h) =>
fieldLabelMatches('Capital próprio', h),
),
false,
);
assert.equal(
fieldLabelMatches('Capital do investidor', 'Capital do proprietário'),
false,
);
});
test('public sources retain legal references and official PHP links, excluding local paths and executable schemes', () => {
for (const value of [
'C:\\laragon\\www\\portal\\app.php',
'docs/auditoria/e25.json',
'QA-20261121',
'http://127.0.0.3:18724/private',
'javascript:alert(1)',
'https://example.org/resources/js/report.ts',
])
assert.equal(publicReference(value), null, value);
assert.equal(publicReference('Decreto n.º 70/2009'), 'Decreto n.º 70/2009');
assert.equal(
publicSourceHref('https://example.gov.mz/documento.php?id=70'),
'https://example.gov.mz/documento.php?id=70',
);
assert.equal(publicSourceHref('Decreto n.º 70/2009'), undefined);
});
test('presentation deduplicates identical editions and preserves source records and full location', () => {
const rows = Object.freeze([
Object.freeze({ ref: 'Lei', date: '2020' }),
Object.freeze({ ref: 'Lei', date: '2020' }),
Object.freeze({ ref: 'Lei', date: '2024' }),
]);
assert.equal(uniquePresentation(rows, (r) => r.ref + r.date).length, 2);
assert.equal(rows.length, 3);
assert.equal(
publicLocation([
{ name: 'Cabo Delgado' },
{ name: 'Balama' },
{ name: 'Balama' },
]),
'Cabo Delgado · Balama · Balama',
);
});
test('published names replace internal cross-reference codes without replacing a code inside a word or altering originals', () => {
const names = Object.freeze({
A06: 'Projecto de arroz',
'CLU-01': 'Cluster industrial',
'A.1': 'Nome pontuado',
});
assert.equal(
publicPortfolioText(
'A06 e CLU-01; A.1. CA06 e A060 permanecem.',
names,
),
'Projecto de arroz e Cluster industrial; Nome pontuado. CA06 e A060 permanecem.',
);
assert.equal(names.A06, 'Projecto de arroz');
});
test('filter removal clears actual indexed arrays and dependent geography without losing deliberate criteria', () => {
assert.equal(
catalogueFilterHref(
'/oportunidades?q=arroz&facets%5B0%5D=a&facets%5B1%5D=b&page=3',
['q', 'facets'],
'facets',
),
'/oportunidades?q=arroz',
);
assert.equal(
catalogueFilterHref(
'/zonas?category=zee&province_id=p&district_id=d&locality_id=l&activity=energia&page=2',
[],
'province_id',
),
'/zonas?category=zee&activity=energia',
);
assert.equal(
catalogueFilterHref('/zonas?category=zee&q=arroz&page=2', [
'category',
'q',
]),
'/zonas',
);
const detail = catalogueDetailHref(
'/zonas/zone',
'category=industrial_park&activity=energia&q=Balama&page=2',
);
const back = new URL(
catalogueReturnHref('/zonas', detail),
'https://portal.invalid',
);
assert.equal(back.searchParams.get('activity'), 'energia');
assert.equal(back.searchParams.get('category'), 'industrial_park');
assert.equal(back.searchParams.get('q'), 'Balama');
assert.equal(back.searchParams.get('page'), '2');
});