config.spec.ts 2.5 KB
Newer Older
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
1
import { test }   from 'ava'
2 3

import Config from './config'
4
import Puppet from './puppet'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
5

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
6
test('Config list vars', t => {
7 8
  // t.truthy(Config.default   , 'should export default')
  // t.truthy(Config.Config    , 'should export Config')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
9

10 11
  t.true('head'     in Config, 'should exist `head` in Config')
  t.true('puppet'   in Config, 'should exist `puppet` in Config')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
12
  t.true('apihost'  in Config, 'should exist `apihost` in Config')
13 14 15
  t.true('port'     in Config, 'should exist `port` in Config')
  t.true('profile'  in Config, 'should exist `profile` in Config')
  t.true('token'    in Config, 'should exist `token` in Config')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
16

17 18 19 20 21
  t.truthy(Config.DEFAULT_PUPPET      , 'should export DEFAULT_PUPPET')
  t.truthy(Config.DEFAULT_PORT        , 'should export DEFAULT_PORT')
  t.truthy(Config.DEFAULT_PROFILE     , 'should export DEFAULT_PROFILE')
  t.truthy(Config.DEFAULT_HEAD        , 'should export DEFAULT_HEAD')
  t.truthy(Config.DEFAULT_PROTOCOL    , 'should export DEFAULT_PROTOCOL')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
22
  t.truthy(Config.DEFAULT_APIHOST     , 'should export DEFAULT_APIHOST')
23
  t.truthy(Config.CMD_CHROMIUM        , 'should export CMD_CHROMIUM')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
24 25
})

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
26 27 28 29 30 31 32 33 34 35
test('Config methods', t => {
  const OK_APIHOSTS = [
    'api.wechaty.io'
    , 'wechaty.io:8080'
  ]
  const ERR_APIHOSTS = [
    'https://api.wechaty.io'
    , 'wechaty.io/'
  ]
  OK_APIHOSTS.forEach(apihost => {
36
    t.notThrows(() => {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
37 38 39
      Config.validApiHost(apihost)
    })
  }, 'should not row for right apihost')
40
  ERR_APIHOSTS.forEach(apihost => {
41
    t.throws(() => {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
42 43 44 45
      Config.validApiHost(apihost)
    })
  }, 'should throw for error apihost')

46
})
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
47

48
test('Config puppetInstance', t => {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  t.throws(() => {
    Config.puppetInstance()
  }, Error, 'should throw when not initialized')

  const EXPECTED = {userId: 'test'}
  const puppet = <Puppet>EXPECTED

  Config.puppetInstance(puppet)

  let instance = Config.puppetInstance(puppet)
  t.deepEqual(instance, <Puppet>EXPECTED, 'should equal with initialized data')

  Config.puppetInstance(null)
  t.throws(() => {
    Config.puppetInstance()
  }, Error, 'should throw after set to null')

66
})
67 68 69

test('Config docker mode', t => {
  t.true('isDocker' in Config, 'should identify docker env by `isDocker`')
Huan (李卓桓)'s avatar
lint  
Huan (李卓桓) 已提交
70

71 72 73 74 75 76 77
  if ('C9_PORT' in process.env) {
    t.is(Config.isDocker, false, 'should not in docker mode in Cloud9 IDE')
  } else if (require('is-ci')) {
    t.is(Config.isDocker, false, 'should not in docker mode in Continuous Integeration System')
  } else {
    // a custom running envioronment, maybe docker, maybe not
  }
Huan (李卓桓)'s avatar
lint  
Huan (李卓桓) 已提交
78 79

})