feat(domain): recurrence dates and occurrence planning

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
YugonitoandClaude Opus 5.5 committed 2026-10-27 18:00:51 +01:00
1 parent 9fb4aab887
commit d722c0034c
2 files changed
+193

No files matched your search

+96
View File
@@ -0,0 +1,96 @@
import { describe, expect, it } from "vitest";
import { occurrenceDates, planOccurrences, splitForAllocation, type RecurringDef } from "@/domain/recurrence";
const users = ["H", "C"];
function rec(partial: Partial<RecurringDef>): RecurringDef {
return {
id: "r",
kind: "EXPENSE",
amount: 100000,
label: "Loyer",
categoryId: "logement",
accountUserId: "H",
split: { H: 5000, C: 5000 },
frequency: "MONTHLY",
dayOfMonth: 5,
startDate: "2026-01-05",
endDate: null,
paused: false,
allocatedToRecurringId: null,
allocationMode: null,
...partial,
};
}
describe("occurrenceDates", () => {
it("clamps day 31 to the end of short months", () => {
expect(occurrenceDates(rec({ dayOfMonth: 31, startDate: "2026-10-31" }), "2026-10", "2027-03")).toEqual([
"2026-10-31", "2026-11-30", "2026-12-31", "2027-01-31", "2027-02-28", "2027-03-31",
]);
});
it("steps quarterly from the start month", () => {
expect(
occurrenceDates(rec({ frequency: "QUARTERLY", dayOfMonth: 15, startDate: "2026-11-15" }), "2026-10", "2027-12"),
).toEqual(["2026-11-15", "2027-02-15", "2027-05-15", "2027-08-15", "2027-11-15"]);
});
it("steps yearly from the start month", () => {
expect(
occurrenceDates(rec({ frequency: "YEARLY", dayOfMonth: 1, startDate: "2025-03-01" }), "2026-01", "2027-12"),
).toEqual(["2026-03-01", "2027-03-01"]);
});
it("stops at the end date", () => {
expect(
occurrenceDates(rec({ dayOfMonth: 20, startDate: "2026-10-20", endDate: "2026-12-15" }), "2026-10", "2027-02"),
).toEqual(["2026-10-20", "2026-11-20"]);
});
it("does not produce a date before the start date in the start month", () => {
expect(occurrenceDates(rec({ dayOfMonth: 5, startDate: "2026-10-27" }), "2026-10", "2026-11")).toEqual(["2026-11-05"]);
});
});
describe("splitForAllocation", () => {
it("copies the charge split, gives all to the beneficiary, or uses the custom split", () => {
expect(splitForAllocation("REDUCE_SHARED", "H", { H: 5000, C: 5000 }, {}, users)).toEqual({ H: 5000, C: 5000 });
expect(splitForAllocation("BENEFICIARY_ONLY", "H", { H: 5000, C: 5000 }, {}, users)).toEqual({ H: 10000, C: 0 });
expect(splitForAllocation("CUSTOM", "H", { H: 5000, C: 5000 }, { H: 7000, C: 3000 }, users)).toEqual({ H: 7000, C: 3000 });
});
});
describe("planOccurrences", () => {
const rent = rec({ id: "rent", startDate: "2024-01-05" });
it("plans 12 months from the current month, never retroactively", () => {
const planned = planOccurrences([rent], new Set(), "2026-10-27", users);
expect(planned).toHaveLength(12);
expect(planned[0]).toMatchObject({ occurrenceDate: "2026-10-05", date: "2026-10-05", status: "PLANNED", recurringId: "rent", amount: 100000 });
expect(planned[11].occurrenceDate).toBe("2027-09-05");
});
it("skips existing occurrences and paused recurrings", () => {
expect(planOccurrences([rent], new Set(["rent|2026-10-05"]), "2026-10-27", users)).toHaveLength(11);
expect(planOccurrences([{ ...rent, paused: true }], new Set(), "2026-10-27", users)).toHaveLength(0);
});
it("gives an unallocated income entirely to its receiver", () => {
const salary = rec({ id: "salary", kind: "INCOME", split: { H: 5000, C: 5000 } });
expect(planOccurrences([salary], new Set(), "2026-10-27", users)[0].split).toEqual({ H: 10000, C: 0 });
});
it("splits an allocated income like its target charge", () => {
const car = rec({ id: "car", split: { H: 10000, C: 0 } });
const ik = rec({ id: "ik", kind: "INCOME", allocatedToRecurringId: "car", allocationMode: "REDUCE_SHARED" });
const planned = planOccurrences([car, ik], new Set(), "2026-10-27", users).filter((p) => p.recurringId === "ik");
expect(planned[0]).toMatchObject({ split: { H: 10000, C: 0 }, allocatedToRecurringId: "car", allocationMode: "REDUCE_SHARED" });
});
it("drops a dangling allocation target", () => {
const ik = rec({ id: "ik", kind: "INCOME", allocatedToRecurringId: "gone", allocationMode: "REDUCE_SHARED" });
const planned = planOccurrences([ik], new Set(), "2026-10-27", users);
expect(planned[0]).toMatchObject({ split: { H: 10000, C: 0 }, allocatedToRecurringId: null, allocationMode: null });
});
});