const expect = require('chai').expect
const Logger = require('../logger/defaultLogger')
const fs = require('fs')


const checkFile = (filePath)=>{
    return new Promise((resolve) => {
        fs.exists(filePath, (error) => {
            if (error) return resolve(true)
            return resolve(false)
            
        })
    }).catch(err => { console.log(err) })
} 
const del = (filePath) => {
    return new Promise((resolve, reject) => {
        fs.unlink((filePath), err => {
            if (err) return reject(err)  
            return resolve()
        })
    }).catch(e=>console.log(e))
}
const cleanup = () => {
    const deleteFiles = [del('./test/t.error.log'), del('./test/t.warning.log'), del('./test/t.info.log')]
    return Promise.all(deleteFiles)
        .then(() => { return fs.rmdirSync('./test/testDir/') })
        .then(() => { return fs.rmdirSync('./test/hello/bye/') })
        .then(() => { return fs.rmdirSync('./test/hello/') })
}



describe('ezcodin-logger.js test:', function() {

    after(cleanup)

    let log = new Logger({ paths: { error: './test/t.error.log', warning: './test/t.warning.log', info: './test/t.info.log' } })

    it('[initialize log] Should create error log file"', function () {  
       
        return checkFile('./test/t.error.log').then(result => { return expect(result).to.equal(true) })
    })


    it('[initialize log]: Should create warning log file"', function () {

        return checkFile('./test/t.warning.log').then(result => { return expect(result).to.equal(true)})
    })

    
    it('[initialize log]: Should create info log file"', function () {

        return checkFile('./test/t.info.log').then(result => { return expect(result).to.equal(true) })
    })


    it('[getDirs]: should return an array of path segmants', function () {

        const dirs = log.getDirs('./mark/jason/ring/file.js')
            .then(result => { return expect(result).is.instanceof(Array) })
    })


    it('[getDirs]: should return an array of path segmants excluding period', function () {

        return log.
            getDirs('mark/jason/ring/file.js')
            .then(result => { return expect(result[0]).to.not.equal('.') })
    })


    it('[mkdir] Should create a directory', function () {

        return log
            .mkdir('/test/testDir')
            .then( () => { return checkFile('test/testDir') })
            .then( result => { return expect(result).to.equal(true) })
        
    })
    it('[mkdirs] Should create nested directories from string path', function () {
        return log
            .getDirs('/test/hello/bye')
            .then(result => { return log.mkdirs(result) })
            .then(() => { return checkFile('test/hello/bye') })
            .then(result => { return expect(result).to.equal(true) })
        
    })

})