import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import path from 'node:path'; import { createRequire } from 'node:module'; import { compileFunction } from 'node:vm'; import React from 'react'; import { renderToStaticMarkup } from 'react-dom/server'; import ts from 'typescript'; const require = createRequire(import.meta.url); const root = path.resolve('resources/js'); function load(file) { const absolute = path.resolve(root, file); const module = { exports: {} }; const code = ts.transpileModule(fs.readFileSync(absolute, 'utf8'), { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022, jsx: ts.JsxEmit.ReactJSX, }, }).outputText; compileFunction(code, ['require', 'module', 'exports'])( (id) => { if (id === '@inertiajs/react') return { Link: ({ children, ...props }) => React.createElement('a', props, children), usePage: () => ({ url: '/carteira/x', props: {} }), }; if (id.startsWith('@/')) { const base = path.resolve(root, id.slice(2)); const found = [base + '.ts', base + '.tsx'].find((p) => fs.existsSync(p), ); if (found) return load(path.relative(root, found)); } return require(id); }, module, module.exports, ); return module.exports; } const source = 'upgrade do portal de investimentos v4 programacao.docx — 4.5.2'; const excerpt = 'Irrigação, sementes, mecanização, frio, silos, embalagem, certificação, financiamento e compradores.'; const origin = { key: 'opportunity:1', title: 'Agro-indústria e segurança alimentar', href: '/carteira/1', }; const option = (code, title) => ({ code, title, origin, territory_id: null, territory: null, source, excerpt, source_sha256: 'a'.repeat(64), opportunities: [], services: [], }); test('complements show the origin and excerpt once, without the internal requirements reference (mapa de erros 4.3)', () => { const { default: InvestmentComplements } = load( 'components/investment-complements.tsx', ); const html = renderToStaticMarkup( React.createElement(InvestmentComplements, { items: [ option('irrigation', 'Regadio'), option('storage', 'Silos e armazenagem'), option('cold', 'Cadeia de frio'), ], }), ); assert.doesNotMatch(html, /programacao|\.docx|Referência:|Trecho:/); assert.doesNotMatch(html, /a{64}/); assert.equal(html.split('Investimento de origem:').length - 1, 1); assert.equal(html.split('Irrigação, sementes').length - 1, 1); for (const title of ['Regadio', 'Silos e armazenagem', 'Cadeia de frio']) assert.match(html, new RegExp(title)); assert.equal( html.split('Iniciar preparação de uma ideia de').length - 1, 3, ); }); test('selected complements in the dossier do not expose the internal source either', () => { const { SelectedComplements } = load( 'components/investment-complements.tsx', ); const html = renderToStaticMarkup( React.createElement(SelectedComplements, { items: [ { key: 'k', code: 'irrigation', title: 'Regadio', kind: 'need', origin, target: null, territory_id: null, territory: null, source, excerpt, source_sha256: 'a'.repeat(64), }, ], }), ); assert.doesNotMatch(html, /programacao|\.docx|Fonte:|Trecho:/); assert.match(html, /Regadio/); });