/* global describe,it,afterEach,beforeEach */

const should = require('should') // eslint-disable-line no-unused-vars
const path = require('path')
const fs = require('fs-extra')
const gv = require('../index')
const pjson = require('../package')

const P = '.tmp/v.js'

const createTemp = (content) => {
  fs.outputFileSync(P, content)
}
const readTemp = () => {
  return fs.readFileSync(P).toString()
}
const removeTemp = () => {
  if (fs.existsSync(P)) {
    fs.unlinkSync(P)
    fs.rmdirSync(path.dirname(P))
  }
}

describe('genversion api', () => {
  beforeEach(() => {
    removeTemp()
  })

  afterEach(() => {
    removeTemp()
  })

  it('should detect missing version file', (done) => {
    gv.check('version.js', (err, doesExist, isByGenversion, isUpToDate) => {
      should.equal(err, null)
      doesExist.should.equal(false)
      isByGenversion.should.equal(false)
      isUpToDate.should.equal(false)
      return done()
    })
  })

  it('should detect unknown version file', (done) => {
    createTemp('// not generated by genversion\n' +
      'module.exports = \'' + pjson.version + '\'\n')
    gv.check(P, (err, doesExist, isByGenversion, isUpToDate) => {
      should.equal(err, null)
      doesExist.should.equal(true)
      isByGenversion.should.equal(false)
      isUpToDate.should.equal(false)
      return done()
    })
  })

  it('should detect outdated version file', (done) => {
    createTemp('// generated by genversion\n') // even misses the export
    gv.check(P, (err, doesExist, isByGenversion, isUpToDate) => {
      should.equal(err, null)
      doesExist.should.equal(true)
      isByGenversion.should.equal(true)
      isUpToDate.should.equal(false)
      return done()
    })
  })

  it('should recognise es6 flag', (done) => {
    gv.generate(P, { useEsmSyntax: true }, (err, version) => {
      should.equal(err, null)
      version.should.equal(pjson.version)

      readTemp().should.equal('// Generated by ' +
        'genversion.\nexport const version = \'' + pjson.version + '\'\n')

      gv.generate(P, (err2) => {
        should.equal(err2, null)
        readTemp().should.equal('// Generated by ' +
          'genversion.\nmodule.exports = \'' + pjson.version + '\'\n')

        return done()
      })
    })
  })

  it('should recognise source flag', (done) => {
    gv.generate(P, { source: './test/fixture' }, (err, v) => {
      should.equal(err, null)
      v.should.equal('0.1.2')

      readTemp().should.equal('// Generated by ' +
        'genversion.\nmodule.exports = \'0.1.2\'\n')

      return done()
    })
  })
})
