import assert from "node:assert/strict"; import test from "node:test"; import { financialReportFixture } from "./fixtures/integral-financial-plan.mjs"; import { forecastBusinessPlan, serializeBusinessPlanSnapshot, } from "../../resources/js/lib/business-financial-plan.ts"; import { buildIntegralFinancialTables } from "../../resources/js/lib/integral-financial-report.ts"; import { irpcScenarioSchedule } from "../../resources/js/lib/irpc-plan-scenario.ts"; import { createInvestorDecisionReport, createProjectDossierPrintHtml, } from "../../resources/js/lib/project-dossier-report.ts"; const toCents = (value) => { const [whole, fraction = "00"] = String(value).replace("-", "").split("."); const amount = BigInt(whole) * 100n + BigInt(fraction.padEnd(2, "0")); return String(value).startsWith("-") ? -amount : amount; }; void test("the fiscal scenario applies the documented 32% IRPC to each year profit, never to a loss (Criar Hotel)", () => { const plan = financialReportFixture(36); const rows = forecastBusinessPlan(plan).rows; const result = irpcScenarioSchedule(plan, "general"); assert.equal(result.expense.length, 36); assert.deepEqual(result.payment, result.expense); for (let year = 0; year < 3; year++) { const profit = rows .slice(year * 12, year * 12 + 12) .reduce((t, r) => t + toCents(r.profitBeforeTax), 0n); const expected = profit > 0n ? (profit * 32n * 2n + 100n) / 200n : 0n; assert.equal(toCents(result.expense[year * 12 + 11]), expected); for (let m = 0; m < 11; m++) assert.equal(result.expense[year * 12 + m], "0.00"); } assert.match(result.source, /artigo 61\.º, n\.º 1/); assert.match(result.source, /não é liquidação/); // The calculated schedule is accepted by the report as declared taxes. plan.reportAssumptions = { ...plan.reportAssumptions, taxExpenseByMonth: result.expense, taxPaymentByMonth: result.payment, taxSource: result.source, }; const pl = buildIntegralFinancialTables(plan).find( (t) => t.id === "profit-loss", ); assert.deepEqual(pl.issues, []); }); void test("the simulation result is a business plan in the model order, without audit columns (Criar Hotel)", async () => { const plan = financialReportFixture(36); const draft = { project_title: "Hotel QA sintético", project_summary: "Hotel de praia.", plan_objectives: "Lucro no segundo ano.", plan_mission: "Servir bem.", plan_success_keys: "Qualidade, localização.", business_plan_snapshot: serializeBusinessPlanSnapshot(plan), }; const report = createInvestorDecisionReport(draft, { generatedAt: "2026-12-04T12:00:00Z", }); assert.equal(report.title, "Hotel QA sintético"); assert.deepEqual( report.sections.map((s) => s.title), [ "1. Resumo Executivo", "2. Resumo da Startup", "4. Análise de Mercado", "5. Estratégia e Implementação", "6. Resumo da Gestão", "7. Plano Financeiro", "Anexo — detalhe mensal do primeiro ano", ], ); assert.ok(report.sections.every((s) => s.rows.length === 0)); // Chapters carry the yearly columns only; the annex carries 12 months. const pl = report.sections[5].tables.find((t) => t.id === "profit-loss"); assert.ok(pl.headers.slice(1).every((h) => /^\d{4}( \(parcial\))?$/.test(h))); assert.equal(pl.headers.length, 4); const annex = report.sections[6].tables.find((t) => t.id === "profit-loss"); assert.equal(annex.headers.length, 13); assert.ok(annex.headers.slice(1).every((h) => /^\d{4}-\d{2}$/.test(h))); assert.deepEqual(report.sections[0].paragraphs, [ "Hotel de praia.", "Objectivos: Lucro no segundo ano.", "Missão: Servir bem.", "Chaves para o sucesso: Qualidade, localização.", ]); const ids = report.sections.slice(0, -1).flatMap((s) => (s.tables ?? []).map((t) => t.id), ); assert.deepEqual( ids.filter((id) => [ "startup", "sales", "funding", "break-even", "profit-loss", "cashflow", "balance", ].includes(id), ), [ "startup", "sales", "funding", "break-even", "profit-loss", "cashflow", "balance", ], ); assert.ok(!report.missing.some((gap) => gap.field)); const html = await createProjectDossierPrintHtml(report, [], false); assert.match(html, /

InvesteMoz
Plano de negócio<\/h1>/); assert.doesNotMatch(html, /Classificação \/ origem/); }); void test("missing executive summary fields are listed with the field to open (Criar Hotel)", () => { const report = createInvestorDecisionReport({ project_title: "Hotel" }); const fields = report.missing.filter((g) => g.field).map((g) => g.field); assert.deepEqual(fields, [ "project_summary", "plan_objectives", "plan_mission", "plan_success_keys", ]); assert.match( report.missing[0].message, /Preencha este campo para continuar/, ); }); void test("the same missing classification for many months is one line with the months (Criar Hotel)", () => { const plan = financialReportFixture(36); delete plan.ratioCurrentBalances; const report = createInvestorDecisionReport({ project_title: "Hotel", business_plan_snapshot: serializeBusinessPlanSnapshot(plan), }); const lines = report.missing.filter((g) => g.message.startsWith("Rácios financeiros / Activo corrente")); assert.equal(lines.length, 1); assert.match(lines[0].message, /— meses 1 a 36:/); }); void test("the executive summary fields follow the summary in step 1 of the guided journey, with their own help (Criar Hotel)", async () => { const { simulationFieldsForSection } = await import("../../resources/js/lib/simulation-form.ts"); const { formFieldGuidance } = await import("../../resources/js/lib/form-help.ts"); // Guided investment journey: steps come from the library. const draft = { journey_type: "investor", detail_level: "simple", purpose: "own_investment", preferred_language: "pt-MZ", project_title: "Criar Hotel", }; const step1 = simulationFieldsForSection(draft, "purpose").map((f) => f.key); const at = step1.indexOf("project_summary"); assert.deepEqual(step1.slice(at, at + 4), [ "project_summary", "plan_objectives", "plan_mission", "plan_success_keys", ]); const step2 = simulationFieldsForSection(draft, "market").map((f) => f.key); assert.ok(!step2.some((k) => k.startsWith("plan_"))); for (const label of ["Objectivos do negócio", "Missão", "Chaves para o sucesso"]) assert.doesNotMatch( formFieldGuidance({ label, type: "textarea", required: false }).instruction, /Explique a informação pedida/, ); // Small-business journey: the component lists step 1 and step 2 itself. const fs = await import("node:fs"); const component = fs.readFileSync( new URL("../../resources/js/components/simulation-form.tsx", import.meta.url), "utf8", ); const lists = [...component.matchAll(/'project_summary',\s*'plan_objectives',\s*'plan_mission',\s*'plan_success_keys',/g)]; assert.equal(lists.length, 2); });