Skip to content

Commit cb3721f

Browse files
authored
weekend day streak (JhaSourav07#2014)
## Description Adds a new test case to `lib/calculate.test.ts` to simulate commits made exclusively on Saturdays and Sundays. This test verifies that the streak calculator computes the correct current and longest streaks under this specific pattern and validates the grace period boundary behavior (e.g., keeping the streak active on Monday but resetting it on Tuesday). This helps prevent regression issues and off-by-one errors in calendar offsets and date boundary calculations. ##fixes JhaSourav07#1504 ## Pillar - [ ] 🎨 Pillar 1 — New Theme Design - [ ] 📐 Pillar 2 — Geometric SVG Improvement - [ ] Pillar 3 — Timezone Logic Optimization - [x] 🛠️ Other (Bug fix, refactoring, docs / Testing) ## Visual Preview *(N/A - This change only affects the test suite and has no impact on visual SVG outputs)* ## Checklist before requesting a review: - [x] I have read the `CONTRIBUTING.md` file. - [x] I have tested these changes locally (`npm test` passes successfully). - [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., `test(calculate): add test case for weekend-only commits`). - [x] I have made sure that i have only one commit to merge in this PR.
2 parents e85dcd5 + 3159f13 commit cb3721f

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

lib/calculate.test.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,79 @@ describe('calculateStreak', () => {
737737
expect(result.longestStreak).toBe(1);
738738
expect(result.totalContributions).toBe(1);
739739
});
740+
741+
it('simulates a timeline with commits exclusively on Saturdays and Sundays and verifies streak metrics', () => {
742+
// 2024-01-01 is a Monday.
743+
// Index: 0 (Mon), 1 (Tue), 2 (Wed), 3 (Thu), 4 (Fri), 5 (Sat), 6 (Sun)
744+
// Custom calendar with commits ONLY on Sat and Sun:
745+
// Week 1: 0, 0, 0, 0, 0, 1, 1 (Sat Jan 6, Sun Jan 7)
746+
// Week 2: 0, 0, 0, 0, 0, 1, 1 (Sat Jan 13, Sun Jan 14)
747+
// Week 3: 0, 0, 0, 0, 0, 1, 1 (Sat Jan 20, Sun Jan 21)
748+
const calendar = buildCalendar([
749+
0,
750+
0,
751+
0,
752+
0,
753+
0,
754+
1,
755+
1, // Week 1 (Jan 1 to Jan 7)
756+
0,
757+
0,
758+
0,
759+
0,
760+
0,
761+
1,
762+
1, // Week 2 (Jan 8 to Jan 14)
763+
0,
764+
0,
765+
0,
766+
0,
767+
0,
768+
1,
769+
1, // Week 3 (Jan 15 to Jan 21)
770+
]);
771+
772+
// Scenario A: Evaluate on Sunday, Jan 21, 2024.
773+
// Sunday has a commit, so current streak is 2 (Sat Jan 20 & Sun Jan 21).
774+
// Longest streak is 2.
775+
const resultSunday = calculateStreak(calendar, 'UTC', new Date('2024-01-21T12:00:00Z'));
776+
expect(resultSunday.currentStreak).toBe(2);
777+
expect(resultSunday.longestStreak).toBe(2);
778+
expect(resultSunday.totalContributions).toBe(6);
779+
780+
// Scenario B: Evaluate on Monday, Jan 22, 2024 (using extended calendar).
781+
// We add Monday (index 21) with 0 commits.
782+
const extendedCalendar = buildCalendar([
783+
0,
784+
0,
785+
0,
786+
0,
787+
0,
788+
1,
789+
1,
790+
0,
791+
0,
792+
0,
793+
0,
794+
0,
795+
1,
796+
1,
797+
0,
798+
0,
799+
0,
800+
0,
801+
0,
802+
1,
803+
1,
804+
0, // Monday, Jan 22 (0 commits)
805+
]);
806+
// Today (Monday) has 0 commits, yesterday (Sunday) has 1 commit.
807+
// Under a grace period of 1, the streak is kept alive.
808+
// Current streak should be 2. Longest streak is 2.
809+
const resultMonday = calculateStreak(extendedCalendar, 'UTC', new Date('2024-01-22T12:00:00Z'));
810+
expect(resultMonday.currentStreak).toBe(2);
811+
expect(resultMonday.longestStreak).toBe(2);
812+
});
740813
});
741814

742815
describe('calculateStreak — timezone awareness', () => {

0 commit comments

Comments
 (0)