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.', measure_kind: 'observed', }; 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('measure kind delta: legacy nature stays unknown and observed quantities cannot align with plans or projections', () => { const observed = { ...row, description: 'Fonte sintética', measure_kind: 'observed', }; const same = { ...observed, id: 'second' }; assert.equal(compareObservations(observed, same).state, 'metadata_aligned'); for (const kind of ['planned', 'projected', 'unknown', null, undefined]) { const other = { ...same, measure_kind: kind }; const before = JSON.stringify(other); const result = compareObservations(observed, other); assert.equal(result.state, 'review_required'); assert.ok( result.issues.some((issue) => issue.includes('Natureza do valor diferente'), ), ); assert.equal(JSON.stringify(other), before); } const legacy = compareObservations( { ...observed, measure_kind: undefined }, { ...same, measure_kind: undefined }, ); assert.equal(legacy.state, 'review_required'); assert.ok( legacy.issues.every((issue) => issue.includes('natureza do valor por confirmar'), ), ); }); test('measure kind delta: public comparison labels nature independently of documentary confidence and retains zero', () => { const first = { ...row, description: 'Fonte sintética', measure_kind: 'observed', }; const second = { ...first, id: 'plan', measure_kind: 'planned' }; const html = renderToStaticMarkup( React.createElement(Comparison, { first, second }), ); assert.match(html, /Observado \/ efectivo/); assert.match(html, /Planeado \/ meta/); assert.match(html, /Documentada/); assert.match(html, />0 { 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')); }); test('temporal delta: unequal duration and overlapping boundaries require review without annualising values', () => { const first = {...row, description:'Synthetic source', measure_kind:'observed', period_start:'2024-01-01', period_end:'2024-12-31', value:'123.4500'}; const second = {...first, id:'second', period_start:'2025-01-01', period_end:'2025-12-31', value:'0'}; const before = JSON.stringify([first,second]); assert.ok(compareObservations(first,second).issues.some(x=>x.includes('Duração dos períodos diferente'))); const boundary = {...second, period_start:'2024-12-31'}; assert.ok(compareObservations(first,boundary).issues.some(x=>x.includes('sobrepõem-se'))); const identical = {...first,id:'same-period'}; assert.equal(compareObservations(first,identical).state,'metadata_aligned'); const html = renderToStaticMarkup(React.createElement(Comparison,{first,second})); assert.match(html,/Duração do período/);assert.match(html,/>3663650 { const first = {...row, description:'Synthetic', measure_kind:'observed'}; for (const change of [{period_start:null},{period_start:'2025'},{period_start:'2025-02-30'},{period_start:'2026-01-01'}]) { const second = {...first,id:'invalid',...change}; const result = compareObservations(first,second); assert.ok(result.issues.some(x=>x.includes('período incompleto ou inválido'))); assert.ok(!result.issues.some(x=>x.includes('Duração dos períodos diferente'))); const html = renderToStaticMarkup(React.createElement(Comparison,{first,second})); assert.match(html,/Por confirmar/);assert.ok(!html.includes('NaN')); } });