Files
YugonitoandClaude Opus 5.5 5b402691f0 fix: opening balance direction, safe return paths, backup failure detection, review fixes
- opening balance: "person 2 owes person 1" was stored the wrong way round
- retour param: reject backslash/control chars (open redirect)
- backup: pipefail + check dump completeness, failed dumps no longer logged OK
- deleting a recurring occurrence marks it skipped so sync does not recreate it
- editing a charge regenerates future occurrences of incomes allocated to it
- settlements (incl. opening balance) listed in settings and deletable

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 18:51:49 +01:00

116 lines
4.8 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("re-splits future allocated incomes when the charge's split changes", async () => {
const rent = await saveRecurring(rentInput, TODAY);
if (!rent.ok) throw new Error(rent.error);
const employer = await saveRecurring(
{
...rentInput,
kind: "INCOME",
amount: "200",
label: "Part loyer employeur",
categoryId: await categoryId("Remboursement employeur"),
split: { [H]: 10000, [C]: 0 },
allocatedToRecurringId: rent.id,
allocationMode: "REDUCE_SHARED",
},
TODAY,
);
if (!employer.ok) throw new Error(employer.error);
await saveRecurring({ ...rentInput, id: rent.id, split: { [H]: 6000, [C]: 4000 } }, TODAY);
const future = await prisma.operation.findMany({
where: { recurringId: employer.id, status: "PLANNED", date: { gte: fromIsoDate(TODAY) } },
include: { splits: true },
});
expect(future.length).toBeGreaterThan(0);
for (const o of future) {
expect(Object.fromEntries(o.splits.map((s) => [s.userId, s.basisPoints]))).toEqual({ [H]: 6000, [C]: 4000 });
}
});
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);
});
});