|
| 1 | +// lib/svg/generator.opacity.test.ts |
| 2 | +// Tests for the ?opacity= URL parameter — Issue # |
| 3 | + |
| 4 | +import { describe, it, expect } from 'vitest'; |
| 5 | +import { generateSVG } from './generator'; |
| 6 | +import type { BadgeParams, ContributionCalendar, StreakStats } from '../../types'; |
| 7 | + |
| 8 | +const mockStats: StreakStats = { |
| 9 | + currentStreak: 5, |
| 10 | + longestStreak: 10, |
| 11 | + totalContributions: 100, |
| 12 | + todayDate: '2024-06-12', |
| 13 | +}; |
| 14 | + |
| 15 | +const mockCalendar: ContributionCalendar = { |
| 16 | + totalContributions: 100, |
| 17 | + weeks: [ |
| 18 | + { |
| 19 | + contributionDays: [ |
| 20 | + { contributionCount: 3, date: '2024-06-10' }, |
| 21 | + { contributionCount: 8, date: '2024-06-11' }, |
| 22 | + { contributionCount: 5, date: '2024-06-12' }, |
| 23 | + ], |
| 24 | + }, |
| 25 | + ], |
| 26 | +}; |
| 27 | + |
| 28 | +describe('?opacity= parameter', () => { |
| 29 | + it('defaults to fully opaque (fill-opacity values unchanged) when opacity is omitted', () => { |
| 30 | + const svgDefault = generateSVG( |
| 31 | + mockStats, |
| 32 | + { user: 'chetan' } as unknown as BadgeParams, |
| 33 | + mockCalendar |
| 34 | + ); |
| 35 | + const svgExplicit1 = generateSVG( |
| 36 | + mockStats, |
| 37 | + { user: 'chetan', opacity: 1.0 } as unknown as BadgeParams, |
| 38 | + mockCalendar |
| 39 | + ); |
| 40 | + // Both should produce identical output |
| 41 | + expect(svgDefault).toBe(svgExplicit1); |
| 42 | + }); |
| 43 | + |
| 44 | + it('applies opacity=0.5 scalar — fill-opacity values are halved', () => { |
| 45 | + const svg = generateSVG( |
| 46 | + mockStats, |
| 47 | + { user: 'chetan', opacity: 0.5 } as unknown as BadgeParams, |
| 48 | + mockCalendar |
| 49 | + ); |
| 50 | + // With opacity=0.5, tower fill-opacity should not exceed 0.5 |
| 51 | + // (0.7 * 0.5 = 0.35 max for top face) |
| 52 | + const towerMatches = [ |
| 53 | + ...svg.matchAll(/<path[^>]*d="M0[^"]*"[^>]*fill-opacity="([\d.]+)"[^>]*>/g), |
| 54 | + ]; |
| 55 | + const towerFaces = towerMatches.filter((match) => !match[0].includes('fill="white"')); |
| 56 | + expect(towerFaces.length).toBeGreaterThan(0); |
| 57 | + for (const match of towerFaces) { |
| 58 | + expect(parseFloat(match[1])).toBeLessThanOrEqual(0.5); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + it('opacity=1.0 produces fill-opacity values identical to the default', () => { |
| 63 | + const svgDefault = generateSVG( |
| 64 | + mockStats, |
| 65 | + { user: 'chetan' } as unknown as BadgeParams, |
| 66 | + mockCalendar |
| 67 | + ); |
| 68 | + const svgOpacity1 = generateSVG( |
| 69 | + mockStats, |
| 70 | + { user: 'chetan', opacity: 1.0 } as unknown as BadgeParams, |
| 71 | + mockCalendar |
| 72 | + ); |
| 73 | + expect(svgDefault).toBe(svgOpacity1); |
| 74 | + }); |
| 75 | + |
| 76 | + it('opacity=0.8 produces fill-opacity values all at or below 0.8', () => { |
| 77 | + const svg = generateSVG( |
| 78 | + mockStats, |
| 79 | + { user: 'chetan', opacity: 0.8 } as unknown as BadgeParams, |
| 80 | + mockCalendar |
| 81 | + ); |
| 82 | + // Tower fill-opacity values should respect opacity parameter |
| 83 | + const towerMatches = [ |
| 84 | + ...svg.matchAll(/<path[^>]*d="M0[^"]*"[^>]*fill-opacity="([\d.]+)"[^>]*>/g), |
| 85 | + ]; |
| 86 | + const towerFaces = towerMatches.filter((match) => !match[0].includes('fill="white"')); |
| 87 | + expect(towerFaces.length).toBeGreaterThan(0); |
| 88 | + for (const match of towerFaces) { |
| 89 | + expect(parseFloat(match[1])).toBeLessThanOrEqual(0.8); |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + it('opacity=0.1 (minimum) produces very low fill-opacity values', () => { |
| 94 | + const svg = generateSVG( |
| 95 | + mockStats, |
| 96 | + { user: 'chetan', opacity: 0.1 } as unknown as BadgeParams, |
| 97 | + mockCalendar |
| 98 | + ); |
| 99 | + // Find tower fill-opacity values (in <path> elements with fill-opacity) |
| 100 | + // The towers should have opacity values <= 0.1 (0.7 * 0.1 = 0.07 max for top face) |
| 101 | + const towerMatches = [ |
| 102 | + ...svg.matchAll(/<path[^>]*d="M0[^"]*"[^>]*fill-opacity="([\d.]+)"[^>]*>/g), |
| 103 | + ]; |
| 104 | + const towerFaces = towerMatches.filter((match) => !match[0].includes('fill="white"')); |
| 105 | + expect(towerFaces.length).toBeGreaterThan(0); |
| 106 | + for (const match of towerFaces) { |
| 107 | + expect(parseFloat(match[1])).toBeLessThanOrEqual(0.1); |
| 108 | + } |
| 109 | + }); |
| 110 | + |
| 111 | + it('different opacity values produce different SVG output', () => { |
| 112 | + const svgFull = generateSVG( |
| 113 | + mockStats, |
| 114 | + { user: 'chetan', opacity: 1.0 } as unknown as BadgeParams, |
| 115 | + mockCalendar |
| 116 | + ); |
| 117 | + const svgHalf = generateSVG( |
| 118 | + mockStats, |
| 119 | + { user: 'chetan', opacity: 0.5 } as unknown as BadgeParams, |
| 120 | + mockCalendar |
| 121 | + ); |
| 122 | + // Different opacity should produce different SVG (tower fill-opacity values differ) |
| 123 | + expect(svgFull).not.toBe(svgHalf); |
| 124 | + // Verify the tower opacities are actually different |
| 125 | + const towerMatches1 = [ |
| 126 | + ...svgFull.matchAll(/<path[^>]*d="M0[^"]*"[^>]*fill-opacity="([\d.]+)"/g), |
| 127 | + ]; |
| 128 | + const towerMatches2 = [ |
| 129 | + ...svgHalf.matchAll(/<path[^>]*d="M0[^"]*"[^>]*fill-opacity="([\d.]+)"/g), |
| 130 | + ]; |
| 131 | + expect(towerMatches1.length).toBeGreaterThan(0); |
| 132 | + expect(towerMatches2.length).toBeGreaterThan(0); |
| 133 | + // The average opacity should be higher for full opacity |
| 134 | + const avg1 = towerMatches1.reduce((sum, m) => sum + parseFloat(m[1]), 0) / towerMatches1.length; |
| 135 | + const avg2 = towerMatches2.reduce((sum, m) => sum + parseFloat(m[1]), 0) / towerMatches2.length; |
| 136 | + expect(avg1).toBeGreaterThan(avg2); |
| 137 | + }); |
| 138 | + |
| 139 | + it('opacity works correctly in auto-theme mode', () => { |
| 140 | + const svg = generateSVG( |
| 141 | + mockStats, |
| 142 | + { user: 'chetan', opacity: 0.5, autoTheme: true } as unknown as BadgeParams, |
| 143 | + mockCalendar |
| 144 | + ); |
| 145 | + expect(svg).toContain('--cp-bg'); |
| 146 | + // Check tower fill-opacity values |
| 147 | + const towerMatches = [ |
| 148 | + ...svg.matchAll(/<path[^>]*d="M0[^"]*"[^>]*fill-opacity="([\d.]+)"[^>]*>/g), |
| 149 | + ]; |
| 150 | + const towerFaces = towerMatches.filter((match) => !match[0].includes('fill="white"')); |
| 151 | + for (const match of towerFaces) { |
| 152 | + expect(parseFloat(match[1])).toBeLessThanOrEqual(0.5); |
| 153 | + } |
| 154 | + }); |
| 155 | + |
| 156 | + it('opacity is rounded to 2 decimal places to avoid floating point noise', () => { |
| 157 | + const svg = generateSVG( |
| 158 | + mockStats, |
| 159 | + { user: 'chetan', opacity: 0.333 } as unknown as BadgeParams, |
| 160 | + mockCalendar |
| 161 | + ); |
| 162 | + // fill-opacity values should not have more than 2 decimal places |
| 163 | + const towerMatches = [ |
| 164 | + ...svg.matchAll(/<path[^>]*d="M0[^"]*"[^>]*fill-opacity="([\d.]+)"[^>]*>/g), |
| 165 | + ]; |
| 166 | + const towerFaces = towerMatches.filter((match) => !match[0].includes('fill="white"')); |
| 167 | + for (const match of towerFaces) { |
| 168 | + const val = match[1]; |
| 169 | + const decimalPlaces = val.includes('.') ? val.split('.')[1].length : 0; |
| 170 | + expect(decimalPlaces).toBeLessThanOrEqual(2); |
| 171 | + } |
| 172 | + }); |
| 173 | +}); |
| 174 | + |
| 175 | +describe('toOpacityValue validation', () => { |
| 176 | + it('returns 1.0 when opacity param is missing', async () => { |
| 177 | + const { streakParamsSchema } = await import('../validations'); |
| 178 | + const result = streakParamsSchema.safeParse({ user: 'chetan' }); |
| 179 | + expect(result.success).toBe(true); |
| 180 | + if (result.success) expect(result.data.opacity).toBe(1.0); |
| 181 | + }); |
| 182 | + |
| 183 | + it('parses valid opacity=0.5 correctly', async () => { |
| 184 | + const { streakParamsSchema } = await import('../validations'); |
| 185 | + const result = streakParamsSchema.safeParse({ user: 'chetan', opacity: '0.5' }); |
| 186 | + expect(result.success).toBe(true); |
| 187 | + if (result.success) expect(result.data.opacity).toBe(0.5); |
| 188 | + }); |
| 189 | + |
| 190 | + it('clamps opacity below 0.1 to 0.1', async () => { |
| 191 | + const { streakParamsSchema } = await import('../validations'); |
| 192 | + const result = streakParamsSchema.safeParse({ user: 'chetan', opacity: '0.0' }); |
| 193 | + expect(result.success).toBe(true); |
| 194 | + if (result.success) expect(result.data.opacity).toBe(0.1); |
| 195 | + }); |
| 196 | + |
| 197 | + it('clamps opacity above 1.0 to 1.0', async () => { |
| 198 | + const { streakParamsSchema } = await import('../validations'); |
| 199 | + const result = streakParamsSchema.safeParse({ user: 'chetan', opacity: '2.5' }); |
| 200 | + expect(result.success).toBe(true); |
| 201 | + if (result.success) expect(result.data.opacity).toBe(1.0); |
| 202 | + }); |
| 203 | + |
| 204 | + it('returns 1.0 for non-numeric opacity value', async () => { |
| 205 | + const { streakParamsSchema } = await import('../validations'); |
| 206 | + const result = streakParamsSchema.safeParse({ user: 'chetan', opacity: 'abc' }); |
| 207 | + expect(result.success).toBe(true); |
| 208 | + if (result.success) expect(result.data.opacity).toBe(1.0); |
| 209 | + }); |
| 210 | + |
| 211 | + it('accepts opacity=1.0 exactly', async () => { |
| 212 | + const { streakParamsSchema } = await import('../validations'); |
| 213 | + const result = streakParamsSchema.safeParse({ user: 'chetan', opacity: '1.0' }); |
| 214 | + expect(result.success).toBe(true); |
| 215 | + if (result.success) expect(result.data.opacity).toBe(1.0); |
| 216 | + }); |
| 217 | + |
| 218 | + it('accepts opacity=0.1 exactly (minimum boundary)', async () => { |
| 219 | + const { streakParamsSchema } = await import('../validations'); |
| 220 | + const result = streakParamsSchema.safeParse({ user: 'chetan', opacity: '0.1' }); |
| 221 | + expect(result.success).toBe(true); |
| 222 | + if (result.success) expect(result.data.opacity).toBe(0.1); |
| 223 | + }); |
| 224 | + |
| 225 | + it('accepts negative opacity and clamps to 0.1', async () => { |
| 226 | + const { streakParamsSchema } = await import('../validations'); |
| 227 | + const result = streakParamsSchema.safeParse({ user: 'chetan', opacity: '-0.5' }); |
| 228 | + expect(result.success).toBe(true); |
| 229 | + if (result.success) expect(result.data.opacity).toBe(0.1); |
| 230 | + }); |
| 231 | +}); |
0 commit comments