Files
TREK/server/tests/unit/nest/budget-money.helpers.test.ts
T
Maurice e3eec8d6d1 fix(budget): a custom split in a foreign currency adds up to what was paid
Each member's amount was converted and rounded to a cent on its own, so four 34,000 ISK quarters of a 136,000 ISK bill came to 997.52 EUR against the 997.51 the payer was credited. The balances ended a few cents off zero and settle-up had no payment for them. A split whose amounts match the payers in the bill's own currency is now converted as one set by largest remainder, in the settlement and in the per-person summary alike.
2026-10-25 10:51:56 +01:00

33 lines
1.2 KiB
TypeScript

import { customSplitCents } from '../../../src/nest/budget/budget-money.helpers';
import { describe, expect, it } from 'vitest';
const isk = (amount: number) => amount / 136.34;
describe('customSplitCents', () => {
it('allocates a split that adds up against the credited cents, ties to the lower user id', () => {
const members = [4, 1, 3, 2].map((user_id) => ({ user_id, amount: 34000 }));
expect(customSplitCents(members, isk, 99751, 13600000)).toEqual({ 1: 24938, 2: 24938, 3: 24938, 4: 24937 });
});
it('takes cents back from the smallest fractions when the credit rounded down', () => {
const members = [
{ user_id: 1, amount: 0.5 },
{ user_id: 2, amount: 0.5 },
];
expect(customSplitCents(members, (a) => a * 1.005, 99, 100)).toEqual({ 1: 49, 2: 50 });
});
it('keeps the member-by-member rounding for a split that does not add up to the payers', () => {
const members = [
{ user_id: 1, amount: 34000 },
{ user_id: 2, amount: null },
];
expect(customSplitCents(members, isk, 99751, 13600000)).toEqual({ 1: 24938, 2: 0 });
});
it('splits nothing among nobody', () => {
expect(customSplitCents([], isk, 100, 100)).toEqual({});
});
});