git-aiflow
Version:
๐ An AI-powered workflow automation tool for effortless Git-based development, combining smart GitLab/GitHub merge & pull request creation with Conan package management.
122 lines โข 5.02 kB
JavaScript
import { logger, Logger, LogLevel } from '../logger.js';
/**
* Test singleton pattern thoroughly
*/
class SingletonTest {
async testBasicSingleton() {
console.log('๐งช Testing basic singleton behavior...');
// Test 1: Multiple getInstance calls return same instance
const logger1 = Logger.getInstance();
const logger2 = Logger.getInstance();
const logger3 = Logger.getInstance();
console.log(`โ
Same instance: ${logger1 === logger2 && logger2 === logger3}`);
console.log(`โ
Global logger same: ${logger === logger1}`);
}
async testConfigurationUpdate() {
console.log('\n๐งช Testing configuration update...');
// Test 2: Configuration update affects existing instance
const originalLevel = Logger.getInstance().getWinston().level;
console.log(`Original level: ${originalLevel}`);
Logger.configure({ level: LogLevel.ERROR });
const newLevel = Logger.getInstance().getWinston().level;
console.log(`New level: ${newLevel}`);
console.log(`โ
Configuration updated: ${newLevel === 'error'}`);
}
async testResetAndRecreate() {
console.log('\n๐งช Testing reset and recreate...');
// Test 3: Reset creates new instance
const originalInstance = Logger.getInstance();
Logger.reset();
const newInstance = Logger.getInstance();
console.log(`โ
Reset worked: ${originalInstance !== newInstance}`);
console.log(`โ
New instance created: ${Logger.hasInstance()}`);
}
async testContextDetection() {
console.log('\n๐งช Testing context detection...');
// Test 4: Context detection in different scenarios
logger.info('Message from testContextDetection method');
// Test in nested function
const nestedFunction = () => {
logger.debug('Message from nested function');
};
nestedFunction();
// Test in arrow function
const arrowFunction = () => {
logger.warn('Message from arrow function');
};
arrowFunction();
}
async testCachePerformance() {
console.log('\n๐งช Testing cache performance...');
// Test 5: Cache performance
Logger.clearCache();
console.log(`Cache size after clear: ${Logger.getCacheSize()}`);
// Generate some logs to populate cache
for (let i = 0; i < 5; i++) {
logger.info(`Test message ${i}`);
}
console.log(`Cache size after logging: ${Logger.getCacheSize()}`);
// Test cache hit
const start = Date.now();
for (let i = 0; i < 1000; i++) {
logger.debug('Cached message');
}
const end = Date.now();
console.log(`โ
1000 cached logs took: ${end - start}ms`);
}
async testConcurrentAccess() {
console.log('\n๐งช Testing concurrent access...');
// Test 6: Concurrent access to singleton
const promises = [];
for (let i = 0; i < 10; i++) {
promises.push(new Promise(resolve => {
setTimeout(() => {
const instance = Logger.getInstance();
logger.info(`Concurrent test ${i}`);
resolve(instance);
}, Math.random() * 10);
}));
}
const instances = await Promise.all(promises);
const allSame = instances.every(instance => instance === instances[0]);
console.log(`โ
Concurrent access safe: ${allSame}`);
}
async testMemoryManagement() {
console.log('\n๐งช Testing memory management...');
// Test 7: Memory management
const initialCacheSize = Logger.getCacheSize();
// Generate many unique contexts
for (let i = 0; i < 100; i++) {
const uniqueFunction = () => {
logger.info(`Unique message ${i}`);
};
uniqueFunction();
}
const finalCacheSize = Logger.getCacheSize();
console.log(`Cache growth: ${initialCacheSize} -> ${finalCacheSize}`);
// Clear cache
Logger.clearCache();
console.log(`โ
Cache cleared: ${Logger.getCacheSize() === 0}`);
}
async runAllTests() {
console.log('๐ Starting comprehensive singleton tests...\n');
try {
await this.testBasicSingleton();
await this.testConfigurationUpdate();
await this.testResetAndRecreate();
await this.testContextDetection();
await this.testCachePerformance();
await this.testConcurrentAccess();
await this.testMemoryManagement();
console.log('\n๐ All singleton tests completed successfully!');
}
catch (error) {
console.error('โ Test failed:', error);
}
}
}
// Run tests
const test = new SingletonTest();
test.runAllTests().catch(console.error);
//# sourceMappingURL=test-singleton.js.map