feat(domain): money parsing/formatting and splits

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
YugonitoandClaude Opus 5.5 committed 2026-10-27 17:57:50 +01:00
1 parent 8d41c2fffe
commit 5ce5363f50
3 files changed
+189

No files matched your search

+85
View File
@@ -0,0 +1,85 @@
import { describe, expect, it } from "vitest";
import {
bpToPercentLabel,
centsFromDecimal,
decimalFromCents,
formatEuro,
fullSplit,
parseEuro,
percentToBp,
shareAmounts,
validateSplit,
} from "@/domain/money";
const fr = (n: number) =>
new Intl.NumberFormat("fr-FR", { style: "currency", currency: "EUR" }).format(n);
describe("parseEuro", () => {
it("accepts French and dotted inputs", () => {
expect(parseEuro("86,40")).toBe(8640);
expect(parseEuro("86.40")).toBe(8640);
expect(parseEuro("1 234,56")).toBe(123456);
expect(parseEuro("1 234,56")).toBe(123456);
expect(parseEuro("1 234,56")).toBe(123456);
expect(parseEuro("1234")).toBe(123400);
expect(parseEuro("0,5")).toBe(50);
});
it("rejects empty, zero, negative and malformed amounts", () => {
for (const s of ["", "0", "0,00", "-5", "abc", "12,345", "1,2,3"]) expect(parseEuro(s)).toBeNull();
});
});
describe("formatEuro", () => {
it("formats cents with Intl fr-FR", () => {
expect(formatEuro(123456)).toBe(fr(1234.56));
});
it("uses a real minus sign and an explicit plus when asked", () => {
expect(formatEuro(-8640, { sign: true })).toBe("−" + fr(86.4));
expect(formatEuro(500, { sign: true })).toBe("+" + fr(5));
expect(formatEuro(-8640)).toBe("−" + fr(86.4));
});
});
describe("decimal conversions", () => {
it("round-trips between DB decimals and cents", () => {
expect(centsFromDecimal("86.40")).toBe(8640);
expect(centsFromDecimal("86.4")).toBe(8640);
expect(centsFromDecimal("-12.05")).toBe(-1205);
expect(centsFromDecimal({ toString: () => "1000.00" })).toBe(100000);
expect(decimalFromCents(8640)).toBe("86.40");
expect(decimalFromCents(5)).toBe("0.05");
});
});
describe("splits", () => {
it("accepts splits summing to 100 %", () => {
expect(validateSplit({ a: 5000, b: 5000 })).toBeNull();
expect(validateSplit({ a: 3333, b: 6667 })).toBeNull();
});
it("rejects splits not summing to 100 % or with negatives", () => {
expect(validateSplit({ a: 3333, b: 3333 })).toBe("La répartition doit totaliser 100 %.");
expect(validateSplit({ a: -100, b: 10100 })).toBe("Les pourcentages doivent être positifs.");
});
it("gives the rounding residue to the account holder", () => {
expect(shareAmounts(1000, { a: 3333, b: 6667 }, "a")).toEqual({ a: 334, b: 666 });
expect(shareAmounts(8640, { a: 5000, b: 5000 }, "b")).toEqual({ a: 4320, b: 4320 });
expect(shareAmounts(1, { a: 5000, b: 5000 }, "b")).toEqual({ a: 0, b: 1 });
});
it("converts percentages to and from basis points", () => {
expect(percentToBp("33,33")).toBe(3333);
expect(percentToBp("50")).toBe(5000);
expect(percentToBp("abc")).toBeNull();
expect(percentToBp("101")).toBeNull();
expect(bpToPercentLabel(3333)).toBe("33,33");
expect(bpToPercentLabel(5000)).toBe("50");
});
it("builds a 100 % split for one person", () => {
expect(fullSplit("a", ["a", "b"])).toEqual({ a: 10000, b: 0 });
});
});