
const chai = require('chai')
const assert = chai.assert
const rewire = require('rewire')
const rewiredLogging = rewire('../server/utils/logging.js')
const path = require('path')
const sinon = require('sinon')

describe('logging.js file', () => {
  describe('dir', () => {
    const dir = rewiredLogging.__get__('dir')

    it('should exist', () => {
      assert.exists(dir, 'dir is none or undefined')
    })

    it('should be equal NODE_LOGGING_DIR env if it exists', () => {
      process.env.NODE_LOGGING_DIR = 'dirname'
      const rewiredLoggingEnvStub = rewire('../server/utils/logging.js')
      const rewiredDir = rewiredLoggingEnvStub.__get__('dir')
      assert.equal(rewiredDir, 'dirname')
    })
    it('should be equal absolute path for utils/logs', () => {
      const expected = path.join(__dirname, '../server/utils/logs')
      assert.equal(dir, expected)
    })
  })
  describe('defaultLogOptions', () => {
    const defaultLogOptions = rewiredLogging.__get__('defaultLogOptions')
    it('should exist', () => {
      assert.exists(defaultLogOptions)
    })
    it('should have propper keys / values', () => {
      const dir = rewiredLogging.__get__('dir')
      const expected = {
        filename: dir + '/passport-%DATE%.log',
        handleExceptions: true,
        datePattern: 'YYYY-MM-DD',
        maxSize: '20m'
      }
      assert.deepEqual(defaultLogOptions, expected)
    })
  })
  describe('fileLogOptions', () => {
    const defaultLogOptions = rewiredLogging.__get__('defaultLogOptions')
    const fileLogOptions = rewiredLogging.__get__('fileLogOptions')

    it('should exist', () => {
      assert.exists(fileLogOptions)
    })

    it('should have propper keys / values', () => {
      const dir = rewiredLogging.__get__('dir')
      const expected = {
        filename: dir + '/passport.log',
        maxSize: defaultLogOptions.maxSize,
        maxFiles: 5,
        options: { flags: 'w' }
      }
      assert.deepEqual(fileLogOptions, expected)
    })
  })
  describe('logger', () => {
    const rewiredLogger = rewiredLogging.__get__('logger')
    it('should exist', () => {
      assert.exists(rewiredLogger)
    })
    it('should be an object', () => {
      assert.isObject(rewiredLogger, 'logger is not an object')
    })
    it('should call winston.createLogger once', () => {
      const rewiredWinston = rewiredLogging.__get__('winston')
      const createLoggerSpy = sinon.spy(rewiredWinston, 'createLogger')
      rewire('../server/utils/logging')
      sinon.assert.calledOnce(createLoggerSpy)
      sinon.restore()
    })
    describe('stream', () => {
      it('should exist', () => {
        assert.exists(rewiredLogger.stream, 'steam is not found')
      })
      it('should have a write property setup', () => {
        assert.exists(rewiredLogger.stream.write, 'steam.write is not found')
      })
      it('write property should be function', () => {
        assert.isFunction(rewiredLogger.stream.write, 'steam.write is not a function')
      })
      it('write property should call log2 once', () => {
        // Todo this case is failing
        // const rewiredLog2 = rewiredLogging.__get__('log2')
        // const log2Spy = sinon.spy(rewiredLog2)
        // rewiredLogger.stream.write('tEMp')
        // sinon.assert.calledOnce(log2Spy)
      })
    })
  })
})

@@ -311,16 +311,84 @@
 _('dir')
+%0A    const logDirPath = path.join(__dirname, '../server/utils/logs')
 %0A%0A    it
@@ -578,25 +578,26 @@
 G_DIR = 
-'dirname'
+logDirPath
 %0A      c
@@ -759,17 +759,18 @@
 ir, 
-'dirname'
+logDirPath
 )%0A  
@@ -841,76 +841,8 @@
 %3E %7B%0A
-      const expected = path.join(__dirname, '../server/utils/logs')%0A
     
@@ -861,24 +861,26 @@
 al(dir, 
-expected
+logDirPath
 )%0A    %7D)

fix empty dirname and use existing log dir path