Skip to content

Commit 6346cc1

Browse files
committed
test(svg): add tests for particle count variation
1 parent 9cf71e1 commit 6346cc1

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)