Skip to content

Commit b1b0b6c

Browse files
authored
leap streak (JhaSourav07#2021)
## Description This pull request introduces a highly specific unit test suite in `lib/calculate.test.ts` to validate timezone boundaries and calendar offsets during the February 28 to March 1 transition. By comparing leap years (e.g., 2020) and non-leap years (e.g., 2021) with both active and inactive leap days, the new test ensures that streak calculations are resilient against off-by-one errors and gap boundary misalignments. ### Scenarios Covered in the Test: 1. **Non-Leap Year (2021)**: Verifies that a user committing on Feb 28 and Mar 1 achieves a continuous **2-day** streak since Feb 29 does not exist. 2. **Leap Year (2020) with a Missed Leap Day**: Verifies that when Feb 29 has 0 commits, the streak correctly breaks, resulting in a current and longest streak of **1 day** on Mar 1. 3. **Leap Year (2020) with an Active Leap Day**: Verifies that committing on Feb 28, Feb 29, and Mar 1 results in a seamless **3-day** streak. fixes JhaSourav07#1501 ## Pillar - [ ] 🎨 Pillar 1 — New Theme Design - [ ] 📐 Pillar 2 — Geometric SVG Improvement - [x] 🕐 Pillar 3 — Timezone Logic Optimization - [x] 🛠️ Other (Unit testing) ## Visual Preview *(N/A - This is a pure logic & unit testing addition)* ## 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 starred 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 628af17 + ec451b5 commit b1b0b6c

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

lib/calculate.test.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,70 @@ describe('calculateStreak', () => {
501501
expect(resultLeapGap.longestStreak).toBe(1);
502502
});
503503

504+
it('handles leap years vs non-leap years Feb 28 to Mar 1 timeline and asserts correct current/longest streaks', () => {
505+
const buildCustomCalendar = (
506+
daysData: { date: string; count: number }[]
507+
): ContributionCalendar => {
508+
const weeks = [];
509+
for (let i = 0; i < daysData.length; i += 7) {
510+
const slice = daysData.slice(i, i + 7);
511+
weeks.push({
512+
contributionDays: slice.map((day) => ({
513+
contributionCount: day.count,
514+
date: day.date,
515+
})),
516+
});
517+
}
518+
return {
519+
totalContributions: daysData.reduce((sum, d) => sum + d.count, 0),
520+
weeks,
521+
};
522+
};
523+
524+
// 1. Non-Leap Year (2021): Feb 28 to Mar 1
525+
// Calendar doesn't have Feb 29.
526+
const nonLeapCalendar = buildCustomCalendar([
527+
{ date: '2021-02-28', count: 1 },
528+
{ date: '2021-03-01', count: 1 },
529+
]);
530+
531+
const resultNonLeap = calculateStreak(nonLeapCalendar, 'UTC', new Date('2021-03-01T12:00:00Z'));
532+
expect(resultNonLeap.currentStreak).toBe(2);
533+
expect(resultNonLeap.longestStreak).toBe(2);
534+
535+
// 2. Leap Year (2020) with missed leap day (Feb 29 has 0 commits)
536+
const leapCalendarWithGap = buildCustomCalendar([
537+
{ date: '2020-02-28', count: 1 },
538+
{ date: '2020-02-29', count: 0 },
539+
{ date: '2020-03-01', count: 1 },
540+
]);
541+
542+
const resultLeapGap = calculateStreak(
543+
leapCalendarWithGap,
544+
'UTC',
545+
new Date('2020-03-01T12:00:00Z')
546+
);
547+
// Since Feb 29 has 0 commits, the streak of consecutive active days is broken.
548+
// However, grace period = 1 keeps current streak alive but only for the continuous active days ending today (Mar 1).
549+
expect(resultLeapGap.currentStreak).toBe(1);
550+
expect(resultLeapGap.longestStreak).toBe(1);
551+
552+
// 3. Leap Year (2020) with active leap day (Feb 29 has 1 commit)
553+
const leapCalendarContinuous = buildCustomCalendar([
554+
{ date: '2020-02-28', count: 1 },
555+
{ date: '2020-02-29', count: 1 },
556+
{ date: '2020-03-01', count: 1 },
557+
]);
558+
559+
const resultLeapContinuous = calculateStreak(
560+
leapCalendarContinuous,
561+
'UTC',
562+
new Date('2020-03-01T12:00:00Z')
563+
);
564+
expect(resultLeapContinuous.currentStreak).toBe(3);
565+
expect(resultLeapContinuous.longestStreak).toBe(3);
566+
});
567+
504568
it('correctly calculates current and longest streaks when commits are made exclusively on Saturdays and Sundays', () => {
505569
// 2024-01-01 is a Monday.
506570
// Days in a week: Mon, Tue, Wed, Thu, Fri, Sat, Sun

0 commit comments

Comments
 (0)