Skip to content

Commit e1f5bca

Browse files
committed
test(utils): add boundary robustness test for rate limit TTL timers
1 parent 4538996 commit e1f5bca

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

utils/time.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)