import {describe, expect, test, vi} from 'vitest'
import Logger from '../src/Logger'
// @ts-ignore
import moment from 'moment'

const VITE_LOGGER_LEVEL = 4

const loggerInfo = {
  "loggerLevel": VITE_LOGGER_LEVEL,
  "serviceName": "live_monitor",
  "machineAddr": "172.25.78.151",
  "port": 6175,
  "datetime": moment().format("YYYY-MM-DDTHH:mm:ss"),
}

describe('Logger', () => {

  test('empty logger info', () => {
    const newLogger = new Logger(null)
    const loggerDebug = newLogger.debug(undefined, undefined)
    const loggerInfo = newLogger.info(undefined, undefined)
    const loggerWarn = newLogger.warn(undefined, undefined)
    const loggerError = newLogger.error(undefined, undefined)

    expect(loggerDebug).toBeUndefined()
    expect(loggerInfo).toBeUndefined()
    expect(loggerWarn).toBeUndefined()
    expect(loggerError).toBeUndefined()
  })

  test('sent logger, empty required params', () => {

    const callback = (text: string) => console.log('callback', text)
    const allParams = {...loggerInfo, callFunc: callback, isPushToServer: true}
    const newLogger = new Logger(allParams)
    const loggerWarn = newLogger.warn('', 1)
    const loggerError = newLogger.error('Test error', undefined)

    expect(loggerWarn).toBeUndefined()
    expect(loggerError).toBeUndefined()
  })

  test('sent logger, empty callback function', () => {

    const allParams = {...loggerInfo, callFunc: null, isPushToServer: true}
    const newLogger = new Logger(allParams)
    const loggerDebug = newLogger.debug('Test debug', 0)
    const loggerInfos = newLogger.info('Test info', 1)
    const loggerWarn = newLogger.warn('Test warn', 2)
    const loggerError = newLogger.error('Test error', 3)

    expect(loggerDebug).toBe('Отладочная информация отправлена на сервер')
    expect(loggerInfos).toBe('Информация отправлена на сервер')
    expect(loggerWarn).toBe('Предупреждение отправлено на сервер')
    expect(loggerError).toBe('Ошибка отправлена на сервер')
  })

  test('sent logger, with callback function', () => {

    const callback = vi.fn((text) => `callback func - ${text}`)
    const allParams = {...loggerInfo, callFunc: callback, isPushToServer: true}
    const newLogger = new Logger(allParams)
    const loggerDebug = newLogger.debug('Test debug', 1)
    const loggerInfos = newLogger.info('Test info', 1)
    const loggerWarn = newLogger.warn('Test warn', 1)
    const loggerError = newLogger.error('Test error', 1)

    expect(loggerDebug).toBe('callback func - Test debug')
    expect(loggerInfos).toBe('callback func - Test info')
    expect(loggerWarn).toBe('callback func - Test warn')
    expect(loggerError).toBe('callback func - Test error')
  })

})