config.spec.ts 2.7 KB
Newer Older
1 2 3 4 5 6 7
/**
 * Wechaty - Wechat for Bot. Connecting ChatBots
 *
 * Licenst: ISC
 * https://github.com/wechaty/wechaty
 *
 */
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
8
import { test }   from 'ava'
9

10
import {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
11
    Config
12
}                 from './config'
13
import { Puppet } from './puppet'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
14

15
test('important variables', t => {
16 17
  t.true('head'     in Config, 'should exist `head` in Config')
  t.true('puppet'   in Config, 'should exist `puppet` in Config')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
18
  t.true('apihost'  in Config, 'should exist `apihost` in Config')
19 20 21
  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 (李卓桓) 已提交
22

23 24 25 26 27
  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 (李卓桓) 已提交
28
  t.truthy(Config.DEFAULT_APIHOST     , 'should export DEFAULT_APIHOST')
29
  t.truthy(Config.CMD_CHROMIUM        , 'should export CMD_CHROMIUM')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
30 31
})

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

52
})
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
53

54
test('puppetInstance()', t => {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
55 56 57 58
  t.throws(() => {
    Config.puppetInstance()
  }, Error, 'should throw when not initialized')

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

62
  Config.puppetInstance(mockPuppet)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
63
  let instance = Config.puppetInstance()
64
  t.deepEqual(instance, EXPECTED, 'should equal with initialized data')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
65 66 67 68 69 70

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

71
})
72

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

76 77 78 79 80 81 82
  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 (李卓桓) 已提交
83 84

})
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
85 86 87 88 89 90 91

/**
 * issue #70 https://github.com/wechaty/wechaty/issues/70#issuecomment-258676376
 */
test('Module Singleton', t => {
  t.is(global['WECHATY_CONFIG_INSTANCE_COUNTER'], 1, 'should only load module for one time')
})