import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { createLogger } from '../../diagnostics/logger.js';
describe('createLogger', () => {
    const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => { });
    const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => { });
    const infoSpy = vi.spyOn(console, 'info').mockImplementation(() => { });
    const debugSpy = vi.spyOn(console, 'debug').mockImplementation(() => { });
    const originalLevel = process.env.ACTUATE_LOG_LEVEL;
    const originalFormat = process.env.ACTUATE_LOG_FORMAT;
    const originalNodeEnv = process.env.NODE_ENV;
    beforeEach(() => {
        errorSpy.mockClear();
        warnSpy.mockClear();
        infoSpy.mockClear();
        debugSpy.mockClear();
    });
    afterEach(() => {
        if (originalLevel === undefined)
            delete process.env.ACTUATE_LOG_LEVEL;
        else
            process.env.ACTUATE_LOG_LEVEL = originalLevel;
        if (originalFormat === undefined)
            delete process.env.ACTUATE_LOG_FORMAT;
        else
            process.env.ACTUATE_LOG_FORMAT = originalFormat;
        if (originalNodeEnv === undefined)
            delete process.env.NODE_ENV;
        else
            process.env.NODE_ENV = originalNodeEnv;
    });
    it('emits all levels by default in non-production', () => {
        delete process.env.NODE_ENV;
        delete process.env.ACTUATE_LOG_LEVEL;
        const log = createLogger('test');
        log.error('e');
        log.warn('w');
        log.info('i');
        log.debug('d');
        expect(errorSpy).toHaveBeenCalled();
        expect(warnSpy).toHaveBeenCalled();
        expect(infoSpy).toHaveBeenCalled();
        // info-level default does NOT include debug
        expect(debugSpy).not.toHaveBeenCalled();
    });
    it('silences info+debug when level=warn', () => {
        process.env.ACTUATE_LOG_LEVEL = 'warn';
        const log = createLogger('test');
        log.error('e');
        log.warn('w');
        log.info('i');
        log.debug('d');
        expect(errorSpy).toHaveBeenCalled();
        expect(warnSpy).toHaveBeenCalled();
        expect(infoSpy).not.toHaveBeenCalled();
        expect(debugSpy).not.toHaveBeenCalled();
    });
    it('only emits errors when level=error', () => {
        process.env.ACTUATE_LOG_LEVEL = 'error';
        const log = createLogger('test');
        log.error('e');
        log.warn('w');
        log.info('i');
        expect(errorSpy).toHaveBeenCalled();
        expect(warnSpy).not.toHaveBeenCalled();
        expect(infoSpy).not.toHaveBeenCalled();
    });
    it('emits nothing when level=silent', () => {
        process.env.ACTUATE_LOG_LEVEL = 'silent';
        const log = createLogger('test');
        log.error('e');
        log.warn('w');
        expect(errorSpy).not.toHaveBeenCalled();
        expect(warnSpy).not.toHaveBeenCalled();
    });
    it('defaults to warn in NODE_ENV=production', () => {
        process.env.NODE_ENV = 'production';
        delete process.env.ACTUATE_LOG_LEVEL;
        const log = createLogger('test');
        log.error('e');
        log.warn('w');
        log.info('i');
        expect(errorSpy).toHaveBeenCalled();
        expect(warnSpy).toHaveBeenCalled();
        expect(infoSpy).not.toHaveBeenCalled();
    });
    it('emits one-line JSON when format=json', () => {
        process.env.ACTUATE_LOG_LEVEL = 'info';
        process.env.ACTUATE_LOG_FORMAT = 'json';
        const log = createLogger('rate-limit');
        log.error('boom', { reason: 'upstream-down', retry: 3 });
        expect(errorSpy).toHaveBeenCalledOnce();
        const line = errorSpy.mock.calls[0][0];
        expect(typeof line).toBe('string');
        const parsed = JSON.parse(line);
        expect(parsed.level).toBe('error');
        expect(parsed.scope).toBe('rate-limit');
        expect(parsed.msg).toBe('boom');
        expect(parsed.details).toEqual({ reason: 'upstream-down', retry: 3 });
        expect(typeof parsed.ts).toBe('string');
    });
    it('uses [actuate][scope] prefix in text mode', () => {
        process.env.ACTUATE_LOG_LEVEL = 'info';
        delete process.env.ACTUATE_LOG_FORMAT;
        const log = createLogger('rate-limit');
        log.warn('hi');
        expect(warnSpy).toHaveBeenCalledOnce();
        expect(warnSpy.mock.calls[0][0]).toBe('[actuate][rate-limit]');
        expect(warnSpy.mock.calls[0][1]).toBe('hi');
    });
});
//# sourceMappingURL=logger.test.js.map