import assert from 'node:assert/strict'; import test from 'node:test'; 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'; import { financialReportFixture } from './fixtures/integral-financial-plan.mjs'; import * as report from '../../resources/js/lib/integral-financial-report.ts'; import * as warnings from '../../resources/js/lib/financial-plan-warnings.ts'; import * as annual from '../../resources/js/lib/annual-example-report.ts'; import { serializeBusinessPlanSnapshot, restoreBusinessPlanSnapshot, } from '../../resources/js/lib/business-financial-plan.ts'; void test('one startup editor retains all rows and persists an edit without duplicating controls or changing reconciliation', () => { const source = fs.readFileSync( new URL( '../../resources/js/components/integral-financial-plan.tsx', import.meta.url, ), 'utf8', ); const code = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022, jsx: ts.JsxEmit.ReactJSX, }, }).outputText; const module = { exports: {} }, require = createRequire(import.meta.url); compileFunction(code, ['require', 'module', 'exports'])( (name) => name === '@/lib/integral-financial-report' ? report : name === './investment-report-tables' ? { InvestmentReportTables: () => null } : name === '@/lib/financial-plan-warnings' ? warnings : name === '@/lib/annual-example-report' ? annual : name === './business-plan-warnings' ? { BusinessPlanWarnings: () => null } : name === './startup-asset-requirements' ? { StartupAssetRequirements: () => null } : require(name), module, module.exports, ); let plan = financialReportFixture(12); const render = () => module.exports.IntegralFinancialPlan({ plan, onChange: (next) => { plan = next; }, }); const html = renderToStaticMarkup(render()); assert.equal( (html.match(/Adicionar despesa de arranque/g) ?? []).length, 1, ); assert.equal((html.match(/Remover despesa/g) ?? []).length, 1); const nodes = (node) => Array.isArray(node) ? node.flatMap(nodes) : node && typeof node === 'object' ? [node, ...nodes(node.props?.children)] : []; const label = nodes(render()).find( (node) => node.type === 'label' && React.Children.toArray(node.props.children).includes( 'Montante de arranque 1', ), ); nodes(label) .find((node) => node.type === 'input') .props.onChange({ target: { value: '100.25' } }); plan = restoreBusinessPlanSnapshot(serializeBusinessPlanSnapshot(plan)); assert.equal(plan.reportAssumptions.startupExpenses[0].amount, '100.25'); assert.match(renderToStaticMarkup(render()), /value="100.25"/); const balance = report .buildIntegralFinancialTables(plan) .find((table) => table.id === 'balance'); assert.ok( balance.rows .find((row) => row[0] === 'Diferença do balanço') .slice(1) .every((value) => value === '0.00'), ); });