45 lines
1.9 KiB
TypeScript
45 lines
1.9 KiB
TypeScript
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 });
|
|
});
|
|
});
|