import { describe, expect, it } from "vitest"; import { budgetForMonth, budgetStatus, householdTotals, netCharges, personFlows } from "@/domain/monthly"; import { op } from "./fixtures"; const H100 = { H: 10000, C: 0 }; const C100 = { H: 0, C: 10000 }; const ops = [ op({ kind: "INCOME", amount: 300000, accountUserId: "H", split: H100, date: "2026-10-25" }), op({ kind: "INCOME", amount: 200000, accountUserId: "C", split: C100, date: "2026-10-25" }), op({ kind: "EXPENSE", amount: 100000, accountUserId: "H", recurringId: "rent", date: "2026-10-05" }), op({ kind: "INCOME", amount: 20000, accountUserId: "H", allocatedToRecurringId: "rent", allocationMode: "REDUCE_SHARED" }), op({ kind: "EXPENSE", amount: 38000, accountUserId: "H", split: H100, recurringId: "car", date: "2026-10-10" }), op({ kind: "INCOME", amount: 22000, accountUserId: "H", split: H100, allocatedToRecurringId: "car", allocationMode: "REDUCE_SHARED" }), op({ kind: "EXPENSE", amount: 8640, accountUserId: "C", categoryId: "courses", date: "2026-10-27" }), op({ kind: "EXPENSE", amount: 35000, accountUserId: "H", split: H100, status: "PLANNED", date: "2026-10-30" }), op({ kind: "EXPENSE", amount: 99999, accountUserId: "H", date: "2026-11-01" }), op({ kind: "EXPENSE", amount: 77777, accountUserId: "H", status: "SKIPPED", recurringId: "rent", categoryId: "courses" }), ]; describe("personFlows", () => { it("computes H's October flows", () => { expect(personFlows(ops, "2026-10", "H")).toEqual({ income: 342000, paidOut: 138000, chargesShare: 60320, ownIncome: 300000, remaining: 239680, }); }); it("computes C's October flows", () => { expect(personFlows(ops, "2026-10", "C")).toEqual({ income: 200000, paidOut: 8640, chargesShare: 44320, ownIncome: 200000, remaining: 155680, }); }); }); describe("householdTotals", () => { it("includes planned operations and excludes other months and skipped ones", () => { expect(householdTotals(ops, "2026-10")).toEqual({ income: 542000, allocatedIncome: 42000, plannedIncome: 0, expense: 181640, plannedExpense: 35000, remaining: 360360, }); }); }); describe("netCharges", () => { const charges = [ { id: "rent", label: "Loyer", kind: "recurring" as const, split: { H: 5000, C: 5000 } }, { id: "car", label: "Crédit voiture", kind: "recurring" as const, split: H100 }, { id: "internet", label: "Box", kind: "recurring" as const, split: { H: 5000, C: 5000 } }, ]; it("lists charges with reimbursements, gross minus reimbursed", () => { const result = netCharges(ops, "2026-10", charges); expect(result).toEqual([ { id: "rent", label: "Loyer", gross: 100000, reimbursed: 20000, net: 80000, split: { H: 5000, C: 5000 } }, { id: "car", label: "Crédit voiture", gross: 38000, reimbursed: 22000, net: 16000, split: H100 }, ]); }); it("handles a one-off expense as a charge", () => { const hotel = op({ id: "hotel", kind: "EXPENSE", amount: 15000, accountUserId: "H", split: H100 }); const refund = op({ kind: "INCOME", amount: 15000, accountUserId: "H", split: H100, allocatedToOperationId: "hotel" }); const result = netCharges([hotel, refund], "2026-10", [{ id: "hotel", label: "Hôtel", kind: "operation", split: H100 }]); expect(result).toEqual([{ id: "hotel", label: "Hôtel", gross: 15000, reimbursed: 15000, net: 0, split: H100 }]); }); }); describe("budgets", () => { it("compares actual spending to the budget", () => { expect(budgetStatus(ops, "2026-10", [{ categoryId: "courses", amount: 40000 }])).toEqual([ { categoryId: "courses", budget: 40000, actual: 8640, plannedRemaining: 0, over: false }, ]); expect(budgetStatus(ops, "2026-10", [{ categoryId: "courses", amount: 5000 }])[0].over).toBe(true); }); it("picks the budget active in the month, latest start wins", () => { const budgets = [ { categoryId: "courses", monthlyAmount: 30000, startMonth: "2026-01", endMonth: null }, { categoryId: "courses", monthlyAmount: 40000, startMonth: "2026-09", endMonth: null }, { categoryId: "loisirs", monthlyAmount: 15000, startMonth: "2026-11", endMonth: null }, { categoryId: "auto", monthlyAmount: 10000, startMonth: "2026-01", endMonth: "2026-09" }, ]; expect(budgetForMonth(budgets, "2026-10")).toEqual([{ categoryId: "courses", amount: 40000 }]); }); });