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

const chai = require('chai')
const chaiAsPromised = require('chai-as-promised')
const requireInject = require('require-inject')
const sinon = require('sinon')

chai.use(chaiAsPromised)
const expect = chai.expect

describe('github.util.writeComment', function () {
  let createComment
  let stubs
  let logger
  const pr = 42

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

  it('writes comment object to log on success', function () {
    const expected = '<comment object>'
    createComment.yields(null, expected)
    const util = requireInject('../../lib/github/util', stubs)

    return expect(util.writeComment('auth data', 'user', 'project', pr, 'comment'))
      .to.be.fulfilled.then(function () {
        expect(logger.debug).to.have.been.calledWith(
          'Comment added to PR#%d: %s', pr, JSON.stringify(expected))
      })
  })

  it('writes error to log on failure', function () {
    const expected = new Error('some error')
    createComment.yields(expected)
    const util = requireInject('../../lib/github/util', stubs)

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

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

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

    expect(util.getSemverComments(commentList))
      .to.deep.equal(['v0.0.0', 'v0.1.0'])
  })

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

@@ -2135,16 +2135,776 @@
 ual(%5B%5D)%0A  %7D)%0A%7D)%0A
+%0Adescribe('github.util.checkAuthorization', function () %7B%0A  let list%0A  let stubs%0A%0A  beforeEach(function () %7B%0A    list = sinon.stub()%0A    stubs = %7B%0A      ghissues: %7B%0A        list%0A      %7D%0A    %7D%0A  %7D)%0A%0A  it('resolves if issues can be retrieved', function () %7B%0A    const authData = 'authorization data'%0A    list.yields()%0A    const util = requireInject('../../lib/github/util', stubs)%0A    return expect(util.checkAuthorization(authData, 'program'))%0A      .to.eventually.equal(authData)%0A  %7D)%0A%0A  it(%22rejects if issues list can't be retrieved%22, function () %7B%0A    list.yields(new Error('some error'))%0A    const util = requireInject('../../lib/github/util', stubs)%0A    return expect(util.checkAuthorization('authorization data', 'program'))%0A      .to.be.rejected%0A  %7D)%0A%7D)%0A

Add checkAuthorization test cases