config.spec.ts 3.0 KB
Newer Older
1
/**
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
2
 *   Wechaty - https://github.com/chatie/wechaty
3
 *
4 5 6 7 8 9 10 11 12 13 14 15 16
 *   Copyright 2016-2017 Huan LI <zixia@zixia.net>
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
17 18
 *
 */
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
19
import { test }   from 'ava'
20

L
lijiarui 已提交
21
import { Config } from './config'
22
import { Puppet } from './puppet'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
23

24
test('important variables', t => {
25 26
  t.true('head'     in Config, 'should exist `head` in Config')
  t.true('puppet'   in Config, 'should exist `puppet` in Config')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
27
  t.true('apihost'  in Config, 'should exist `apihost` in Config')
28 29 30
  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 (李卓桓) 已提交
31

32 33 34 35 36
  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 (李卓桓) 已提交
37
  t.truthy(Config.DEFAULT_APIHOST     , 'should export DEFAULT_APIHOST')
38
  t.truthy(Config.CMD_CHROMIUM        , 'should export CMD_CHROMIUM')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
39 40
})

41
test('validApiHost()', t => {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
42
  const OK_APIHOSTS = [
L
lijiarui 已提交
43 44
    'api.wechaty.io',
    'wechaty.io:8080',
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
45 46
  ]
  const ERR_APIHOSTS = [
L
lijiarui 已提交
47 48
    'https://api.wechaty.io',
    'wechaty.io/',
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
49 50
  ]
  OK_APIHOSTS.forEach(apihost => {
51
    t.notThrows(() => {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
52 53 54
      Config.validApiHost(apihost)
    })
  }, 'should not row for right apihost')
55
  ERR_APIHOSTS.forEach(apihost => {
56
    t.throws(() => {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
57 58 59 60
      Config.validApiHost(apihost)
    })
  }, 'should throw for error apihost')

61
})
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
62

63
test('puppetInstance()', t => {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
64 65 66 67
  t.throws(() => {
    Config.puppetInstance()
  }, Error, 'should throw when not initialized')

68 69
  const EXPECTED = <Puppet>{userId: 'test'}
  const mockPuppet = EXPECTED
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
70

71
  Config.puppetInstance(mockPuppet)
72
  const instance = Config.puppetInstance()
73
  t.deepEqual(instance, EXPECTED, 'should equal with initialized data')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
74 75 76 77 78 79

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

80
})
81

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

85 86 87 88 89 90 91
  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 (李卓桓) 已提交
92 93

})