Skip to content

Commit fadaeb3

Browse files
authored
test(api validation): check query validation boundaries for ?theme= parameter (Variation 2) (JhaSourav07#1439) (JhaSourav07#2232)
## Description Fixes JhaSourav07#1439 ## Pillar - [ ] 🎨 Pillar 1 — New Theme Design - [ ] 📐 Pillar 2 — Geometric SVG Improvement - [ ] 🕐 Pillar 3 — Timezone Logic Optimization - [x] 🛠️ Other (Bug fix, refactoring, docs) ## Visual Preview ## 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): ...`). - [ ] I have updated `README.md` if I added a new theme or URL parameter. - [x] I have started 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. --- I have run tests on them, and they are working perfectly fine - - lib/validations.test.ts <img width="954" height="134" alt="Screenshot 2026-06-01 125734" src="https://github.com/user-attachments/assets/c366e437-3bbd-473b-81ba-437e9a0e209d" /> - app/api/streak/route.test.ts <img width="1021" height="130" alt="Screenshot 2026-06-01 125816" src="https://github.com/user-attachments/assets/f333238c-a9aa-457b-bed7-0a4b55f42c6d" />
2 parents 20f589c + a86a3e9 commit fadaeb3

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

app/api/streak/route.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,28 @@ describe('GET /api/streak', () => {
259259
expect(response.status).toBe(400);
260260
});
261261

262+
it('returns 400 when an invalid theme value is provided and lists allowed themes', async () => {
263+
const response = await GET(
264+
makeRequest({
265+
user: 'octocat',
266+
theme: 'nonexistent_theme_name',
267+
})
268+
);
269+
270+
expect(response.status).toBe(400);
271+
const body = await response.json();
272+
expect(body.error).toBe('Invalid parameters');
273+
expect(body.details.fieldErrors.theme).toBeDefined();
274+
const errorMessage = body.details.fieldErrors.theme[0];
275+
expect(errorMessage).toContain('Invalid theme');
276+
expect(errorMessage).toContain('Supported themes:');
277+
expect(errorMessage).toContain('auto');
278+
expect(errorMessage).toContain('random');
279+
expect(errorMessage).toContain('dark');
280+
expect(errorMessage).toContain('light');
281+
expect(fetchGitHubContributions).not.toHaveBeenCalled();
282+
});
283+
262284
it('should return 200 OK and valid SVG when the optional repo query parameter is provided', async () => {
263285
// 1. Make request with both parameters present
264286
const response = await GET(makeRequest({ user: 'octocat', repo: 'commitpulse' }));

lib/validations.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,29 @@ describe('streakParamsSchema — theme validation', () => {
850850
expect(fieldError).toContain('neon');
851851
}
852852
});
853+
854+
it('should reject nonexistent_theme_name and verify allowed themes are listed in error', () => {
855+
const result = streakParamsSchema.safeParse({
856+
user: 'octocat',
857+
theme: 'nonexistent_theme_name',
858+
});
859+
860+
expect(result.success).toBe(false);
861+
if (!result.success) {
862+
const fieldErrors = result.error.flatten().fieldErrors;
863+
expect(fieldErrors.theme).toBeDefined();
864+
const errorMessage = fieldErrors.theme?.[0];
865+
expect(errorMessage).toContain('Invalid theme');
866+
expect(errorMessage).toContain('Supported themes:');
867+
expect(errorMessage).toContain('auto');
868+
expect(errorMessage).toContain('random');
869+
expect(errorMessage).toContain('dark');
870+
expect(errorMessage).toContain('light');
871+
expect(errorMessage).toContain('neon');
872+
expect(errorMessage).toContain('github');
873+
expect(errorMessage).toContain('dracula');
874+
}
875+
});
853876
});
854877

855878
describe('streakParamsSchema — view fallback behavior', () => {

0 commit comments

Comments
 (0)