/* 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 createComment
  let logger
  const pr = 42

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

  it('writes comment object to log on success', function () {
    createComment.callsArgWith(5, undefined, '<comment object>')
    const github = requireInject('../lib/github', stubs)

    github.writeComment('auth data', 'user', 'project', pr, 'comment')
    expect(logger.debug).to.have.been.calledWith('Comment: %s', '<comment object>')
  })

  it('writes error to log on failure', function () {
    const expectedError = new Error('some error')
    createComment.callsArgWith(5, expectedError, undefined)
    const github = requireInject('../lib/github', stubs)

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

@@ -2137,28 +2137,8 @@
 ubs%0A
-  let createComment%0A
   le
@@ -2210,41 +2210,8 @@
 ) %7B%0A
-    createComment = sinon.stub()%0A
     
@@ -2315,37 +2315,8 @@
 s: %7B
-%0A        createComment%0A      
 %7D%0A  
@@ -2443,32 +2443,36 @@
 ess', function (
+done
 ) %7B%0A    createCo
@@ -2468,67 +2468,171 @@
    c
-reateComment.callsArgWith(5, undefined, '%3Ccomment object%3E')
+onst expected = '%3Ccomment object%3E'%0A    stubs.ghissues.createComment = function (%0A        authData, user, project, pr, comment, cb) %7B%0A      cb(null, expected)%0A    %7D
 %0A   
@@ -2748,33 +2748,63 @@
 , pr, 'comment')
-%0A
+.then(%0A      function () %7B%0A    
     expect(logge
@@ -2851,34 +2851,48 @@
 t: %25s', 
-'%3Ccomment object%3E'
+expected)%0A        done()%0A      %7D
 )%0A  %7D)%0A%0A
@@ -2932,32 +2932,36 @@
 ure', function (
+done
 ) %7B%0A    const ex
@@ -2966,21 +2966,16 @@
 expected
-Error
  = new E
@@ -3001,63 +3001,132 @@
     
-createComment.callsArgWith(5, expectedError, undefined)
+stubs.ghissues.createComment = function (%0A        authData, user, project, pr, comment, cb) %7B%0A      cb(expected, null)%0A    %7D
 %0A   
@@ -3250,17 +3250,47 @@
 omment')
-%0A
+.then(%0A      function () %7B%0A    
     expe
@@ -3337,16 +3337,20 @@
 (%0A      
+    
 'Error a
@@ -3390,21 +3390,40 @@
 expected
-Error
+)%0A        done()%0A      %7D
 )%0A  %7D)%0A%7D

Update writeComment test cases