Massive v2 rewrite
This commit is contained in:
parent
1025f3b523
commit
5f1328f626
77 changed files with 28105 additions and 3542 deletions
231
.tests/logs.test.js
Normal file
231
.tests/logs.test.js
Normal file
|
|
@ -0,0 +1,231 @@
|
|||
import { jest } from '@jest/globals';
|
||||
import {
|
||||
init,
|
||||
safeAsync,
|
||||
safeSync,
|
||||
plugin,
|
||||
config,
|
||||
db,
|
||||
server,
|
||||
section,
|
||||
warn,
|
||||
error,
|
||||
msg
|
||||
} from '../dist/utils/logs.js';
|
||||
|
||||
describe('Logs utilities', () => {
|
||||
beforeEach(() => {
|
||||
jest.spyOn(console, 'log').mockImplementation();
|
||||
jest.spyOn(console, 'warn').mockImplementation();
|
||||
jest.spyOn(console, 'error').mockImplementation();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.restoreAllMocks();
|
||||
});
|
||||
|
||||
describe('init', () => {
|
||||
test('should log initialization message', () => {
|
||||
init('Initializing Checkpoint...');
|
||||
expect(console.log).toHaveBeenCalledWith('Initializing Checkpoint...');
|
||||
});
|
||||
});
|
||||
|
||||
describe('safeAsync', () => {
|
||||
test('should return result of successful async operation', async () => {
|
||||
const successfulOperation = async () => 'success';
|
||||
const result = await safeAsync(successfulOperation, 'test', 'Failed to process');
|
||||
expect(result).toBe('success');
|
||||
});
|
||||
|
||||
test('should handle async errors and return fallback', async () => {
|
||||
const failingOperation = async () => {
|
||||
throw new Error('Operation failed');
|
||||
};
|
||||
const result = await safeAsync(failingOperation, 'test', 'Failed to process', 'fallback');
|
||||
expect(result).toBe('fallback');
|
||||
});
|
||||
|
||||
test('should return null by default on error', async () => {
|
||||
const failingOperation = async () => {
|
||||
throw new Error('Operation failed');
|
||||
};
|
||||
const result = await safeAsync(failingOperation, 'test', 'Failed to process');
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
test('should handle non-Error objects thrown', async () => {
|
||||
const failingOperation = async () => {
|
||||
throw 'string error';
|
||||
};
|
||||
const result = await safeAsync(failingOperation, 'test', 'Failed to process', 'fallback');
|
||||
expect(result).toBe('fallback');
|
||||
});
|
||||
|
||||
test('should handle undefined errors', async () => {
|
||||
const failingOperation = async () => {
|
||||
throw undefined;
|
||||
};
|
||||
const result = await safeAsync(failingOperation, 'test', 'Failed to process', 'fallback');
|
||||
expect(result).toBe('fallback');
|
||||
});
|
||||
});
|
||||
|
||||
describe('safeSync', () => {
|
||||
test('should return result of successful sync operation', () => {
|
||||
const successfulOperation = () => 'success';
|
||||
const result = safeSync(successfulOperation, 'test', 'Failed to process');
|
||||
expect(result).toBe('success');
|
||||
});
|
||||
|
||||
test('should handle sync errors and return fallback', () => {
|
||||
const failingOperation = () => {
|
||||
throw new Error('Operation failed');
|
||||
};
|
||||
const result = safeSync(failingOperation, 'test', 'Failed to process', 'fallback');
|
||||
expect(result).toBe('fallback');
|
||||
});
|
||||
|
||||
test('should return null by default on error', () => {
|
||||
const failingOperation = () => {
|
||||
throw new Error('Operation failed');
|
||||
};
|
||||
const result = safeSync(failingOperation, 'test', 'Failed to process');
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
test('should handle non-Error objects thrown', () => {
|
||||
const failingOperation = () => {
|
||||
throw 'string error';
|
||||
};
|
||||
const result = safeSync(failingOperation, 'test', 'Failed to process', 'fallback');
|
||||
expect(result).toBe('fallback');
|
||||
});
|
||||
|
||||
test('should handle number errors', () => {
|
||||
const failingOperation = () => {
|
||||
throw 42;
|
||||
};
|
||||
const result = safeSync(failingOperation, 'test', 'Failed to process', 'fallback');
|
||||
expect(result).toBe('fallback');
|
||||
});
|
||||
});
|
||||
|
||||
describe('plugin', () => {
|
||||
test('should log plugin message', () => {
|
||||
plugin('test-plugin', 'Plugin loaded successfully');
|
||||
expect(console.log).toHaveBeenCalledWith('Plugin loaded successfully');
|
||||
});
|
||||
|
||||
test('should ignore plugin name parameter', () => {
|
||||
plugin('ignored-name', 'Test message');
|
||||
expect(console.log).toHaveBeenCalledWith('Test message');
|
||||
});
|
||||
});
|
||||
|
||||
describe('config', () => {
|
||||
test('should log config message for first occurrence', () => {
|
||||
config('database', 'Database configuration loaded');
|
||||
expect(console.log).toHaveBeenCalledWith('Config Database configuration loaded for database');
|
||||
});
|
||||
|
||||
test('should not log duplicate config messages', () => {
|
||||
config('test-config', 'Test configuration loaded');
|
||||
config('test-config', 'Test configuration loaded');
|
||||
expect(console.log).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
test('should log different config names', () => {
|
||||
config('database-unique1', 'Database config');
|
||||
config('server-unique1', 'Server config');
|
||||
expect(console.log).toHaveBeenCalledTimes(2);
|
||||
expect(console.log).toHaveBeenCalledWith('Config Database config for database-unique1');
|
||||
expect(console.log).toHaveBeenCalledWith('Config Server config for server-unique1');
|
||||
});
|
||||
});
|
||||
|
||||
describe('db', () => {
|
||||
test('should log database message', () => {
|
||||
db('Database connection established');
|
||||
expect(console.log).toHaveBeenCalledWith('Database connection established');
|
||||
});
|
||||
});
|
||||
|
||||
describe('server', () => {
|
||||
test('should log server message', () => {
|
||||
server('Server started on port 3000');
|
||||
expect(console.log).toHaveBeenCalledWith('Server started on port 3000');
|
||||
});
|
||||
});
|
||||
|
||||
describe('section', () => {
|
||||
test('should log section header with uppercase title', () => {
|
||||
section('initialization');
|
||||
expect(console.log).toHaveBeenCalledWith('\n=== INITIALIZATION ===');
|
||||
});
|
||||
|
||||
test('should handle already uppercase titles', () => {
|
||||
section('ALREADY_UPPER');
|
||||
expect(console.log).toHaveBeenCalledWith('\n=== ALREADY_UPPER ===');
|
||||
});
|
||||
|
||||
test('should handle mixed case titles', () => {
|
||||
section('MiXeD_cAsE');
|
||||
expect(console.log).toHaveBeenCalledWith('\n=== MIXED_CASE ===');
|
||||
});
|
||||
});
|
||||
|
||||
describe('warn', () => {
|
||||
test('should log warning with prefix', () => {
|
||||
warn('security', 'Potential security issue detected');
|
||||
expect(console.warn).toHaveBeenCalledWith('WARNING: Potential security issue detected');
|
||||
});
|
||||
|
||||
test('should ignore category parameter', () => {
|
||||
warn('ignored-category', 'Warning message');
|
||||
expect(console.warn).toHaveBeenCalledWith('WARNING: Warning message');
|
||||
});
|
||||
});
|
||||
|
||||
describe('error', () => {
|
||||
test('should log error with prefix', () => {
|
||||
error('database', 'Failed to connect to database');
|
||||
expect(console.error).toHaveBeenCalledWith('ERROR: Failed to connect to database');
|
||||
});
|
||||
|
||||
test('should ignore category parameter', () => {
|
||||
error('ignored-category', 'Error message');
|
||||
expect(console.error).toHaveBeenCalledWith('ERROR: Error message');
|
||||
});
|
||||
});
|
||||
|
||||
describe('msg', () => {
|
||||
test('should log general message', () => {
|
||||
msg('General information message');
|
||||
expect(console.log).toHaveBeenCalledWith('General information message');
|
||||
});
|
||||
});
|
||||
|
||||
describe('edge cases', () => {
|
||||
test('should handle empty messages', () => {
|
||||
msg('');
|
||||
warn('category', '');
|
||||
error('category', '');
|
||||
|
||||
expect(console.log).toHaveBeenCalledWith('');
|
||||
expect(console.warn).toHaveBeenCalledWith('WARNING: ');
|
||||
expect(console.error).toHaveBeenCalledWith('ERROR: ');
|
||||
});
|
||||
|
||||
test('should handle special characters in messages', () => {
|
||||
msg('Message with émojis 🚀 and spëcial chars!');
|
||||
expect(console.log).toHaveBeenCalledWith('Message with émojis 🚀 and spëcial chars!');
|
||||
});
|
||||
|
||||
test('should handle very long messages', () => {
|
||||
const longMessage = 'a'.repeat(1000);
|
||||
msg(longMessage);
|
||||
expect(console.log).toHaveBeenCalledWith(longMessage);
|
||||
});
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue