import test from 'node:test'; import fs from 'node:fs'; import { createRequire } from 'node:module'; import { compileFunction } from 'node:vm'; import React from 'react'; import { renderToStaticMarkup } from 'react-dom/server'; import ts from 'typescript'; import * as dossier from '../../resources/js/lib/project-dossier.ts'; import * as complementary from '../../resources/js/lib/complementary-report.ts'; import { decimalInputModule } from './helpers/decimal-input.mjs'; import assert from 'node:assert/strict'; import { complementaryFixture } from './fixtures/complementary-plan.mjs'; import * as business from '../../resources/js/lib/business-financial-plan.ts'; import { adoptTeamPayroll, buildComplementaryTables, teamPayrollSchedule, } from '../../resources/js/lib/complementary-report.ts'; const staffTable = (draft) => buildComplementaryTables( draft, business.restoreBusinessPlanSnapshot(draft.business_plan_snapshot), )[2]; test('the personnel map is the payroll source: the investor adopts it and the plan then agrees (tarefa 21)', () => { const { draft } = complementaryFixture(); const team = [ { competency: 'Recepção', department: 'Alojamento', quantity: '2', monthly_salary: '15000.00', start_month: '1', end_month: '13', state: 'gap', action: '', estimated_cost: '', due_date: '', }, { competency: 'Cozinha', department: 'Restauração', quantity: '1', monthly_salary: '20000.00', start_month: '4', end_month: '13', state: 'gap', action: '', estimated_cost: '', due_date: '', }, ]; draft.project_team_snapshot = JSON.stringify(team); const schedule = teamPayrollSchedule(draft.project_team_snapshot, 13); assert.deepEqual(schedule.slice(0, 4), [ '30000.00', '30000.00', '30000.00', '50000.00', ]); assert.ok( staffTable(draft).issues.some((issue) => /diverge da despesa de pessoal/.test(issue), ), 'before adoption the two sources disagree', ); const paymentsBefore = business.restoreBusinessPlanSnapshot( draft.business_plan_snapshot, ).monthlyPayrollPayments; const adopted = { ...draft, business_plan_snapshot: adoptTeamPayroll( draft.business_plan_snapshot, schedule, false, ), }; const plan = business.restoreBusinessPlanSnapshot( adopted.business_plan_snapshot, ); assert.deepEqual(plan.monthlyPayrollExpenseByPeriod, schedule); assert.equal( plan.monthlyPayrollPayments, paymentsBefore, 'payments only change when asked', ); assert.ok( !staffTable(adopted).issues.some((issue) => /diverge da despesa de pessoal/.test(issue), ), 'after adoption the plan agrees with the map', ); const forecast = business.forecastBusinessPlan(plan); assert.equal(forecast.rows[3].payrollExpense, '50000.00'); const withPayments = business.restoreBusinessPlanSnapshot( adoptTeamPayroll(draft.business_plan_snapshot, schedule, true), ); assert.deepEqual(withPayments.monthlyPayrollPaymentsByPeriod, schedule); }); test('an incomplete personnel map is never adopted as the plan payroll', () => { const row = { competency: 'Recepção', department: 'Alojamento', quantity: '2', monthly_salary: '', start_month: '1', end_month: '12', state: 'gap', action: '', estimated_cost: '', due_date: '', }; assert.equal(teamPayrollSchedule(JSON.stringify([row]), 12), null); assert.equal( teamPayrollSchedule( JSON.stringify([ { ...row, monthly_salary: '100', start_month: '5', end_month: '2', }, ]), 12, ), null, ); assert.equal(teamPayrollSchedule('[]', 12), null); const constant = teamPayrollSchedule( JSON.stringify([{ ...row, monthly_salary: '100' }]), 12, ); assert.equal(constant.length, 12); const { plan } = complementaryFixture(); const restored = business.restoreBusinessPlanSnapshot( adoptTeamPayroll( business.serializeBusinessPlanSnapshot({ ...plan, periods: 12 }), constant, false, ), ); assert.equal(restored.monthlyPayrollExpense, '200.00'); assert.equal(restored.monthlyPayrollExpenseByPeriod, undefined); }); test('the team step offers the adoption only while the plan differs, and says so once it agrees', () => { const require = createRequire(import.meta.url); const module = { exports: {} }; const mocks = { '@/lib/project-dossier': dossier, '@/lib/business-financial-plan': business, '@/lib/complementary-report': complementary, '@/components/decimal-input': decimalInputModule, './investment-report-tables': { InvestmentReportTables: () => null }, }; compileFunction( ts.transpileModule( fs.readFileSync( 'resources/js/components/project-planning.tsx', 'utf8', ), { compilerOptions: { module: ts.ModuleKind.CommonJS, jsx: ts.JsxEmit.ReactJSX, }, }, ).outputText, ['require', 'module', 'exports'], )((key) => mocks[key] ?? require(key), module, module.exports); const { draft, team } = complementaryFixture(); // The fixture plan matches the map; a raise makes the two sources differ. draft.project_team_snapshot = JSON.stringify([ { ...team[0], monthly_salary: '150.00' }, ]); const props = { value: draft.project_team_snapshot, onChange() {}, financialPlanSnapshot: draft.business_plan_snapshot, projectCurrency: 'MZN', onAdoptPayroll() {}, }; const render = (extra) => renderToStaticMarkup( React.createElement(module.exports.ProjectTeamPlanner, { ...props, ...extra, }), ); assert.match(render(), /Usar como custo mensal de pessoal/); const schedule = teamPayrollSchedule(draft.project_team_snapshot, 13); const agreed = render({ financialPlanSnapshot: adoptTeamPayroll( draft.business_plan_snapshot, schedule, false, ), }); assert.doesNotMatch(agreed, /Usar como custo mensal de pessoal/); assert.match(agreed, /coincide com este\s+mapa de pessoal/); assert.doesNotMatch( render({ projectCurrency: 'USD' }), /Usar como custo mensal de pessoal/, 'no adoption across currencies', ); });