/* global describe it beforeEach */
'use strict'

const chai = require('chai')
const requireInject = require('require-inject')
const sinon = require('sinon')

const expect = chai.expect

describe('getSemverTags', function () {
  let stubs
  let exec

  beforeEach('create stubs', function () {
    exec = sinon.stub()
    stubs = {}
    stubs[require.resolve('../lib/util')] = { exec }
  })

  it('returns semver tags only in an array', function () {
    exec.returns(new Buffer('v1.0.0\nnot-semver\nv0.2.1\nv0.2.0\nv0.1.0\n'))
    const github = requireInject('../lib/github', stubs)

    const tags = github.getSemverTags()
    expect(tags).to.deep.equal(['v1.0.0', 'v0.2.1', 'v0.2.0', 'v0.1.0'])
  })

  it('returns empty array when no tags found', function () {
    exec.returns(new Buffer('not-semver\n'))
    const github = requireInject('../lib/github', stubs)

    const tags = github.getSemverTags()
    expect(tags).to.deep.equal([])
  })
})

describe('getMergeCommits', function () {
  let stubs
  let exec

  beforeEach('create stubs', function () {
    exec = sinon.stub()
    stubs = {}
    stubs[require.resolve('../lib/util')] = { exec }
  })

  it('returns one commit per line', function () {
    exec.returns(new Buffer('commit 1\ncommit 2\n'))
    const github = requireInject('../lib/github', stubs)

    expect(github.getMergeCommits('a..b'))
      .to.deep.equal(['commit 1', 'commit 2'])
  })

  it('returns empty array when no commits are found', function () {
    exec.returns(new Buffer(''))
    const github = requireInject('../lib/github', stubs)

    expect(github.getMergeCommits('a..b')).to.deep.equal([])
  })
})

describe('getPRs', function () {
  const getPRs = require('../lib/github').getPRs

  it('filters commits with PRs', function () {
    const prs = getPRs([
      'this is a commit',
      'Merge pull request #42 from ',
      'this is another commit'
    ])
    expect(prs).to.deep.equal([42])
  })

  it('return empty array when no commits have been passed', function () {
    const prs = getPRs([])
    expect(prs).to.deep.equal([])
  })
})

describe('writeComment', function () {
  let stubs
  let logger
  const pr = 42

  beforeEach('create stubs', function () {
    logger = {
      debug: sinon.stub(),
      error: sinon.stub()
    }
    stubs = {
      ghissues: {}
    }
    stubs[require.resolve('../lib/logging')] = {
      logger
    }
  })

  it('writes comment object to log on success', function (done) {
    const expected = '<comment object>'
    stubs.ghissues.createComment = function (
        authData, user, project, pr, comment, cb) {
      cb(null, expected)
    }
    const github = requireInject('../lib/github', stubs)

    github.writeComment('auth data', 'user', 'project', pr, 'comment').then(
      function () {
        expect(logger.debug).to.have.been.calledWith('Comment: %s', expected)
        done()
      })
  })

  it('writes error to log on failure', function (done) {
    const expected = new Error('some error')
    stubs.ghissues.createComment = function (
        authData, user, project, pr, comment, cb) {
      cb(expected, null)
    }
    const github = requireInject('../lib/github', stubs)

    github.writeComment('auth data', 'user', 'project', pr, 'comment').then(
      function () {
        expect(logger.error).to.have.been.calledWith(
          'Error adding comment to PR#%d: %s', pr, expected)
        done()
      })
  })
})

describe('getSemverComments', function () {
  const github = require('../lib/github')

  it('filters semver comments', function () {
    const commentList = [
      {body: 'a comment'},
      {body: 'v0.0.0'},
      {body: 'another comment'}
    ]

    expect(github.getSemverComments(commentList))
      .to.deep.equal(['v0.0.0'])
  })

  it('returns empty array on no comments', function () {
    const commentList = []
    expect(github.getSemverComments(commentList))
      .to.deep.equal([])
  })
})

@@ -26,16 +26,26 @@
 oreEach 
+afterEach 
 */%0A'use 
@@ -2084,32 +2084,1177 @@
 al(%5B%5D)%0A  %7D)%0A%7D)%0A%0A
+describe('authorize', function () %7B%0A  let stubs%0A  const authOptions = %7B%7D%0A%0A  beforeEach('create stubs', function () %7B%0A    stubs = %7B%0A      ghauth: null%0A    %7D%0A    stubs%5Brequire.resolve('../lib/logging')%5D = %7B%0A      logger: %7B%0A        debug: sinon.stub(),%0A        error: sinon.stub()%0A      %7D%0A    %7D%0A    sinon.stub(process, 'exit')%0A  %7D)%0A%0A  afterEach('restore stubs', function () %7B%0A    process.exit.restore()%0A  %7D)%0A%0A  it('returns authorization data on success', function (done) %7B%0A    const expected = %7Buser: 'some user', token: 'some token'%7D%0A    stubs.ghauth = function (options, cb) %7B%0A      cb(null, expected)%0A    %7D%0A    const github = requireInject('../lib/github', stubs)%0A%0A    github.authorize(authOptions).then(%0A      function (authData) %7B%0A        expect(authData).to.equal(expected)%0A        done()%0A      %7D%0A    )%0A  %7D)%0A%0A  it('exits on failure', function (done) %7B%0A    stubs.ghauth = function (options, cb) %7B%0A      cb(new Error(), null)%0A    %7D%0A    const github = requireInject('../lib/github', stubs)%0A%0A    github.authorize(authOptions).then(%0A      function () %7B%0A        expect(process.exit).to.have.been.calledWith(1)%0A        done()%0A      %7D%0A    )%0A  %7D)%0A%7D)%0A%0A
 describe('writeC

Add authorize test cases