Skip to content

Commit 266f4da

Browse files
authored
Merge branch 'main' into github-action-bot-spamming-limit
2 parents 8c72f32 + 4960e6c commit 266f4da

10 files changed

Lines changed: 532 additions & 21 deletions

File tree

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ URL Parameter > Theme Default > System Fallback
189189
| `labelColor` | `hex` | No || Custom text color for the isometric labels — **without** `#` |
190190
| `versus` | `string` | No || GitHub username of an opponent to compare against in side-by-side versus mode |
191191
| `shading` | `boolean` | No | `false` | Apply intensity-based opacity shading to tower faces so lower intensity levels appear slightly dimmer |
192+
| `opacity` | `number` | No | `1.0` | Global opacity scalar for all tower fill-opacity values (0.1–1.0). `opacity=0.5` = semi-transparent ghost look. `opacity=0.8` = faded, great on light backgrounds. |
192193
| `gradient` | `boolean` | No | `false` | Opt-in to show volumetric gradients on the monolith floor |
193194

194195
### Grace Period Examples
@@ -322,6 +323,14 @@ Explore some of the built-in CommitPulse themes and quickly copy the style you l
322323

323324
![](https://commitpulse.vercel.app/api/streak?user=jhasourav07&gradient=true&shading=true)
324325

326+
<!-- Semi-transparent ghost city look -->
327+
328+
![](https://commitpulse.vercel.app/api/streak?user=jhasourav07&opacity=0.5)
329+
330+
<!-- Slightly faded — perfect for light background embeds -->
331+
332+
![](https://commitpulse.vercel.app/api/streak?user=jhasourav07&opacity=0.8)
333+
325334
<!-- GitHub-style Heatmap View -->
326335

327336
![](https://commitpulse.vercel.app/api/streak?user=jhasourav07&view=heatmap)

app/api/compare/route.test.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { describe, it, expect, vi, beforeEach } from 'vitest';
2+
import { GET } from './route';
3+
4+
vi.mock('@/lib/github', () => ({
5+
getFullDashboardData: vi.fn(),
6+
}));
7+
8+
import { getFullDashboardData } from '@/lib/github';
9+
10+
const makeRequest = (search: string) => new Request(`http://localhost:3000/api/compare?${search}`);
11+
12+
describe('GET /api/compare', () => {
13+
beforeEach(() => {
14+
vi.clearAllMocks();
15+
vi.mocked(getFullDashboardData).mockResolvedValue({
16+
calendar: { totalContributions: 50, weeks: [] },
17+
} as never);
18+
});
19+
20+
// ── Validation ────────────────────────────────────────────────────────────
21+
22+
it('returns 400 when user1 is missing', async () => {
23+
const res = await GET(makeRequest('user2=octocat'));
24+
expect(res.status).toBe(400);
25+
});
26+
27+
it('returns 400 when user2 is missing', async () => {
28+
const res = await GET(makeRequest('user1=octocat'));
29+
expect(res.status).toBe(400);
30+
});
31+
32+
it('returns 400 when both users are missing', async () => {
33+
const res = await GET(makeRequest(''));
34+
expect(res.status).toBe(400);
35+
});
36+
37+
it('returns 400 for invalid GitHub username format for user1', async () => {
38+
const res = await GET(makeRequest('user1=-invalid&user2=octocat'));
39+
expect(res.status).toBe(400);
40+
const data = await res.json();
41+
expect(data.details.fieldErrors.user1).toBeDefined();
42+
});
43+
44+
it('returns 400 for invalid GitHub username format for user2', async () => {
45+
const res = await GET(makeRequest('user1=octocat&user2=-invalid'));
46+
expect(res.status).toBe(400);
47+
const data = await res.json();
48+
expect(data.details.fieldErrors.user2).toBeDefined();
49+
});
50+
51+
it('returns 400 for username exceeding 39 characters', async () => {
52+
const res = await GET(makeRequest(`user1=${'a'.repeat(40)}&user2=octocat`));
53+
expect(res.status).toBe(400);
54+
});
55+
56+
it('returns 400 when comparing a user with themselves', async () => {
57+
const res = await GET(makeRequest('user1=octocat&user2=octocat'));
58+
expect(res.status).toBe(400);
59+
const data = await res.json();
60+
expect(data.details.fieldErrors.user2).toContain('Cannot compare a user with themselves.');
61+
});
62+
63+
it('returns 400 for self-comparison regardless of case', async () => {
64+
const res = await GET(makeRequest('user1=OctoCat&user2=octocat'));
65+
expect(res.status).toBe(400);
66+
});
67+
68+
// ── Success ──────────────────────────────────────────────────────────────
69+
70+
it('returns 200 with comparison data for valid users', async () => {
71+
const res = await GET(makeRequest('user1=alice&user2=bob'));
72+
expect(res.status).toBe(200);
73+
const data = await res.json();
74+
expect(data.user1).toBeDefined();
75+
expect(data.user2).toBeDefined();
76+
});
77+
78+
// ── Error handling ────────────────────────────────────────────────────────
79+
80+
it('returns 404 when user1 is not found on GitHub', async () => {
81+
vi.mocked(getFullDashboardData).mockRejectedValueOnce(new Error('Not found'));
82+
const res = await GET(makeRequest('user1=ghost123&user2=octocat'));
83+
expect(res.status).toBe(404);
84+
});
85+
86+
it('returns 404 when user2 is not found on GitHub', async () => {
87+
vi.mocked(getFullDashboardData)
88+
.mockResolvedValueOnce({ calendar: { totalContributions: 0, weeks: [] } } as never)
89+
.mockRejectedValueOnce(new Error('Not found'));
90+
const res = await GET(makeRequest('user1=octocat&user2=ghost123'));
91+
expect(res.status).toBe(404);
92+
});
93+
});

app/api/compare/route.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
import { NextResponse } from 'next/server';
22
import { getFullDashboardData } from '@/lib/github';
3+
import { compareParamsSchema } from '@/lib/validations';
34

45
export const revalidate = 3600;
56

67
export async function GET(request: Request) {
78
const { searchParams } = new URL(request.url);
8-
const user1 = searchParams.get('user1');
9-
const user2 = searchParams.get('user2');
109

11-
if (!user1 || !user2) {
10+
const parseResult = compareParamsSchema.safeParse(Object.fromEntries(searchParams.entries()));
11+
12+
if (!parseResult.success) {
13+
const fieldErrors = parseResult.error.flatten();
1214
return NextResponse.json(
13-
{ error: 'Both user1 and user2 query parameters are required.' },
15+
{ error: 'Invalid parameters', details: fieldErrors },
1416
{ status: 400 }
1517
);
1618
}
1719

18-
if (user1.toLowerCase() === user2.toLowerCase()) {
19-
return NextResponse.json({ error: 'Cannot compare a user with themselves.' }, { status: 400 });
20-
}
20+
const { user1, user2 } = parseResult.data;
2121

2222
try {
2323
const [result1, result2] = await Promise.allSettled([

app/api/streak/route.test.ts

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -724,12 +724,17 @@ describe('GET /api/streak', () => {
724724
expect(body).toContain('--cp-bg');
725725
});
726726

727-
it('falls back to the dark theme without crashing when an unknown theme is given', async () => {
728-
const response = await GET(makeRequest({ user: 'octocat', theme: 'does-not-exist' }));
729-
const body = await response.text();
727+
it('returns 400 Bad Request listing allowed themes when an invalid theme is provided', async () => {
728+
const response = await GET(makeRequest({ user: 'octocat', theme: 'nonexistent_theme_name' }));
729+
expect(response.status).toBe(400);
730730

731-
expect(response.status).toBe(200);
732-
expect(body).toContain('58a6ff');
731+
const body = await response.json();
732+
expect(body.error).toBe('Invalid parameters');
733+
const fieldError = body.details.fieldErrors.theme[0];
734+
expect(fieldError).toContain('Invalid theme. Supported themes:');
735+
expect(fieldError).toContain('dark');
736+
expect(fieldError).toContain('light');
737+
expect(fieldError).toContain('neon');
733738
});
734739
});
735740

@@ -1337,4 +1342,54 @@ describe('GET /api/streak', () => {
13371342
expect(body).toContain('strictly for organizations');
13381343
});
13391344
});
1345+
1346+
describe('JSON output mode (format=json)', () => {
1347+
it('returns JSON with correct Content-Type when format=json is set', async () => {
1348+
const response = await GET(makeRequest({ user: 'octocat', format: 'json' }));
1349+
expect(response.status).toBe(200);
1350+
expect(response.headers.get('Content-Type')).toContain('application/json');
1351+
});
1352+
1353+
it('returns stats, monthlyStats, and calendar in JSON response', async () => {
1354+
const response = await GET(makeRequest({ user: 'octocat', format: 'json' }));
1355+
const data = await response.json();
1356+
1357+
expect(data.user).toBe('octocat');
1358+
expect(data.stats).toBeDefined();
1359+
expect(data.stats.currentStreak).toBeDefined();
1360+
expect(data.stats.longestStreak).toBeDefined();
1361+
expect(data.stats.totalContributions).toBeDefined();
1362+
expect(data.monthlyStats).toBeDefined();
1363+
expect(data.monthlyStats.currentMonthTotal).toBeDefined();
1364+
expect(data.calendar).toBeDefined();
1365+
expect(data.calendar.totalContributions).toBe(10);
1366+
expect(data.calendar.weeks).toHaveLength(2);
1367+
});
1368+
1369+
it('includes Cache-Control header in JSON response', async () => {
1370+
const response = await GET(makeRequest({ user: 'octocat', format: 'json' }));
1371+
expect(response.headers.get('Cache-Control')).toContain('s-maxage=');
1372+
});
1373+
1374+
it('includes X-Cache-Status header in JSON response', async () => {
1375+
const response = await GET(makeRequest({ user: 'octocat', format: 'json' }));
1376+
expect(response.headers.get('X-Cache-Status')).toBe('HIT');
1377+
});
1378+
1379+
it('returns SVG when format is not set (default)', async () => {
1380+
const response = await GET(makeRequest({ user: 'octocat' }));
1381+
expect(response.headers.get('Content-Type')).toBe('image/svg+xml');
1382+
});
1383+
1384+
it('falls back to SVG for invalid format values', async () => {
1385+
const response = await GET(makeRequest({ user: 'octocat', format: 'xml' }));
1386+
expect(response.headers.get('Content-Type')).toBe('image/svg+xml');
1387+
});
1388+
1389+
it('uses org name as user field when org parameter is provided', async () => {
1390+
const response = await GET(makeRequest({ user: 'octocat', org: 'github', format: 'json' }));
1391+
const data = await response.json();
1392+
expect(data.user).toBe('github');
1393+
});
1394+
});
13401395
});

app/api/streak/route.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,11 @@ export async function GET(request: Request) {
9090
versus,
9191
shading,
9292
gradient,
93+
opacity,
9394
tz: tzParam,
9495
disable_particles,
9596
glow,
97+
format,
9698
} = parseResult.data;
9799

98100
const themeName = theme || 'dark';
@@ -106,6 +108,8 @@ export async function GET(request: Request) {
106108
: year
107109
? `${year}-12-31T23:59:59Z`
108110
: undefined;
111+
const currentYear = new Date().getUTCFullYear();
112+
const isHistoricalYear = !!year && Number(year) < currentYear;
109113

110114
let timezone = 'UTC';
111115
if (tzParam) {
@@ -160,6 +164,7 @@ export async function GET(request: Request) {
160164
versus,
161165
shading,
162166
gradient,
167+
opacity,
163168
disable_particles,
164169
glow,
165170
animate,
@@ -194,6 +199,42 @@ export async function GET(request: Request) {
194199
}
195200
}
196201

202+
// ─── JSON output mode ──────────────────────────────────────────────────
203+
if (format === 'json') {
204+
const stats = calculateStreak(calendar, timezone, undefined, grace);
205+
const monthlyStats = calculateMonthlyStats(
206+
calendar,
207+
timezone,
208+
getMonthlyReferenceDate(year, timezone)
209+
);
210+
211+
const secondsToMidnight = tzParam
212+
? getSecondsUntilMidnightInTimezone(timezone)
213+
: getSecondsUntilUTCMidnight();
214+
const cacheControl = refresh
215+
? 'no-cache, no-store, must-revalidate'
216+
: `public, s-maxage=${secondsToMidnight}, stale-while-revalidate=86400`;
217+
218+
return NextResponse.json(
219+
{
220+
user: targetEntity,
221+
stats,
222+
monthlyStats,
223+
calendar: {
224+
totalContributions: calendar.totalContributions,
225+
weeks: calendar.weeks,
226+
},
227+
},
228+
{
229+
headers: {
230+
'Cache-Control': cacheControl,
231+
'X-Cache-Status': refresh ? `BYPASS, fetched=${new Date().toISOString()}` : 'HIT',
232+
},
233+
}
234+
);
235+
}
236+
237+
// ─── SVG output mode (default) ──────────────────────────────────────────
197238
let svg = '';
198239
if (view === 'monthly') {
199240
const stats = calculateMonthlyStats(
@@ -224,7 +265,9 @@ export async function GET(request: Request) {
224265
: getSecondsUntilUTCMidnight();
225266
const cacheControl = refresh
226267
? 'no-cache, no-store, must-revalidate'
227-
: `public, s-maxage=${secondsToMidnight}, stale-while-revalidate=86400`;
268+
: isHistoricalYear
269+
? 'public, s-maxage=31536000, immutable'
270+
: `public, s-maxage=${secondsToMidnight}, stale-while-revalidate=86400`;
228271

229272
return new NextResponse(svg, {
230273
headers: {

0 commit comments

Comments
 (0)