import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import { createRequire } from 'node:module'; import { compileFunction } from 'node:vm'; import ts from 'typescript'; import React from 'react'; import { renderToStaticMarkup } from 'react-dom/server'; const require = createRequire(import.meta.url), calls = []; const labelsSource = fs .readFileSync('resources/js/pages/partnerships/shared.tsx', 'utf8') .match( /export const labels: Record = (\{[\s\S]*?\n\});/, )[1]; const labels = compileFunction(`return (${labelsSource});`, [])(); const module = { exports: {} }; const source = ts.transpileModule( fs.readFileSync('resources/js/pages/partnerships/dashboard.tsx', 'utf8'), { compilerOptions: { module: ts.ModuleKind.CommonJS, jsx: ts.JsxEmit.ReactJSX, }, }, ).outputText; compileFunction(source, ['require', 'module', 'exports'])( (id) => { if (id === './shared') return { Frame: ({ title, children }) => React.createElement( 'main', {}, React.createElement('h1', {}, title), children, ), labels, }; if (id === '@inertiajs/react') return { Link: ({ children, ...props }) => React.createElement('a', props, children), router: { post: (...args) => calls.push(args) }, }; return require(id); }, module, module.exports, ); const Dashboard = module.exports.default; const props = () => ({ mine: [], interests: [ { id: 10, partnership_id: 1, title: null, partnership_href: null, status: 'withdrawn', decision_reason: null, room_id: null, }, ], moderation: [], notifications: [], }); const render = (p) => renderToStaticMarkup(React.createElement(Dashboard, p)); const nodes = (value) => Array.isArray(value) ? value.flatMap(nodes) : React.isValidElement(value) ? [value, ...nodes(value.props.children)] : []; test('unavailable parent has an explanation and history link without a broken proposal link', () => { const html = render(props()); assert.ok(html.includes('consulta da proposta indisponível')); assert.ok(!html.includes('href="/parcerias/1"')); assert.ok(html.includes('href="/parcerias/candidaturas/10/historico"')); }); test('authorized parent title and supplied proposal link are preserved', () => { const p = props(); p.interests[0].title = 'SYNTHETIC published title'; p.interests[0].partnership_href = '/parcerias/1'; const html = render(p); assert.ok(html.includes('SYNTHETIC published title')); assert.ok(html.includes('href="/parcerias/1"')); }); test('authorized room remains reachable independently of parent publication', () => { const p = props(); p.interests[0].room_id = 7; const html = render(p); assert.ok(html.includes('href="/parcerias/salas/7"')); assert.ok(!html.includes('href="/parcerias/1"')); }); test('notification titles are escaped and the historical scope limitation is explained', () => { const p = props(); p.notifications = [ { id: 30, title: '', href: '/parcerias/candidaturas/10/historico', read_at: null, }, ]; const html = render(p); assert.ok(html.includes('<script>')); assert.ok(!html.includes('