'use strict';

/**
 * Unit tests for logger.js
 */

const { describe, it, beforeEach, afterEach } = require('node:test');
const assert = require('node:assert/strict');

const { getLogger, sessionLogger, resetLogger } = require('../../src/logger');

describe('logger.js', () => {
  beforeEach(() => {
    resetLogger();
  });

  afterEach(() => {
    resetLogger();
  });

  it('getLogger() returns a pino logger', () => {
    const logger = getLogger();
    assert.ok(logger);
    assert.equal(typeof logger.info, 'function');
    assert.equal(typeof logger.warn, 'function');
    assert.equal(typeof logger.error, 'function');
    assert.equal(typeof logger.debug, 'function');
  });

  it('getLogger() returns the same instance each time (singleton)', () => {
    const a = getLogger();
    const b = getLogger();
    assert.equal(a, b);
  });

  it('respects LOG_LEVEL env var', () => {
    const original = process.env.LOG_LEVEL;
    process.env.LOG_LEVEL = 'warn';
    const logger = getLogger();
    assert.equal(logger.level, 'warn');
    process.env.LOG_LEVEL = original;
    resetLogger();
  });

  it('sessionLogger() returns a child logger', () => {
    const child = sessionLogger('agent:main:test');
    assert.ok(child);
    assert.equal(typeof child.info, 'function');
  });

  it('resetLogger() clears the singleton', () => {
    const a = getLogger();
    resetLogger();
    const b = getLogger();
    assert.notEqual(a, b);
  });
});
