Skip to content

Commit 10651a2

Browse files
authored
test(svg): add tests for particle count variation (JhaSourav07#428)
## Description Fixes JhaSourav07#402 ## Pillar - [ ] 🎨 Pillar 1 β€” New Theme Design - [ ] πŸ“ Pillar 2 β€” Geometric SVG Improvement - [ ] πŸ• Pillar 3 β€” Timezone Logic Optimization - [x] πŸ› οΈ Other (Bug fix, refactoring, docs) ## Visual Preview None needed for logic tests ## 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): ...`). - [x] 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. ## Summary - extracted particleCount helper - added unit tests for clamp/scaling behavior - replaced inline particle count logic
2 parents d215de2 + 6346cc1 commit 10651a2

2 files changed

Lines changed: 30 additions & 5 deletions

File tree

β€Žlib/svg/generator.test.tsβ€Ž

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, expect } from 'vitest';
2-
import { generateSVG, generateMonthlySVG } from './generator';
2+
import { generateSVG, generateMonthlySVG, particleCount } from './generator';
33
import type { BadgeParams, ContributionCalendar, StreakStats, MonthlyStats } from '../../types';
44

55
describe('generateSVG', () => {
@@ -424,3 +424,22 @@ describe('generateMonthlySVG', () => {
424424
expect(svg).toContain('height="200"');
425425
});
426426
});
427+
428+
describe('particleCount', () => {
429+
it('returns 0 when count is 0', () => {
430+
expect(particleCount(0)).toBe(0);
431+
});
432+
433+
it('clamps to lower bound of 3 for low counts (e.g., 10 -> 3)', () => {
434+
expect(particleCount(10)).toBe(3);
435+
});
436+
437+
it('scales correctly between bounds (e.g., 16 -> 4)', () => {
438+
expect(particleCount(16)).toBe(4);
439+
});
440+
441+
it('clamps to upper bound of 5 for high counts (e.g., 20 -> 5, 100 -> 5)', () => {
442+
expect(particleCount(20)).toBe(5);
443+
expect(particleCount(100)).toBe(5);
444+
});
445+
});

β€Žlib/svg/generator.tsβ€Ž

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ export function escapeXML(str: string): string {
4545
.replace(/"/g, '"')
4646
.replace(/'/g, ''');
4747
}
48+
49+
export function particleCount(count: number): number {
50+
if (count === 0) return 0;
51+
return Math.min(5, Math.max(3, Math.floor(count / 4)));
52+
}
53+
4854
function generateParticles(
4955
x: number,
5056
y: number,
@@ -54,9 +60,9 @@ function generateParticles(
5460
sf: number
5561
): string {
5662
let particles = '';
57-
const particleCount = Math.min(5, Math.max(3, Math.floor(count / 4)));
63+
const numParticles = particleCount(count);
5864

59-
for (let i = 0; i < particleCount; i++) {
65+
for (let i = 0; i < numParticles; i++) {
6066
const seed = `${x}:${y}:${height}:${color}:${count}:${i}`;
6167
const offsetX = deterministicRandom(`${seed}:offsetX`) * 6 - 3;
6268
const delay = deterministicRandom(`${seed}:delay`) * 1.5;
@@ -79,9 +85,9 @@ function generateAutoParticles(
7985
sf: number
8086
): string {
8187
let particles = '';
82-
const particleCount = Math.min(5, Math.max(3, Math.floor(count / 4)));
88+
const numParticles = particleCount(count);
8389

84-
for (let i = 0; i < particleCount; i++) {
90+
for (let i = 0; i < numParticles; i++) {
8591
const seed = `${x}:${y}:${height}:auto:${count}:${i}`;
8692
const offsetX = deterministicRandom(`${seed}:offsetX`) * 6 - 3;
8793
const delay = deterministicRandom(`${seed}:delay`) * 1.5;

0 commit comments

Comments
Β (0)