Initial TrueGrowth source import

This commit is contained in:
2026-07-07 09:36:36 +08:00
commit 3b6781d695
2283 changed files with 691996 additions and 0 deletions

View File

@@ -0,0 +1,72 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { createLogger, logger } from './index';
describe('createLogger', () => {
beforeEach(() => {
vi.spyOn(console, 'log').mockImplementation(() => {});
vi.spyOn(console, 'warn').mockImplementation(() => {});
vi.spyOn(console, 'error').mockImplementation(() => {});
});
afterEach(() => {
vi.restoreAllMocks();
});
it('should create a namespaced logger', () => {
const testLogger = createLogger('TestModule');
testLogger.debug('debug message');
testLogger.info('info message');
testLogger.warn('warn message');
testLogger.error('error message');
expect(console.warn).toHaveBeenCalledWith('[TestModule]', 'warn message');
expect(console.error).toHaveBeenCalledWith('[TestModule]', 'error message');
});
it('should support multiple arguments', () => {
const testLogger = createLogger('Test');
testLogger.warn('message', 1, 2, { key: 'value' });
expect(console.warn).toHaveBeenCalledWith(
'[Test]',
'message',
1,
2,
{ key: 'value' }
);
});
});
describe('logger (default instance)', () => {
beforeEach(() => {
vi.spyOn(console, 'log').mockImplementation(() => {});
vi.spyOn(console, 'warn').mockImplementation(() => {});
vi.spyOn(console, 'error').mockImplementation(() => {});
});
afterEach(() => {
vi.restoreAllMocks();
});
it('should log warnings', () => {
logger.warn('warning message');
expect(console.warn).toHaveBeenCalledWith('warning message');
});
it('should log errors', () => {
logger.error('error message');
expect(console.error).toHaveBeenCalledWith('error message');
});
it('should support multiple arguments', () => {
logger.error('error', 'with', 'multiple', 'args');
expect(console.error).toHaveBeenCalledWith(
'error',
'with',
'multiple',
'args'
);
});
});

View File

@@ -0,0 +1,83 @@
/**
* Logger Utility
*
* Provides a configurable logging utility that respects environment settings.
* In production, debug and info logs are suppressed while errors and warnings are preserved.
*/
const isDev = import.meta.env.DEV;
export type LogLevel = 'debug' | 'info' | 'warn' | 'error';
/**
* Create a namespaced logger for a specific module
*
* @param namespace - Module name prefix (e.g., 'CharacterAPI', 'TaskQueue')
* @returns Logger instance with namespaced methods
*
* @example
* ```typescript
* const logger = createLogger('MyModule');
* logger.debug('Initialization started'); // Only in dev
* logger.error('Failed to load data'); // Always shown
* ```
*/
export function createLogger(namespace: string) {
const prefix = `[${namespace}]`;
return {
/**
* Debug log - only shown in development
*/
debug: (...args: unknown[]) => {
if (isDev) {
console.log(prefix, ...args);
}
},
/**
* Info log - only shown in development
*/
info: (...args: unknown[]) => {
if (isDev) {
console.log(prefix, ...args);
}
},
/**
* Warning log - always shown
*/
warn: (...args: unknown[]) => {
console.warn(prefix, ...args);
},
/**
* Error log - always shown
*/
error: (...args: unknown[]) => {
console.error(prefix, ...args);
},
};
}
/**
* Default logger instance for general use
*
* @example
* ```typescript
* import { logger } from '@aitu/utils';
*
* logger.debug('Debug message'); // Only in dev
* logger.info('Info message'); // Only in dev
* logger.warn('Warning message'); // Always
* logger.error('Error message'); // Always
* ```
*/
export const logger = {
debug: (...args: unknown[]) => isDev && console.log(...args),
info: (...args: unknown[]) => isDev && console.log(...args),
warn: (...args: unknown[]) => console.warn(...args),
error: (...args: unknown[]) => console.error(...args),
};
export default logger;