|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +vi.mock('../lib/svg/generator', () => ({ |
| 4 | + generateSVG: vi.fn(() => '<svg/>'), |
| 5 | +})); |
| 6 | + |
| 7 | +vi.mock('../lib/svg/sanitizer', () => ({ |
| 8 | + hexColor: vi.fn((value: string) => `#${value}`), |
| 9 | + sanitizeSpeed: vi.fn(() => '8s'), |
| 10 | +})); |
| 11 | + |
| 12 | +vi.mock('perf_hooks', () => ({ |
| 13 | + performance: { |
| 14 | + now: vi.fn(), |
| 15 | + }, |
| 16 | +})); |
| 17 | + |
| 18 | +async function runBenchmarkScript() { |
| 19 | + vi.resetModules(); |
| 20 | + await import('./benchmark-svg'); |
| 21 | +} |
| 22 | + |
| 23 | +describe('scripts/benchmark-svg', () => { |
| 24 | + beforeEach(() => { |
| 25 | + vi.clearAllMocks(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('prints the benchmark header', async () => { |
| 29 | + const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 30 | + const { performance } = await import('perf_hooks'); |
| 31 | + vi.mocked(performance.now).mockImplementation(() => 1); |
| 32 | + |
| 33 | + await runBenchmarkScript(); |
| 34 | + |
| 35 | + expect(logSpy).toHaveBeenCalledWith('\nSVG Benchmark Results\n'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('runs warmup plus 20 timed iterations for each theme', async () => { |
| 39 | + const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 40 | + const { performance } = await import('perf_hooks'); |
| 41 | + const { generateSVG } = await import('../lib/svg/generator'); |
| 42 | + |
| 43 | + let tick = 0; |
| 44 | + vi.mocked(performance.now).mockImplementation(() => { |
| 45 | + tick += 1; |
| 46 | + return tick; |
| 47 | + }); |
| 48 | + |
| 49 | + await runBenchmarkScript(); |
| 50 | + |
| 51 | + expect(generateSVG).toHaveBeenCalledTimes(63); |
| 52 | + expect(vi.mocked(generateSVG).mock.calls[0]?.[1]).toMatchObject({ |
| 53 | + bg: '#0d1117', |
| 54 | + accent: '#00ffaa', |
| 55 | + text: '#ffffff', |
| 56 | + }); |
| 57 | + expect(logSpy).toHaveBeenCalled(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('logs all theme names', async () => { |
| 61 | + const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 62 | + const { performance } = await import('perf_hooks'); |
| 63 | + vi.mocked(performance.now).mockImplementation(() => 1); |
| 64 | + |
| 65 | + await runBenchmarkScript(); |
| 66 | + |
| 67 | + expect(logSpy).toHaveBeenCalledWith('Theme: dark'); |
| 68 | + expect(logSpy).toHaveBeenCalledWith('Theme: light'); |
| 69 | + expect(logSpy).toHaveBeenCalledWith('Theme: purple'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('formats floating-point benchmark numbers to 2 decimals', async () => { |
| 73 | + const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 74 | + const { performance } = await import('perf_hooks'); |
| 75 | + |
| 76 | + let call = 0; |
| 77 | + vi.mocked(performance.now).mockImplementation(() => { |
| 78 | + call += 1; |
| 79 | + if (call % 2 === 1) return call; |
| 80 | + return call + 1.2345; |
| 81 | + }); |
| 82 | + |
| 83 | + await runBenchmarkScript(); |
| 84 | + |
| 85 | + const lines = logSpy.mock.calls |
| 86 | + .map((args) => String(args[0])) |
| 87 | + .filter((line) => line.startsWith('Average:') || line.startsWith('Min:') || line.startsWith('Max:')); |
| 88 | + |
| 89 | + expect(lines.length).toBe(9); |
| 90 | + for (const line of lines) { |
| 91 | + expect(line).toMatch(/:\s\d+\.\d{2}ms$/); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + it('prints separator line after each theme block', async () => { |
| 96 | + const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 97 | + const { performance } = await import('perf_hooks'); |
| 98 | + vi.mocked(performance.now).mockImplementation(() => 1); |
| 99 | + |
| 100 | + await runBenchmarkScript(); |
| 101 | + |
| 102 | + const separatorCalls = logSpy.mock.calls.filter( |
| 103 | + (args) => args[0] === '--------------------------' |
| 104 | + ); |
| 105 | + expect(separatorCalls).toHaveLength(3); |
| 106 | + }); |
| 107 | +}); |
0 commit comments