88 lines
3.9 KiB
TypeScript
88 lines
3.9 KiB
TypeScript
import { beforeEach, describe, expect, it } from "vitest";
|
||
import { formatEuro } from "@/domain/money";
|
||
import { loadDashboard } from "@/server/dashboard";
|
||
import { prisma } from "@/server/db";
|
||
import { saveOperation } from "@/server/services/operations";
|
||
import { categoryId, makeUsers, resetDb } from "./helpers";
|
||
|
||
let H: string;
|
||
let C: string;
|
||
|
||
async function add(input: Parameters<typeof saveOperation>[0]) {
|
||
const r = await saveOperation(input, H);
|
||
if (!r.ok) throw new Error(r.error);
|
||
return r.id;
|
||
}
|
||
|
||
beforeEach(async () => {
|
||
await resetDb();
|
||
const users = await makeUsers();
|
||
H = users.h.id;
|
||
C = users.c.id;
|
||
const half = { [H]: 5000, [C]: 5000 };
|
||
const allH = { [H]: 10000, [C]: 0 };
|
||
const base = { status: "ACTUAL" as const, date: "2026-10-15" };
|
||
|
||
await add({ ...base, kind: "INCOME", amount: "3000", label: "Salaire", categoryId: await categoryId("Salaire"), accountUserId: H, split: allH, date: "2026-10-25" });
|
||
await add({ ...base, kind: "INCOME", amount: "2000", label: "Salaire", categoryId: await categoryId("Salaire"), accountUserId: C, split: { [H]: 0, [C]: 10000 }, date: "2026-10-25" });
|
||
await add({
|
||
...base,
|
||
kind: "EXPENSE",
|
||
amount: "1000",
|
||
label: "Loyer",
|
||
categoryId: await categoryId("Logement"),
|
||
accountUserId: H,
|
||
split: half,
|
||
date: "2026-10-05",
|
||
makeRecurring: { frequency: "MONTHLY", endDate: null },
|
||
});
|
||
const rent = await prisma.recurring.findFirstOrThrow();
|
||
await add({
|
||
...base,
|
||
kind: "INCOME",
|
||
amount: "200",
|
||
label: "Part loyer employeur",
|
||
categoryId: await categoryId("Remboursement employeur"),
|
||
accountUserId: H,
|
||
split: half,
|
||
allocatedToRecurringId: rent.id,
|
||
allocationMode: "REDUCE_SHARED",
|
||
});
|
||
await add({ ...base, kind: "EXPENSE", amount: "86,40", label: "Carrefour", categoryId: await categoryId("Courses"), accountUserId: C, split: half, date: "2026-10-27" });
|
||
await add({ ...base, kind: "EXPENSE", amount: "350", label: "Révision", categoryId: await categoryId("Auto"), accountUserId: H, split: allH, status: "PLANNED", date: "2026-10-30" });
|
||
await prisma.budget.create({ data: { categoryId: await categoryId("Courses"), monthlyAmount: "400", startMonth: "2026-01" } });
|
||
});
|
||
|
||
describe("loadDashboard", () => {
|
||
it("phrases the balance from the viewer's point of view", async () => {
|
||
// H : loyer +500, employeur −100, courses payées par C −43,20 = 356,80 €
|
||
expect((await loadDashboard("2026-10", H, "2026-10-27")).balance?.text).toBe(`C te doit ${formatEuro(35680)}`);
|
||
expect((await loadDashboard("2026-10", C, "2026-10-27")).balance?.text).toBe(`Tu dois ${formatEuro(35680)} à H`);
|
||
});
|
||
|
||
it("assembles flows, totals, budgets, net charges and recent operations", async () => {
|
||
const d = await loadDashboard("2026-10", H, "2026-10-27");
|
||
expect(d.persons.map((p) => p.user.firstName)).toEqual(["H", "C"]);
|
||
// H : revenus propres 3000 − (500 + 43,20 − 100) = 2556,80
|
||
expect(d.persons[0].flows.remaining).toBe(255680);
|
||
expect(d.totals.plannedExpense).toBe(35000);
|
||
expect(d.budgets).toEqual([
|
||
expect.objectContaining({ name: "Courses", emoji: "🛒", budget: 40000, actual: 8640, over: false }),
|
||
]);
|
||
expect(d.netCharges).toEqual([expect.objectContaining({ label: "Loyer", gross: 100000, reimbursed: 20000, net: 80000 })]);
|
||
expect(d.recent[0].label).toBe("Révision");
|
||
expect(d.recent.length).toBeLessThanOrEqual(8);
|
||
expect(d.toConfirmCount).toBe(0);
|
||
expect((await loadDashboard("2026-10", H, "2026-10-31")).toConfirmCount).toBe(1);
|
||
});
|
||
|
||
it("reports a balanced household", async () => {
|
||
await prisma.settlement.create({
|
||
data: { fromUserId: C, toUserId: H, amount: "356.80", date: new Date("2026-10-28T00:00:00Z") },
|
||
});
|
||
const d = await loadDashboard("2026-10", H, "2026-10-28");
|
||
expect(d.balance?.text).toBe("Vous êtes à l'équilibre");
|
||
expect(d.lastSettlementDate).toBe("2026-10-28");
|
||
});
|
||
});
|