|
1 | | -import { describe, it, expect } from 'vitest'; |
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
2 | 2 | import { |
3 | 3 | calculateStreak, |
4 | 4 | calculateMonthlyStats, |
5 | | - isStreakAlive, |
6 | 5 | aggregateCalendars, |
7 | 6 | calculateWrappedStats, |
8 | 7 | } from './calculate'; |
@@ -332,27 +331,41 @@ describe('calculateStreak — timezone awareness', () => { |
332 | 331 | const result = calculateStreak(tzCalendar, 'UTC', nowUTC); |
333 | 332 | expect(result.todayDate).toBe('2024-06-16'); |
334 | 333 | }); |
335 | | -}); |
336 | 334 |
|
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(); |
341 | 338 |
|
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'; |
345 | 341 |
|
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); |
349 | 346 |
|
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); |
353 | 365 |
|
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 = ''; |
356 | 369 | }); |
357 | 370 | }); |
358 | 371 |
|
|
0 commit comments