@@ -152,3 +152,31 @@ describe('getSecondsUntilMidnightInTimezone', () => {
152152 expect ( getSecondsUntilMidnightInTimezone ( 'Etc/GMT-14' ) ) . toBe ( 10 * 3600 ) ;
153153 } ) ;
154154} ) ;
155+
156+ describe ( 'getSecondsUntilUTCMidnight — sliding window boundary robustness' , ( ) => {
157+ beforeEach ( ( ) => {
158+ vi . useFakeTimers ( ) ;
159+ } ) ;
160+
161+ afterEach ( ( ) => {
162+ vi . useRealTimers ( ) ;
163+ } ) ;
164+
165+ it ( 'returns correct TTL across a sliding window of times approaching UTC midnight' , ( ) => {
166+ // Verifies that the utility guarantees keys expire exactly at the window limit.
167+ // Each entry is [UTC time string, expected seconds until next midnight].
168+ const cases : [ string , number ] [ ] = [
169+ [ '2024-06-15T23:00:00.000Z' , 3600 ] , // 1 hour before midnight
170+ [ '2024-06-15T23:30:00.000Z' , 1800 ] , // 30 minutes before midnight
171+ [ '2024-06-15T23:45:00.000Z' , 900 ] , // 15 minutes before midnight
172+ [ '2024-06-15T23:59:00.000Z' , 60 ] , // 1 minute before midnight
173+ [ '2024-06-15T23:59:59.000Z' , 1 ] , // 1 second before midnight
174+ [ '2024-06-16T00:00:00.000Z' , 86400 ] , // exactly at midnight, resets to full day
175+ ] ;
176+
177+ for ( const [ time , expected ] of cases ) {
178+ vi . setSystemTime ( new Date ( time ) ) ;
179+ expect ( getSecondsUntilUTCMidnight ( ) ) . toBe ( expected ) ;
180+ }
181+ } ) ;
182+ } ) ;
0 commit comments