|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 2 | +import { createLogger, logger } from '../../src/logger'; |
| 3 | + |
| 4 | +describe('Logger', () => { |
| 5 | + const originalEnv = process.env.LOG_LEVEL; |
| 6 | + |
| 7 | + afterEach(() => { |
| 8 | + if (originalEnv === undefined) { |
| 9 | + delete process.env.LOG_LEVEL; |
| 10 | + } else { |
| 11 | + process.env.LOG_LEVEL = originalEnv; |
| 12 | + } |
| 13 | + }); |
| 14 | + |
| 15 | + describe('createLogger', () => { |
| 16 | + it('should create a logger with the specified module name', () => { |
| 17 | + const log = createLogger('crm:account'); |
| 18 | + // pino exposes bindings() which includes "name" |
| 19 | + expect((log as any).bindings().name).toBe('crm:account'); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should default to info level', () => { |
| 23 | + const log = createLogger('test:module'); |
| 24 | + expect(log.level).toBe('info'); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should accept a custom level via options', () => { |
| 28 | + const log = createLogger('test:debug', { level: 'debug' }); |
| 29 | + expect(log.level).toBe('debug'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should respect LOG_LEVEL env variable', () => { |
| 33 | + process.env.LOG_LEVEL = 'warn'; |
| 34 | + const log = createLogger('test:env'); |
| 35 | + expect(log.level).toBe('warn'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should prefer explicit option over env variable', () => { |
| 39 | + process.env.LOG_LEVEL = 'warn'; |
| 40 | + const log = createLogger('test:override', { level: 'error' }); |
| 41 | + expect(log.level).toBe('error'); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + describe('logger (default instance)', () => { |
| 46 | + it('should be a valid pino logger', () => { |
| 47 | + expect(typeof logger.info).toBe('function'); |
| 48 | + expect(typeof logger.warn).toBe('function'); |
| 49 | + expect(typeof logger.error).toBe('function'); |
| 50 | + expect(typeof logger.debug).toBe('function'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should have name "hotcrm"', () => { |
| 54 | + expect((logger as any).bindings().name).toBe('hotcrm'); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('structured output', () => { |
| 59 | + it('should produce JSON output with structured data', () => { |
| 60 | + const chunks: string[] = []; |
| 61 | + const dest = { |
| 62 | + write(chunk: string) { chunks.push(chunk); }, |
| 63 | + } as any; |
| 64 | + |
| 65 | + // Create a pino logger writing to our mock destination |
| 66 | + const pino = require('pino'); |
| 67 | + const log = pino({ name: 'test:json', level: 'info' }, dest); |
| 68 | + log.info({ accountId: '123', score: 85 }, 'Account scored'); |
| 69 | + |
| 70 | + expect(chunks.length).toBe(1); |
| 71 | + const parsed = JSON.parse(chunks[0]); |
| 72 | + expect(parsed.name).toBe('test:json'); |
| 73 | + expect(parsed.accountId).toBe('123'); |
| 74 | + expect(parsed.score).toBe(85); |
| 75 | + expect(parsed.msg).toBe('Account scored'); |
| 76 | + expect(parsed.level).toBe(30); // pino info = 30 |
| 77 | + }); |
| 78 | + |
| 79 | + it('should not contain emoji in structured output', () => { |
| 80 | + const chunks: string[] = []; |
| 81 | + const dest = { |
| 82 | + write(chunk: string) { chunks.push(chunk); }, |
| 83 | + } as any; |
| 84 | + |
| 85 | + const pino = require('pino'); |
| 86 | + const log = pino({ name: 'test:no-emoji', level: 'info' }, dest); |
| 87 | + log.info('Account created successfully'); |
| 88 | + |
| 89 | + const parsed = JSON.parse(chunks[0]); |
| 90 | + // Verify no emoji characters (common emoji ranges) |
| 91 | + const emojiRegex = /[\u{1F300}-\u{1F9FF}]|[\u{2600}-\u{26FF}]|[\u{2700}-\u{27BF}]/u; |
| 92 | + expect(emojiRegex.test(parsed.msg)).toBe(false); |
| 93 | + }); |
| 94 | + }); |
| 95 | +}); |
0 commit comments