import test from 'node:test'; import assert from 'node:assert/strict'; import JSZip from 'jszip'; import { complementaryFixture } from './fixtures/complementary-plan.mjs'; import { buildIntegralFinancialTables } from '../../resources/js/lib/integral-financial-report.ts'; import { serializeBusinessPlanSnapshot } from '../../resources/js/lib/business-financial-plan.ts'; import { createIntegralFinancialReport, createProjectDossierReport, integralFinancialTableIds, generateProjectDossierDocx, createProjectDossierPrintHtml, } from '../../resources/js/lib/project-dossier-report.ts'; import { createInvestorDossierReport, createInvestorStageReport, investorFinancialReportChoices, } from '../../resources/js/lib/investor-dossier-report.ts'; export function consolidatedFixture() { const { plan, draft } = complementaryFixture(), balance = buildIntegralFinancialTables(plan).find( (t) => t.id === 'balance', ); plan.ratioCurrentBalances = { assetsByMonth: balance.rows .find((r) => r[0] === 'Caixa líquida') .slice(1, 14), liabilitiesByMonth: balance.rows .find((r) => r[0] === 'Total do passivo') .slice(1, 14), }; draft.business_plan_snapshot = serializeBusinessPlanSnapshot(plan); return { plan, draft }; } const dossier = () => { const { draft } = consolidatedFixture(); return { case: { id: 'synthetic-case', title: 'Hotel QA sintético', version: 3 }, context_opportunity: null, opportunity_options: [], investment_context: { revision: 1, fields: {} }, phases: {}, steps: [ { code: 'S03', title: 'Viabilidade', state: 'preparing', reason: '', phase: 1, detail: {}, }, ], documents: [], results: [ { id: 'saved-one', draft_id: 'synthetic-draft', draft_version: 1, recorded_at: '2026-11-20T12:00:00Z', currency: 'MZN', engine: 'financial-calculators:v1', financial_snapshot: '', preparation_snapshot: draft, context_revision: 1, context_state: 'current', }, ], }; }; void test('eleven tables and summary come from one immutable saved scenario and appear in both export formats', async () => { const { draft } = consolidatedFixture(), before = JSON.stringify(draft), report = createIntegralFinancialReport(draft, { version: 'QA v1', generatedAt: '2026-11-20T12:00:00Z', }); assert.equal(report.sections.length, 1); assert.deepEqual( report.sections[0].tables.map((t) => t.id), [...integralFinancialTableIds], ); assert.equal(new Set(report.sections[0].tables.map((t) => t.id)).size, 11); const original = createProjectDossierReport(draft, { selectedSections: new Set(['finance', 'market', 'team', 'execution']), }); for (const t of report.sections[0].tables) assert.deepEqual( t, original.sections .flatMap((s) => s.tables ?? []) .find((r) => r.id === t.id), ); const cash = report.sections[0].rows.find( (r) => r.field === 'Caixa final projectada', ); assert.match(cash.value, /4 540,00/); assert.equal(JSON.stringify(draft), before); const html = await createProjectDossierPrintHtml(report, []), doc = await generateProjectDossierDocx(report, []), zip = await JSZip.loadAsync( doc instanceof Blob ? await doc.arrayBuffer() : doc, ), xml = await zip.file('word/document.xml').async('string'); for (const t of report.sections[0].tables) { assert.ok(html.includes(t.title)); assert.ok(xml.includes(t.title)); } }); void test('dossier uses only the chosen preserved scenario, requires choice between versions and never leaks another opportunity', () => { const input = dossier(); let report = createInvestorDossierReport(input); assert.equal( report.sections.find((s) => s.id === 'financial-report').tables.length, 11, ); const other = structuredClone(input.results[0]); other.id = 'other-opportunity'; other.context_opportunity = 'foreign'; other.preparation_snapshot.project_team_snapshot = 'FOREIGN PRIVATE DATA'; input.results.push(other); assert.equal(investorFinancialReportChoices(input).length, 1); assert.ok( !JSON.stringify(createInvestorDossierReport(input)).includes( 'FOREIGN PRIVATE DATA', ), ); const second = structuredClone(input.results[0]); second.id = 'saved-two'; second.draft_version = 2; const snapshot = JSON.parse( second.preparation_snapshot.business_plan_snapshot, ); snapshot.plan.reportAssumptions.startupExpenses[0].amount = '100.25'; second.preparation_snapshot.business_plan_snapshot = JSON.stringify(snapshot); input.results.push(second); assert.throws(() => createInvestorDossierReport(input), /Escolha/); const chosen = createInvestorDossierReport(input, undefined, { financialResultId: 'saved-two', }).sections.find((s) => s.id === 'financial-report'); assert.match( chosen.rows.find((r) => r.field === 'Caixa final projectada').value, /4 539,75/, ); assert.equal( chosen.tables .find((t) => t.id === 'funding') .rows.find((r) => r[0] === 'Diferença por financiar')[1], '100.25', ); assert.throws( () => createInvestorDossierReport(input, undefined, { financialResultId: other.id, }), /contexto/, ); report = createInvestorDossierReport(input, undefined, { financialResultId: second.id, }); assert.match( report.sections .find((s) => s.id === 'financial-report') .rows.find((r) => r.field === 'Caixa final projectada').value, /4 539,75/, ); assert.equal( createInvestorStageReport( input, 'S03', undefined, second.id, ).sections.find((s) => s.id === 'financial-report').tables.length, 11, ); }); void test('historical or absent snapshots are not silently reconstructed from another version', () => { const input = dossier(); input.results[0].context_revision = 0; let report = createInvestorDossierReport(input); assert.match( report.sections .find((s) => s.id === 'financial-report') .paragraphs.join(' '), /histórica/, ); delete input.results[0].preparation_snapshot.business_plan_snapshot; report = createInvestorDossierReport(input); assert.ok(!report.sections.some((s) => s.id === 'financial-report')); }); void test('currency differences remain explicit without relabelling or converting the saved plan', () => { const { draft } = consolidatedFixture(); const report = createIntegralFinancialReport({ ...draft, currency: 'USD' }); assert.match(report.sections[0].paragraphs.join(' '), /MZN.*USD/); assert.equal( report.sections[0].rows.find((r) => r.field === 'Moeda do plano').value, 'MZN', ); });