forked from MergeFi/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathteam-split.util.spec.ts
More file actions
68 lines (60 loc) · 1.91 KB
/
Copy pathteam-split.util.spec.ts
File metadata and controls
68 lines (60 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { BadRequestException } from '@nestjs/common';
import {
computeSplitShares,
validateSplitPercentages,
} from './team-split.util';
describe('team split percentage math', () => {
it('accepts splits that sum to exactly 100', () => {
expect(() =>
validateSplitPercentages([
{ percentage: 40 },
{ percentage: 40 },
{ percentage: 20 },
]),
).not.toThrow();
});
it('accepts splits within floating point tolerance of 100', () => {
expect(() =>
validateSplitPercentages([
{ percentage: 33.33 },
{ percentage: 33.33 },
{ percentage: 33.34 },
]),
).not.toThrow();
});
it('rejects splits that sum to less than 100', () => {
expect(() =>
validateSplitPercentages([{ percentage: 40 }, { percentage: 40 }]),
).toThrow(BadRequestException);
});
it('rejects splits that sum to more than 100', () => {
expect(() =>
validateSplitPercentages([{ percentage: 60 }, { percentage: 60 }]),
).toThrow(BadRequestException);
});
it('rejects a zero or negative percentage', () => {
expect(() =>
validateSplitPercentages([{ percentage: 0 }, { percentage: 100 }]),
).toThrow(BadRequestException);
});
it('rejects an empty split list', () => {
expect(() => validateSplitPercentages([])).toThrow(BadRequestException);
});
it('computeSplitShares divides an amount proportionally', () => {
const shares = computeSplitShares(1000, [
{ percentage: 40 },
{ percentage: 40 },
{ percentage: 20 },
]);
expect(shares).toEqual([400, 400, 200]);
});
it('computeSplitShares handles uneven thirds without losing precision beyond 7dp', () => {
const shares = computeSplitShares(100, [
{ percentage: 33.33 },
{ percentage: 33.33 },
{ percentage: 33.34 },
]);
const total = shares.reduce((a, b) => a + b, 0);
expect(total).toBeCloseTo(100, 5);
});
});