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 Map = () => React.createElement('div', { 'data-test-map': true });
function setup() {
let theme;
let mounted = false;
const source = fs.readFileSync(
new URL(
'../../resources/js/components/thematic-investment-map.tsx',
import.meta.url,
),
'utf8',
);
const output = ts.transpileModule(source, {
compilerOptions: {
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES2022,
jsx: ts.JsxEmit.ReactJSX,
},
}).outputText;
const module = { exports: {} };
compileFunction(output, ['require', 'module', 'exports'])(
(name) =>
name === 'react'
? {
...React,
useEffect: (effect) => {
if (!mounted) {
mounted = true;
effect();
}
},
useState: (initial) => {
if (theme === undefined)
theme =
typeof initial === 'function'
? initial()
: initial;
return [
theme,
(next) => {
theme = next;
},
];
},
}
: name === './investment-map-indicators'
? { default: () => null }
: name === './catalogue-investment-map'
? { default: Map }
: require(name),
module,
module.exports,
);
return (...args) => {
module.exports.default(...args);
return module.exports.default(...args);
};
}
const flat = (node) =>
!node || typeof node !== 'object'
? []
: [node, ...React.Children.toArray(node.props.children).flatMap(flat)];
const proof = (category) => ({
id: category,
category,
statement: '',
source: 'Synthetic source',
locator: 'Table 4',
period: '2024',
scope: 'District A',
coverage: 'Partial coverage',
verified_on: '2026-10-01',
});
const records = [
{
id: 1,
title: 'Resource fixture',
url: '/fixture/1',
location: 'District A',
state: 'hypothesis',
limitations: 'No availability confirmed',
evidence: [proof('resource')],
},
{
id: 2,
title: 'Infrastructure fixture',
url: '/fixture/2',
location: 'Province B',
state: 'hypothesis',
limitations: null,
evidence: [proof('infrastructure')],
},
];
const points = [1, 3].map((id) => ({
geometry: { coordinates: [32, -25] },
properties: {
id,
title: 'Site ' + id,
url: '/fixture/' + id,
location: 'Confirmed opportunity site',
},
}));
const areas = [1, 2, 3].map((id) => ({
opportunity_id: id,
title: 'Area ' + id,
geometry: { type: 'Polygon', coordinates: [] },
}));
const props = {
points,
areas,
records,
territoryId: 'province-a',
truncated: false,
};
test('default preserves every opportunity and one map, with sourced thematic evidence and no new location claims', () => {
const Component = setup(),
tree = Component(props),
nodes = flat(tree);
const map = nodes.find((node) => node.type === Map);
assert.equal(nodes.filter((node) => node.type === Map).length, 1);
assert.equal(map.props.points, points);
assert.equal(map.props.areas, areas);
assert.equal(map.props.territoryId, 'province-a');
const html = renderToStaticMarkup(tree);
for (const text of [
'Synthetic source',
'Table 4',
'2024',
'District A',
'Partial coverage',
'2026-10-01',
'não representam a posição exacta',
'sem índice',
])
assert.ok(html.includes(text), text);
assert.match(html, /2 fichas com evidência temática pública/);
assert.match(html, /1 sem ponto público utilizável/);
assert.doesNotMatch(html, /