|
| 1 | +import { getProposalValue } from '../../../src/helpers/entityValue'; |
| 2 | + |
| 3 | +describe('getProposalValue', () => { |
| 4 | + it('should calculate correct proposal value with single strategy', () => { |
| 5 | + const proposal = { |
| 6 | + scores_by_strategy: '[[100], [200]]', |
| 7 | + vp_value_by_strategy: '[2.5]' |
| 8 | + }; |
| 9 | + |
| 10 | + const result = getProposalValue(proposal); |
| 11 | + |
| 12 | + expect(result).toBe(750); // (100 + 200) * 2.5 = 300 * 2.5 = 750 |
| 13 | + }); |
| 14 | + |
| 15 | + it('should calculate correct proposal value with multiple strategies', () => { |
| 16 | + const proposal = { |
| 17 | + scores_by_strategy: '[[100, 50], [200, 75], [300, 25]]', |
| 18 | + vp_value_by_strategy: '[1.5, 3.0]' |
| 19 | + }; |
| 20 | + |
| 21 | + const result = getProposalValue(proposal); |
| 22 | + |
| 23 | + expect(result).toBe(1350); // (100+200+300)*1.5 + (50+75+25)*3.0 = 600*1.5 + 150*3.0 = 900 + 450 = 1350 |
| 24 | + }); |
| 25 | + |
| 26 | + it('should return 0 when scores_by_strategy is empty', () => { |
| 27 | + const proposal = { |
| 28 | + scores_by_strategy: '[]', |
| 29 | + vp_value_by_strategy: '[2.0]' |
| 30 | + }; |
| 31 | + |
| 32 | + const result = getProposalValue(proposal); |
| 33 | + |
| 34 | + expect(result).toBe(0); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should return 0 when first strategy array is empty', () => { |
| 38 | + const proposal = { |
| 39 | + scores_by_strategy: '[[]]', |
| 40 | + vp_value_by_strategy: '[2.0]' |
| 41 | + }; |
| 42 | + |
| 43 | + const result = getProposalValue(proposal); |
| 44 | + |
| 45 | + expect(result).toBe(0); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should handle zero values correctly', () => { |
| 49 | + const proposal = { |
| 50 | + scores_by_strategy: '[[0, 0], [0, 0]]', |
| 51 | + vp_value_by_strategy: '[2.0, 1.5]' |
| 52 | + }; |
| 53 | + |
| 54 | + const result = getProposalValue(proposal); |
| 55 | + |
| 56 | + expect(result).toBe(0); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should handle zero vp_value_by_strategy correctly', () => { |
| 60 | + const proposal = { |
| 61 | + scores_by_strategy: '[[100, 50], [200, 75]]', |
| 62 | + vp_value_by_strategy: '[0, 0]' |
| 63 | + }; |
| 64 | + |
| 65 | + const result = getProposalValue(proposal); |
| 66 | + |
| 67 | + expect(result).toBe(0); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should handle decimal values correctly', () => { |
| 71 | + const proposal = { |
| 72 | + scores_by_strategy: '[[10.5, 20.5], [15.5, 25.5]]', |
| 73 | + vp_value_by_strategy: '[0.1, 0.2]' |
| 74 | + }; |
| 75 | + |
| 76 | + const result = getProposalValue(proposal); |
| 77 | + |
| 78 | + expect(result).toBe(11.8); // (10.5+15.5)*0.1 + (20.5+25.5)*0.2 = 26*0.1 + 46*0.2 = 2.6 + 9.2 = 11.8 |
| 79 | + }); |
| 80 | + |
| 81 | + it('should handle single vote scenario', () => { |
| 82 | + const proposal = { |
| 83 | + scores_by_strategy: '[[100]]', |
| 84 | + vp_value_by_strategy: '[2.0]' |
| 85 | + }; |
| 86 | + |
| 87 | + const result = getProposalValue(proposal); |
| 88 | + |
| 89 | + expect(result).toBe(200); // 100 * 2.0 = 200 |
| 90 | + }); |
| 91 | + |
| 92 | + it('should throw error for invalid JSON in scores_by_strategy', () => { |
| 93 | + const proposal = { |
| 94 | + scores_by_strategy: 'invalid json', |
| 95 | + vp_value_by_strategy: '[2.0]' |
| 96 | + }; |
| 97 | + |
| 98 | + expect(() => getProposalValue(proposal)).toThrow(); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should throw error for invalid JSON in vp_value_by_strategy', () => { |
| 102 | + const proposal = { |
| 103 | + scores_by_strategy: '[[100]]', |
| 104 | + vp_value_by_strategy: 'invalid json' |
| 105 | + }; |
| 106 | + |
| 107 | + expect(() => getProposalValue(proposal)).toThrow(); |
| 108 | + }); |
| 109 | +}); |
0 commit comments