Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions src/money/cents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,20 @@ export function parseCents(input: string): number {
/**
* Split an integer amount of cents evenly across member ids.
*
* SEEDED DEFECT (issue #2): this floors every share and drops the
* remainder instead of handing the leftover cents to the first N members,
* so the shares can sum to less than totalCents. `bun test` does not catch
* this because the baseline tests only split amounts that divide evenly.
* The correct remainder rule is documented in
* The first `remainder` members (in the order given) get one extra cent, so
* the shares always sum to exactly totalCents. See the remainder rule in
* .claude/skills/handling-money/references/rules.md.
*/
export function splitCents(totalCents: number, memberIds: string[]): Record<string, number> {
if (memberIds.length === 0) {
throw new Error("cannot split among zero members");
}
const share = Math.floor(totalCents / memberIds.length);
const base = Math.floor(totalCents / memberIds.length);
const remainder = totalCents - base * memberIds.length;
const shares: Record<string, number> = {};
for (const id of memberIds) {
shares[id] = share;
}
memberIds.forEach((id, i) => {
shares[id] = i < remainder ? base + 1 : base;
});
return shares;
}

Expand Down
12 changes: 12 additions & 0 deletions tests/money.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,16 @@ describe("splitCents", () => {
const shares = splitCents(2000, ["a", "b"]);
expect(sumCents(Object.values(shares))).toBe(2000);
});

test("gives the leftover cent to the first member (issue #178)", () => {
const shares = splitCents(1000, ["a", "b", "c"]);
expect(shares).toEqual({ a: 334, b: 333, c: 333 });
expect(sumCents(Object.values(shares))).toBe(1000);
});

test("gives leftover cents to the first N members in order", () => {
const shares = splitCents(1001, ["a", "b", "c"]);
expect(shares).toEqual({ a: 334, b: 334, c: 333 });
expect(sumCents(Object.values(shares))).toBe(1001);
});
});
3 changes: 0 additions & 3 deletions tests/routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,6 @@ describe("POST and DELETE /api/groups/:id/expenses", () => {

describe("GET /api/groups/:id/balances", () => {
test("transfers settle every balance to zero net", async () => {
// Flat 4B's seeded amounts all divide evenly by its 3 members, so this
// fixture is unaffected by the splitCents remainder bug (issue #2) and
// exercises settle-up on its own.
const app = setupApp();
const res = await app.request("/api/groups/flat-4b/balances", { headers: auth("flat-priya") });
const body = (await res.json()) as { balances: { netCents: number }[]; transfers: unknown[] };
Expand Down
Loading