Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/svg/generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
escapeXML,
getSizeScale,
truncateUsername,
deterministicRandom,
} from './generator';
import type { BadgeParams, ContributionCalendar, StreakStats, MonthlyStats } from '../../types';
import { hexColor } from './sanitizer';
Expand Down Expand Up @@ -1444,3 +1445,24 @@ describe('Radar Scan Line Animation Alignment', () => {
expect(geometryLong).toEqual(geometryBaseline);
});
});
describe('deterministicRandom', () => {
it('returns the same value for the same seed (determinism)', () => {
const seed = 'test-seed-42';
expect(deterministicRandom(seed)).toBe(deterministicRandom(seed));
});

it('result is always in the range [0, 1)', () => {
const seeds = ['hello', 'world', '', 'abc:def:0:offsetX', '12345'];
for (const seed of seeds) {
const result = deterministicRandom(seed);
expect(result).toBeGreaterThanOrEqual(0);
expect(result).toBeLessThan(1);
}
});

it('returns different values for different seeds', () => {
const a = deterministicRandom('seed-alpha');
const b = deterministicRandom('seed-beta');
expect(a).not.toBe(b);
});
});
24 changes: 12 additions & 12 deletions lib/svg/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function truncateUsername(username: string): string {
return username.length > 12 ? `${username.slice(0, 12)}...` : username;
}

function deterministicRandom(seed: string): number {
export function deterministicRandom(seed: string): number {
let hash = 2166136261;
for (let i = 0; i < seed.length; i++) {
hash ^= seed.charCodeAt(i);
Expand Down Expand Up @@ -930,13 +930,13 @@ export function generateWrappedSVG(

.title-user { font-family: ${selectedFont || '"Syncopate", sans-serif'}; font-weight: 700; font-size: 13px; letter-spacing: 2.5px; opacity: 0.85; }
.title-wrapped { font-family: ${selectedFont || '"Syncopate", sans-serif'}; font-weight: 700; font-size: 13px; letter-spacing: 2.5px; }

.total-commits { font-family: ${statsFont}; font-size: 46px; font-weight: 700; }
.total-label { font-family: "Roboto", sans-serif; font-size: 9.5px; font-weight: 700; letter-spacing: 1.5px; opacity: 0.5; }

.grid-label { font-family: "Roboto", sans-serif; font-size: 9px; font-weight: 700; letter-spacing: 1.5px; opacity: 0.5; }
.grid-val { font-family: ${statsFont}; font-size: 14.5px; font-weight: 600; }

.scan-line {
animation: scan-sweep var(--scan-speed, 8s) linear infinite;
transform-box: fill-box;
Expand Down Expand Up @@ -1085,11 +1085,11 @@ function generateAutoThemeMonthlySVG(stats: MonthlyStats, params: BadgeParams):
${googleFontsImport}
:root { --cp-bg: #${light.bg}; --cp-text: #${light.text}; --cp-accent: #${light.accent}; --cp-negative: #${light.negative || 'cf222e'}; }
@media (prefers-color-scheme: dark) { :root { --cp-bg: #${dark.bg}; --cp-text: #${dark.text}; --cp-accent: #${dark.accent}; --cp-negative: #${dark.negative || 'f85149'}; } }
.cp-bg-fill { fill: var(--cp-bg); }
.cp-text-fill { fill: var(--cp-text); color: var(--cp-text); }
.cp-bg-fill { fill: var(--cp-bg); }
.cp-text-fill { fill: var(--cp-text); color: var(--cp-text); }
.cp-accent-fill { fill: var(--cp-accent); color: var(--cp-accent); }
.cp-delta-fill { fill: ${stats.deltaAbsolute >= 0 ? 'var(--cp-accent)' : 'var(--cp-negative)'}; }

.title { font-family: ${selectedFont || '"Syncopate", sans-serif'}; fill: var(--cp-text); font-size: 14px; letter-spacing: 2px; font-weight: 400; opacity: 0.8; }
.stats { font-family: ${statsFont}; fill: var(--cp-accent); font-size: 36px; font-weight: 600; letter-spacing: 0; }
.label { font-family: "Roboto", sans-serif; fill: var(--cp-text); font-size: 10px; font-weight: 400; letter-spacing: 1px; opacity: 0.7; }
Expand Down Expand Up @@ -1776,7 +1776,7 @@ export function generateVersusSVG(
${renderDefs(sf, params)}
${renderStyle(selectedFont, statsFont, googleFontsImport, text, accent, sf, bg)}
<rect width="${W}" height="${H}" rx="${radius}" fill="${params.hideBackground ? 'transparent' : bg}" />

<g transform="translate(0, 0)">
<g transform="translate(0, ${Math.round(20 * sf)})">${towers1}</g>
${renderIsometricLabels(calendar1, params, text, sf)}
Expand All @@ -1790,7 +1790,7 @@ export function generateVersusSVG(
</g>

<line x1="${singleW}" y1="${s(40)}" x2="${singleW}" y2="${H - s(40)}" stroke="${text}" stroke-opacity="0.2" stroke-width="2" stroke-dasharray="4 4" />

<g transform="translate(${singleW}, ${H / 2})">
<circle cx="0" cy="0" r="${s(24)}" fill="${bg}" stroke="${accent}" stroke-width="2" />
<text x="0" y="${s(6)}" text-anchor="middle" font-family="${statsFont}" fill="${accent}" font-size="${s(16)}" font-weight="bold">VS</text>
Expand Down Expand Up @@ -1911,7 +1911,7 @@ function generateAutoThemeVersusSVG(
<title>CommitPulse Versus Stats: ${safeUser1} vs ${safeUser2}</title>
<desc>${safeUser1} has ${stats1.totalContributions} ${unit}. ${safeUser2} has ${stats2.totalContributions} ${unit}.</desc>
${renderDefs(sf, params)}

<style>
@import url('https://fonts.googleapis.com/css2?family=Fira+Code&amp;family=JetBrains+Mono&amp;family=Roboto&amp;family=Syncopate:wght@400;700&amp;family=Space+Grotesk:wght@400;500;600;700&amp;display=swap');
:root { --cp-bg: #${light.bg}; --cp-text: #${light.text}; --cp-accent: #${light.accent}; --cp-label-fill: ${lightLabelFill}; --cp-label-opacity: ${lightLabelOpacity}; }
Expand Down Expand Up @@ -1944,7 +1944,7 @@ function generateAutoThemeVersusSVG(
</style>

<rect width="${W}" height="${H}" rx="${radius}" class="${params.hideBackground ? '' : 'cp-bg-fill'}" fill="${params.hideBackground ? 'transparent' : ''}" />

<g transform="translate(0, 0)">
<g transform="translate(0, ${Math.round(20 * sf)})">${towers1}</g>
${renderIsometricLabels(calendar1, params, '', sf)}
Expand All @@ -1958,7 +1958,7 @@ function generateAutoThemeVersusSVG(
</g>

<line x1="${singleW}" y1="${s(40)}" x2="${singleW}" y2="${H - s(40)}" stroke="var(--cp-text)" stroke-opacity="0.2" stroke-width="2" stroke-dasharray="4 4" />

<g transform="translate(${singleW}, ${H / 2})">
<circle cx="0" cy="0" r="${s(24)}" class="cp-bg-fill" stroke="var(--cp-accent)" stroke-width="2" />
<text x="0" y="${s(6)}" text-anchor="middle" font-family="${statsFont}" class="cp-accent-fill" font-size="${s(16)}" font-weight="bold">VS</text>
Expand Down
Loading