|
| 1 | +import { DateNormalizer, FlexibleDateInput } from '../src/DateNormalizer'; |
| 2 | +import { moment } from 'obsidian'; |
| 3 | + |
| 4 | +describe('DateNormalizer', () => { |
| 5 | + describe('normalizeDateInput', () => { |
| 6 | + describe('string inputs', () => { |
| 7 | + it('should handle YYYY-MM-DD format (already normalized)', () => { |
| 8 | + const result = DateNormalizer.normalizeDateInput('2025-03-01'); |
| 9 | + expect(result).toBe('2025-03-01'); |
| 10 | + }); |
| 11 | + |
| 12 | + it('should parse and normalize various string formats', () => { |
| 13 | + const testCases = [ |
| 14 | + { input: '2025/03/01', expected: '2025-03-01' }, |
| 15 | + { input: '03/01/2025', expected: '2025-03-01' }, |
| 16 | + { input: 'March 1, 2025', expected: '2025-03-01' }, |
| 17 | + { input: '1 Mar 2025', expected: '2025-03-01' }, |
| 18 | + { input: '2025-03-01T12:30:00', expected: '2025-03-01' }, |
| 19 | + { input: '2025-03-01T12:30:00Z', expected: '2025-03-01' } |
| 20 | + ]; |
| 21 | + |
| 22 | + testCases.forEach(({ input, expected }) => { |
| 23 | + const result = DateNormalizer.normalizeDateInput(input); |
| 24 | + expect(result).toBe(expected); |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should throw error for invalid date strings', () => { |
| 29 | + const invalidDates = [ |
| 30 | + 'invalid-date', |
| 31 | + 'not-a-date', |
| 32 | + 'foo bar', |
| 33 | + '' |
| 34 | + ]; |
| 35 | + |
| 36 | + invalidDates.forEach(invalidDate => { |
| 37 | + expect(() => DateNormalizer.normalizeDateInput(invalidDate)) |
| 38 | + .toThrow(`Invalid date string: ${invalidDate}`); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should handle edge case invalid dates that moment accepts', () => { |
| 43 | + // Note: moment is quite permissive - these dates are technically invalid |
| 44 | + // but moment will try to parse them, so we test that they at least return something |
| 45 | + const edgeCaseDates = [ |
| 46 | + '2025-13-01', // Invalid month - moment converts to 2026-01-01 |
| 47 | + '2025-02-30' // Invalid day for February - moment converts to 2025-03-02 |
| 48 | + ]; |
| 49 | + |
| 50 | + edgeCaseDates.forEach(edgeDate => { |
| 51 | + // These should not throw, but should return some normalized date |
| 52 | + const result = DateNormalizer.normalizeDateInput(edgeDate); |
| 53 | + expect(typeof result).toBe('string'); |
| 54 | + expect(result).toMatch(/^\d{4}-\d{2}-\d{2}$/); |
| 55 | + }); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('moment object inputs', () => { |
| 60 | + it('should handle moment objects', () => { |
| 61 | + const momentObj = moment('2025-03-01T12:30:00'); |
| 62 | + const result = DateNormalizer.normalizeDateInput(momentObj); |
| 63 | + expect(result).toBe('2025-03-01'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should handle moment objects with different timezones', () => { |
| 67 | + const momentUtc = moment.utc('2025-03-01T23:30:00'); |
| 68 | + const result = DateNormalizer.normalizeDateInput(momentUtc); |
| 69 | + expect(result).toBe('2025-03-01'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should handle moment objects created from different sources', () => { |
| 73 | + const fromString = moment('March 1, 2025'); |
| 74 | + const fromArray = moment([2025, 2, 1]); // month is 0-indexed in moment |
| 75 | + |
| 76 | + expect(DateNormalizer.normalizeDateInput(fromString)).toBe('2025-03-01'); |
| 77 | + expect(DateNormalizer.normalizeDateInput(fromArray)).toBe('2025-03-01'); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe('Date object inputs', () => { |
| 82 | + it('should handle Date objects', () => { |
| 83 | + const dateObj = new Date('2025-03-01T12:30:00'); |
| 84 | + const result = DateNormalizer.normalizeDateInput(dateObj); |
| 85 | + expect(result).toBe('2025-03-01'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should handle Date objects with different times', () => { |
| 89 | + const morningDate = new Date('2025-03-01T06:00:00'); |
| 90 | + const eveningDate = new Date('2025-03-01T18:30:00'); |
| 91 | + |
| 92 | + expect(DateNormalizer.normalizeDateInput(morningDate)).toBe('2025-03-01'); |
| 93 | + expect(DateNormalizer.normalizeDateInput(eveningDate)).toBe('2025-03-01'); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + describe('error handling', () => { |
| 98 | + it('should throw error for null input', () => { |
| 99 | + expect(() => DateNormalizer.normalizeDateInput(null as any)) |
| 100 | + .toThrow('Date input cannot be null or undefined'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should throw error for undefined input', () => { |
| 104 | + expect(() => DateNormalizer.normalizeDateInput(undefined as any)) |
| 105 | + .toThrow('Date input cannot be null or undefined'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should throw error for unsupported input types', () => { |
| 109 | + const unsupportedInputs = [ |
| 110 | + 123, |
| 111 | + true, |
| 112 | + false, |
| 113 | + {}, |
| 114 | + [], |
| 115 | + Symbol('test') |
| 116 | + ]; |
| 117 | + |
| 118 | + unsupportedInputs.forEach(input => { |
| 119 | + expect(() => DateNormalizer.normalizeDateInput(input as any)) |
| 120 | + .toThrow(`Unsupported date input type: ${typeof input}`); |
| 121 | + }); |
| 122 | + }); |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + describe('normalizeDateInputs', () => { |
| 127 | + it('should handle arrays of mixed input types', () => { |
| 128 | + const inputs: FlexibleDateInput[] = [ |
| 129 | + '2025-03-01', |
| 130 | + moment('2025-03-02'), |
| 131 | + new Date('2025-03-03T12:00:00'), |
| 132 | + '2025/03/04', |
| 133 | + 'March 5, 2025' |
| 134 | + ]; |
| 135 | + |
| 136 | + const results = DateNormalizer.normalizeDateInputs(inputs); |
| 137 | + expect(results).toEqual([ |
| 138 | + '2025-03-01', |
| 139 | + '2025-03-02', |
| 140 | + '2025-03-03', |
| 141 | + '2025-03-04', |
| 142 | + '2025-03-05' |
| 143 | + ]); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should handle empty arrays', () => { |
| 147 | + const results = DateNormalizer.normalizeDateInputs([]); |
| 148 | + expect(results).toEqual([]); |
| 149 | + }); |
| 150 | + |
| 151 | + it('should propagate errors from individual date parsing', () => { |
| 152 | + const inputs: FlexibleDateInput[] = ['2025-03-01', 'invalid-date']; |
| 153 | + expect(() => DateNormalizer.normalizeDateInputs(inputs)) |
| 154 | + .toThrow('Invalid date string: invalid-date'); |
| 155 | + }); |
| 156 | + |
| 157 | + it('should handle single-element arrays', () => { |
| 158 | + const results = DateNormalizer.normalizeDateInputs([moment('2025-03-01')]); |
| 159 | + expect(results).toEqual(['2025-03-01']); |
| 160 | + }); |
| 161 | + }); |
| 162 | + |
| 163 | + describe('isSupportedType', () => { |
| 164 | + it('should return true for supported types', () => { |
| 165 | + const supportedInputs = [ |
| 166 | + '2025-03-01', |
| 167 | + moment('2025-03-01'), |
| 168 | + new Date('2025-03-01'), |
| 169 | + 'March 1, 2025', |
| 170 | + '2025/03/01' |
| 171 | + ]; |
| 172 | + |
| 173 | + supportedInputs.forEach(input => { |
| 174 | + expect(DateNormalizer.isSupportedType(input)).toBe(true); |
| 175 | + }); |
| 176 | + }); |
| 177 | + |
| 178 | + it('should return false for unsupported types', () => { |
| 179 | + const unsupportedInputs = [ |
| 180 | + 123, |
| 181 | + true, |
| 182 | + null, |
| 183 | + undefined, |
| 184 | + {}, |
| 185 | + [], |
| 186 | + Symbol('test') |
| 187 | + ]; |
| 188 | + |
| 189 | + unsupportedInputs.forEach(input => { |
| 190 | + expect(DateNormalizer.isSupportedType(input)).toBe(false); |
| 191 | + }); |
| 192 | + }); |
| 193 | + }); |
| 194 | + |
| 195 | + describe('getSupportedFormatsDescription', () => { |
| 196 | + it('should return a helpful description string', () => { |
| 197 | + const description = DateNormalizer.getSupportedFormatsDescription(); |
| 198 | + expect(typeof description).toBe('string'); |
| 199 | + expect(description.length).toBeGreaterThan(0); |
| 200 | + expect(description).toContain('YYYY-MM-DD'); |
| 201 | + expect(description).toContain('moment'); |
| 202 | + expect(description).toContain('Date'); |
| 203 | + }); |
| 204 | + }); |
| 205 | + |
| 206 | + describe('integration scenarios', () => { |
| 207 | + it('should handle Templater-style usage patterns', () => { |
| 208 | + // Simulate common Templater scenarios |
| 209 | + const fileTitle = '20250301'; // YYYYMMDD format |
| 210 | + const momentFromTitle = moment(fileTitle, 'YYYYMMDD'); |
| 211 | + const result = DateNormalizer.normalizeDateInput(momentFromTitle); |
| 212 | + expect(result).toBe('2025-03-01'); |
| 213 | + }); |
| 214 | + |
| 215 | + it('should handle date ranges', () => { |
| 216 | + const startDate = '2025-03-01'; |
| 217 | + const endDate = moment('2025-03-07'); |
| 218 | + |
| 219 | + const results = DateNormalizer.normalizeDateInputs([startDate, endDate]); |
| 220 | + expect(results).toEqual(['2025-03-01', '2025-03-07']); |
| 221 | + }); |
| 222 | + |
| 223 | + it('should handle timezone edge cases', () => { |
| 224 | + // Test dates around timezone boundaries |
| 225 | + const utcDate = new Date('2025-03-01T23:59:59Z'); |
| 226 | + const result = DateNormalizer.normalizeDateInput(utcDate); |
| 227 | + expect(result).toBe('2025-03-01'); // Should be same day regardless of local timezone |
| 228 | + }); |
| 229 | + }); |
| 230 | +}); |
0 commit comments