Skip to content

Commit 2966f40

Browse files
committed
test(calculate): add spring-forward dst edge case
1 parent 6ede310 commit 2966f40

1 file changed

Lines changed: 31 additions & 18 deletions

File tree

lib/calculate.test.ts

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { describe, it, expect } from 'vitest';
1+
import { describe, it, expect, vi } from 'vitest';
22
import {
33
calculateStreak,
44
calculateMonthlyStats,
5-
isStreakAlive,
65
aggregateCalendars,
76
calculateWrappedStats,
87
} from './calculate';
@@ -332,27 +331,41 @@ describe('calculateStreak — timezone awareness', () => {
332331
const result = calculateStreak(tzCalendar, 'UTC', nowUTC);
333332
expect(result.todayDate).toBe('2024-06-16');
334333
});
335-
});
336334

337-
describe('isStreakAlive', () => {
338-
it('returns true when both today and yesterday have contributions', () => {
339-
expect(isStreakAlive({ contributionCount: 1 }, { contributionCount: 1 })).toBe(true);
340-
});
335+
it('calculates streak correctly during a spring-forward DST transition edge case', () => {
336+
// 1. We must mock the system clock so 'new Date()' behaves predictably
337+
vi.useFakeTimers();
341338

342-
it('returns true when only today has contributions', () => {
343-
expect(isStreakAlive({ contributionCount: 1 }, { contributionCount: 0 })).toBe(true);
344-
});
339+
// 2. Use America/New_York (spring-forward: 2024-03-10)
340+
process.env.TZ = 'America/New_York';
345341

346-
it('returns true when only yesterday has contributions', () => {
347-
expect(isStreakAlive({ contributionCount: 0 }, { contributionCount: 1 })).toBe(true);
348-
});
342+
// 3. Set `now` to early UTC on March 10.
343+
// 03:00:00 UTC on March 10 is 22:00:00 (10:00 PM) on March 9 in New York (EST).
344+
const mockNow = new Date('2024-03-10T03:00:00.000Z');
345+
vi.setSystemTime(mockNow);
349346

350-
it('returns false when both today and yesterday have zero contributions', () => {
351-
expect(isStreakAlive({ contributionCount: 0 }, { contributionCount: 0 })).toBe(false);
352-
});
347+
// 4. Build a calendar with contributions on March 9 and March 10
348+
const dstCalendar = {
349+
totalContributions: 2,
350+
weeks: [
351+
{
352+
contributionDays: [
353+
{ contributionCount: 1, date: '2024-03-09' },
354+
{ contributionCount: 1, date: '2024-03-10' },
355+
],
356+
},
357+
],
358+
} as Parameters<typeof calculateStreak>[0];
359+
360+
// 5. Assert currentStreak is calculated correctly
361+
const result = calculateStreak(dstCalendar, 'America/New_York');
362+
363+
// Because it is currently March 9th in New York, the current streak should securely be 1
364+
expect(result.currentStreak).toBe(1);
353365

354-
it('returns false when yesterday is null and today has no contributions', () => {
355-
expect(isStreakAlive({ contributionCount: 0 }, null)).toBe(false);
366+
// 6. Cleanup to prevent breaking other tests
367+
vi.useRealTimers();
368+
process.env.TZ = '';
356369
});
357370
});
358371

0 commit comments

Comments
 (0)