Skip to content

Commit 1488bff

Browse files
committed
test: add logger.test
1 parent 0b2d0d0 commit 1488bff

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2+
import { logger } from '../../src/logger';
3+
4+
describe('logger', () => {
5+
// Spy on console methods
6+
const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
7+
const consoleErrorSpy = vi
8+
.spyOn(console, 'error')
9+
.mockImplementation(() => {});
10+
11+
beforeEach(() => {
12+
// Clear all mocks before each test
13+
vi.clearAllMocks();
14+
});
15+
16+
afterEach(() => {
17+
// Reset verbose mode after each test
18+
logger.setVerbose(false);
19+
});
20+
21+
describe('log levels', () => {
22+
it('should log info messages', () => {
23+
logger.info('Test info message');
24+
25+
expect(consoleLogSpy).toHaveBeenCalledTimes(1);
26+
expect(consoleLogSpy.mock.calls[0][0]).toContain('[INFO]');
27+
expect(consoleLogSpy.mock.calls[0][0]).toContain('Test info message');
28+
expect(consoleLogSpy.mock.calls[0][0]).toContain('ℹ');
29+
});
30+
31+
it('should log success messages', () => {
32+
logger.success('Test success message');
33+
34+
expect(consoleLogSpy).toHaveBeenCalledTimes(1);
35+
expect(consoleLogSpy.mock.calls[0][0]).toContain('[SUCCESS]');
36+
expect(consoleLogSpy.mock.calls[0][0]).toContain('Test success message');
37+
expect(consoleLogSpy.mock.calls[0][0]).toContain('✓');
38+
});
39+
40+
it('should log warning messages', () => {
41+
logger.warn('Test warning message');
42+
43+
expect(consoleLogSpy).toHaveBeenCalledTimes(1);
44+
expect(consoleLogSpy.mock.calls[0][0]).toContain('[WARN]');
45+
expect(consoleLogSpy.mock.calls[0][0]).toContain('Test warning message');
46+
expect(consoleLogSpy.mock.calls[0][0]).toContain('⚠');
47+
});
48+
49+
it('should log error messages', () => {
50+
logger.error('Test error message');
51+
52+
expect(consoleErrorSpy).toHaveBeenCalledTimes(1);
53+
expect(consoleErrorSpy.mock.calls[0][0]).toContain('[ERROR]');
54+
expect(consoleErrorSpy.mock.calls[0][0]).toContain('Test error message');
55+
expect(consoleErrorSpy.mock.calls[0][0]).toContain('✗');
56+
});
57+
});
58+
59+
describe('debug logging', () => {
60+
it('should not log debug messages when verbose is false', () => {
61+
logger.setVerbose(false);
62+
logger.debug('Test debug message');
63+
64+
expect(consoleLogSpy).not.toHaveBeenCalled();
65+
});
66+
67+
it('should log debug messages when verbose is true', () => {
68+
logger.setVerbose(true);
69+
logger.debug('Test debug message');
70+
71+
expect(consoleLogSpy).toHaveBeenCalledTimes(1);
72+
expect(consoleLogSpy.mock.calls[0][0]).toContain('[DEBUG]');
73+
expect(consoleLogSpy.mock.calls[0][0]).toContain('Test debug message');
74+
expect(consoleLogSpy.mock.calls[0][0]).toContain('•');
75+
});
76+
});
77+
78+
describe('divider', () => {
79+
it('should log a divider line', () => {
80+
logger.divider();
81+
82+
expect(consoleLogSpy).toHaveBeenCalledTimes(1);
83+
expect(consoleLogSpy.mock.calls[0][0]).toContain('='.repeat(80));
84+
});
85+
});
86+
87+
describe('progress', () => {
88+
it('should display progress bar with percentage', () => {
89+
logger.progress(50, 100, 'Processing items');
90+
91+
expect(consoleLogSpy).toHaveBeenCalledTimes(1);
92+
expect(consoleLogSpy.mock.calls[0][0]).toContain('Processing items:');
93+
expect(consoleLogSpy.mock.calls[0][0]).toContain('50/100');
94+
expect(consoleLogSpy.mock.calls[0][0]).toContain('(50%)');
95+
// Check for progress bar characters
96+
expect(consoleLogSpy.mock.calls[0][0]).toContain('[');
97+
expect(consoleLogSpy.mock.calls[0][0]).toContain(']');
98+
});
99+
100+
it('should include action message when provided', () => {
101+
logger.progress(25, 100, 'Processing items', 'Processing item 25');
102+
103+
expect(consoleLogSpy).toHaveBeenCalledTimes(1);
104+
expect(consoleLogSpy.mock.calls[0][0]).toContain('Processing items:');
105+
expect(consoleLogSpy.mock.calls[0][0]).toContain('25/100');
106+
expect(consoleLogSpy.mock.calls[0][0]).toContain('Processing item 25');
107+
});
108+
109+
it('should handle 0% progress correctly', () => {
110+
logger.progress(0, 100, 'Starting process');
111+
112+
expect(consoleLogSpy).toHaveBeenCalledTimes(1);
113+
expect(consoleLogSpy.mock.calls[0][0]).toContain('Starting process:');
114+
expect(consoleLogSpy.mock.calls[0][0]).toContain('0/100');
115+
expect(consoleLogSpy.mock.calls[0][0]).toContain('(0%)');
116+
});
117+
118+
it('should handle 100% progress correctly', () => {
119+
logger.progress(100, 100, 'Completed process');
120+
121+
expect(consoleLogSpy).toHaveBeenCalledTimes(1);
122+
expect(consoleLogSpy.mock.calls[0][0]).toContain('Completed process:');
123+
expect(consoleLogSpy.mock.calls[0][0]).toContain('100/100');
124+
expect(consoleLogSpy.mock.calls[0][0]).toContain('(100%)');
125+
// For 100%, the progress bar should be full
126+
const progressBarPattern = /\[{20}\]/;
127+
expect(progressBarPattern.test(consoleLogSpy.mock.calls[0][0])).toBe(
128+
true,
129+
);
130+
});
131+
});
132+
});

0 commit comments

Comments
 (0)