wechaty.spec.ts 4.5 KB
Newer Older
1 2
#!/usr/bin/env ts-node

3
/**
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
4
 *   Wechaty - https://github.com/chatie/wechaty
5
 *
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
6
 *   @copyright 2016-2018 Huan LI <zixia@zixia.net>
7 8 9 10 11 12 13 14 15 16 17 18
 *
 *   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.
19 20
 *
 */
21
// tslint:disable:no-shadowed-variable
22 23
import test  from 'blue-tape'
import sinon from 'sinon'
24

25
// import asyncHooks from 'async_hooks'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
26

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
27 28 29 30
import {
  Wechaty,
}                             from './wechaty'

31
import {
32
  config,
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
33
  Contact,
34
  Friendship,
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
35 36 37
  IoClient,
  Message,
  Room,
38

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
39 40
  log,
  VERSION,
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
41
}                 from './'
42 43 44

import {
  Puppet,
45
}                 from './puppet/'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
46 47
import { PuppetMock } from './puppet-mock'
import { MemoryCard } from 'memory-card'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
48

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
49
test('Export of the Framework', async t => {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
50
  t.ok(Contact        , 'should export Contact')
51
  t.ok(Friendship     , 'should export Friendship')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
52 53 54 55 56 57
  t.ok(IoClient       , 'should export IoClient')
  t.ok(Message        , 'should export Message')
  t.ok(Puppet , 'should export Puppet')
  t.ok(Room           , 'should export Room')
  t.ok(Wechaty        , 'should export Wechaty')
  t.ok(log            , 'should export log')
58

59
  const bot = Wechaty.instance()
L
lijiarui 已提交
60 61
  t.is(bot.version(true), require('../package.json').version,
                          'should return version as the same in package.json',
62
  )
L
lijiarui 已提交
63 64
  t.is(VERSION, require('../package.json').version,
                  'should export version in package.json',
65
  )
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
66
})
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
67

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
68
test('Config setting', async t => {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
69 70
  t.ok(config                         , 'should export Config')
  t.ok(config.default.DEFAULT_PUPPET  , 'should has DEFAULT_PUPPET')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
71
})
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
72 73

test('event:start/stop', async t => {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
74
  const wechaty = new Wechaty()
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
75 76 77 78 79 80 81 82 83 84

  const startSpy = sinon.spy()
  const stopSpy  = sinon.spy()

  wechaty.on('start', startSpy)
  wechaty.on('stop',  stopSpy)

  await wechaty.start()
  await wechaty.stop()

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
85
  // console.log(startSpy.callCount)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
86 87 88 89
  t.ok(startSpy.calledOnce, 'should get event:start once')
  t.ok(stopSpy.calledOnce,  'should get event:stop once')
})

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
90 91 92 93 94
//
// FIXME: restore this unit test !!!
//
// test.only('event:scan', async t => {
//   const m = {} as any
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
95

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
//   const asyncHook = asyncHooks.createHook({
//     init(asyncId: number, type: string, triggerAsyncId: number, resource: Object) {
//       m[asyncId] = type
//     },
//     before(asyncId) {
//       // delete m[asyncId]
//     },
//     after(asyncId) {
//       // delete m[asyncId]
//     },
//     destroy(asyncId) {
//       delete m[asyncId]
//     },
//   })
//   asyncHook.enable()
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
111

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
112
//   const wechaty = Wechaty.instance()
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
113

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
114
//   const spy = sinon.spy()
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
115

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
//   wechaty.on('scan', spy)

//   const scanFuture  = new Promise(resolve => wechaty.once('scan', resolve))
//   // wechaty.once('scan', () => console.log('FAINT'))

//   await wechaty.start()
//   await scanFuture
//   // await new Promise(r => setTimeout(r, 1000))
//   await wechaty.stop()

//   t.ok(spy.calledOnce, 'should get event:scan')
//   asyncHook.disable()

//   console.log(m)
// })
131

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
132
test('on(event, Function)', async t => {
133 134 135 136 137 138 139 140 141
  const spy     = sinon.spy()
  const wechaty = Wechaty.instance()

  const EXPECTED_ERROR = new Error('testing123')
  wechaty.on('message', () => { throw EXPECTED_ERROR })
  wechaty.on('scan',    () => 42)
  wechaty.on('error',   spy)

  const messageFuture  = new Promise(resolve => wechaty.once('message', resolve))
142
  wechaty.emit('message', {} as any)
143 144

  await messageFuture
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
145
  await wechaty.stop()
146 147 148

  t.ok(spy.calledOnce, 'should get event:error once')
  t.equal(spy.firstCall.args[0], EXPECTED_ERROR, 'should get error from message listener')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
149

150
})
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
151

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164
test('initPuppetAccessory()', async t => {
  class WechatyTest extends Wechaty {
    public initPuppetAccessoryTest(puppet: Puppet): void {
      return this.initPuppetAccessory(puppet)
    }
  }
  const wechatyTest = new WechatyTest()

  const puppet = new PuppetMock({ memory: new MemoryCard() })
  t.doesNotThrow(() => wechatyTest.initPuppetAccessoryTest(puppet), 'should not throw for the 1st time init')
  t.throws(() => wechatyTest.initPuppetAccessoryTest(puppet),       'should throw for the 2nd time init')
})

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
165
// TODO: add test for event args