Skip to content

Commit 432b9c6

Browse files
authored
test(calculate): add test case for commits made exclusively on weekends (JhaSourav07#1773)
## Description Fixes JhaSourav07#1474 ## 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 — Test suite enhancement. No user interface or SVG layout modifications were made. ## 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 7644928 + 40fd0f6 commit 432b9c6

1 file changed

Lines changed: 114 additions & 0 deletions

File tree

lib/calculate.test.ts

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,120 @@ describe('calculateStreak', () => {
374374
expect(resultLeapGap.currentStreak).toBe(1);
375375
expect(resultLeapGap.longestStreak).toBe(1);
376376
});
377+
378+
it('correctly calculates current and longest streaks when commits are made exclusively on Saturdays and Sundays', () => {
379+
// 2024-01-01 is a Monday.
380+
// Days in a week: Mon, Tue, Wed, Thu, Fri, Sat, Sun
381+
// Index: 0, 1, 2, 3, 4, 5, 6
382+
// Commits only on Sat (index 5) and Sun (index 6).
383+
// Week 1: 0, 0, 0, 0, 0, 1, 1 (Sat Jan 6, Sun Jan 7)
384+
// Week 2: 0, 0, 0, 0, 0, 1, 1 (Sat Jan 13, Sun Jan 14)
385+
// Week 3: 0, 0, 0, 0, 0, 1, 1 (Sat Jan 20, Sun Jan 21)
386+
const calendar = buildCalendar([
387+
0,
388+
0,
389+
0,
390+
0,
391+
0,
392+
1,
393+
1, // Week 1 (Jan 1 to Jan 7)
394+
0,
395+
0,
396+
0,
397+
0,
398+
0,
399+
1,
400+
1, // Week 2 (Jan 8 to Jan 14)
401+
0,
402+
0,
403+
0,
404+
0,
405+
0,
406+
1,
407+
1, // Week 3 (Jan 15 to Jan 21)
408+
]);
409+
410+
// 1. Evaluate on Sunday, Jan 21, 2024 (which is the last day with commits)
411+
// The current streak should be 2 (Sat & Sun) because weekdays are empty.
412+
// The longest streak should be 2.
413+
const resultSunday = calculateStreak(calendar, 'UTC', new Date('2024-01-21T12:00:00Z'));
414+
expect(resultSunday.currentStreak).toBe(2);
415+
expect(resultSunday.longestStreak).toBe(2);
416+
417+
// 2. Evaluate on Monday, Jan 22, 2024 (weekdays have no commits, index 21 has 0 commits)
418+
// Let's construct a calendar including Monday Jan 22 so "today" is explicitly present in the data.
419+
const calendarWithMonday = buildCalendar([
420+
0,
421+
0,
422+
0,
423+
0,
424+
0,
425+
1,
426+
1, // Week 1 (Jan 1 to Jan 7)
427+
0,
428+
0,
429+
0,
430+
0,
431+
0,
432+
1,
433+
1, // Week 2 (Jan 8 to Jan 14)
434+
0,
435+
0,
436+
0,
437+
0,
438+
0,
439+
1,
440+
1, // Week 3 (Jan 15 to Jan 21)
441+
0, // Monday, Jan 22 (0 commits)
442+
]);
443+
444+
// Monday (today is 0, yesterday Sunday was 1) - grace period of 1 should keep the streak alive.
445+
// So current streak should still be 2.
446+
const resultMonday = calculateStreak(
447+
calendarWithMonday,
448+
'UTC',
449+
new Date('2024-01-22T12:00:00Z')
450+
);
451+
expect(resultMonday.currentStreak).toBe(2);
452+
expect(resultMonday.longestStreak).toBe(2);
453+
454+
// 3. Evaluate on Tuesday, Jan 23, 2024 (index 22 has 0 commits)
455+
const calendarWithTuesday = buildCalendar([
456+
0,
457+
0,
458+
0,
459+
0,
460+
0,
461+
1,
462+
1, // Week 1 (Jan 1 to Jan 7)
463+
0,
464+
0,
465+
0,
466+
0,
467+
0,
468+
1,
469+
1, // Week 2 (Jan 8 to Jan 14)
470+
0,
471+
0,
472+
0,
473+
0,
474+
0,
475+
1,
476+
1, // Week 3 (Jan 15 to Jan 21)
477+
0,
478+
0, // Monday Jan 22, Tuesday Jan 23 (0 commits)
479+
]);
480+
481+
// Tuesday (today is 0, yesterday Monday is 0) - grace period of 1 cannot keep it alive.
482+
// So current streak resets to 0.
483+
const resultTuesday = calculateStreak(
484+
calendarWithTuesday,
485+
'UTC',
486+
new Date('2024-01-23T12:00:00Z')
487+
);
488+
expect(resultTuesday.currentStreak).toBe(0);
489+
expect(resultTuesday.longestStreak).toBe(2);
490+
});
377491
});
378492

379493
it('handles massive single-day commit spike timeline', () => {

0 commit comments

Comments
 (0)