Skip to content

Commit 8338eb8

Browse files
authored
test: add benchmark-svg test coverage (JhaSourav07#2383)
## Description Adds a focused unit test file for scripts/benchmark-svg.ts with 5 targeted checks: - benchmark header logging - warmup + timed run invocation counts - theme block logging coverage - floating-point timing formatting to 2 decimals - separator output per theme Fixes JhaSourav07#2361 ## Pillar Testing reliability ## Checklist - [x] I read the contributing guide - [x] I kept the change small and focused - [x] I added focused tests ## Testing - Local itest run was blocked in this environment due disk space (ENOSPC) while resolving test runner dependencies. - CI should run the full checks on PR.
2 parents 43c79e5 + e757b1f commit 8338eb8

1 file changed

Lines changed: 114 additions & 0 deletions

File tree

scripts/benchmark-svg.test.ts

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

0 commit comments

Comments
 (0)