import chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
import requireInject from 'require-inject'
import sinon from 'sinon'
import 'sinon-as-promised'
import test from 'ava'

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

test.beforeEach('create stubs', (t) => {
  const ghauth = sinon.stub()
  const checkAuthorization = sinon.stub()
  const stubs = {
    'application-config': sinon.stub().returns({filePath: 'a path'}),
    ghauth,
    [require.resolve('../../../src/logging')]: {
      logger: {
        debug: sinon.spy(),
        error: sinon.spy()
      }
    },
    [require.resolve('../../../src/github/util')]: { checkAuthorization }
  }
  const github = requireInject('../../../src/github', stubs)
  const program = {
    name: 'a name',
    user: 'a user',
    project: 'a project'
  }
  t.context = { checkAuthorization, ghauth, github, program }
})

test('github.authorize: uses token from command line if available', (t) => {
  const { checkAuthorization, ghauth, github, program } = t.context
  const expected = {user: 'some user', token: 'some token'}
  program.accessToken = 'some token'
  checkAuthorization.resolves(expected)

  return expect(github.authorize(program)).to.be.fulfilled.then(
    (authData) => {
      expect(ghauth).to.not.have.been.called
    })
})

test('github.authorize: uses token from configuration file by default', (t) => {
  const { checkAuthorization, ghauth, github, program } = t.context
  ghauth.yields()
  checkAuthorization.resolves()

  return expect(github.authorize(program)).to.be.fulfilled.then(
    (authData) => {
      expect(ghauth).to.have.been.called
    })
})

test('github.authorize: resolves to authorization data on success', (t) => {
  const { checkAuthorization, ghauth, github, program } = t.context
  const expected = {user: 'some user', token: 'some token'}
  ghauth.yields()
  checkAuthorization.resolves(expected)

  return expect(github.authorize(program)).to.eventually.equal(expected)
})

test('github.authorize: rejects on general failure', (t) => {
  const { ghauth, github, program } = t.context
  const message = 'some error'
  ghauth.yields(new Error(message))

  return expect(github.authorize(program)).to.be.rejected.then(
    (error) => {
      expect(error).to.equal('GitHub Authorization failure: Error: some error')
    }
  )
})

test('github.authorize: rejects on bad credentials failure', (t) => {
  const { ghauth, github, program } = t.context
  ghauth.yields(new Error('Bad credentials'))

  return expect(github.authorize(program)).to.be.rejected.then(
    (error) => {
      expect(error).to.have.string('To troubleshoot the problem')
    }
  )
})

@@ -1,74 +1,4 @@
-import chai from 'chai'%0Aimport chaiAsPromised from 'chai-as-promised'%0A
 impo
@@ -36,16 +36,16 @@
 inject'%0A
+
 import s
@@ -117,61 +117,8 @@
 a'%0A%0A
-chai.use(chaiAsPromised)%0Aconst expect = chai.expect%0A%0A
 test
@@ -1039,39 +1039,32 @@
 cted)%0A%0A  return 
-expect(
 github.authorize
@@ -1076,36 +1076,14 @@
 ram)
-).to.be.fulfilled
 .then(
-%0A    
 (aut
@@ -1102,51 +1102,31 @@
     
-  expect(ghauth).to.not.have.been
+t.false(ghauth
 .called
+)
 %0A
-  
   %7D)
@@ -1331,39 +1331,32 @@
 ves()%0A%0A  return 
-expect(
 github.authorize
@@ -1368,36 +1368,14 @@
 ram)
-).to.be.fulfilled
 .then(
-%0A    
 (aut
@@ -1394,47 +1394,30 @@
     
-  expect(ghauth).to.have.been
+t.true(ghauth
 .called
+)
 %0A
-  
   %7D)
@@ -1686,39 +1686,32 @@
 cted)%0A%0A  return 
-expect(
 github.authorize
@@ -1723,30 +1723,51 @@
 ram)
-)
 .t
-o.eventually.equal(
+hen((authData) =%3E %7B%0A    t.same(authData, 
 expe
@@ -1772,16 +1772,21 @@
 pected)%0A
+  %7D)%0A
 %7D)%0A%0Atest
@@ -1960,39 +1960,32 @@
 age))%0A%0A  return 
-expect(
 github.authorize
@@ -1997,77 +1997,44 @@
 ram)
-).to.be.rejected.then(%0A    (error) =%3E %7B%0A      expect(error).to.equal(
+.catch((error) =%3E %7B%0A    t.is(error, 
 'Git
@@ -2078,30 +2078,25 @@
 e error')%0A  
-  %7D%0A  
+%7D
 )%0A%7D)%0A%0Atest('
@@ -2263,23 +2263,16 @@
  return 
-expect(
 github.a
@@ -2292,82 +2292,53 @@
 ram)
-).to.be.rejected.then(%0A    (error) =%3E %7B%0A      expect(error).to.have.string
+.catch((error) =%3E %7B%0A    t.true(error.includes
 ('To
@@ -2368,17 +2368,13 @@
 em')
-%0A    %7D%0A  
+)%0A  %7D
 )%0A%7D)

Update test/github/index/authorize to use ava assertions