|
| 1 | +import { resolveRounding } from '@/hooks/useSplitCalculator'; |
| 2 | + |
| 3 | +describe('resolveRounding', () => { |
| 4 | + const STROOP_SCALE = 1e7; |
| 5 | + |
| 6 | + it('should resolve rounding for 3-recipient split', () => { |
| 7 | + const percentages = [33.33, 33.33, 33.34]; |
| 8 | + const totalAmount = 100; |
| 9 | + |
| 10 | + const result = resolveRounding(percentages, totalAmount); |
| 11 | + |
| 12 | + expect(result.amounts).toHaveLength(3); |
| 13 | + const sumStroops = result.amounts.reduce( |
| 14 | + (s, a) => s + Math.round(a * STROOP_SCALE), |
| 15 | + 0 |
| 16 | + ); |
| 17 | + const totalStroops = Math.round(totalAmount * STROOP_SCALE); |
| 18 | + expect(sumStroops).toBe(totalStroops); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should resolve rounding for 7-recipient split', () => { |
| 22 | + const percentages = [14.28, 14.28, 14.28, 14.28, 14.28, 14.29, 14.31]; |
| 23 | + const totalAmount = 1000; |
| 24 | + |
| 25 | + const result = resolveRounding(percentages, totalAmount); |
| 26 | + |
| 27 | + expect(result.amounts).toHaveLength(7); |
| 28 | + const sumStroops = result.amounts.reduce( |
| 29 | + (s, a) => s + Math.round(a * STROOP_SCALE), |
| 30 | + 0 |
| 31 | + ); |
| 32 | + const totalStroops = Math.round(totalAmount * STROOP_SCALE); |
| 33 | + expect(sumStroops).toBe(totalStroops); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should resolve rounding for 11-recipient split', () => { |
| 37 | + const percentages = Array(11) |
| 38 | + .fill(0) |
| 39 | + .map((_, i) => (i === 10 ? 9.1 : 9.09)); |
| 40 | + const totalAmount = 5000; |
| 41 | + |
| 42 | + const result = resolveRounding(percentages, totalAmount); |
| 43 | + |
| 44 | + expect(result.amounts).toHaveLength(11); |
| 45 | + const sumStroops = result.amounts.reduce( |
| 46 | + (s, a) => s + Math.round(a * STROOP_SCALE), |
| 47 | + 0 |
| 48 | + ); |
| 49 | + const totalStroops = Math.round(totalAmount * STROOP_SCALE); |
| 50 | + expect(sumStroops).toBe(totalStroops); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should assign adjustment to first recipient', () => { |
| 54 | + const percentages = [50, 50]; |
| 55 | + const totalAmount = 100; |
| 56 | + |
| 57 | + const result = resolveRounding(percentages, totalAmount); |
| 58 | + |
| 59 | + expect(result.recipientIndex).toBe(0); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should return zero adjustment when no rounding needed', () => { |
| 63 | + const percentages = [25, 25, 25, 25]; |
| 64 | + const totalAmount = 100; |
| 65 | + |
| 66 | + const result = resolveRounding(percentages, totalAmount); |
| 67 | + |
| 68 | + const sumStroops = result.amounts.reduce( |
| 69 | + (s, a) => s + Math.round(a * STROOP_SCALE), |
| 70 | + 0 |
| 71 | + ); |
| 72 | + const totalStroops = Math.round(totalAmount * STROOP_SCALE); |
| 73 | + expect(sumStroops).toBe(totalStroops); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should handle fractional stroop amounts', () => { |
| 77 | + const percentages = [33.333, 33.333, 33.334]; |
| 78 | + const totalAmount = 123.456789; |
| 79 | + |
| 80 | + const result = resolveRounding(percentages, totalAmount); |
| 81 | + |
| 82 | + const sumStroops = result.amounts.reduce( |
| 83 | + (s, a) => s + Math.round(a * STROOP_SCALE), |
| 84 | + 0 |
| 85 | + ); |
| 86 | + const totalStroops = Math.round(totalAmount * STROOP_SCALE); |
| 87 | + expect(sumStroops).toBe(totalStroops); |
| 88 | + }); |
| 89 | +}); |
0 commit comments