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'; import { financialReportFixture } from './fixtures/integral-financial-plan.mjs'; import { complementaryFixture } from './fixtures/complementary-plan.mjs'; import * as business from '../../resources/js/lib/business-financial-plan.ts'; import * as dossier from '../../resources/js/lib/project-dossier.ts'; import * as complementary from '../../resources/js/lib/complementary-report.ts'; test('V12 final: supplier import without product stock identifies the incompatible controls without weakening rejection', () => { const plan = financialReportFixture(2); plan.useSupplierPaymentTerms = true; plan.useProductInventorySchedule = false; const raw = JSON.stringify({ version: 17, plan }); assert.equal(business.restoreBusinessPlanSnapshot(raw), null); assert.match( business.financialPlanImportMessage(raw), /Fluxo de Caixa \/ Calcular pagamentos pelo prazo do fornecedor/, ); assert.match( business.financialPlanImportMessage(raw), /Active o stock por produto ou desactive os prazos/, ); }); test('V12 final: imported out-of-horizon recovery identifies exact line and range; correction restores calculation', () => { const plan = financialReportFixture(2); const before = business.forecastBusinessPlan(plan); plan.salesRecoverySchedule = [{ month: 3, percent: '0' }]; const imported = business.restoreBusinessPlanSnapshot( business.serializeBusinessPlanSnapshot(plan), ); assert.ok(imported); assert.throws( () => business.forecastBusinessPlan(imported), /linha 1 \/ Mês \(3\): seleccione um mês inteiro entre 1 e 2/, ); imported.salesRecoverySchedule[0].month = 1; assert.deepEqual(business.forecastBusinessPlan(imported), before); }); test('V12 final: quantity cell diagnostic retains exact calculation and rejection rules', () => { const plan = financialReportFixture(2); plan.products[0].monthlyUnitsByPeriod = ['0', '0']; const valid = business.forecastBusinessPlan(plan); for (const value of ['', '-1', '1.0001']) { plan.products[0].monthlyUnitsByPeriod[1] = value; assert.throws( () => business.forecastBusinessPlan(plan), /Previsão de Vendas \/ Unidades do produto 1, mês 2: preencha/, ); } plan.products[0].monthlyUnitsByPeriod[1] = '0'; assert.deepEqual(business.forecastBusinessPlan(plan), valid); }); test('V12 final: rejected short import names original agenda and never relaxes strict restore', () => { const plan = financialReportFixture(2); const valid = business.serializeBusinessPlanSnapshot(plan); const snapshot = JSON.parse(valid); snapshot.plan.products[0].monthlyUnitsByPeriod = ['0']; const short = JSON.stringify(snapshot); assert.equal(business.restoreBusinessPlanSnapshot(short), null); assert.match( business.financialPlanImportMessage(short), /Previsão de Vendas \/ Unidades do produto 1: a agenda contém 1 meses; deve cobrir os 2/, ); assert.match( business.financialPlanImportMessage(short), /plano actual foi conservado/, ); assert.ok(business.restoreBusinessPlanSnapshot(valid)); assert.match(business.financialPlanImportMessage('{'), /JSON reconhecido/); }); test('V12 final: actual personnel component receives currency, withholds reconciliation and removes warning after correction', () => { const source = fs.readFileSync( new URL( '../../resources/js/components/project-planning.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 require = createRequire(import.meta.url), module = { exports: {} }; const mocks = { '@/lib/project-dossier': dossier, '@/lib/business-financial-plan': business, '@/lib/complementary-report': complementary, './investment-report-tables': { InvestmentReportTables: ({ tables }) => React.createElement('div', null, JSON.stringify(tables)), }, }; compileFunction(code, ['require', 'module', 'exports'])( (key) => mocks[key] ?? require(key), module, module.exports, ); const { plan, team } = complementaryFixture(); const snapshot = business.serializeBusinessPlanSnapshot(plan); const props = { value: JSON.stringify(team), onChange: () => {}, financialPlanSnapshot: snapshot, projectCurrency: 'USD', }; const bad = renderToStaticMarkup( React.createElement(module.exports.ProjectTeamPlanner, props), ); assert.match(bad, /Moeda das remunerações \(USD\).*Moeda \(MZN\)/); assert.match(bad, /sem conversão automática/); assert.ok(!bad.includes('Diferença face à despesa de pessoal no plano')); const fixed = renderToStaticMarkup( React.createElement(module.exports.ProjectTeamPlanner, { ...props, projectCurrency: 'MZN', }), ); assert.ok(!fixed.includes('difere de Plano financeiro')); assert.ok(fixed.includes('Diferença face à despesa de pessoal no plano')); assert.equal(business.serializeBusinessPlanSnapshot(plan), snapshot); });