import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import { compileFunction } from 'node:vm';
import { createRequire } from 'node:module';
import React from 'react';
import { renderToStaticMarkup } from 'react-dom/server';
import ts from 'typescript';
const require = createRequire(import.meta.url);
const source = fs.readFileSync(
new URL(
'../../resources/js/components/investment-map-indicators.tsx',
import.meta.url,
),
'utf8',
);
const compiled = ts.transpileModule(source, {
compilerOptions: {
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES2022,
jsx: ts.JsxEmit.ReactJSX,
},
}).outputText;
const module = { exports: {} };
compileFunction(compiled, ['require', 'module', 'exports'])(
require,
module,
module.exports,
);
const Component = module.exports.default;
const filterIndicators = module.exports.filterMapIndicators;
const compareObservations = module.exports.compareMapObservations;
const Comparison = module.exports.MapObservationComparison;
const row = {
id: '1',
zone_id: 'park-a',
zone_name: 'Synthetic park',
zone_version: 3,
url: '/zonas/park-a',
theme: 'workforce',
indicator: 'employment',
label: 'Synthetic declared jobs',
value: '0',
unit: 'pessoas',
period_start: '2025-01-01',
period_end: '2025-12-31',
scope: 'Park A only',
population: 'Declared establishments',
methodology: 'Documentary register',
source_reference: '',
source_date: '2026-01-15',
knowledge: 'documented',
missing_metadata: [],
};
const context = {
state: 'available',
records: [
row,
{
...row,
id: '2',
theme: 'economic_potential',
indicator: 'production',
label: 'Synthetic production',
value: '12.3400',
unit: 'toneladas',
period_start: '2024-01-01',
period_end: '2024-12-31',
},
],
zones_consulted: 1,
zone_limit: 30,
record_limit: 200,
truncated: false,
};
const render = (data, theme = 'all') =>
renderToStaticMarkup(
React.createElement(Component, { context: data, theme }),
);
test('quantitative observations keep zero, decimal precision, units, periods, scope, methodology and public source', () => {
const html = render(context);
for (const value of [
'0',
'12.3400',
'pessoas',
'toneladas',
'2025-01-01',
'2024-01-01',
'Park A only',
'Declared establishments',
'Documentary register',
'2026-01-15',
'Versão pública 3',
])
assert.ok(html.includes(value), value);
assert.match(html, /Sem somas, conversões de unidade ou moeda/);
assert.match(html, /Emprego não mede trabalhadores disponíveis/);
assert.match(html, /não constituem um índice de potencial/);
assert.doesNotMatch(html, /'));
assert.match(html, /<script>synthetic source/);
assert.equal(JSON.stringify([first, second]), before);
});
test('comparison delta: differences, missing evidence, unknown values and disputed information require review', () => {
const documented = {
...row,
description: 'Emprego observado na fonte sintética.',
};
const second = { ...documented, id: 'different' };
assert.equal(
compareObservations(documented, second).state,
'metadata_aligned',
);
for (const change of [
{ zone_id: 'other' },
{ indicator: 'exports' },
{ unit: 'USD' },
{ scope: 'other base' },
{ population: 'other population' },
{ methodology: 'other method' },
{ knowledge: 'disputed' },
{ knowledge: 'unknown', value: null },
{ value: 'NaN' },
{ source_reference: '' },
{ description: '' },
{ source_date: '2026-02-30' },
{ period_end: '2024-01-01' },
]) {
const result = compareObservations(documented, {
...second,
...change,
});
assert.equal(result.state, 'review_required', JSON.stringify(change));
assert.ok(result.issues.length);
}
assert.equal(compareObservations(row, row).state, 'review_required');
const html = renderToStaticMarkup(
React.createElement(Comparison, {
first: row,
second: { ...second, knowledge: 'unknown', value: null },
}),
);
assert.match(html, /Por confirmar/);
assert.match(html, /lacunas a conferir/);
});
test('comparison delta: progressive selection stays inside the received filters and shows an incomplete pair explicitly', () => {
const html = render(context);
assert.match(html, /Comparar duas observações/);
assert.match(html, /Primeira.*observação/);
assert.match(html, /Segunda.*observação/);
assert.match(html, /uma versão nova exige nova selecção/);
assert.match(html, /Seleccione duas observações/);
const single = render({ ...context, records: [row] });
assert.ok(!single.includes('Comparar duas observações'));
});