Skip to content

Commit 17e3f28

Browse files
authored
test(utils): check boundary robustness of time normalization (Variati… (JhaSourav07#1645)
…on 3) ## Description test(utils): check boundary robustness of time normalization (Variation 3) Fixes JhaSourav07#1569 ## Pillar - [ ] 🎨 Pillar 1 — New Theme Design - [ ] 📐 Pillar 2 — Geometric SVG Improvement - [ ] 🕐 Pillar 3 — Timezone Logic Optimization - [x] 🛠️ Other (Bug fix, refactoring, docs) ## Visual Preview ## Checklist before requesting a review: - [x] I have read the `CONTRIBUTING.md` file. - [x] I have tested these changes locally (`localhost:3000/api/streak?user=YOUR_USERNAME`). - [x] I have run `npm run format` and `npm run lint` locally and resolved all errors (CI will fail otherwise). - [x] My commits follow the Conventional Commits format (e.g., `feat(themes): ...`, `fix(calculate): ...`). - [x] I have updated `README.md` if I added a new theme or URL parameter. - [x] I have started the repo. - [x] I have made sure that i have only one commit to merge in this PR. - [x] The SVG output matches the CommitPulse "premium quality" aesthetic standard (no raw elements, smooth animations, correct fonts). - [x] (Recommended) I joined the CommitPulse Discord community for contributor discussions, mentorship, and faster PR support.
2 parents 33e9777 + 8fae94f commit 17e3f28

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

components/ReturnToTop.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { render } from '@testing-library/react';
2-
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2+
import { describe, it, expect, vi, beforeEach } from 'vitest';
33
import ReturnToTop from './ReturnToTop';
44
import type React from 'react';
55
import { screen, fireEvent } from '@testing-library/react';

utils/time.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,59 @@ describe('getSecondsUntilMidnightInTimezone', () => {
151151

152152
expect(getSecondsUntilMidnightInTimezone('Etc/GMT-14')).toBe(10 * 3600);
153153
});
154+
155+
it('should calculate seconds until UTC midnight correctly at a calendar boundary', () => {
156+
// Arrange: Set time to Dec 31, 2023, 23:59:50 UTC (10 seconds before New Year)
157+
const boundaryTime = new Date(Date.UTC(2023, 11, 31, 23, 59, 50));
158+
vi.setSystemTime(boundaryTime);
159+
160+
// Act
161+
const seconds = getSecondsUntilUTCMidnight();
162+
163+
// Assert: Exactly 10 seconds should remain
164+
expect(seconds).toBe(10);
165+
});
166+
167+
it('should handle extreme positive timezone offset boundary (+14:00)', () => {
168+
// Arrange: Pacific/Kiritimati is UTC+14.
169+
// When UTC is Dec 31, 09:59:50, Kiritimati is Dec 31, 23:59:50 (10 seconds to midnight)
170+
const boundaryTime = new Date(Date.UTC(2023, 11, 31, 9, 59, 50));
171+
vi.setSystemTime(boundaryTime);
172+
173+
// Act
174+
const seconds = getSecondsUntilMidnightInTimezone('Pacific/Kiritimati');
175+
176+
// Assert
177+
expect(seconds).toBe(10);
178+
});
179+
180+
it('should handle extreme negative timezone offset boundary (-11:00)', () => {
181+
// Arrange: Pacific/Midway is UTC-11.
182+
// When UTC is Jan 1, 10:59:50, Midway is Dec 31, 23:59:50 (10 seconds to midnight)
183+
const boundaryTime = new Date(Date.UTC(2024, 0, 1, 10, 59, 50));
184+
vi.setSystemTime(boundaryTime);
185+
186+
// Act
187+
const seconds = getSecondsUntilMidnightInTimezone('Pacific/Midway');
188+
189+
// Assert
190+
expect(seconds).toBe(10);
191+
});
192+
193+
it('should handle exact midnight boundary gracefully (0 seconds remaining)', () => {
194+
// Arrange: Exactly midnight in UTC
195+
const midnightTime = new Date(Date.UTC(2024, 0, 1, 0, 0, 0));
196+
vi.setSystemTime(midnightTime);
197+
198+
// Act
199+
const secondsUTC = getSecondsUntilUTCMidnight();
200+
const secondsLondon = getSecondsUntilMidnightInTimezone('Europe/London');
201+
202+
// Assert: Both should recognize it's 24 hours (86400 seconds) until the NEXT midnight
203+
// Since the logic does `86400 - (hour * 3600 + ...)`, at 00:00:00 it returns 86400.
204+
expect(secondsUTC).toBe(86400);
205+
expect(secondsLondon).toBe(86400);
206+
});
154207
});
155208

156209
describe('getSecondsUntilUTCMidnight — sliding window boundary robustness', () => {

0 commit comments

Comments
 (0)