const fs = require('fs')
const test = require('ava')
const path = require('path')
const Fanfou = require('../')
const {
  CONSUMER_KEY,
  CONSUMER_SECRET,
  OAUTH_TOKEN,
  OAUTH_TOKEN_SECRET,
  FANFOU_USERNAME,
  FANFOU_PASSWORD
} = process.env

const nonceText = Math.random().toString(36).substr(2, 5)

const oauth = () => {
  const ff = new Fanfou({
    auth_type: 'oauth',
    consumer_key: CONSUMER_KEY,
    consumer_secret: CONSUMER_SECRET,
    oauth_token: OAUTH_TOKEN,
    oauth_token_secret: OAUTH_TOKEN_SECRET
  })
  return ff
}

const xauth = () => {
  const ff = new Fanfou({
    auth_type: 'xauth',
    consumer_key: CONSUMER_KEY,
    consumer_secret: CONSUMER_SECRET,
    username: FANFOU_USERNAME,
    password: FANFOU_PASSWORD
  })
  return new Promise((resolve, reject) => {
    ff.xauth((err, res) => {
      if (err) reject(err)
      else resolve(res)
    })
  })
}

const favorites = () => {
  const ff = oauth()
  return new Promise((resolve, reject) => {
    ff.get('/favorites', {id: 'testcase'}, (err, res) => {
      if (err) reject(err)
      else resolve(res)
    })
  })
}

const update = () => {
  const ff = oauth()
  return new Promise((resolve, reject) => {
    ff.post('/statuses/update', {status: nonceText}, (err, res) => {
      if (err) reject(err)
      else resolve(res)
    })
  })
}

const upload = () => {
  const ff = oauth()
  return new Promise((resolve, reject) => {
    ff.upload(
      fs.createReadStream(path.join(__dirname, '/img/parentheses.png')),
      nonceText + '-upload',
      (err, res) => {
        if (err) reject(err)
        else resolve(res)
      }
    )
  })
}

test('oauth test', async t => {
  const ff = oauth()
  t.deepEqual([
    ff.consumer_key,
    ff.consumer_secret,
    ff.oauth_token,
    ff.oauth_token_secret
  ], [
    CONSUMER_KEY,
    CONSUMER_SECRET,
    OAUTH_TOKEN,
    OAUTH_TOKEN_SECRET
  ])
})

test('xauth test', async t => {
  const results = await xauth()
  t.deepEqual(results, {
    oauth_token: OAUTH_TOKEN,
    oauth_token_secret: OAUTH_TOKEN_SECRET
  })
})

test('GET /favorites', async t => {
  const {text} = (await favorites())[0]
  t.is(text, 'hi fanfou')
})

test('POST /statuses/update', async t => {
  const {text} = (await update())
  t.is(text, nonceText)
})

test('POST /statuses/upload', async t => {
  const status = await upload()
  const hasPhoto = !!status.photo
  const {text} = status
  t.deepEqual([hasPhoto, text], [true, `${nonceText}-upload`])
})

@@ -237,16 +237,159 @@
 cess.env
+%0Aconst PULL_REQUEST_FROM_FORKED = !(CONSUMER_KEY && CONSUMER_SECRET && OAUTH_TOKEN && OAUTH_TOKEN_SECRET && FANFOU_USERNAME && FANFOU_PASSWORD)
 %0A%0Aconst 
@@ -970,32 +970,41 @@
  if (err) re
-ject(err
+solve(err.message
 )%0A      else
@@ -1195,32 +1195,41 @@
  if (err) re
-ject(err
+solve(err.message
 )%0A      else
@@ -1427,32 +1427,41 @@
  if (err) re
-ject(err
+solve(err.message
 )%0A      else
@@ -1739,16 +1739,25 @@
 ) re
-ject(err
+solve(err.message
 )%0A  
@@ -1851,24 +1851,195 @@
 f = oauth()%0A
+  const should = PULL_REQUEST_FROM_FORKED ? %5BCONSUMER_KEY, CONSUMER_SECRET, '', ''%5D : %5B%0A    CONSUMER_KEY,%0A    CONSUMER_SECRET,%0A    OAUTH_TOKEN,%0A    OAUTH_TOKEN_SECRET%0A  %5D%0A
   t.deepEqua
@@ -2142,92 +2142,14 @@
  %5D, 
-%5B%0A    CONSUMER_KEY,%0A    CONSUMER_SECRET,%0A    OAUTH_TOKEN,%0A    OAUTH_TOKEN_SECRET%0A  %5D
+should
 )%0A%7D)
@@ -2220,28 +2220,74 @@
 )%0A  
-t.deepEqual(results,
+const should = PULL_REQUEST_FROM_FORKED ? 'Invalid consumer key' :
  %7B%0A 
@@ -2361,16 +2361,46 @@
 CRET%0A  %7D
+%0A  t.deepEqual(results, should
 )%0A%7D)%0A%0Ate
@@ -2581,16 +2581,67 @@
 ate())%0A 
+ PULL_REQUEST_FROM_FORKED ? t.is(text, undefined) :
  t.is(te
@@ -2793,16 +2793,95 @@
 status%0A 
+ PULL_REQUEST_FROM_FORKED ? t.deepEqual(%5BhasPhoto, text%5D, %5Bfalse, undefined%5D) :
  t.deepE

add 'pull request from forked' use case