import { beforeEach, describe, expect, it } from "vitest"; import { prisma } from "@/server/db"; import { deleteLoan, saveLoan, type LoanFormInput } from "@/server/services/loans"; import { makeUsers, resetDb } from "./helpers"; const TODAY = "2026-10-27"; let input: LoanFormInput; beforeEach(async () => { await resetDb(); const { h, c } = await makeUsers(); input = { name: "Crédit voiture", lender: "Banque", principal: "20000", annualRate: "4", durationMonths: 48, startDate: "2026-01-05", insuranceMonthly: "15", accountUserId: h.id, split: { [h.id]: 10000, [c.id]: 0 }, paymentOverride: "", referenceDate: "", referenceRemainingCapital: "", }; }); describe("saveLoan", () => { it("creates the loan's monthly recurring, without retroactive occurrences", async () => { const r = await saveLoan(input, TODAY); if (!r.ok) throw new Error(r.error); const rec = await prisma.recurring.findFirstOrThrow({ where: { loanId: r.id }, include: { category: true } }); expect(rec.amount.toString()).toBe("466.58"); expect(rec.endDate?.toISOString().slice(0, 10)).toBe("2029-12-05"); expect(rec.category.name).toBe("Crédit"); const first = await prisma.operation.findFirstOrThrow({ where: { recurringId: rec.id }, orderBy: { date: "asc" } }); expect(first.date.toISOString().slice(0, 10)).toBe("2026-10-05"); }); it("follows the bank's figures for a loan already running", async () => { const r = await saveLoan(input, TODAY); if (!r.ok) throw new Error(r.error); await saveLoan( { ...input, id: r.id, referenceDate: "2026-06-10", referenceRemainingCapital: "18000", paymentOverride: "460" }, TODAY, ); const rec = await prisma.recurring.findFirstOrThrow({ where: { loanId: r.id } }); expect(rec.amount.toString()).toBe("475"); const future = await prisma.operation.findMany({ where: { recurringId: rec.id, status: "PLANNED", date: { gte: new Date("2026-10-27T00:00:00Z") } }, }); expect(future.length).toBeGreaterThan(0); expect(future.every((o) => o.amount.toString() === "475")).toBe(true); }); it("rejects a payment that cannot cover the interest", async () => { const r = await saveLoan({ ...input, referenceDate: "2026-06-10", referenceRemainingCapital: "18000", paymentOverride: "50" }, TODAY); expect(r).toEqual({ ok: false, error: "Mensualité insuffisante pour couvrir les intérêts" }); }); it("validates the rate and the capital", async () => { expect(await saveLoan({ ...input, annualRate: "abc" }, TODAY)).toEqual({ ok: false, error: "Taux invalide." }); expect(await saveLoan({ ...input, principal: "0" }, TODAY)).toEqual({ ok: false, error: "Capital emprunté invalide (ex. 40000 ou 40 000,00)." }); }); }); describe("deleteLoan", () => { it("removes the loan and its recurring but keeps actual history", async () => { const r = await saveLoan(input, TODAY); if (!r.ok) throw new Error(r.error); await prisma.operation.updateMany({ where: { date: new Date("2026-10-05T00:00:00Z") }, data: { status: "ACTUAL" } }); await deleteLoan(r.id, TODAY); expect(await prisma.loan.count()).toBe(0); expect(await prisma.recurring.count()).toBe(0); expect(await prisma.operation.count({ where: { status: "ACTUAL" } })).toBe(1); expect(await prisma.operation.count({ where: { status: "PLANNED", date: { gte: new Date("2026-10-27T00:00:00Z") } } })).toBe(0); }); });