|
| 1 | +import { fillUsageTimeSeriesGaps } from 'src/engine/core-modules/usage/utils/fill-usage-time-series-gaps.util'; |
| 2 | + |
| 3 | +describe('fillUsageTimeSeriesGaps', () => { |
| 4 | + it('should return all-zero series across the period when no rows are returned', () => { |
| 5 | + const result = fillUsageTimeSeriesGaps({ |
| 6 | + rows: [], |
| 7 | + periodStart: new Date('2026-04-20T00:00:00.000Z'), |
| 8 | + periodEnd: new Date('2026-04-23T23:59:59.999Z'), |
| 9 | + }); |
| 10 | + |
| 11 | + expect(result).toEqual([ |
| 12 | + { date: '2026-04-20', creditsUsed: 0 }, |
| 13 | + { date: '2026-04-21', creditsUsed: 0 }, |
| 14 | + { date: '2026-04-22', creditsUsed: 0 }, |
| 15 | + { date: '2026-04-23', creditsUsed: 0 }, |
| 16 | + ]); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should fill internal gaps with zero while preserving real values', () => { |
| 20 | + const result = fillUsageTimeSeriesGaps({ |
| 21 | + rows: [ |
| 22 | + { date: '2026-04-20', creditsUsed: 100 }, |
| 23 | + { date: '2026-04-22', creditsUsed: 250 }, |
| 24 | + ], |
| 25 | + periodStart: new Date('2026-04-20T00:00:00.000Z'), |
| 26 | + periodEnd: new Date('2026-04-23T23:59:59.999Z'), |
| 27 | + }); |
| 28 | + |
| 29 | + expect(result).toEqual([ |
| 30 | + { date: '2026-04-20', creditsUsed: 100 }, |
| 31 | + { date: '2026-04-21', creditsUsed: 0 }, |
| 32 | + { date: '2026-04-22', creditsUsed: 250 }, |
| 33 | + { date: '2026-04-23', creditsUsed: 0 }, |
| 34 | + ]); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should pad trailing dates with zero when data stops before period end (Félix bug)', () => { |
| 38 | + const result = fillUsageTimeSeriesGaps({ |
| 39 | + rows: [ |
| 40 | + { date: '2026-04-15', creditsUsed: 50 }, |
| 41 | + { date: '2026-04-16', creditsUsed: 75 }, |
| 42 | + ], |
| 43 | + periodStart: new Date('2026-04-15T00:00:00.000Z'), |
| 44 | + periodEnd: new Date('2026-04-20T23:59:59.999Z'), |
| 45 | + }); |
| 46 | + |
| 47 | + expect(result).toEqual([ |
| 48 | + { date: '2026-04-15', creditsUsed: 50 }, |
| 49 | + { date: '2026-04-16', creditsUsed: 75 }, |
| 50 | + { date: '2026-04-17', creditsUsed: 0 }, |
| 51 | + { date: '2026-04-18', creditsUsed: 0 }, |
| 52 | + { date: '2026-04-19', creditsUsed: 0 }, |
| 53 | + { date: '2026-04-20', creditsUsed: 0 }, |
| 54 | + ]); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should pad leading dates with zero when data starts after period start', () => { |
| 58 | + const result = fillUsageTimeSeriesGaps({ |
| 59 | + rows: [{ date: '2026-04-22', creditsUsed: 99 }], |
| 60 | + periodStart: new Date('2026-04-20T00:00:00.000Z'), |
| 61 | + periodEnd: new Date('2026-04-23T23:59:59.999Z'), |
| 62 | + }); |
| 63 | + |
| 64 | + expect(result).toEqual([ |
| 65 | + { date: '2026-04-20', creditsUsed: 0 }, |
| 66 | + { date: '2026-04-21', creditsUsed: 0 }, |
| 67 | + { date: '2026-04-22', creditsUsed: 99 }, |
| 68 | + { date: '2026-04-23', creditsUsed: 0 }, |
| 69 | + ]); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should return data unchanged when every day in period is already present', () => { |
| 73 | + const rows = [ |
| 74 | + { date: '2026-04-20', creditsUsed: 1 }, |
| 75 | + { date: '2026-04-21', creditsUsed: 2 }, |
| 76 | + { date: '2026-04-22', creditsUsed: 3 }, |
| 77 | + ]; |
| 78 | + |
| 79 | + const result = fillUsageTimeSeriesGaps({ |
| 80 | + rows, |
| 81 | + periodStart: new Date('2026-04-20T00:00:00.000Z'), |
| 82 | + periodEnd: new Date('2026-04-22T23:59:59.999Z'), |
| 83 | + }); |
| 84 | + |
| 85 | + expect(result).toEqual(rows); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should return a single entry when period spans one day', () => { |
| 89 | + const result = fillUsageTimeSeriesGaps({ |
| 90 | + rows: [{ date: '2026-04-23', creditsUsed: 42 }], |
| 91 | + periodStart: new Date('2026-04-23T00:00:00.000Z'), |
| 92 | + periodEnd: new Date('2026-04-23T23:59:59.999Z'), |
| 93 | + }); |
| 94 | + |
| 95 | + expect(result).toEqual([{ date: '2026-04-23', creditsUsed: 42 }]); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should return an empty array when periodStart is after periodEnd', () => { |
| 99 | + const result = fillUsageTimeSeriesGaps({ |
| 100 | + rows: [], |
| 101 | + periodStart: new Date('2026-04-25T00:00:00.000Z'), |
| 102 | + periodEnd: new Date('2026-04-20T23:59:59.999Z'), |
| 103 | + }); |
| 104 | + |
| 105 | + expect(result).toEqual([]); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should treat periodEnd as exclusive to match SQL `timestamp < periodEnd` semantics', () => { |
| 109 | + const result = fillUsageTimeSeriesGaps({ |
| 110 | + rows: [{ date: '2026-04-23', creditsUsed: 10 }], |
| 111 | + periodStart: new Date('2026-04-22T00:00:00.000Z'), |
| 112 | + periodEnd: new Date('2026-04-24T00:00:00.000Z'), |
| 113 | + }); |
| 114 | + |
| 115 | + expect(result).toEqual([ |
| 116 | + { date: '2026-04-22', creditsUsed: 0 }, |
| 117 | + { date: '2026-04-23', creditsUsed: 10 }, |
| 118 | + ]); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should return dates in ascending order', () => { |
| 122 | + const result = fillUsageTimeSeriesGaps({ |
| 123 | + rows: [ |
| 124 | + { date: '2026-04-22', creditsUsed: 5 }, |
| 125 | + { date: '2026-04-20', creditsUsed: 1 }, |
| 126 | + ], |
| 127 | + periodStart: new Date('2026-04-20T00:00:00.000Z'), |
| 128 | + periodEnd: new Date('2026-04-23T23:59:59.999Z'), |
| 129 | + }); |
| 130 | + |
| 131 | + expect(result.map((point) => point.date)).toEqual([ |
| 132 | + '2026-04-20', |
| 133 | + '2026-04-21', |
| 134 | + '2026-04-22', |
| 135 | + '2026-04-23', |
| 136 | + ]); |
| 137 | + }); |
| 138 | +}); |
0 commit comments