import path from 'path'
import test from 'ava'
import fileUtils from '../src/fileUtils'
import config from '../src/config'
import utils from './helpers/utils'

const fixturesDir = `${__dirname}${path.sep}fixtures`

function getTestDir(root = false) {
  const rootDir = `${__dirname}${path.sep}config-test`
  return root ? rootDir : `${rootDir}${path.sep}${utils.randomString()}`
}

test.before(async () => {
  await fileUtils.remove(getTestDir(true))
})

test.after(async () => {
  await fileUtils.remove(getTestDir(true))
})

test('should getConfigPath', async t => {
  const testDir = getTestDir()
  await fileUtils.fs.copyAsync(fixturesDir, testDir)

  const configPath = await config.getConfigPath(testDir)
  t.is(configPath, `${testDir}${path.sep}evermark.json`)

  t.throws(config.getConfigPath(`${path.sep}test`),
    'Please run `evermark init [destination]` to init a new Evermark folder')
})

test('should getDbPath', async t => {
  const testDir = getTestDir()
  await fileUtils.fs.copyAsync(fixturesDir, testDir)

  const dbPath = await config.getDbPath(testDir)
  t.is(dbPath, `${testDir}${path.sep}evermark.db`)
})

test('should readConfig', async t => {
  const testDir = getTestDir()
  await fileUtils.fs.copyAsync(fixturesDir, testDir)

  const conf = await config.readConfig(testDir)
  t.is(conf.token, 'foo')
  t.false(conf.china)
  t.true(conf.sandbox)

  t.throws(config.readConfig(),
    'Please run `evermark init [destination]` to init a new Evermark folder')
})

test('should getConfig', async t => {
  const testDir = getTestDir()
  await fileUtils.fs.copyAsync(fixturesDir, testDir)

  const token = await config.getConfig('token', testDir)
  const china = await config.getConfig('china', testDir)
  const sandbox = await config.getConfig('sandbox', testDir)
  const hello = await config.getConfig('hello', testDir)

  t.is(token, 'foo')
  t.false(china)
  t.true(sandbox)
  t.is(hello, undefined)
})

test('should setConfig', async t => {
  const testDir = getTestDir()
  await fileUtils.fs.copyAsync(fixturesDir, testDir)

  let result = await config.setConfig('token', 'bar', testDir)
  t.is(result, `{
  "token": "bar",
  "china": false,
  "sandbox": true
}`)

  result = await config.setConfig('china', 'true', testDir)
  t.is(result, `{
  "token": "bar",
  "china": true,
  "sandbox": true
}`)

  result = await config.setConfig('hello', 'false', testDir)
  t.is(result, `{
  "token": "bar",
  "china": true,
  "sandbox": true,
  "hello": false
}`)

  const conf = await config.readConfig(testDir)
  t.is(conf.token, 'bar')
  t.true(conf.china)
  t.true(conf.sandbox)
  t.false(conf.hello)
  t.is(conf.hi, undefined)
})

test('should initConfig', async t => {
  let testDir = getTestDir()
  await config.initConfig(testDir)

  let conf = await config.readConfig(testDir)
  t.is(conf.token, 'Your developer token')
  t.true(conf.china)
  t.false(conf.sandbox)
  t.is(conf.highlight, 'github')

  testDir = `${testDir}${path.sep}`
  await config.initConfig(testDir)

  conf = await config.readConfig(testDir)
  t.is(conf.token, 'Your developer token')
  t.true(conf.china)
  t.false(conf.sandbox)
  t.is(conf.highlight, 'github')
})

@@ -1366,16 +1366,89 @@
 andbox)%0A
+%7D)%0A%0Atest('should readConfig error when config file invalid', async t =%3E %7B
 %0A  t.thr
@@ -1541,32 +1541,682 @@
 vermark folder')
+%0A%0A  const testDir = getTestDir()%0A  const configPath = %60$%7BtestDir%7D/evermark.json%60%0A  await fileUtils.fs.copyAsync(fixturesDir, testDir)%0A%0A  await fileUtils.writeFile(configPath, 'token')%0A  await t.throws(config.readConfig(testDir),%0A    %60Please write to $%7BconfigPath%7D:%5Cn%5Cn%60 +%0A    %60%7B%5Cn  %22token%22: %22xxx%22,%5Cn  %22china%22: xxx%5Cn%7D%60)%0A%0A  await fileUtils.writeFile(configPath, '%7B %22china%22: true %7D')%0A  t.throws(config.readConfig(testDir),%0A    %60Please write developer token to $%7BconfigPath%7D%5Cn%5Cn%60 +%0A    'To get a developer token, please visit:%5Cn  ' +%0A    'https://www.evernote.com/api/DeveloperToken.action or ' +%0A    'https://app.yinxiang.com/api/DeveloperToken.action')
 %0A%7D)%0A%0Atest('shoul

improve config.js test