feat(domain): balance between people

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
YugonitoandClaude Opus 5.5 committed 2026-10-27 17:58:21 +01:00
1 parent 5ce5363f50
commit 89bf2a821c
4 files changed
+108

No files matched your search

+44
View File
@@ -0,0 +1,44 @@
import { describe, expect, it } from "vitest";
import { computeBalances, describeBalance } from "@/domain/balance";
import { op } from "./fixtures";
const users = ["H", "C"];
const rent = op({ kind: "EXPENSE", amount: 100000, accountUserId: "H" });
const employerRent = op({
kind: "INCOME",
amount: 20000,
accountUserId: "H",
allocatedToRecurringId: "rent",
allocationMode: "REDUCE_SHARED",
});
describe("computeBalances", () => {
it("matches the spec control example: net rent 800 split 400/400", () => {
const b = computeBalances([rent, employerRent], [], users);
expect(b).toEqual({ H: 40000, C: -40000 });
expect(describeBalance(b)).toEqual({ debtor: "C", creditor: "H", amount: 40000 });
});
it("does not create a debt for an income kept 100 % by its receiver", () => {
const salary = op({ kind: "INCOME", amount: 300000, accountUserId: "H", split: { H: 10000, C: 0 } });
const b = computeBalances([salary], [], users);
expect(b).toEqual({ H: 0, C: 0 });
expect(describeBalance(b)).toBeNull();
});
it("ignores planned and skipped operations", () => {
const planned = op({ kind: "EXPENSE", amount: 5000, accountUserId: "H", status: "PLANNED" });
const skipped = op({ kind: "EXPENSE", amount: 5000, accountUserId: "H", status: "SKIPPED" });
expect(computeBalances([planned, skipped], [], users)).toEqual({ H: 0, C: 0 });
});
it("settles debts with person-to-person repayments", () => {
const settlement = { id: "s1", fromUserId: "C", toUserId: "H", amount: 40000, date: "2026-10-20" };
expect(computeBalances([rent, employerRent], [settlement], users)).toEqual({ H: 0, C: 0 });
});
it("uses cent-exact shares for uneven splits", () => {
const uneven = op({ kind: "EXPENSE", amount: 1000, accountUserId: "H", split: { H: 3333, C: 6667 } });
expect(computeBalances([uneven], [], users)).toEqual({ H: 666, C: -666 });
});
});