config.spec.ts 2.5 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 11
import { Config } from './config'
import { Puppet } from './puppet'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
12

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

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

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

50
})
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
51

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

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

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

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

69
})
70

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

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

})