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

217 lines
7.5 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import { createRequire } from 'node:module';
import { compileFunction } from 'node:vm';
import ts from 'typescript';
import React from 'react';
import { renderToStaticMarkup } from 'react-dom/server';
import * as publicPresentation from '../../resources/js/lib/public-presentation.ts';
import { displayReportMoney } from '../../resources/js/lib/integral-financial-report.ts';
const require = createRequire(import.meta.url);
function component(name) {
const module = { exports: {} };
const code = ts.transpileModule(
fs.readFileSync(
new URL(
`../../resources/js/components/${name}.tsx`,
import.meta.url,
),
'utf8',
),
{
compilerOptions: {
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES2022,
jsx: ts.JsxEmit.ReactJSX,
},
},
).outputText;
compileFunction(code, ['require', 'module', 'exports'])(
(id) =>
id === '@/lib/public-presentation'
? publicPresentation
: id.startsWith('./')
? component(id.slice(2))
: require(id),
module,
module.exports,
);
return module.exports;
}
test('real public source list separates links, merges duplicate notes, retains different editions and original data', () => {
const sources = [
{
title: 'Lei de investimento',
url: 'https://example.gov.mz/lei.php',
source_date: '2020-01-01',
supports: 'Âmbito territorial',
},
{
title: 'Lei de investimento',
url: 'https://example.gov.mz/lei.php',
source_date: '2020-01-01',
supports: 'Âmbito territorial',
},
{
title: 'Lei de investimento',
url: 'https://example.gov.mz/lei.php',
source_date: '2024-01-01',
supports: 'Edição posterior',
},
{
title: 'docs/interno.json',
url: 'C:\\portal\\app.php',
supports: 'QA-20261121',
},
];
const before = JSON.stringify(sources);
const html = renderToStaticMarkup(
React.createElement(component('public-source-list').default, {
sources,
}),
);
assert.equal((html.match(/<li>/g) || []).length, 2);
assert.equal((html.match(/Âmbito territorial/g) || []).length, 1);
assert.match(html, /2020-01-01/);
assert.match(html, /2024-01-01/);
assert.match(html, /href="https:\/\/example.gov.mz\/lei.php"/);
assert.doesNotMatch(html, /docs\/interno|QA-20261121|C:\\portal/);
assert.equal(JSON.stringify(sources), before);
});
test('real report tables expose existing message for navigation without changing their rows or values', () => {
const module = { exports: {} };
const code = ts.transpileModule(
fs.readFileSync(
new URL(
'../../resources/js/components/investment-report-tables.tsx',
import.meta.url,
),
'utf8',
),
{
compilerOptions: {
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES2022,
jsx: ts.JsxEmit.ReactJSX,
},
},
).outputText;
compileFunction(code, ['require', 'module', 'exports'])(
(id) =>
id === '@/lib/integral-financial-report'
? { displayReportMoney }
: id === '@/lib/financial-issue-presentation'
? {
financialIssuesForDisplay: () => [
{
message: 'Confira «Capital próprio».',
tables: ['Quadro'],
},
],
}
: require(id),
module,
module.exports,
);
const tables = [
{
id: 'synthetic',
title: 'Quadro',
headers: ['Rubrica', 'Janeiro'],
rows: [['Saldo declarado', '123.45']],
notes: [],
issues: ['Confira «Capital próprio».'],
},
];
const before = JSON.stringify(tables);
const html = renderToStaticMarkup(
React.createElement(module.exports.InvestmentReportTables, { tables }),
);
assert.match(html, /data-correction-message="Confira «Capital próprio»."/);
assert.doesNotMatch(html, /href="#portal-corrections"/); // Read-only report consumers need no dead editor link.
assert.match(html, /Saldo declarado/);
assert.equal(JSON.stringify(tables), before);
});
test('real territorial presentation removes identical geometry editions but preserves distinct dates and full hierarchy', () => {
const module = { exports: {} };
const code = ts.transpileModule(
fs.readFileSync(
new URL(
'../../resources/js/components/structure-geography.tsx',
import.meta.url,
),
'utf8',
),
{
compilerOptions: {
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES2022,
jsx: ts.JsxEmit.ReactJSX,
},
},
).outputText;
compileFunction(code, ['require', 'module', 'exports'])(
(id) =>
id === '@/lib/public-presentation'
? publicPresentation
: id === './public-reference'
? component('public-reference')
: id === '@inertiajs/react'
? {
Link: ({ children, ...p }) =>
React.createElement('a', p, children),
}
: id.startsWith('./')
? { default: () => null }
: require(id),
module,
module.exports,
);
const point = {
geometry_id: 'point-1',
source_reference: 'Decreto 70/2009',
source_date: '2020-01-01',
geometry: { coordinates: [32, -25] },
};
const boundary = {
geometry_id: 'boundary-1',
source_reference: 'Decreto 70/2009',
source_date: '2020-01-01',
geometry: { type: 'Polygon', coordinates: [] },
};
const locations = [
{
id: 'location',
name: 'Balama sintético',
role: 'primary',
location_type: 'district',
path: [
{ id: 'p', name: 'Cabo Delgado', location_type: 'province' },
{ id: 'd', name: 'Balama', location_type: 'district' },
],
coordinates: [
point,
{ ...point },
{ ...point, source_date: '2024-01-01' },
],
boundaries: [
boundary,
{ ...boundary },
{ ...boundary, source_date: '2024-01-01' },
],
},
];
const before = JSON.stringify(locations);
const html = renderToStaticMarkup(
React.createElement(module.exports.LocationList, { locations }),
);
assert.equal((html.match(/Coordenadas documentadas/g) || []).length, 2);
assert.equal((html.match(/Polígono cadastrado/g) || []).length, 2);
assert.match(html, /Cabo Delgado/);
assert.match(html, /Balama/);
assert.match(html, /2024-01-01/);
assert.equal(JSON.stringify(locations), before);
});