|
1 | 1 | import { dateParser, parseTimeRangeInput } from '../utils'; |
2 | 2 |
|
3 | 3 | describe('dateParser', () => { |
| 4 | + let mockDate: Date; |
| 5 | + |
4 | 6 | beforeEach(() => { |
5 | 7 | // Mock current date to ensure consistent test results |
6 | 8 | jest.useFakeTimers(); |
7 | | - jest.setSystemTime(new Date('2025-01-15T22:00')); |
| 9 | + mockDate = new Date('2025-01-15T22:00:00'); |
| 10 | + jest.setSystemTime(mockDate); |
8 | 11 | }); |
9 | 12 |
|
10 | 13 | afterEach(() => { |
@@ -44,6 +47,58 @@ describe('dateParser', () => { |
44 | 47 | it('parses non-future month name/date with current year', () => { |
45 | 48 | expect(dateParser('Jan 15')).toEqual(new Date('2025-01-15T12:00:00')); |
46 | 49 | }); |
| 50 | + |
| 51 | + it('clamps slightly future dates to now (within 1 day) - no year specified', () => { |
| 52 | + // Input: 23:00. Now: 22:00. Should clamp to now (22:00) |
| 53 | + const result = dateParser('Jan 15 23:00:00'); |
| 54 | + expect(result?.getTime()).toEqual(mockDate.getTime()); |
| 55 | + expect(result?.getFullYear()).toEqual(2025); |
| 56 | + }); |
| 57 | + |
| 58 | + it('clamps slightly future dates to now (within 1 day) - year specified', () => { |
| 59 | + // Explicit year should be preserved even if in future, but clamped to now |
| 60 | + const result = dateParser('2025-01-15 23:00:00'); |
| 61 | + expect(result?.getTime()).toEqual(mockDate.getTime()); |
| 62 | + // Verify it didn't shift to 2024 |
| 63 | + expect(result?.getFullYear()).toEqual(2025); |
| 64 | + }); |
| 65 | + |
| 66 | + it('shifts year back for dates more than 1 day in future with inferred year', () => { |
| 67 | + // mocked time is 2025-01-15 22:00 |
| 68 | + // Jan 17 is more than 1 day in future, should shift to 2024 |
| 69 | + const result = dateParser('Jan 17 12:00:00'); |
| 70 | + expect(result).toEqual(new Date('2024-01-17T12:00:00')); |
| 71 | + }); |
| 72 | + |
| 73 | + it('does NOT shift year back for dates more than 1 day in future with explicit year', () => { |
| 74 | + // mocked time is 2025-01-15 22:00 |
| 75 | + // Jan 17, 2025 is more than 1 day in future, but year is explicit |
| 76 | + const result = dateParser('2025-01-17 12:00:00'); |
| 77 | + expect(result).toEqual(new Date('2025-01-17T12:00:00')); |
| 78 | + expect(result?.getFullYear()).toEqual(2025); |
| 79 | + }); |
| 80 | + |
| 81 | + it('handles dates in the past correctly', () => { |
| 82 | + // mocked time is 2025-01-15 22:00 |
| 83 | + const result = dateParser('Jan 10 12:00:00'); |
| 84 | + expect(result).toEqual(new Date('2025-01-10T12:00:00')); |
| 85 | + expect(result?.getFullYear()).toEqual(2025); |
| 86 | + }); |
| 87 | + |
| 88 | + it('handles edge case: exactly 1 day in the future', () => { |
| 89 | + // mocked time is 2025-01-15 22:00:00 |
| 90 | + // Exactly 24 hours later: 2025-01-16 22:00:00 |
| 91 | + // Should be clamped since it's <= 1 day from now |
| 92 | + const result = dateParser('Jan 16 22:00:00'); |
| 93 | + expect(result?.getTime()).toEqual(mockDate.getTime()); |
| 94 | + }); |
| 95 | + |
| 96 | + it('handles edge case: just over 1 day in the future', () => { |
| 97 | + // mocked time is 2025-01-15 22:00:00 |
| 98 | + // 24 hours + 1 second later should shift year |
| 99 | + const result = dateParser('Jan 16 22:00:01'); |
| 100 | + expect(result).toEqual(new Date('2024-01-16T22:00:01')); |
| 101 | + }); |
47 | 102 | }); |
48 | 103 |
|
49 | 104 | describe('parseTimeRangeInput', () => { |
|
0 commit comments