88 lines
3.7 KiB
TypeScript
88 lines
3.7 KiB
TypeScript
import { beforeEach, describe, expect, it } from "vitest";
|
|
import { fromIsoDate } from "@/lib/dates";
|
|
import { prisma } from "@/server/db";
|
|
import { deleteRecurring, endRecurring, saveRecurring, setRecurringPaused, type RecurringInput } from "@/server/services/recurrings";
|
|
import { categoryId, makeUsers, resetDb } from "./helpers";
|
|
|
|
const TODAY = "2026-10-27";
|
|
let H: string;
|
|
let C: string;
|
|
let rentInput: RecurringInput;
|
|
|
|
const planned = (recurringId: string) =>
|
|
prisma.operation.findMany({ where: { recurringId, status: "PLANNED" }, orderBy: { date: "asc" } });
|
|
|
|
beforeEach(async () => {
|
|
await resetDb();
|
|
const u = await makeUsers();
|
|
H = u.h.id;
|
|
C = u.c.id;
|
|
rentInput = {
|
|
kind: "EXPENSE",
|
|
amount: "1000",
|
|
label: "Loyer",
|
|
categoryId: await categoryId("Logement"),
|
|
accountUserId: H,
|
|
split: { [H]: 5000, [C]: 5000 },
|
|
frequency: "MONTHLY",
|
|
dayOfMonth: 5,
|
|
startDate: "2026-01-05",
|
|
endDate: null,
|
|
};
|
|
});
|
|
|
|
describe("saveRecurring", () => {
|
|
it("creates the template and its planned occurrences", async () => {
|
|
const r = await saveRecurring(rentInput, TODAY);
|
|
if (!r.ok) throw new Error(r.error);
|
|
expect(await planned(r.id)).toHaveLength(12);
|
|
});
|
|
|
|
it("applies an edit to future occurrences only", async () => {
|
|
const r = await saveRecurring(rentInput, TODAY);
|
|
if (!r.ok) throw new Error(r.error);
|
|
await prisma.operation.updateMany({ where: { recurringId: r.id, date: fromIsoDate("2026-10-05") }, data: { status: "ACTUAL" } });
|
|
await saveRecurring({ ...rentInput, id: r.id, amount: "1050" }, TODAY);
|
|
const october = await prisma.operation.findFirstOrThrow({ where: { recurringId: r.id, date: fromIsoDate("2026-10-05") } });
|
|
expect(october.amount.toString()).toBe("1000");
|
|
const future = await planned(r.id);
|
|
expect(future[0].date.toISOString().slice(0, 10)).toBe("2026-11-05");
|
|
expect(future.every((o) => o.amount.toString() === "1050")).toBe(true);
|
|
});
|
|
|
|
it("validates the input", async () => {
|
|
expect(await saveRecurring({ ...rentInput, amount: "0" }, TODAY)).toEqual({ ok: false, error: "Montant invalide." });
|
|
expect(await saveRecurring({ ...rentInput, dayOfMonth: 32 }, TODAY)).toEqual({ ok: false, error: "Jour du mois invalide (1 à 31)." });
|
|
});
|
|
});
|
|
|
|
describe("pause / end / delete", () => {
|
|
it("pausing removes future occurrences and resuming brings them back", async () => {
|
|
const r = await saveRecurring(rentInput, TODAY);
|
|
if (!r.ok) throw new Error(r.error);
|
|
await prisma.operation.updateMany({ where: { recurringId: r.id, date: fromIsoDate("2026-10-05") }, data: { status: "ACTUAL" } });
|
|
await setRecurringPaused(r.id, true, TODAY);
|
|
expect((await planned(r.id)).filter((o) => o.date >= fromIsoDate(TODAY))).toHaveLength(0);
|
|
await setRecurringPaused(r.id, false, TODAY);
|
|
expect(await planned(r.id)).toHaveLength(11);
|
|
});
|
|
|
|
it("ending stops occurrences after the end date", async () => {
|
|
const r = await saveRecurring(rentInput, TODAY);
|
|
if (!r.ok) throw new Error(r.error);
|
|
await endRecurring(r.id, "2027-01-31", TODAY);
|
|
const rest = await planned(r.id);
|
|
expect(rest[rest.length - 1].date.toISOString().slice(0, 10)).toBe("2027-01-05");
|
|
});
|
|
|
|
it("deleting keeps actual history", async () => {
|
|
const r = await saveRecurring(rentInput, TODAY);
|
|
if (!r.ok) throw new Error(r.error);
|
|
await prisma.operation.updateMany({ where: { recurringId: r.id, date: fromIsoDate("2026-10-05") }, data: { status: "ACTUAL" } });
|
|
await deleteRecurring(r.id, TODAY);
|
|
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: fromIsoDate(TODAY) } } })).toBe(0);
|
|
});
|
|
});
|