import {expect, jest, test} from '@jest/globals'; import { basicConfig } from '../../src/config'; import { MANAGER } from '../../src/manager'; import { Handler } from '../../src/handler'; import { LogRecord } from '../../src/log-record'; import { Formatter } from '../../src/formatter'; import { DEBUG, WARNING, ERROR } from '../../src/log-level'; describe('basicConfig', () => { beforeEach(() => { MANAGER.root.handlers.splice(0, MANAGER.root.handlers.length); }); test('adds a handler to the root logger', () => { basicConfig({}); expect(MANAGER.root.handlers.length).toBeGreaterThan(0); }); test('sets root logger level', () => { basicConfig({ level: DEBUG }); expect(MANAGER.root.level).toBe(DEBUG); }); test('does nothing when handlers already exist and force is false', () => { basicConfig({ level: DEBUG }); const handlerCount = MANAGER.root.handlers.length; basicConfig({ level: ERROR }); expect(MANAGER.root.handlers.length).toBe(handlerCount); expect(MANAGER.root.level).toBe(DEBUG); }); test('replaces handlers when force is true', () => { basicConfig({ level: DEBUG }); basicConfig({ level: ERROR, force: true }); expect(MANAGER.root.level).toBe(ERROR); }); test('throws on invalid style', () => { expect(() => { basicConfig({ style: 'X' }); }).toThrow('style must be one of'); }); test('throws when stream and filename both specified', () => { expect(() => { basicConfig({ stream: process.stderr, filename: 'test.log', }); }).toThrow('should not be specified together'); }); test('throws when handlers and stream both specified', () => { class TestHandler extends Handler { emit(record: LogRecord) {} } expect(() => { basicConfig({ handlers: [new TestHandler()], stream: process.stderr, }); }).toThrow('should not be specified together'); }); test('uses provided handlers', () => { class TestHandler extends Handler { emit(record: LogRecord) {} } const handler = new TestHandler(); basicConfig({ handlers: [handler] }); expect(MANAGER.root.handlers).toContain(handler); }); test('assigns formatter to handlers without one', () => { class TestHandler extends Handler { emit(record: LogRecord) {} } const handler = new TestHandler(); expect(handler.formatter).toBeNull(); basicConfig({ handlers: [handler] }); expect(handler.formatter).not.toBeNull(); }); test('preserves existing formatter on handlers', () => { class TestHandler extends Handler { emit(record: LogRecord) {} } const handler = new TestHandler(); const fmt = new Formatter({ fmt: '%(message)s' }); handler.formatter = fmt; basicConfig({ handlers: [handler] }); expect(handler.formatter).toBe(fmt); }); });