|
1 | | -import { convertInputType } from '#~/pages/modelServing/screens/metrics/utils'; |
| 1 | +import { |
| 2 | + convertInputType, |
| 3 | + toPercentage, |
| 4 | + per100, |
| 5 | +} from '#~/pages/modelServing/screens/metrics/utils'; |
2 | 6 |
|
3 | 7 | describe('convertInputType', () => { |
4 | 8 | describe('string inputs', () => { |
@@ -40,3 +44,82 @@ describe('convertInputType', () => { |
40 | 44 | }); |
41 | 45 | }); |
42 | 46 | }); |
| 47 | + |
| 48 | +describe('toPercentage', () => { |
| 49 | + it('should round percentage values to 2 decimal places', () => { |
| 50 | + const input = { x: 1000, y: 0.09896161479334678, name: 'test' }; |
| 51 | + const result = toPercentage(input); |
| 52 | + expect(result.y).toBe(9.9); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should handle values that round up', () => { |
| 56 | + const input = { x: 1000, y: Number('0.15228456789012345'), name: 'test' }; |
| 57 | + const result = toPercentage(input); |
| 58 | + expect(result.y).toBe(15.23); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should handle values that round down', () => { |
| 62 | + const input = { x: 1000, y: 0.15224, name: 'test' }; |
| 63 | + const result = toPercentage(input); |
| 64 | + expect(result.y).toBe(15.22); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should handle edge case near 100%', () => { |
| 68 | + const input = { x: 1000, y: 0.999999, name: 'test' }; |
| 69 | + const result = toPercentage(input); |
| 70 | + expect(result.y).toBe(100); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should handle edge case near 0%', () => { |
| 74 | + const input = { x: 1000, y: 0.005, name: 'test' }; |
| 75 | + const result = toPercentage(input); |
| 76 | + expect(result.y).toBe(0.5); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should handle exact zero', () => { |
| 80 | + const input = { x: 1000, y: 0, name: 'test' }; |
| 81 | + const result = toPercentage(input); |
| 82 | + expect(result.y).toBe(0); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should preserve x and name properties', () => { |
| 86 | + const input = { x: 12345, y: 0.5, name: 'Memory Usage' }; |
| 87 | + const result = toPercentage(input); |
| 88 | + expect(result.x).toBe(12345); |
| 89 | + expect(result.name).toBe('Memory Usage'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should handle floating-point precision artifacts', () => { |
| 93 | + // Test that floating-point errors like 99.99999999999999 get properly rounded |
| 94 | + const input = { x: 1000, y: 0.9999999999999999, name: 'test' }; |
| 95 | + const result = toPercentage(input); |
| 96 | + expect(result.y).toBe(100); |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
| 100 | +describe('per100', () => { |
| 101 | + it('should divide by 100 and round to 2 decimal places', () => { |
| 102 | + const input = { x: 1000, y: 989.6161479334678, name: 'test' }; |
| 103 | + const result = per100(input); |
| 104 | + expect(result.y).toBe(9.9); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should handle values that round up', () => { |
| 108 | + const input = { x: 1000, y: Number('1522.8456789012345'), name: 'test' }; |
| 109 | + const result = per100(input); |
| 110 | + expect(result.y).toBe(15.23); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should handle exact zero', () => { |
| 114 | + const input = { x: 1000, y: 0, name: 'test' }; |
| 115 | + const result = per100(input); |
| 116 | + expect(result.y).toBe(0); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should preserve x and name properties', () => { |
| 120 | + const input = { x: 12345, y: 5000, name: 'CPU Usage' }; |
| 121 | + const result = per100(input); |
| 122 | + expect(result.x).toBe(12345); |
| 123 | + expect(result.name).toBe('CPU Usage'); |
| 124 | + }); |
| 125 | +}); |
0 commit comments