提交 3b556453 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

code clean & add unit test to wechaty

上级 ef449f08
......@@ -6,9 +6,5 @@ export * from './schemas/room'
export * from './schemas/puppet'
export {
Puppet,
} from './puppet'
export {
YOU,
} from './schemas/puppet'
Puppet,
} from './puppet'
import {
MemoryCard,
} from 'memory-card'
import { MemoryCard } from 'memory-card'
/**
* This is used internally to as a placeholder for the bot name.
......@@ -52,7 +50,6 @@ export type ChatEventName = keyof typeof CHAT_EVENT_DICT
export const PUPPET_EVENT_DICT = {
...CHAT_EVENT_DICT,
error : 'document can be writen at here',
// heartbeat : 'document can be writen at here',
start : 'document can be writen at here',
stop : 'document can be writen at here',
watchdog : 'document can be writen at here',
......
......@@ -42,10 +42,16 @@ import {
import {
Puppet,
} from './puppet/'
} from './puppet/'
import { PuppetMock } from './puppet-mock'
import { MemoryCard } from 'memory-card'
class WechatyTest extends Wechaty {
public initPuppetAccessoryTest(puppet: Puppet): void {
return this.initPuppetAccessory(puppet)
}
}
test('Export of the Framework', async t => {
t.ok(Contact , 'should export Contact')
t.ok(Friendship , 'should export Friendship')
......@@ -150,11 +156,6 @@ test('on(event, Function)', async t => {
})
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() })
......@@ -163,3 +164,21 @@ test('initPuppetAccessory()', async t => {
})
// TODO: add test for event args
test.only('Wechaty restart for many times', async t => {
const wechaty = Wechaty.instance({
puppet: 'mock',
})
try {
for (let i = 0; i < 3; i++) {
await wechaty.start()
await wechaty.stop()
t.pass('start/stop-ed at #' + i)
}
t.pass('Wechaty start/restart successed.')
} catch (e) {
t.fail(e)
}
})
......@@ -437,10 +437,23 @@ export class Wechaty extends Accessory implements Sayable {
private initPuppet(): void {
log.verbose('Wechaty', 'initPuppet(%s)', this.options.puppet)
let inited = false
try {
inited = !!this.puppet
} catch (e) {
inited = false
}
if (inited) {
log.verbose('Wechaty', 'initPuppet(%s) had already been inited, no need to init twice', this.options.puppet)
return
}
const puppet = this.initPuppetResolver(this.options.puppet)
this.initPuppetVersionSatisfy(puppet)
this.initPuppetEventBridge(puppet)
this.initPuppetAccessory(puppet)
}
......@@ -668,20 +681,20 @@ export class Wechaty extends Accessory implements Sayable {
/**
* 1. Set Wechaty
*/
this.Contact.wechaty = this
this.ContactSelf.wechaty = this
this.Friendship.wechaty = this
this.Message.wechaty = this
this.Room.wechaty = this
this.Contact.wechaty = this
this.ContactSelf.wechaty = this
this.Friendship.wechaty = this
this.Message.wechaty = this
this.Room.wechaty = this
/**
* 2. Set Puppet
*/
this.Contact.puppet = puppet
this.ContactSelf.puppet = puppet
this.Friendship.puppet = puppet
this.Message.puppet = puppet
this.Room.puppet = puppet
this.Contact.puppet = puppet
this.ContactSelf.puppet = puppet
this.Friendship.puppet = puppet
this.Message.puppet = puppet
this.Room.puppet = puppet
this.puppet = puppet
}
......@@ -695,7 +708,7 @@ export class Wechaty extends Accessory implements Sayable {
* // do other stuff with bot here
*/
public async start(): Promise<void> {
log.info('Wechaty', 'v%s starting...' , this.version())
log.info('Wechaty', 'start() v%s is starting...' , this.version())
log.verbose('Wechaty', 'puppet: %s' , this.options.puppet)
log.verbose('Wechaty', 'profile: %s' , this.options.profile)
log.verbose('Wechaty', 'id: %s' , this.id)
......@@ -724,7 +737,17 @@ export class Wechaty extends Accessory implements Sayable {
console.error(e)
log.error('Wechaty', 'start() exception: %s', e && e.message)
Raven.captureException(e)
throw e
this.emit('error', e)
try {
await this.stop()
} catch (e) {
log.error('Wechaty', 'start() stop() exception: %s', e && e.message)
Raven.captureException(e)
this.emit('error', e)
} finally {
return
}
}
this.on('heartbeat', () => this.memoryCheck())
......@@ -747,7 +770,7 @@ export class Wechaty extends Accessory implements Sayable {
* await bot.stop()
*/
public async stop(): Promise<void> {
log.verbose('Wechaty', 'stop()')
log.info('Wechaty', 'stop() v%s is stoping ...' , this.version())
if (this.state.off()) {
log.silly('Wechaty', 'stop() on an stopping/stopped instance')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册