Skip to content

Commit a9b3c35

Browse files
authored
test(api): add schema-level validation test for invalid accent hex color (JhaSourav07#1891)
## Description Added schema-level validation tests targeting the `?accent=` parameter under HEX color validation check. The tests verify that passing an invalid hex value like `#ZZZZZZZ` is correctly rejected with a 400 Bad Request, and that valid hex colors are accepted. Changes - Added a new test suite in `lib/validations.test.ts` targeting accent HEX color validation - Added a route-level test in `app/api/streak/route.test.ts` asserting 400 is returned for invalid accent hex Fixes JhaSourav07#1450 ## Pillar - [ ] 🎨 Pillar 1 — New Theme Design - [ ] 📐 Pillar 2 — Geometric SVG Improvement - [ ] 🕐 Pillar 3 — Timezone Logic Optimization - [x] 🛠️ Other (Bug fix, refactoring, docs) Visual Preview N/A (test-only change) ## Checklist - [x] I have read the `CONTRIBUTING.md` file. - [x] I have tested these changes locally (`npm run test -- lib/validations.test.ts` and `npm run test -- app/api/streak/route.test.ts`). - [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., `test(api): ...`). - [ ] 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). - [ ] (Recommended) I joined the CommitPulse Discord community for contributor discussions, mentorship, and faster PR support.
2 parents 01cd94c + 7979dcf commit a9b3c35

2 files changed

Lines changed: 26 additions & 9 deletions

File tree

app/api/streak/route.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,12 @@ describe('GET /api/streak', () => {
648648
it('does not crash when an invalid text color is provided', async () => {
649649
const response = await GET(makeRequest({ user: 'octocat', text: 'notacolor' }));
650650

651+
expect(response.status).toBe(400);
652+
});
653+
it('returns 400 when an invalid hex color is passed as accent', async () => {
654+
// #ZZZZZZZ contains non-hex characters — schema must reject it with 400
655+
const response = await GET(makeRequest({ user: 'octocat', accent: '#ZZZZZZZ' }));
656+
651657
expect(response.status).toBe(400);
652658
});
653659
});

lib/validations.test.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -695,23 +695,37 @@ describe('streakParamsSchema — view fallback behavior', () => {
695695
});
696696
});
697697

698-
/* ==========================================================================
699-
* DATE RANGE BOUNDARY ROBUSTNESS (VARIATION 1)
700-
* ========================================================================== */
698+
describe('streakParamsSchema — accent parameter HEX color validation', () => {
699+
it('rejects an invalid hex color like "#ZZZZZZZ" for accent', () => {
700+
// #ZZZZZZZ contains non-hex characters — must fail schema validation
701+
const result = streakParamsSchema.safeParse({
702+
user: 'octocat',
703+
accent: '#ZZZZZZZ',
704+
});
705+
706+
expect(result.success).toBe(false);
707+
});
708+
709+
it('accepts a valid 6-character hex color for accent', () => {
710+
const result = streakParamsSchema.safeParse({
711+
user: 'octocat',
712+
accent: 'ff0000',
713+
});
714+
715+
expect(result.success).toBe(true);
716+
});
717+
});
701718

702719
describe('streakParamsSchema — Date Range Boundary Robustness (Variation 1)', () => {
703720
it('should process validation safely and fallback when partial or missing year parameters are passed', () => {
704-
// Arrange: Provide a mock payload missing a full YYYY format sequence
705721
const partialYearPayload = {
706722
user: 'octocat',
707723
from: '05-12',
708724
to: '05-30',
709725
};
710726

711-
// Act: Pass the object through the validator schema matrix
712727
const result = streakParamsSchema.safeParse(partialYearPayload);
713728

714-
// Assert: The validator handles it safely using implicit date engine fallbacks
715729
expect(result.success).toBe(true);
716730
if (result.success) {
717731
expect(result.data.from).toBeDefined();
@@ -720,15 +734,12 @@ describe('streakParamsSchema — Date Range Boundary Robustness (Variation 1)',
720734
});
721735

722736
it('should pass cleanly and fallback to default ranges when date bounds are completely omitted', () => {
723-
// Arrange: Pass only the bare minimum required parameters
724737
const minimalPayload = {
725738
user: 'octocat',
726739
};
727740

728-
// Act
729741
const result = streakParamsSchema.safeParse(minimalPayload);
730742

731-
// Assert: Verify that omitted range options return undefined to use downstream defaults smoothly
732743
expect(result.success).toBe(true);
733744
if (result.success) {
734745
expect(result.data.from).toBeUndefined();

0 commit comments

Comments
 (0)