Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions utils/dateHelpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,54 +68,47 @@ describe('dateHelpers', () => {
});

it('returns zero metrics for an array containing only Invalid Date strings', () => {
// Removed Z here
const result = processCommitTimestamps(['2024-13-99T25:99:00', 'hello world']);
const result = processCommitTimestamps(['2024-13-99T25:99:00Z', 'hello world']);
expect(result).toEqual({ morning: 0, afternoon: 0, evening: 0, night: 0 });
});

it('counts valid morning commits correctly', () => {
// Removed Z from both strings
const result = processCommitTimestamps(['2024-03-10T09:00:00', '2024-03-10T11:30:00']);
const result = processCommitTimestamps(['2024-03-10T09:00:00Z', '2024-03-10T11:30:00Z']);
expect(result.morning).toBe(2);
expect(result.afternoon).toBe(0);
expect(result.evening).toBe(0);
expect(result.night).toBe(0);
});

it('counts valid afternoon commits correctly', () => {
// Removed Z from both strings
const result = processCommitTimestamps(['2024-03-10T12:00:00', '2024-03-10T17:59:00']);
const result = processCommitTimestamps(['2024-03-10T12:00:00Z', '2024-03-10T17:59:00Z']);
expect(result.morning).toBe(0);
expect(result.afternoon).toBe(2);
});

it('counts valid evening commits correctly', () => {
// Removed Z from both strings
const result = processCommitTimestamps(['2024-03-10T18:00:00', '2024-03-10T23:59:00']);
const result = processCommitTimestamps(['2024-03-10T18:00:00Z', '2024-03-10T23:59:00Z']);
expect(result.evening).toBe(2);
});

it('counts valid night commits correctly', () => {
// Removed Z from both strings
const result = processCommitTimestamps(['2024-03-10T00:00:00', '2024-03-10T05:59:00']);
const result = processCommitTimestamps(['2024-03-10T00:00:00Z', '2024-03-10T05:59:00Z']);
expect(result.night).toBe(2);
});

it('ignores invalid dates while counting valid ones', () => {
// Removed Z from strings
const result = processCommitTimestamps([
'2024-03-10T09:00:00',
'2024-03-10T09:00:00Z',
'invalid-date',
'2024-03-10T14:00:00',
'2024-03-10T14:00:00Z',
]);
expect(result.morning).toBe(1);
expect(result.afternoon).toBe(1);
expect(result.night).toBe(0);
expect(result.evening).toBe(0);
});

// Regression test: verifies timezone-agnostic behavior.
// This test would fail with getHours() in IST but passes with getUTCHours().
// Regression test: verifies timezone-agnostic behavior across system timezones.
it('produces consistent results regardless of system timezone', () => {
const timestamps = [
'2024-03-10T09:00:00Z', // 09:00 UTC = morning
Expand Down
14 changes: 6 additions & 8 deletions utils/dateHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@ export interface TimeOfDayMetrics {
night: number; // 12 AM - 6 AM
}

export function processCommitTimestamps(commitDates: string[] | Date[]): TimeOfDayMetrics {
export function processCommitTimestamps(commitDates: (string | Date)[]): TimeOfDayMetrics {
const metrics: TimeOfDayMetrics = { morning: 0, afternoon: 0, evening: 0, night: 0 };

commitDates.forEach((dateString) => {
if (!dateString) return;
const date = new Date(dateString);
commitDates.forEach((dateItem) => {
if (!dateItem) return;
const date = new Date(dateItem);
if (isNaN(date.getTime())) return;

// Use getUTCHours() instead of getHours() to ensure timezone-agnostic
// results — getHours() returns local time which causes test failures
// in non-UTC timezones like IST (UTC+5:30).
const hour = date.getUTCHours();
const hour =
typeof dateItem === 'string' ? getAuthorLocalHour(dateItem) : dateItem.getUTCHours();
Comment on lines +16 to +17

if (hour >= 6 && hour < 12) {
metrics.morning++;
Expand Down
Loading