|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { getNextCronRun, parseCron } from '../../src/utils/cronParser.js'; |
| 3 | + |
| 4 | +describe('parseCron', () => { |
| 5 | + describe('wildcards', () => { |
| 6 | + it('should expand * to full range for minute (0-59)', () => { |
| 7 | + const result = parseCron('* * * * *'); |
| 8 | + expect(result.minute).toHaveLength(60); |
| 9 | + expect(result.minute[0]).toBe(0); |
| 10 | + expect(result.minute[59]).toBe(59); |
| 11 | + }); |
| 12 | + |
| 13 | + it('should expand * to full range for hour (0-23)', () => { |
| 14 | + const result = parseCron('* * * * *'); |
| 15 | + expect(result.hour).toHaveLength(24); |
| 16 | + expect(result.hour[0]).toBe(0); |
| 17 | + expect(result.hour[23]).toBe(23); |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + describe('single values', () => { |
| 22 | + it('should parse single values for all fields', () => { |
| 23 | + const result = parseCron('30 14 15 6 3'); |
| 24 | + expect(result.minute).toEqual([30]); |
| 25 | + expect(result.hour).toEqual([14]); |
| 26 | + expect(result.day).toEqual([15]); |
| 27 | + expect(result.month).toEqual([6]); |
| 28 | + expect(result.weekday).toEqual([3]); |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + describe('lists', () => { |
| 33 | + it('should parse comma-separated values', () => { |
| 34 | + const result = parseCron('0,15,30,45 * * * *'); |
| 35 | + expect(result.minute).toEqual([0, 15, 30, 45]); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + describe('ranges', () => { |
| 40 | + it('should parse range expressions', () => { |
| 41 | + const result = parseCron('0-5 * * * *'); |
| 42 | + expect(result.minute).toEqual([0, 1, 2, 3, 4, 5]); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('steps', () => { |
| 47 | + it('should parse step expressions with wildcard base', () => { |
| 48 | + const result = parseCron('*/15 * * * *'); |
| 49 | + expect(result.minute).toEqual([0, 15, 30, 45]); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should parse step expressions with numeric base', () => { |
| 53 | + const result = parseCron('10/5 * * * *'); |
| 54 | + expect(result.minute).toEqual([10, 15, 20, 25, 30, 35, 40, 45, 50, 55]); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('validation', () => { |
| 59 | + it('should reject expressions with wrong number of fields', () => { |
| 60 | + expect(() => parseCron('* * * *')).toThrow('expected 5 fields'); |
| 61 | + expect(() => parseCron('* * * * * *')).toThrow('expected 5 fields'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should reject out-of-range values', () => { |
| 65 | + expect(() => parseCron('60 * * * *')).toThrow('Invalid cron value'); |
| 66 | + expect(() => parseCron('24 * * * *')).toThrow('Invalid cron value'); |
| 67 | + expect(() => parseCron('* 25 * * *')).toThrow('Invalid cron value'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should reject invalid range (start > end)', () => { |
| 71 | + expect(() => parseCron('30-20 * * * *')).toThrow('Invalid cron range'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should reject invalid step values', () => { |
| 75 | + expect(() => parseCron('*/0 * * * *')).toThrow('Invalid cron step'); |
| 76 | + }); |
| 77 | + }); |
| 78 | +}); |
| 79 | + |
| 80 | +describe('getNextCronRun', () => { |
| 81 | + it('should find next occurrence of daily cron', () => { |
| 82 | + const cron = '0 12 * * *'; // Every day at noon |
| 83 | + const from = new Date('2024-06-15T10:00:00Z'); |
| 84 | + const next = getNextCronRun(cron, from); |
| 85 | + |
| 86 | + expect(next.getHours()).toBe(12); |
| 87 | + expect(next.getMinutes()).toBe(0); |
| 88 | + expect(next.getDate()).toBe(15); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should advance to next day if time has passed', () => { |
| 92 | + const cron = '0 12 * * *'; // Every day at noon |
| 93 | + const from = new Date('2024-06-15T14:00:00Z'); |
| 94 | + const next = getNextCronRun(cron, from); |
| 95 | + |
| 96 | + expect(next.getDate()).toBe(16); |
| 97 | + expect(next.getHours()).toBe(12); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should handle hourly cron', () => { |
| 101 | + const cron = '30 * * * *'; // Every hour at minute 30 |
| 102 | + const from = new Date('2024-06-15T10:00:00Z'); |
| 103 | + const next = getNextCronRun(cron, from); |
| 104 | + |
| 105 | + expect(next.getMinutes()).toBe(30); |
| 106 | + expect(next.getHours()).toBe(10); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should throw if no match within 2 years', () => { |
| 110 | + // Impossible cron: Feb 30th |
| 111 | + const cron = '0 0 30 2 *'; |
| 112 | + const from = new Date('2024-01-01T00:00:00Z'); |
| 113 | + |
| 114 | + expect(() => getNextCronRun(cron, from)).toThrow('No matching cron time found'); |
| 115 | + }); |
| 116 | +}); |
0 commit comments