import fs from "node:fs"; import assert from "node:assert/strict"; import { combinedCase } from "../tests/frontend/helpers/irpc-investment-combination-case.mjs"; import { calculateAnnual, recordAnnual, annualBook, annualCurrent, saveAnnualBook, annualHtml, } from "../resources/js/lib/irpc-annual-settlement.ts"; import { createProjectDossierReport } from "../resources/js/lib/project-dossier-report.ts"; const out = new URL( "../outputs/irpc-cbf15-combination-20261111/", import.meta.url, ); fs.mkdirSync(out, { recursive: true }); const expected = { combined: 138, quota: 438, overallocated: null, reconciled: -500, expired: 538, }; for (const [name, balance] of Object.entries(expected)) { if (process.argv[2] === "recovered") { const d = JSON.parse( fs.readFileSync( new URL("recovered-" + name + ".json", out), "utf8", ), ), b = annualBook(d), m = calculateAnnual(d, b); assert.equal(annualCurrent(d, b), true); assert.deepEqual(m, b.result.model); assert.deepEqual( m.arithmeticBalance, balance === null ? null : { numerator: String(balance), denominator: "1" }, ); const html = annualHtml(d), report = createProjectDossierReport(d, { selectedSections: new Set(["finance"]), generatedAt: "2026-11-11T09:00:00+02:00", }); assert.match(html, /CBF15/); assert.match(JSON.stringify(report), /CBF15/); fs.writeFileSync(new URL(name + "-report.html", out), html); fs.writeFileSync( new URL(name + "-verification.json", out), JSON.stringify( { synthetic: true, apiRecovered: true, exactRecalculation: true, html: true, dossierReport: true, balance: m.arithmeticBalance, pending: m.pending, investment: m.investmentCredit, carry: m.creditCarry, officialBalancesConsumed: false, activeChanged: false, }, null, 2, ), ); } else { const c = combinedCase(); c.b.history = [ recordAnnual(c.d, structuredClone(c.b), "2026-11-11T08:00:00+02:00") .result, ]; if (name === "quota") { c.b.investmentCredit.soleProject = "no"; c.b.investmentCredit.projectQuota = { amount: "100", reference: "SYNTHETIC documented residual project quota100", }; } if (["overallocated", "reconciled"].includes(name)) Object.assign(c.b.investmentCredit.assets[0], { amount: "40000", proofAmount: "40000", }); if (name === "reconciled") { c.b.creditCarry.currentPec = "0"; c.b.creditCarry.lots[1].selected = "0"; } if (name === "expired") c.b.investmentCredit.startDate = "2021-01-01"; const book = recordAnnual(c.d, c.b, "2026-11-11T09:00:00+02:00"); assert.deepEqual( book.result.model.arithmeticBalance, balance === null ? null : { numerator: String(balance), denominator: "1" }, ); fs.writeFileSync( new URL(name + ".json", out), JSON.stringify({ ...c.d, modelo_a_snapshot: saveAnnualBook(c.d, book), }), ); } } console.log( process.argv[2] === "recovered" ? "Five API-recovered combinations recalculated and reported." : "Five synthetic combinations prepared.", );