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'; import * as catalogue from '../../resources/js/lib/catalogue-context.ts'; import * as territory from '../../resources/js/lib/search-territory.ts'; const require = createRequire(import.meta.url); const Frame = ({ children }) => React.createElement('main', null, children); const shared = { Layout: Frame, Management: Frame, Field: ({ children }) => React.createElement('label', null, children), control: '', button: '', Pagination: () => null, kind: () => '', }; const module = { exports: {} }; compileFunction( ts.transpileModule( fs.readFileSync('resources/js/pages/portfolio/index.tsx', 'utf8'), { compilerOptions: { module: ts.ModuleKind.CommonJS, jsx: ts.JsxEmit.ReactJSX, }, }, ).outputText, ['require', 'module', 'exports'], )( (name) => { if (name === '@inertiajs/react') return { Link: ({ href, children }) => React.createElement('a', { href }, children), useForm: (data) => ({ data, setData() {}, get() {} }), usePage: () => ({ url: '/carteira?q=Soja' }), }; if (name === '@/lib/catalogue-context') return catalogue; if (name === '@/lib/search-territory') return territory; if (name === '@/components/investment-territory') return { TerritoryFields: () => null }; if (name === '../management/shared' || name === './shared') return shared; return require(name); }, module, module.exports, ); const page = (data) => renderToStaticMarkup( React.createElement(module.exports.default, { management: false, records: { data: [] }, filters: { q: 'Soja' }, publishedOpportunities: { data }, territorySelection: [], sectors: [], }), ); test('real documentary opportunity appears within the empty portfolio consultation with its own detail link', () => { // Existing public QA record, from the unchanged documentary corridor source. const source = JSON.parse( fs.readFileSync( 'database/data/corridor-investment-opportunities.json', 'utf8', ), ); const summary = source.groups .find((g) => g.name === 'Corredor da Beira') .opportunities.find((o) => o.chain === 'Soja').description; const html = page([ { id: 287, slug: 'inv-mz-gen-5563232446855414', title: 'Soja — Corredor da Beira', summary, location: 'Localização não indicada', }, ]); assert.match(html, /Oportunidades publicadas nesta consulta/); assert.match(html, /Soja — Corredor da Beira/); assert.match( html, /href="\/oportunidades\/inv-mz-gen-5563232446855414\?catalogue_query=q%3DSoja"/, ); assert.match(html, /Localização não indicada/); }); test('no matching public observations produce an explicit empty state rather than invented cards', () => { const html = page([]); assert.match( html, /Não há oportunidades publicadas com correspondência documentada/, ); assert.doesNotMatch(html, /inv-mz-gen-/); });