198 lines
7.8 KiB
JavaScript
198 lines
7.8 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import JSZip from 'jszip';
|
|
import { complementaryFixture } from './fixtures/complementary-plan.mjs';
|
|
import { buildComplementaryTables } from '../../resources/js/lib/complementary-report.ts';
|
|
import { buildIntegralFinancialTables } from '../../resources/js/lib/integral-financial-report.ts';
|
|
import {
|
|
restoreBusinessPlanSnapshot,
|
|
serializeBusinessPlanSnapshot,
|
|
} from '../../resources/js/lib/business-financial-plan.ts';
|
|
import {
|
|
partitionSimulationDraft,
|
|
restoreSimulationDraft,
|
|
} from '../../resources/js/lib/simulation-form.ts';
|
|
import {
|
|
createProjectDossierReport,
|
|
createProjectDossierPrintHtml,
|
|
generateProjectDossierDocx,
|
|
} from '../../resources/js/lib/project-dossier-report.ts';
|
|
|
|
void test('personnel in a different currency remains unreconciled without automatic conversion', () => {
|
|
const { plan, draft } = complementaryFixture();
|
|
const staff = buildComplementaryTables(
|
|
{ ...draft, currency: 'USD' },
|
|
plan,
|
|
)[2];
|
|
assert.match(staff.issues.join(' '), /moeda/);
|
|
assert.ok(
|
|
!staff.rows.some(
|
|
(r) => r[0] === 'Diferença face à despesa de pessoal no plano',
|
|
),
|
|
);
|
|
});
|
|
|
|
void test('malformed rows never produce misleading partial totals or erase original data', () => {
|
|
const { plan, draft, team, milestones } = complementaryFixture();
|
|
const input = {
|
|
...draft,
|
|
project_team_snapshot: JSON.stringify([...team, { bad: 'row' }]),
|
|
project_execution_snapshot: JSON.stringify([
|
|
...milestones,
|
|
{ ...milestones[0], responsible: 42 },
|
|
]),
|
|
};
|
|
const before = JSON.stringify(input);
|
|
const t = buildComplementaryTables(input, plan);
|
|
assert.ok(t[1].issues.length);
|
|
assert.equal(t[1].rows.at(-1).at(-2), 'Por determinar');
|
|
assert.ok(t[2].issues.length);
|
|
assert.equal(t[2].rows.at(-1)[1], 'Por determinar');
|
|
assert.equal(JSON.stringify(input), before);
|
|
plan.marketAnalysis.segments = [null];
|
|
assert.ok(buildComplementaryTables(draft, plan)[0].issues.length);
|
|
});
|
|
|
|
void test('market projects integer customers and CAGR, never replaces sales; high growth is a warning', () => {
|
|
const { plan, draft } = complementaryFixture();
|
|
const before = buildIntegralFinancialTables(plan);
|
|
let t = buildComplementaryTables(draft, plan)[0];
|
|
assert.deepEqual(t.rows[0], [
|
|
'Segmento ilustrativo',
|
|
'100',
|
|
'110',
|
|
'121',
|
|
'10,00%',
|
|
]);
|
|
assert.equal(t.issues.length, 0);
|
|
plan.marketAnalysis.segments[0].growthPercent = '150';
|
|
t = buildComplementaryTables(draft, plan)[0];
|
|
assert.deepEqual(t.rows[0].slice(1, 4), ['100', '250', '625']);
|
|
assert.equal(t.issues.length, 0);
|
|
assert.match(t.notes.join(' '), /não bloqueante/);
|
|
assert.deepEqual(buildIntegralFinancialTables(plan), before);
|
|
plan.marketAnalysis.segments[0].customers = '0';
|
|
assert.equal(
|
|
buildComplementaryTables(draft, plan)[0].rows[0].at(-1),
|
|
'Por determinar',
|
|
);
|
|
plan.marketAnalysis.segments[0].growthPercent = '-100';
|
|
assert.equal(buildComplementaryTables(draft, plan)[0].issues.length, 0);
|
|
plan.marketAnalysis.segments[0].customers = '1.5';
|
|
assert.ok(buildComplementaryTables(draft, plan)[0].issues.length);
|
|
plan.marketAnalysis.segments[0].customers = '100';
|
|
plan.marketAnalysis.segments[0].basis = '';
|
|
assert.ok(buildComplementaryTables(draft, plan)[0].issues.length);
|
|
});
|
|
void test('milestone totals distinguish optional unknown actuals, invalid dates and exact centavos', () => {
|
|
const { plan, draft, milestones } = complementaryFixture();
|
|
let t = buildComplementaryTables(draft, plan)[1];
|
|
assert.equal(t.issues.length, 0);
|
|
assert.deepEqual(t.rows.at(-1).slice(-2), ['100.25', 'Por determinar']);
|
|
milestones[0].actual_amount = '0';
|
|
milestones[0].start_date = '2027-02-30';
|
|
draft.project_execution_snapshot = JSON.stringify(milestones);
|
|
t = buildComplementaryTables(draft, plan)[1];
|
|
assert.ok(t.issues.length);
|
|
assert.equal(t.rows.at(-1).at(-1), '0.00');
|
|
milestones[0].start_date = '2027-03-01';
|
|
assert.match(
|
|
buildComplementaryTables(
|
|
{
|
|
...draft,
|
|
project_execution_snapshot: JSON.stringify(milestones),
|
|
},
|
|
plan,
|
|
)[1].issues.join(' '),
|
|
/ordenadas/,
|
|
);
|
|
});
|
|
void test('personnel reconciles 13 months and partial years, preserves financial figures on mismatch', () => {
|
|
const { plan, draft, team } = complementaryFixture();
|
|
const before = buildIntegralFinancialTables(plan);
|
|
let t = buildComplementaryTables(draft, plan)[2];
|
|
assert.equal(t.issues.length, 0);
|
|
assert.deepEqual(
|
|
t.rows.find((r) => r[0] === 'Folha salarial prevista').slice(-2),
|
|
['2400.00', '200.00'],
|
|
);
|
|
assert.ok(
|
|
t.rows
|
|
.at(-1)
|
|
.slice(1)
|
|
.every((x) => x === '0.00'),
|
|
);
|
|
team[0].monthly_salary = '100.01';
|
|
draft.project_team_snapshot = JSON.stringify(team);
|
|
t = buildComplementaryTables(draft, plan)[2];
|
|
assert.equal(t.rows.at(-1)[1], '0.02');
|
|
assert.match(t.issues.join(' '), /diverge/);
|
|
assert.deepEqual(buildIntegralFinancialTables(plan), before);
|
|
team[0].quantity = '1.5';
|
|
assert.ok(
|
|
buildComplementaryTables(
|
|
{ ...draft, project_team_snapshot: JSON.stringify(team) },
|
|
plan,
|
|
)[2].issues.length,
|
|
);
|
|
});
|
|
void test('legacy rows remain preserved and missing supplements remain explicit, not zero', () => {
|
|
const { plan, draft, team, milestones } = complementaryFixture();
|
|
delete plan.marketAnalysis;
|
|
for (const k of [
|
|
'department',
|
|
'quantity',
|
|
'monthly_salary',
|
|
'start_month',
|
|
'end_month',
|
|
])
|
|
delete team[0][k];
|
|
for (const k of ['department', 'responsible', 'start_date'])
|
|
delete milestones[0][k];
|
|
const old = {
|
|
...draft,
|
|
project_team_snapshot: JSON.stringify(team),
|
|
project_execution_snapshot: JSON.stringify(milestones),
|
|
};
|
|
const t = buildComplementaryTables(old, plan);
|
|
assert.ok(t.every((x) => x.issues.length));
|
|
assert.equal(t[2].rows[0][1], 'Por determinar');
|
|
assert.equal(team[0].estimated_cost, '');
|
|
assert.equal(t[1].rows[0].at(-2), '100.25');
|
|
});
|
|
void test('economic draft roundtrip and existing Word/print exports include three complementary tables', async () => {
|
|
const { plan, draft } = complementaryFixture();
|
|
const parts = partitionSimulationDraft(draft);
|
|
const restored = restoreSimulationDraft(
|
|
parts.personal_snapshot,
|
|
parts.economic_snapshot,
|
|
);
|
|
assert.equal(restored.project_team_snapshot, draft.project_team_snapshot);
|
|
assert.equal(
|
|
restored.project_execution_snapshot,
|
|
draft.project_execution_snapshot,
|
|
);
|
|
const recovered = restoreBusinessPlanSnapshot(
|
|
serializeBusinessPlanSnapshot(plan),
|
|
);
|
|
assert.deepEqual(recovered.marketAnalysis, plan.marketAnalysis);
|
|
assert.deepEqual(
|
|
buildComplementaryTables(restored, recovered),
|
|
buildComplementaryTables(draft, plan),
|
|
);
|
|
const report = createProjectDossierReport(draft, {
|
|
selectedSections: new Set(['market', 'team', 'execution', 'finance']),
|
|
});
|
|
assert.equal(report.sections.flatMap((s) => s.tables ?? []).length, 10);
|
|
const html = await createProjectDossierPrintHtml(report, []);
|
|
for (const title of ['Análise de Mercado', 'Marcos', 'Pessoal'])
|
|
assert.ok(html.includes(title));
|
|
const doc = await generateProjectDossierDocx(report, []);
|
|
const zip = await JSZip.loadAsync(
|
|
doc instanceof Blob ? await doc.arrayBuffer() : doc,
|
|
);
|
|
const xml = await zip.file('word/document.xml').async('string');
|
|
for (const title of ['Análise de Mercado', 'Marcos', 'Pessoal'])
|
|
assert.ok(xml.includes(title));
|
|
});
|