|
1 | 1 | // Cron schedule tests cover schedule parsing and next-run calculations. |
2 | | -import { beforeEach, describe, expect, it } from "vitest"; |
| 2 | +import { Cron } from "croner"; |
| 3 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
3 | 4 | import { |
4 | 5 | coerceFiniteScheduleNumber, |
5 | 6 | computeNextRunAtMs, |
@@ -49,6 +50,281 @@ describe("cron schedule", () => { |
49 | 50 | expect(new Date(next ?? 0).getUTCFullYear()).toBe(2026); |
50 | 51 | }); |
51 | 52 |
|
| 53 | + describe("daylight-saving transitions", () => { |
| 54 | + it.each([ |
| 55 | + { |
| 56 | + label: "New York repeated hour retains the later same-day reminder", |
| 57 | + timezone: "America/New_York", |
| 58 | + expression: "30 1,3 * * *", |
| 59 | + now: "2026-11-01T06:15:00.000Z", |
| 60 | + next: "2026-11-01T08:30:00.000Z", |
| 61 | + previous: "2026-11-01T05:30:00.000Z", |
| 62 | + }, |
| 63 | + { |
| 64 | + label: "New York repeated hour retains six-field reminders", |
| 65 | + timezone: "America/New_York", |
| 66 | + expression: "15 30 1,3 * * *", |
| 67 | + now: "2026-11-01T06:15:00.000Z", |
| 68 | + next: "2026-11-01T08:30:15.000Z", |
| 69 | + previous: "2026-11-01T05:30:15.000Z", |
| 70 | + }, |
| 71 | + { |
| 72 | + label: "New York repeated hour skips duplicate per-minute occurrences", |
| 73 | + timezone: "America/New_York", |
| 74 | + expression: "* * * * *", |
| 75 | + now: "2026-11-01T06:15:00.000Z", |
| 76 | + next: "2026-11-01T07:00:00.000Z", |
| 77 | + previous: "2026-11-01T05:59:00.000Z", |
| 78 | + }, |
| 79 | + { |
| 80 | + label: "New York repeated hour skips duplicate per-second occurrences", |
| 81 | + timezone: "America/New_York", |
| 82 | + expression: "* * * * * *", |
| 83 | + now: "2026-11-01T06:15:00.000Z", |
| 84 | + next: "2026-11-01T07:00:00.000Z", |
| 85 | + previous: "2026-11-01T05:59:59.000Z", |
| 86 | + }, |
| 87 | + { |
| 88 | + label: "Oslo repeated hour retains the later same-day reminder", |
| 89 | + timezone: "Europe/Oslo", |
| 90 | + expression: "30 2,4 * * *", |
| 91 | + now: "2026-10-25T01:15:00.000Z", |
| 92 | + next: "2026-10-25T03:30:00.000Z", |
| 93 | + previous: "2026-10-25T00:30:00.000Z", |
| 94 | + }, |
| 95 | + { |
| 96 | + label: "Lord Howe schedules the first occurrence of a half-hour fold", |
| 97 | + timezone: "Australia/Lord_Howe", |
| 98 | + expression: "45 1 * * *", |
| 99 | + now: "2026-04-04T14:40:00.000Z", |
| 100 | + next: "2026-04-04T14:45:00.000Z", |
| 101 | + previous: "2026-04-03T14:45:00.000Z", |
| 102 | + }, |
| 103 | + { |
| 104 | + label: "Lord Howe repeated half-hour retains the later same-day reminder", |
| 105 | + timezone: "Australia/Lord_Howe", |
| 106 | + expression: "45 1,2 * * *", |
| 107 | + now: "2026-04-04T15:10:00.000Z", |
| 108 | + next: "2026-04-04T16:15:00.000Z", |
| 109 | + previous: "2026-04-04T14:45:00.000Z", |
| 110 | + }, |
| 111 | + { |
| 112 | + label: "Lord Howe repeated half-hour skips duplicate per-second occurrences", |
| 113 | + timezone: "Australia/Lord_Howe", |
| 114 | + expression: "* * * * * *", |
| 115 | + now: "2026-04-04T15:10:00.000Z", |
| 116 | + next: "2026-04-04T15:30:00.000Z", |
| 117 | + previous: "2026-04-04T14:59:59.000Z", |
| 118 | + }, |
| 119 | + { |
| 120 | + label: "New York skips a nonexistent spring-forward reminder", |
| 121 | + timezone: "America/New_York", |
| 122 | + expression: "30 2 * * *", |
| 123 | + now: "2027-03-14T06:45:00.000Z", |
| 124 | + next: "2027-03-15T06:30:00.000Z", |
| 125 | + previous: "2027-03-13T07:30:00.000Z", |
| 126 | + }, |
| 127 | + { |
| 128 | + label: "New York previous occurrence never comes from its spring-forward gap", |
| 129 | + timezone: "America/New_York", |
| 130 | + expression: "30 2 * * *", |
| 131 | + now: "2027-03-14T07:10:00.000Z", |
| 132 | + next: "2027-03-15T06:30:00.000Z", |
| 133 | + previous: "2027-03-13T07:30:00.000Z", |
| 134 | + }, |
| 135 | + { |
| 136 | + label: "New York preserves the final valid second before a spring-forward gap", |
| 137 | + timezone: "America/New_York", |
| 138 | + expression: "59 59 1,2 * * *", |
| 139 | + now: "2027-03-14T07:05:00.000Z", |
| 140 | + next: "2027-03-15T05:59:59.000Z", |
| 141 | + previous: "2027-03-14T06:59:59.000Z", |
| 142 | + }, |
| 143 | + { |
| 144 | + label: "Oslo skips a nonexistent spring-forward reminder", |
| 145 | + timezone: "Europe/Oslo", |
| 146 | + expression: "30 2 * * *", |
| 147 | + now: "2026-03-29T00:45:00.000Z", |
| 148 | + next: "2026-03-30T00:30:00.000Z", |
| 149 | + previous: "2026-03-28T01:30:00.000Z", |
| 150 | + }, |
| 151 | + { |
| 152 | + label: "Lord Howe keeps the valid boundary after its half-hour spring gap", |
| 153 | + timezone: "Australia/Lord_Howe", |
| 154 | + expression: "15,30 2 * * *", |
| 155 | + now: "2026-10-03T15:20:00.000Z", |
| 156 | + next: "2026-10-03T15:30:00.000Z", |
| 157 | + previous: "2026-10-02T16:00:00.000Z", |
| 158 | + }, |
| 159 | + { |
| 160 | + label: "Lord Howe keeps a valid later occurrence after its half-hour spring gap", |
| 161 | + timezone: "Australia/Lord_Howe", |
| 162 | + expression: "15,35 2 * * *", |
| 163 | + now: "2026-10-03T15:20:00.000Z", |
| 164 | + next: "2026-10-03T15:35:00.000Z", |
| 165 | + previous: "2026-10-02T16:05:00.000Z", |
| 166 | + }, |
| 167 | + { |
| 168 | + label: "Lord Howe previous occurrence never comes from its half-hour spring gap", |
| 169 | + timezone: "Australia/Lord_Howe", |
| 170 | + expression: "15 2 * * *", |
| 171 | + now: "2026-10-03T15:32:00.000Z", |
| 172 | + next: "2026-10-04T15:15:00.000Z", |
| 173 | + previous: "2026-10-02T15:45:00.000Z", |
| 174 | + }, |
| 175 | + { |
| 176 | + label: "Antarctica schedules the first occurrence of a two-hour fold", |
| 177 | + timezone: "Antarctica/Troll", |
| 178 | + expression: "0 2 * * *", |
| 179 | + now: "2026-10-24T23:30:00.000Z", |
| 180 | + next: "2026-10-25T00:00:00.000Z", |
| 181 | + previous: "2026-10-24T00:00:00.000Z", |
| 182 | + }, |
| 183 | + { |
| 184 | + label: "Antarctica skips every repeated occurrence in a two-hour fold", |
| 185 | + timezone: "Antarctica/Troll", |
| 186 | + expression: "0 1,2,4 * * *", |
| 187 | + now: "2026-10-25T01:30:00.000Z", |
| 188 | + next: "2026-10-25T04:00:00.000Z", |
| 189 | + previous: "2026-10-25T00:00:00.000Z", |
| 190 | + }, |
| 191 | + { |
| 192 | + label: "Antarctica skips duplicate per-second occurrences in a two-hour fold", |
| 193 | + timezone: "Antarctica/Troll", |
| 194 | + expression: "* * * * * *", |
| 195 | + now: "2026-10-25T01:30:00.250Z", |
| 196 | + next: "2026-10-25T03:00:00.000Z", |
| 197 | + previous: "2026-10-25T00:59:59.000Z", |
| 198 | + }, |
| 199 | + { |
| 200 | + label: "Antarctica keeps the valid reminder after its two-hour spring gap", |
| 201 | + timezone: "Antarctica/Troll", |
| 202 | + expression: "30 2,3 * * *", |
| 203 | + now: "2026-03-29T00:30:00.000Z", |
| 204 | + next: "2026-03-29T01:30:00.000Z", |
| 205 | + previous: "2026-03-28T03:30:00.000Z", |
| 206 | + }, |
| 207 | + { |
| 208 | + label: "New York skips a nonexistent annual reminder after multiple offset changes", |
| 209 | + timezone: "America/New_York", |
| 210 | + expression: "30 2 14 3 *", |
| 211 | + now: "2026-07-01T00:00:00.000Z", |
| 212 | + next: "2028-03-14T06:30:00.000Z", |
| 213 | + previous: "2026-03-14T06:30:00.000Z", |
| 214 | + }, |
| 215 | + { |
| 216 | + label: "New York skips a nonexistent historical annual reminder", |
| 217 | + timezone: "America/New_York", |
| 218 | + expression: "30 2 8 3 *", |
| 219 | + now: "2026-07-01T00:00:00.000Z", |
| 220 | + next: "2027-03-08T07:30:00.000Z", |
| 221 | + previous: "2025-03-08T07:30:00.000Z", |
| 222 | + }, |
| 223 | + { |
| 224 | + label: "Moscow keeps valid annual reminders beyond multiple historical rule changes", |
| 225 | + timezone: "Europe/Moscow", |
| 226 | + expression: "30 2 * 3 SUN#L", |
| 227 | + now: "2007-03-01T00:00:00.000Z", |
| 228 | + next: "2012-03-24T22:30:00.000Z", |
| 229 | + previous: "1991-03-30T23:30:00.000Z", |
| 230 | + }, |
| 231 | + ])("$label", ({ timezone, expression, now, next, previous }) => { |
| 232 | + const schedule = { kind: "cron" as const, expr: expression, tz: timezone }; |
| 233 | + const nowMs = Date.parse(now); |
| 234 | + |
| 235 | + expect(computeNextRunAtMs(schedule, nowMs)).toBe(Date.parse(next)); |
| 236 | + expect(computePreviousRunAtMs(schedule, nowMs)).toBe(Date.parse(previous)); |
| 237 | + }); |
| 238 | + |
| 239 | + it.each(["30 2 * 3 SUN#2", "0 30 2 * 3 SUN#2"])( |
| 240 | + "skips nonexistent future occurrences but preserves historical timezone rules for %s", |
| 241 | + (expression) => { |
| 242 | + const schedule = { kind: "cron" as const, expr: expression, tz: "America/New_York" }; |
| 243 | + const nowMs = Date.parse("2026-01-01T00:00:00.000Z"); |
| 244 | + |
| 245 | + expect(computeNextRunAtMs(schedule, nowMs)).toBeUndefined(); |
| 246 | + expect(computePreviousRunAtMs(schedule, nowMs)).toBe( |
| 247 | + Date.parse("2006-03-12T07:30:00.000Z"), |
| 248 | + ); |
| 249 | + }, |
| 250 | + ); |
| 251 | + |
| 252 | + it("never spills a nonexistent year-limited reminder into another year", () => { |
| 253 | + const schedule = { |
| 254 | + kind: "cron" as const, |
| 255 | + expr: "0 30 2 14 3 * 2027", |
| 256 | + tz: "America/New_York", |
| 257 | + }; |
| 258 | + const nowMs = Date.parse("2026-07-01T00:00:00.000Z"); |
| 259 | + |
| 260 | + expect(computeNextRunAtMs(schedule, nowMs)).toBeUndefined(); |
| 261 | + expect(computePreviousRunAtMs(schedule, nowMs)).toBeUndefined(); |
| 262 | + }); |
| 263 | + |
| 264 | + it("recovers the first occurrence of a year-limited reminder during its repeated hour", () => { |
| 265 | + const schedule = { |
| 266 | + kind: "cron" as const, |
| 267 | + expr: "0 30 1 1 11 * 2026", |
| 268 | + tz: "America/New_York", |
| 269 | + }; |
| 270 | + const nowMs = Date.parse("2026-11-01T06:15:00.000Z"); |
| 271 | + |
| 272 | + expect(computeNextRunAtMs(schedule, nowMs)).toBeUndefined(); |
| 273 | + expect(computePreviousRunAtMs(schedule, nowMs)).toBe(Date.parse("2026-11-01T05:30:00.000Z")); |
| 274 | + }); |
| 275 | + |
| 276 | + it.each([ |
| 277 | + { |
| 278 | + label: "next-second retry rejects a real spring-forward gap", |
| 279 | + timezone: "America/New_York", |
| 280 | + expression: "30 2 * * *", |
| 281 | + now: "2027-03-14T06:45:00.000Z", |
| 282 | + prior: "2027-03-13T07:30:00.000Z", |
| 283 | + forcedPastCalls: 1, |
| 284 | + expected: "2027-03-15T06:30:00.000Z", |
| 285 | + }, |
| 286 | + { |
| 287 | + label: "next-second retry selects the first real half-hour fold", |
| 288 | + timezone: "Australia/Lord_Howe", |
| 289 | + expression: "45 1 * * *", |
| 290 | + now: "2026-04-04T14:40:00.000Z", |
| 291 | + prior: "2026-04-03T14:45:00.000Z", |
| 292 | + forcedPastCalls: 1, |
| 293 | + expected: "2026-04-04T14:45:00.000Z", |
| 294 | + }, |
| 295 | + { |
| 296 | + label: "tomorrow retry rejects a real spring-forward gap", |
| 297 | + timezone: "America/New_York", |
| 298 | + expression: "30 2 * * *", |
| 299 | + now: "2027-03-13T23:45:00.000Z", |
| 300 | + prior: "2027-03-13T07:30:00.000Z", |
| 301 | + forcedPastCalls: 2, |
| 302 | + expected: "2027-03-15T06:30:00.000Z", |
| 303 | + }, |
| 304 | + { |
| 305 | + label: "tomorrow retry selects the first real half-hour fold", |
| 306 | + timezone: "Australia/Lord_Howe", |
| 307 | + expression: "45 1 * * *", |
| 308 | + now: "2026-04-03T23:45:00.000Z", |
| 309 | + prior: "2026-04-03T14:45:00.000Z", |
| 310 | + forcedPastCalls: 2, |
| 311 | + expected: "2026-04-04T14:45:00.000Z", |
| 312 | + }, |
| 313 | + ])("$label", ({ timezone, expression, now, prior, forcedPastCalls, expected }) => { |
| 314 | + const spy = vi.spyOn(Cron.prototype, "nextRun"); |
| 315 | + for (let count = 0; count < forcedPastCalls; count += 1) { |
| 316 | + spy.mockImplementationOnce(() => new Date(prior)); |
| 317 | + } |
| 318 | + try { |
| 319 | + expect( |
| 320 | + computeNextRunAtMs({ kind: "cron", expr: expression, tz: timezone }, Date.parse(now)), |
| 321 | + ).toBe(Date.parse(expected)); |
| 322 | + } finally { |
| 323 | + spy.mockRestore(); |
| 324 | + } |
| 325 | + }); |
| 326 | + }); |
| 327 | + |
52 | 328 | it("throws a clear error when cron expr is missing at runtime", () => { |
53 | 329 | const nowMs = Date.parse("2025-12-13T00:00:00.000Z"); |
54 | 330 | expect(() => |
|
0 commit comments