Fix the one-cent discount drift

Needs your answer

CL

You

Invoices come out a cent off when a discount is split across lines. Find out why, fix it, and add a test.

allocateDiscount rounds every line on its own. Three $10.00 lines sharing a $10.00 discount each get $3.33, so the discount comes to $9.99.

The new test fails by exactly that cent. One choice is yours before I change the rounding.

Which line should get a leftover cent?

Choose one to continue.

 import { describe, expect, it } from "vitest";
 import { allocateDiscount } from "./allocate";
 
 describe("allocateDiscount", () => {
   it("splits by share of the subtotal", () => {
     const lines = allocateDiscount([{ amount: 7500 }, { amount: 2500 }], 1000);
     expect(lines.map((line) => line.discount)).toEqual([750, 250]);
   });
 
   it("gives a single line the whole discount", () => {
     const [line] = allocateDiscount([{ amount: 4200 }], 500);
     expect(line.discount).toBe(500);
   });
+
+  it("adds up to the whole discount", () => {
+    const items = [{ amount: 1000 }, { amount: 1000 }, { amount: 1000 }];
+    const lines = allocateDiscount(items, 1000);
+    const total = lines.reduce((sum, line) => sum + line.discount, 0);
+    expect(total).toBe(1000);
+  });
 });