forked from CredenceOrg/Credence-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpenalty.test.ts
More file actions
70 lines (60 loc) · 2.51 KB
/
Copy pathpenalty.test.ts
File metadata and controls
70 lines (60 loc) · 2.51 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
69
70
import { describe, it, expect } from 'vitest'
import { getPenaltyRate, computeWithdrawBreakdown } from './penalty'
import type { BondStatus, MockBond } from './penalty'
describe('getPenaltyRate', () => {
it('returns 0 for active bonds', () => {
expect(getPenaltyRate('active')).toBe(0)
})
it('returns 0.1 for grace-period bonds', () => {
expect(getPenaltyRate('grace-period')).toBe(0.1)
})
it('returns 0.2 for locked bonds', () => {
expect(getPenaltyRate('locked')).toBe(0.2)
})
})
describe('computeWithdrawBreakdown', () => {
const makeBond = (amountUsdc: number, status: BondStatus): MockBond => ({
id: 1,
amountUsdc,
status,
})
it('computes zero-penalty breakdown for active bond', () => {
const result = computeWithdrawBreakdown(makeBond(1000, 'active'))
expect(result.penaltyUsdc).toBe(0)
expect(result.penaltyPercent).toBe(0)
expect(result.bondAmount).toBe('1,000 USDC')
expect(result.penaltyAmount).toBe('0 USDC')
expect(result.resultingBalance).toBe('1,000 USDC')
})
it('computes 10% penalty for grace-period bond', () => {
const result = computeWithdrawBreakdown(makeBond(1000, 'grace-period'))
expect(result.penaltyUsdc).toBe(100)
expect(result.penaltyPercent).toBe(10)
expect(result.bondAmount).toBe('1,000 USDC')
expect(result.penaltyAmount).toBe('100 USDC')
expect(result.resultingBalance).toBe('900 USDC')
})
it('computes 20% penalty for locked bond', () => {
const result = computeWithdrawBreakdown(makeBond(1000, 'locked'))
expect(result.penaltyUsdc).toBe(200)
expect(result.penaltyPercent).toBe(20)
expect(result.bondAmount).toBe('1,000 USDC')
expect(result.penaltyAmount).toBe('200 USDC')
expect(result.resultingBalance).toBe('800 USDC')
})
it('breakdown arithmetic: penalty + resulting = amount (grace-period)', () => {
const bond = makeBond(2500, 'grace-period')
const result = computeWithdrawBreakdown(bond)
expect(result.penaltyUsdc + (bond.amountUsdc - result.penaltyUsdc)).toBe(bond.amountUsdc)
})
it('breakdown arithmetic: penalty + resulting = amount (locked)', () => {
const bond = makeBond(750, 'locked')
const result = computeWithdrawBreakdown(bond)
expect(result.penaltyUsdc + (bond.amountUsdc - result.penaltyUsdc)).toBe(bond.amountUsdc)
})
it('handles fractional USDC amounts correctly', () => {
const result = computeWithdrawBreakdown(makeBond(333.33, 'locked'))
expect(result.penaltyUsdc).toBeCloseTo(66.666, 3)
expect(result.penaltyPercent).toBe(20)
})
})