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
59 changes: 38 additions & 21 deletions lib/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
GITHUB_CACHE_TTL_MS,
validateGitHubUsername,
cacheKey,
buildCommitClock,
buildInsights,
} from './github';
import type { ContributionCalendar } from '../types';

Expand Down Expand Up @@ -595,26 +595,43 @@ describe('cacheKey', () => {
expect(cacheKey('contributions', 'testuser')).toContain('contributions');
});
});
describe('buildInsights', () => {
it('uses active streak message when current streak > 3', () => {
const result = buildInsights(
{
totalContributions: 120,
currentStreak: 7,
longestStreak: 20,
},
[{ name: 'TypeScript' }]
);

describe('buildCommitClock', () => {
it('aggregates commits correctly by day of week', () => {
const allDays = [
{ date: '2024-06-09', contributionCount: 2 }, // Sun
{ date: '2024-06-10', contributionCount: 5 }, // Mon
{ date: '2024-06-10', contributionCount: 3 }, // Mon
{ date: '2024-06-12', contributionCount: 4 }, // Wed
];

const result = buildCommitClock(allDays);

expect(result).toEqual([
{ day: 'Sun', commits: 2 },
{ day: 'Mon', commits: 8 },
{ day: 'Tue', commits: 0 },
{ day: 'Wed', commits: 4 },
{ day: 'Thu', commits: 0 },
{ day: 'Fri', commits: 0 },
{ day: 'Sat', commits: 0 },
]);
expect(result[2].text).toContain('active 7-day streak');
});

it('uses longest streak message when current streak <= 3', () => {
const result = buildInsights(
{
totalContributions: 120,
currentStreak: 2,
longestStreak: 15,
},
[{ name: 'Rust' }]
);

expect(result[2].text).toContain('15 days');
});

it('falls back to Unknown when languages list is empty', () => {
const result = buildInsights(
{
totalContributions: 50,
currentStreak: 1,
longestStreak: 5,
},
[]
);

expect(result[1].text).toContain('Unknown');
});
});
76 changes: 51 additions & 25 deletions lib/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,46 @@ export function generateAchievements(totalContributions: number, currentStreak:

return achievements;
}
type StreakStats = {
totalContributions: number;
currentStreak: number;
longestStreak: number;
};

type Language = {
name: string;
};

export function buildInsights(streakStats: StreakStats, languages: Language[]) {
const insights = [
{
id: '1',
icon: 'Flame',
text: `You have a total of ${streakStats.totalContributions} contributions this year.`,
},
{
id: '2',
icon: 'Code',
text: `Your primary language is ${languages[0]?.name || 'Unknown'}.`,
},
];

if (streakStats.currentStreak > 3) {
insights.push({
id: '3',
icon: 'Zap',
text: `You are currently on an active ${streakStats.currentStreak}-day streak! Keep it going!`,
});
} else {
insights.push({
id: '3',
icon: 'Star',
text: `Your longest coding streak is ${streakStats.longestStreak} days!`,
});
}

return insights;
}

export function buildCommitClock(allDays: ContributionDay[]) {
const dayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
Expand Down Expand Up @@ -608,33 +648,19 @@ export async function getFullDashboardData(username: string, options: FetchOptio
);

// 4. Insights Generation
const insights = [
{
id: '1',
icon: 'Flame',
text: `You have a total of ${streakStats.totalContributions} contributions this year.`,
},
{
id: '2',
icon: 'Code',
text: `Your primary language is ${languages[0]?.name || 'Unknown'}.`,
},
];
const insights = buildInsights(streakStats, languages);

if (streakStats.currentStreak > 3) {
insights.push({
id: '3',
icon: 'Zap',
text: `You are currently on an active ${streakStats.currentStreak}-day streak! Keep it going!`,
});
} else {
insights.push({
id: '3',
icon: 'Star',
text: `Your longest coding streak is ${streakStats.longestStreak} days!`,
});
// Aggregate real contribution data by day of week from the already-fetched calendar
const dayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
const dayTotals = new Array(7).fill(0);
for (const day of allDays) {
const dow = new Date(day.date).getUTCDay();
dayTotals[dow] += day.contributionCount;
}
const commitClock = buildCommitClock(allDays);
const commitClock = dayNames.map((name, i) => ({
day: name,
commits: dayTotals[i],
}));

return {
profile,
Expand Down
Loading