1- import { describe , it , expect , beforeEach , afterEach } from 'vitest' ;
1+ import { describe , it , expect , beforeEach , afterEach , vi } from 'vitest' ;
22import { addDays } from 'date-fns' ;
33
44import { setupServer , superRequest } from '../../../vitest.utils.js' ;
5- import { getNowUsCentral , getUtcMidnight } from '../utils/helpers.js' ;
65
76function dateToDateParam ( date : Date ) : string {
87 return date . toISOString ( ) . split ( 'T' ) [ 0 ] as string ;
98}
109
11- const todayUsCentral = getNowUsCentral ( ) ;
12- const todayUtcMidnight = getUtcMidnight ( todayUsCentral ) ;
10+ const todayUsCentral = new Date ( Date . UTC ( 2025 , 9 , 2 , 5 ) ) ; // 2025-10-02 00:00:00 in US Central
11+ const todayUtcMidnight = new Date ( Date . UTC ( 2025 , 9 , 2 , 0 , 0 , 0 ) ) ;
12+
1313const todayDateParam = dateToDateParam ( todayUtcMidnight ) ;
1414
15- const yesterdayUsCentral = addDays ( todayUsCentral , - 1 ) ;
16- const yesterdayUtcMidnight = getUtcMidnight ( yesterdayUsCentral ) ;
15+ const yesterdayUtcMidnight = addDays ( todayUtcMidnight , - 1 ) ;
1716
18- const twoDaysAgoUsCentral = addDays ( todayUsCentral , - 2 ) ;
19- const twoDaysAgoUtcMidnight = getUtcMidnight ( twoDaysAgoUsCentral ) ;
17+ const twoDaysAgoUtcMidnight = addDays ( todayUtcMidnight , - 2 ) ;
2018const twoDaysAgoDateParam = dateToDateParam ( twoDaysAgoUtcMidnight ) ;
2119
22- const tomorrowUsCentral = addDays ( todayUsCentral , 1 ) ;
23- const tomorrowUtcMidnight = getUtcMidnight ( tomorrowUsCentral ) ;
20+ const tomorrowUtcMidnight = addDays ( todayUtcMidnight , 1 ) ;
2421const tomorrowDateParam = dateToDateParam ( tomorrowUtcMidnight ) ;
2522
2623const yesterdaysChallenge = {
@@ -79,6 +76,13 @@ const mockChallenges = [
7976
8077describe ( '/daily-coding-challenge' , ( ) => {
8178 setupServer ( ) ;
79+ // This has to happen after setupServer since it needs real timers.
80+ beforeEach ( ( ) => {
81+ vi . useFakeTimers ( { now : todayUsCentral } ) ;
82+ } ) ;
83+ afterEach ( ( ) => {
84+ vi . useRealTimers ( ) ;
85+ } ) ;
8286
8387 describe ( 'GET /daily-coding-challenge/date/:date' , ( ) => {
8488 beforeEach ( async ( ) => {
@@ -229,18 +233,10 @@ describe('/daily-coding-challenge', () => {
229233 }
230234 } ) ;
231235
232- it ( 'should return { id, date, challengeNumber, title } for all available challenges of the given month up to today US Central' , async ( ) => {
233- const currentMonth = todayUsCentral . toISOString ( ) . slice ( 0 , 7 ) ;
234-
235- const res = await superRequest (
236- `/daily-coding-challenge/month/${ currentMonth } ` ,
237- {
238- method : 'GET'
239- }
240- ) . send ( { } ) ;
241-
242- expect ( res . status ) . toBe ( 200 ) ;
243- expect ( Array . isArray ( res . body ) ) . toBe ( true ) ;
236+ it ( 'should return two challenges on the second day of the month' , async ( ) => {
237+ const res = await superRequest ( `/daily-coding-challenge/month/2025-10` , {
238+ method : 'GET'
239+ } ) . send ( { } ) ;
244240
245241 // Should include yesterday's and today's challenges, but not tomorrow's
246242 const expectedResponse = [
@@ -258,8 +254,29 @@ describe('/daily-coding-challenge', () => {
258254 }
259255 ] ;
260256
261- expect ( res . body ) . toHaveLength ( 2 ) ;
262257 expect ( res . body ) . toEqual ( expectedResponse ) ;
258+ expect ( res . status ) . toBe ( 200 ) ;
259+ } ) ;
260+
261+ it ( 'should return one challenge on the first day of the month' , async ( ) => {
262+ vi . setSystemTime ( new Date ( Date . UTC ( 2025 , 9 , 1 , 5 ) ) ) ; // 2025-10-01 00:00:00 in US Central
263+
264+ const res = await superRequest ( `/daily-coding-challenge/month/2025-10` , {
265+ method : 'GET'
266+ } ) . send ( { } ) ;
267+
268+ // Should include yesterday's challenges
269+ const expectedResponse = [
270+ {
271+ id : yesterdaysChallenge . id ,
272+ challengeNumber : yesterdaysChallenge . challengeNumber ,
273+ date : yesterdaysChallenge . date . toISOString ( ) ,
274+ title : yesterdaysChallenge . title
275+ }
276+ ] ;
277+
278+ expect ( res . body ) . toEqual ( expectedResponse ) ;
279+ expect ( res . status ) . toBe ( 200 ) ;
263280 } ) ;
264281
265282 it ( 'should return 404 when no challenges exist for the given month' , async ( ) => {
0 commit comments