|
| 1 | +import {Interval} from '../../lib/slo/interval' |
| 2 | +import {CalendarIntervalProps, IntervalProps} from '../../lib'; |
| 3 | +import {DurationUnit} from "../../lib/slo/constants"; |
| 4 | + |
| 5 | +describe('Interval', () => { |
| 6 | + describe('calendar()', () => { |
| 7 | + it('should create a calendar interval with valid props', () => { |
| 8 | + const props: CalendarIntervalProps = { |
| 9 | + unit: DurationUnit.MONTH, |
| 10 | + duration: 1 |
| 11 | + }; |
| 12 | + |
| 13 | + const interval = Interval.calendar(props); |
| 14 | + expect(interval).toBeDefined(); |
| 15 | + expect(interval.bind).toBeDefined(); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should create a calendar interval with different time units', () => { |
| 19 | + const units = [ |
| 20 | + DurationUnit.DAY, |
| 21 | + DurationUnit.MONTH |
| 22 | + ]; |
| 23 | + |
| 24 | + units.forEach(unit => { |
| 25 | + const props: CalendarIntervalProps = { |
| 26 | + unit: unit, |
| 27 | + duration: 1 |
| 28 | + }; |
| 29 | + const interval = Interval.calendar(props); |
| 30 | + expect(interval).toBeDefined(); |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should handle different duration values', () => { |
| 35 | + const durations = [1, 7, 14, 30]; |
| 36 | + |
| 37 | + durations.forEach(duration => { |
| 38 | + const props: CalendarIntervalProps = { |
| 39 | + unit: DurationUnit.DAY, |
| 40 | + duration |
| 41 | + }; |
| 42 | + const interval = Interval.calendar(props); |
| 43 | + expect(interval).toBeDefined(); |
| 44 | + }); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('rolling()', () => { |
| 49 | + it('should create a rolling interval with valid props', () => { |
| 50 | + const props: IntervalProps = { |
| 51 | + duration: 24, |
| 52 | + unit: DurationUnit.HOUR |
| 53 | + }; |
| 54 | + |
| 55 | + const interval = Interval.rolling(props); |
| 56 | + expect(interval).toBeDefined(); |
| 57 | + expect(interval.bind).toBeDefined(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should handle different duration values', () => { |
| 61 | + const durations = [1, 7, 14, 31]; |
| 62 | + |
| 63 | + durations.forEach(duration => { |
| 64 | + const props: CalendarIntervalProps = { |
| 65 | + unit: DurationUnit.DAY, |
| 66 | + duration |
| 67 | + }; |
| 68 | + const interval = Interval.calendar(props); |
| 69 | + expect(interval).toBeDefined(); |
| 70 | + }); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('bind()', () => { |
| 75 | + it('should return the correct structure for calendar intervals', () => { |
| 76 | + const props: CalendarIntervalProps = { |
| 77 | + unit: DurationUnit.MONTH, |
| 78 | + duration: 1 |
| 79 | + }; |
| 80 | + |
| 81 | + const interval = Interval.calendar(props); |
| 82 | + const bound = interval.bind(); |
| 83 | + expect(bound).toBeDefined(); |
| 84 | + |
| 85 | + }); |
| 86 | + |
| 87 | + it('should return the correct structure for rolling intervals', () => { |
| 88 | + const props: IntervalProps = { |
| 89 | + duration: 24, |
| 90 | + unit: DurationUnit.HOUR |
| 91 | + }; |
| 92 | + |
| 93 | + const interval = Interval.rolling(props); |
| 94 | + const bound = interval.bind(); |
| 95 | + expect(bound).toBeDefined(); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + describe('error cases', () => { |
| 100 | + it('should handle invalid calendar interval props', () => { |
| 101 | + expect(() => { |
| 102 | + Interval.calendar({ |
| 103 | + unit: DurationUnit.DAY, |
| 104 | + duration: -1 |
| 105 | + }).bind() |
| 106 | + }).toThrow(); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should handle invalid rolling interval props', () => { |
| 110 | + expect(() => { |
| 111 | + Interval.rolling({ |
| 112 | + duration: -1, |
| 113 | + unit: DurationUnit.HOUR |
| 114 | + } as any).bind() |
| 115 | + }).toThrow(); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should handle missing required properties', () => { |
| 119 | + expect(() => { |
| 120 | + Interval.calendar({ |
| 121 | + unit: DurationUnit.DAY, |
| 122 | + duration: 0 |
| 123 | + } as CalendarIntervalProps).bind(); |
| 124 | + }).toThrow('Duration must be greater than 0'); |
| 125 | + |
| 126 | + expect(() => { |
| 127 | + Interval.rolling({ |
| 128 | + unit: DurationUnit.HOUR, |
| 129 | + duration: 0 |
| 130 | + } as IntervalProps).bind(); |
| 131 | + }).toThrow('Duration must be greater than 0'); |
| 132 | + }); |
| 133 | + }); |
| 134 | +}); |
0 commit comments