1
1
import { describe , test , expect , beforeEach , afterEach , vi } from "vitest" ;
2
- import { calculateNextScheduledTimestamp } from "../app/v3/utils/calculateNextSchedule.server" ;
2
+ import { calculateNextScheduledTimestampFromNow } from "../app/v3/utils/calculateNextSchedule.server" ;
3
3
4
- describe ( "calculateNextScheduledTimestamp " , ( ) => {
4
+ describe ( "calculateNextScheduledTimestampFromNow " , ( ) => {
5
5
beforeEach ( ( ) => {
6
6
// Mock the current time to make tests deterministic
7
7
vi . useFakeTimers ( ) ;
@@ -16,7 +16,7 @@ describe("calculateNextScheduledTimestamp", () => {
16
16
const schedule = "0 * * * *" ; // Every hour
17
17
const lastRun = new Date ( "2024-01-01T11:00:00.000Z" ) ; // 1.5 hours ago
18
18
19
- const nextRun = calculateNextScheduledTimestamp ( schedule , null , lastRun ) ;
19
+ const nextRun = calculateNextScheduledTimestampFromNow ( schedule , null ) ;
20
20
21
21
// Should be 13:00 (next hour after current time 12:30)
22
22
expect ( nextRun ) . toEqual ( new Date ( "2024-01-01T13:00:00.000Z" ) ) ;
@@ -26,7 +26,7 @@ describe("calculateNextScheduledTimestamp", () => {
26
26
const schedule = "0 * * * *" ; // Every hour
27
27
const lastRun = new Date ( "2024-01-01T11:00:00.000Z" ) ;
28
28
29
- const nextRun = calculateNextScheduledTimestamp ( schedule , "America/New_York" , lastRun ) ;
29
+ const nextRun = calculateNextScheduledTimestampFromNow ( schedule , "America/New_York" ) ;
30
30
31
31
// The exact time will depend on timezone calculation, but should be in the future
32
32
expect ( nextRun ) . toBeInstanceOf ( Date ) ;
@@ -38,7 +38,7 @@ describe("calculateNextScheduledTimestamp", () => {
38
38
const veryOldTimestamp = new Date ( "2020-01-01T00:00:00.000Z" ) ; // 4 years ago
39
39
40
40
const startTime = performance . now ( ) ;
41
- const nextRun = calculateNextScheduledTimestamp ( schedule , null , veryOldTimestamp ) ;
41
+ const nextRun = calculateNextScheduledTimestampFromNow ( schedule , null ) ;
42
42
const duration = performance . now ( ) - startTime ;
43
43
44
44
// Should complete quickly (under 10ms) instead of iterating millions of times
@@ -55,7 +55,7 @@ describe("calculateNextScheduledTimestamp", () => {
55
55
const schedule = "0 */2 * * *" ; // Every 2 hours
56
56
const recentTimestamp = new Date ( "2024-01-01T10:00:00.000Z" ) ; // 2.5 hours ago
57
57
58
- const nextRun = calculateNextScheduledTimestamp ( schedule , null , recentTimestamp ) ;
58
+ const nextRun = calculateNextScheduledTimestampFromNow ( schedule , null ) ;
59
59
60
60
// Should properly iterate: 10:00 -> 12:00 -> 14:00 (since current time is 12:30)
61
61
expect ( nextRun ) . toEqual ( new Date ( "2024-01-01T14:00:00.000Z" ) ) ;
@@ -66,7 +66,7 @@ describe("calculateNextScheduledTimestamp", () => {
66
66
const oldTimestamp = new Date ( "2023-12-01T00:00:00.000Z" ) ; // Over a month ago
67
67
68
68
const startTime = performance . now ( ) ;
69
- const nextRun = calculateNextScheduledTimestamp ( schedule , null , oldTimestamp ) ;
69
+ const nextRun = calculateNextScheduledTimestampFromNow ( schedule , null ) ;
70
70
const duration = performance . now ( ) - startTime ;
71
71
72
72
// Should be fast due to dynamic skip-ahead optimization
@@ -80,7 +80,7 @@ describe("calculateNextScheduledTimestamp", () => {
80
80
const schedule = "0 9 * * MON" ; // Every Monday at 9 AM
81
81
const oldTimestamp = new Date ( "2022-01-01T00:00:00.000Z" ) ; // Very old (beyond 1hr threshold)
82
82
83
- const nextRun = calculateNextScheduledTimestamp ( schedule , null , oldTimestamp ) ;
83
+ const nextRun = calculateNextScheduledTimestampFromNow ( schedule , null ) ;
84
84
85
85
// Should return a valid future Monday at 9 AM
86
86
expect ( nextRun . getHours ( ) ) . toBe ( 9 ) ;
@@ -95,7 +95,7 @@ describe("calculateNextScheduledTimestamp", () => {
95
95
const extremelyOldTimestamp = new Date ( "2000-01-01T00:00:00.000Z" ) ; // 24 years ago
96
96
97
97
const startTime = performance . now ( ) ;
98
- const nextRun = calculateNextScheduledTimestamp ( schedule , null , extremelyOldTimestamp ) ;
98
+ const nextRun = calculateNextScheduledTimestampFromNow ( schedule , null ) ;
99
99
const duration = performance . now ( ) - startTime ;
100
100
101
101
// Should complete extremely quickly due to dynamic skip-ahead
@@ -111,7 +111,7 @@ describe("calculateNextScheduledTimestamp", () => {
111
111
const oldTimestamp = new Date ( "2023-12-31T12:31:00.000Z" ) ; // 23h59m ago
112
112
113
113
const startTime = performance . now ( ) ;
114
- const nextRun = calculateNextScheduledTimestamp ( schedule , null , oldTimestamp ) ;
114
+ const nextRun = calculateNextScheduledTimestampFromNow ( schedule , null ) ;
115
115
const duration = performance . now ( ) - startTime ;
116
116
117
117
// Should be fast due to dynamic skip-ahead (1439 steps > 10 threshold)
@@ -127,7 +127,7 @@ describe("calculateNextScheduledTimestamp", () => {
127
127
const recentTimestamp = new Date ( "2024-01-01T12:00:00.000Z" ) ; // 30 minutes ago (6 steps)
128
128
129
129
const startTime = performance . now ( ) ;
130
- const nextRun = calculateNextScheduledTimestamp ( schedule , null , recentTimestamp ) ;
130
+ const nextRun = calculateNextScheduledTimestampFromNow ( schedule , null ) ;
131
131
const duration = performance . now ( ) - startTime ;
132
132
133
133
// Should still be reasonably fast with normal iteration
@@ -142,7 +142,7 @@ describe("calculateNextScheduledTimestamp", () => {
142
142
const oldTimestamp = new Date ( "2023-12-25T09:00:00.000Z" ) ; // Old Monday
143
143
144
144
const startTime = performance . now ( ) ;
145
- const nextRun = calculateNextScheduledTimestamp ( schedule , null , oldTimestamp ) ;
145
+ const nextRun = calculateNextScheduledTimestampFromNow ( schedule , null ) ;
146
146
const duration = performance . now ( ) - startTime ;
147
147
148
148
// Should be fast and still calculate correctly from the old timestamp
@@ -160,7 +160,7 @@ describe("calculateNextScheduledTimestamp", () => {
160
160
const schedule = "0 14 * * SUN" ; // Every Sunday at 2 PM
161
161
const twoHoursAgo = new Date ( "2024-01-01T10:30:00.000Z" ) ; // 2 hours before current time (12:30)
162
162
163
- const nextRun = calculateNextScheduledTimestamp ( schedule , null , twoHoursAgo ) ;
163
+ const nextRun = calculateNextScheduledTimestampFromNow ( schedule , null ) ;
164
164
165
165
// Should properly calculate the next Sunday at 2 PM, not skip to "now"
166
166
expect ( nextRun . getHours ( ) ) . toBe ( 14 ) ;
@@ -170,7 +170,7 @@ describe("calculateNextScheduledTimestamp", () => {
170
170
} ) ;
171
171
} ) ;
172
172
173
- describe ( "calculateNextScheduledTimestamp - Fuzzy Testing" , ( ) => {
173
+ describe ( "calculateNextScheduledTimestampFromNow - Fuzzy Testing" , ( ) => {
174
174
beforeEach ( ( ) => {
175
175
vi . useFakeTimers ( ) ;
176
176
vi . setSystemTime ( new Date ( "2024-01-15T12:30:00.000Z" ) ) ; // Monday, mid-day
@@ -254,7 +254,7 @@ describe("calculateNextScheduledTimestamp - Fuzzy Testing", () => {
254
254
255
255
try {
256
256
const startTime = performance . now ( ) ;
257
- const nextRun = calculateNextScheduledTimestamp ( schedule , timezone , lastTimestamp ) ;
257
+ const nextRun = calculateNextScheduledTimestampFromNow ( schedule , timezone ) ;
258
258
const duration = performance . now ( ) - startTime ;
259
259
260
260
// Invariant 1: Result should always be a valid Date
@@ -270,7 +270,7 @@ describe("calculateNextScheduledTimestamp - Fuzzy Testing", () => {
270
270
expect ( duration ) . toBeLessThan ( 100 ) ; // Should complete within 100ms
271
271
272
272
// Invariant 4: Function should be deterministic
273
- const nextRun2 = calculateNextScheduledTimestamp ( schedule , timezone , lastTimestamp ) ;
273
+ const nextRun2 = calculateNextScheduledTimestampFromNow ( schedule , timezone ) ;
274
274
expect ( nextRun . getTime ( ) ) . toBe ( nextRun2 . getTime ( ) ) ;
275
275
} catch ( error ) {
276
276
// If there's an error, log the inputs for debugging
@@ -292,7 +292,7 @@ describe("calculateNextScheduledTimestamp - Fuzzy Testing", () => {
292
292
const veryOldTimestamp = new Date ( Date . now ( ) - Math . random ( ) * 5 * 365 * 24 * 60 * 60 * 1000 ) ;
293
293
294
294
const startTime = performance . now ( ) ;
295
- const nextRun = calculateNextScheduledTimestamp ( schedule , null , veryOldTimestamp ) ;
295
+ const nextRun = calculateNextScheduledTimestampFromNow ( schedule , null ) ;
296
296
const duration = performance . now ( ) - startTime ;
297
297
298
298
// Should complete quickly even with very old timestamps
@@ -321,7 +321,7 @@ describe("calculateNextScheduledTimestamp - Fuzzy Testing", () => {
321
321
322
322
const lastTimestamp = new Date ( Date . now ( ) - Math . random ( ) * 7 * 24 * 60 * 60 * 1000 ) ;
323
323
324
- const nextRun = calculateNextScheduledTimestamp ( schedule , timezone , lastTimestamp ) ;
324
+ const nextRun = calculateNextScheduledTimestampFromNow ( schedule , timezone ) ;
325
325
326
326
// Should handle DST transitions gracefully
327
327
expect ( nextRun ) . toBeInstanceOf ( Date ) ;
@@ -354,8 +354,8 @@ describe("calculateNextScheduledTimestamp - Fuzzy Testing", () => {
354
354
const beforeBoundary = new Date ( Date . now ( ) - 1000 ) ;
355
355
const afterBoundary = new Date ( Date . now ( ) + 1000 ) ;
356
356
357
- const nextRun1 = calculateNextScheduledTimestamp ( test . schedule , null , beforeBoundary ) ;
358
- const nextRun2 = calculateNextScheduledTimestamp ( test . schedule , null , afterBoundary ) ;
357
+ const nextRun1 = calculateNextScheduledTimestampFromNow ( test . schedule , null ) ;
358
+ const nextRun2 = calculateNextScheduledTimestampFromNow ( test . schedule , null ) ;
359
359
360
360
expect ( nextRun1 . getTime ( ) ) . toBeGreaterThan ( Date . now ( ) ) ;
361
361
expect ( nextRun2 . getTime ( ) ) . toBeGreaterThan ( Date . now ( ) ) ;
@@ -378,7 +378,7 @@ describe("calculateNextScheduledTimestamp - Fuzzy Testing", () => {
378
378
379
379
try {
380
380
const startTime = performance . now ( ) ;
381
- const nextRun = calculateNextScheduledTimestamp ( schedule , null , lastTimestamp ) ;
381
+ const nextRun = calculateNextScheduledTimestampFromNow ( schedule , null ) ;
382
382
const duration = performance . now ( ) - startTime ;
383
383
384
384
expect ( nextRun ) . toBeInstanceOf ( Date ) ;
@@ -409,7 +409,7 @@ describe("calculateNextScheduledTimestamp - Fuzzy Testing", () => {
409
409
410
410
const results : Date [ ] = [ ] ;
411
411
for ( let j = 0 ; j < 5 ; j ++ ) {
412
- results . push ( calculateNextScheduledTimestamp ( schedule , timezone , lastTimestamp ) ) ;
412
+ results . push ( calculateNextScheduledTimestampFromNow ( schedule , timezone ) ) ;
413
413
}
414
414
415
415
// All results should be identical
@@ -436,7 +436,7 @@ describe("calculateNextScheduledTimestamp - Fuzzy Testing", () => {
436
436
const lastTimestamp = new Date ( Date . now ( ) - testCase . minutesAgo * 60 * 1000 ) ;
437
437
438
438
const startTime = performance . now ( ) ;
439
- const nextRun = calculateNextScheduledTimestamp ( testCase . schedule , null , lastTimestamp ) ;
439
+ const nextRun = calculateNextScheduledTimestampFromNow ( testCase . schedule , null ) ;
440
440
const duration = performance . now ( ) - startTime ;
441
441
442
442
// All cases should complete quickly and return valid results
0 commit comments