diff --git a/apps/edr-freight-api/src/modules/bookings/carriage-acceptance-price-split.spec.ts b/apps/edr-freight-api/src/modules/bookings/carriage-acceptance-price-split.spec.ts new file mode 100644 index 000000000..195e0cab0 --- /dev/null +++ b/apps/edr-freight-api/src/modules/bookings/carriage-acceptance-price-split.spec.ts @@ -0,0 +1,26 @@ +import { BookingsService } from './bookings.service'; + +// The split is a pure helper on the prototype (never touches `this`), so it can be +// exercised without constructing the service and its dependency graph. +const split = (total: number, weights: number[]): number[] => + ( + BookingsService.prototype as unknown as { + splitAmountAcrossWagons(total: number, weights: number[]): number[]; + } + ).splitAmountAcrossWagons(total, weights); + +describe('carriage acceptance sheet — price split', () => { + it('splits proportionally to allocated weight', () => { + expect(split(100, [30, 10])).toEqual([75, 25]); + }); + + it('splits equally when no weights are recorded', () => { + expect(split(90, [0, 0, 0])).toEqual([30, 30, 30]); + }); + + it('always sums back to the booking total despite rounding', () => { + const shares = split(100, [1, 1, 1]); + expect(shares.reduce((a, b) => a + b, 0)).toBe(100); + expect(shares).toEqual([33.33, 33.33, 33.34]); + }); +});