|
| 1 | +import { Temporal } from 'temporal-polyfill'; |
| 2 | +import { describe, it, expect } from 'vitest'; |
| 3 | + |
| 4 | +import { |
| 5 | + hourID, |
| 6 | + parseHourID, |
| 7 | + dayID, |
| 8 | + parseDayID, |
| 9 | + weekID, |
| 10 | + parseWeekID, |
| 11 | + monthID, |
| 12 | + parseMonthID, |
| 13 | + yearID, |
| 14 | + parseYearID, |
| 15 | + getPlainDateIds, |
| 16 | +} from './calendar'; |
| 17 | + |
| 18 | +describe('calendar', () => { |
| 19 | + describe('hourID', () => { |
| 20 | + it('should generate correct hour IDs for various cases', () => { |
| 21 | + const testCases = [ |
| 22 | + { |
| 23 | + input: new Temporal.PlainDateTime(2025, 1, 5, 9), |
| 24 | + expected: '2025_01_05_09', |
| 25 | + description: 'hourID single digit values', |
| 26 | + }, |
| 27 | + { |
| 28 | + input: new Temporal.PlainDateTime(2025, 12, 25, 23), |
| 29 | + expected: '2025_12_25_23', |
| 30 | + description: 'hourID double digit values', |
| 31 | + }, |
| 32 | + { |
| 33 | + input: new Temporal.PlainDateTime(2000, 1, 1, 0), |
| 34 | + expected: '2000_01_01_0', |
| 35 | + description: 'leading zero hour', |
| 36 | + }, |
| 37 | + ]; |
| 38 | + |
| 39 | + testCases.forEach(({ input, expected }) => { |
| 40 | + expect(hourID(input)).toBe(expected); |
| 41 | + }); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + describe('dayID', () => { |
| 46 | + it('should generate correct day IDs for various cases', () => { |
| 47 | + const testCases = [ |
| 48 | + { input: new Temporal.PlainDate(2025, 1, 5), expected: '2025_01_05', description: 'dayID single digit values' }, |
| 49 | + { |
| 50 | + input: new Temporal.PlainDate(2025, 12, 25), |
| 51 | + expected: '2025_12_25', |
| 52 | + description: 'dayID double digit values', |
| 53 | + }, |
| 54 | + { input: new Temporal.PlainDate(2000, 1, 1), expected: '2000_01_01', description: 'leading zero day' }, |
| 55 | + ]; |
| 56 | + |
| 57 | + testCases.forEach(({ input, expected }) => { |
| 58 | + expect(dayID(input)).toBe(expected); |
| 59 | + }); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe('weekID', () => { |
| 64 | + it('should generate correct week IDs for various cases', () => { |
| 65 | + const testCases = [ |
| 66 | + { input: new Temporal.PlainDate(2025, 1, 1), expected: '2024_12_30', description: 'Wednesday (start of week)' }, |
| 67 | + { input: new Temporal.PlainDate(2025, 1, 7), expected: '2025_01_06', description: 'Tuesday (same week)' }, |
| 68 | + { input: new Temporal.PlainDate(2025, 1, 3), expected: '2024_12_30', description: 'Friday (same week)' }, |
| 69 | + { |
| 70 | + input: new Temporal.PlainDate(2025, 1, 31), |
| 71 | + expected: '2025_01_27', |
| 72 | + description: 'week spanning across months', |
| 73 | + }, |
| 74 | + ]; |
| 75 | + |
| 76 | + testCases.forEach(({ input, expected }) => { |
| 77 | + expect(weekID(input)).toBe(expected); |
| 78 | + }); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + describe('monthID', () => { |
| 83 | + it('should generate correct month IDs for various cases', () => { |
| 84 | + const testCases = [ |
| 85 | + { |
| 86 | + input: new Temporal.PlainYearMonth(2025, 1), |
| 87 | + expected: '2025_01', |
| 88 | + description: 'monthID single digit values', |
| 89 | + }, |
| 90 | + { |
| 91 | + input: new Temporal.PlainYearMonth(2025, 12), |
| 92 | + expected: '2025_12', |
| 93 | + description: 'monthID double digit values', |
| 94 | + }, |
| 95 | + { input: new Temporal.PlainYearMonth(2000, 1), expected: '2000_01', description: 'leading zero month' }, |
| 96 | + ]; |
| 97 | + |
| 98 | + testCases.forEach(({ input, expected }) => { |
| 99 | + expect(monthID(input)).toBe(expected); |
| 100 | + }); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + describe('yearID', () => { |
| 105 | + it('should generate correct year IDs for various cases', () => { |
| 106 | + const testCases = [ |
| 107 | + { input: '2025', expected: '2025', description: 'string year' }, |
| 108 | + { input: '2000', expected: '2000', description: 'string year 2000' }, |
| 109 | + { input: 2025, expected: '2025', description: 'number year' }, |
| 110 | + { input: 2000, expected: '2000', description: 'number year 2000' }, |
| 111 | + { input: 0, expected: '0', description: 'zero year' }, |
| 112 | + { input: '0', expected: '0', description: 'zero string year' }, |
| 113 | + ]; |
| 114 | + |
| 115 | + testCases.forEach(({ input, expected }) => { |
| 116 | + expect(yearID(input)).toBe(expected); |
| 117 | + }); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + describe('getPlainDateIds', () => { |
| 122 | + it('should return correct IDs for various dates', () => { |
| 123 | + const testCases = [ |
| 124 | + { |
| 125 | + input: new Temporal.PlainDate(2025, 1, 5), |
| 126 | + expected: ['2025', '2025_01', '2025_01_05'], |
| 127 | + description: 'getPlainDateIds single digit values', |
| 128 | + }, |
| 129 | + { |
| 130 | + input: new Temporal.PlainDate(2025, 12, 25), |
| 131 | + expected: ['2025', '2025_12', '2025_12_25'], |
| 132 | + description: 'getPlainDateIds double digit values', |
| 133 | + }, |
| 134 | + { |
| 135 | + input: new Temporal.PlainDate(2000, 1, 1), |
| 136 | + expected: ['2000', '2000_01', '2000_01_01'], |
| 137 | + description: 'year 2000', |
| 138 | + }, |
| 139 | + ]; |
| 140 | + |
| 141 | + testCases.forEach(({ input, expected }) => { |
| 142 | + expect(getPlainDateIds(input)).toEqual(expected); |
| 143 | + }); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + describe('parseHourID', () => { |
| 148 | + it('should parse hour IDs correctly for various cases', () => { |
| 149 | + const testCases = [ |
| 150 | + { |
| 151 | + input: '2025_01_05_09', |
| 152 | + expected: { year: 2025, month: 1, day: 5, hour: 9 }, |
| 153 | + description: 'parseHourID single digit values', |
| 154 | + }, |
| 155 | + { |
| 156 | + input: '2025_12_25_23', |
| 157 | + expected: { year: 2025, month: 12, day: 25, hour: 23 }, |
| 158 | + description: 'parseHourID double digit values', |
| 159 | + }, |
| 160 | + { |
| 161 | + input: '2000_01_01_00', |
| 162 | + expected: { year: 2000, month: 1, day: 1, hour: 0 }, |
| 163 | + description: 'leading zero hour', |
| 164 | + }, |
| 165 | + ]; |
| 166 | + |
| 167 | + testCases.forEach(({ input, expected }) => { |
| 168 | + const result = parseHourID(input); |
| 169 | + |
| 170 | + expect(result.year).toBe(expected.year); |
| 171 | + expect(result.month).toBe(expected.month); |
| 172 | + expect(result.day).toBe(expected.day); |
| 173 | + expect(result.hour).toBe(expected.hour); |
| 174 | + }); |
| 175 | + }); |
| 176 | + }); |
| 177 | + |
| 178 | + describe('parseDayID', () => { |
| 179 | + it('should parse day IDs correctly for various cases', () => { |
| 180 | + const testCases = [ |
| 181 | + { input: '2025_01_05', expected: { year: 2025, month: 1, day: 5 }, description: 'single digit values' }, |
| 182 | + { input: '2025_12_25', expected: { year: 2025, month: 12, day: 25 }, description: 'double digit values' }, |
| 183 | + { input: '2000_01_01', expected: { year: 2000, month: 1, day: 1 }, description: 'leading zero day' }, |
| 184 | + ]; |
| 185 | + |
| 186 | + testCases.forEach(({ input, expected }) => { |
| 187 | + const result = parseDayID(input); |
| 188 | + |
| 189 | + expect(result.year).toBe(expected.year); |
| 190 | + expect(result.month).toBe(expected.month); |
| 191 | + expect(result.day).toBe(expected.day); |
| 192 | + }); |
| 193 | + }); |
| 194 | + }); |
| 195 | + |
| 196 | + describe('parseWeekID', () => { |
| 197 | + it('should parse week IDs correctly for various cases', () => { |
| 198 | + const testCases = [ |
| 199 | + { input: '2025_01_01', expected: { year: 2025, month: 1, day: 1 }, description: 'single digit values' }, |
| 200 | + { input: '2025_12_25', expected: { year: 2025, month: 12, day: 25 }, description: 'double digit values' }, |
| 201 | + ]; |
| 202 | + |
| 203 | + testCases.forEach(({ input, expected }) => { |
| 204 | + const result = parseWeekID(input); |
| 205 | + |
| 206 | + expect(result.year).toBe(expected.year); |
| 207 | + expect(result.month).toBe(expected.month); |
| 208 | + expect(result.day).toBe(expected.day); |
| 209 | + }); |
| 210 | + }); |
| 211 | + }); |
| 212 | + |
| 213 | + describe('parseMonthID', () => { |
| 214 | + it('should parse month IDs correctly for various cases', () => { |
| 215 | + const testCases = [ |
| 216 | + { input: '2025_01', expected: { year: 2025, month: 1 }, description: 'single digit month' }, |
| 217 | + { input: '2025_12', expected: { year: 2025, month: 12 }, description: 'double digit month' }, |
| 218 | + { input: '2000_01', expected: { year: 2000, month: 1 }, description: 'leading zero month' }, |
| 219 | + ]; |
| 220 | + |
| 221 | + testCases.forEach(({ input, expected }) => { |
| 222 | + const result = parseMonthID(input); |
| 223 | + |
| 224 | + expect(result.year).toBe(expected.year); |
| 225 | + expect(result.month).toBe(expected.month); |
| 226 | + }); |
| 227 | + }); |
| 228 | + }); |
| 229 | + |
| 230 | + describe('parseYearID', () => { |
| 231 | + it('should parse year IDs correctly for various cases', () => { |
| 232 | + const testCases = [ |
| 233 | + { input: '2025', expected: 2025, description: 'normal year' }, |
| 234 | + { input: '2000', expected: 2000, description: 'year 2000' }, |
| 235 | + { input: '0', expected: 0, description: 'zero year' }, |
| 236 | + { input: '1', expected: 1, description: 'single digit year' }, |
| 237 | + ]; |
| 238 | + |
| 239 | + testCases.forEach(({ input, expected }) => { |
| 240 | + expect(parseYearID(input)).toBe(expected); |
| 241 | + }); |
| 242 | + }); |
| 243 | + }); |
| 244 | + |
| 245 | + describe('round-trip tests', () => { |
| 246 | + it('should maintain consistency for hourID/parseHourID', () => { |
| 247 | + const originalDateTime = new Temporal.PlainDateTime(2025, 1, 5, 9); |
| 248 | + const hourId = hourID(originalDateTime); |
| 249 | + const parsedDateTime = parseHourID(hourId); |
| 250 | + |
| 251 | + expect(parsedDateTime.equals(originalDateTime)).toBe(true); |
| 252 | + }); |
| 253 | + |
| 254 | + it('should maintain consistency for dayID/parseDayID', () => { |
| 255 | + const originalDate = new Temporal.PlainDate(2025, 1, 5); |
| 256 | + const dayId = dayID(originalDate); |
| 257 | + const parsedDate = parseDayID(dayId); |
| 258 | + |
| 259 | + expect(parsedDate.equals(originalDate)).toBe(true); |
| 260 | + }); |
| 261 | + |
| 262 | + it('should maintain consistency for monthID/parseMonthID', () => { |
| 263 | + const originalYearMonth = new Temporal.PlainYearMonth(2025, 1); |
| 264 | + const monthId = monthID(originalYearMonth); |
| 265 | + const parsedYearMonth = parseMonthID(monthId); |
| 266 | + |
| 267 | + expect(parsedYearMonth.equals(originalYearMonth)).toBe(true); |
| 268 | + }); |
| 269 | + |
| 270 | + it('should maintain consistency for yearID/parseYearID', () => { |
| 271 | + const originalYear = 2025; |
| 272 | + const yearId = yearID(originalYear); |
| 273 | + const parsedYear = parseYearID(yearId); |
| 274 | + |
| 275 | + expect(parsedYear).toBe(originalYear); |
| 276 | + }); |
| 277 | + }); |
| 278 | +}); |
0 commit comments